Skip to content

Commit

Permalink
Change file storage system in test to avoid save files
Browse files Browse the repository at this point in the history
  • Loading branch information
Heibert committed Aug 9, 2024
1 parent 052e512 commit 521454d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 33 deletions.
18 changes: 1 addition & 17 deletions INSIGHTSAPI/sgc/tests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Test module for SGC"""

import os
import shutil
import tempfile
from services.tests import BaseTestCase
from rest_framework import status
Expand All @@ -14,7 +13,7 @@
from .models import SGCFile, SGCArea


@override_settings(MEDIA_ROOT=tempfile.mkdtemp())
@override_settings(DEFAULT_FILE_STORAGE="django.core.files.storage.InMemoryStorage")
class TestSGC(BaseTestCase):
"""Test module for SGC"""

Expand Down Expand Up @@ -42,11 +41,6 @@ def setUp(self):
permission_delete = Permission.objects.get(codename="delete_sgcfile")
user.user_permissions.add(permission_delete)
user.save()
# temp_folder = tempfile.mkdtemp()
# print(settings.MEDIA_ROOT)
self.media_directory = settings.MEDIA_ROOT
# settings.MEDIA_ROOT = self.media_directory
# print(self.media_directory)

def test_get_file(self):
"""Test getting a file"""
Expand Down Expand Up @@ -247,13 +241,3 @@ def test_delete_file_without_permission(self):
# self.assertEqual(response.status_code, status.HTTP_200_OK, response.data)
# self.assertGreater(SGCFile.objects.count(), 0)
# self.assertEqual(response.data["message"], "Archivos creados")

@classmethod
def tearDownClass(cls):
"""Tear down for the test"""
if str(settings.MEDIA_ROOT).startswith("/tmp"):
shutil.rmtree(settings.MEDIA_ROOT)
# pass
else:
print(f"Not removing {settings.MEDIA_ROOT}")
super().tearDownClass()
23 changes: 7 additions & 16 deletions INSIGHTSAPI/vacancy/tests.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
"""Test for vacancy."""

import os
import tempfile
import shutil
from services.tests import BaseTestCase
from users.models import User
from django.test import override_settings
from django.urls import reverse
from django.core.files.uploadedfile import SimpleUploadedFile
from django.contrib.auth.models import Permission
from django.conf import settings
from .models import Vacancy, Reference


@override_settings(DEFAULT_FILE_STORAGE="django.core.files.storage.InMemoryStorage")
class VacancyTest(BaseTestCase):
"""Test for vacancy email."""

Expand All @@ -25,9 +23,6 @@ def setUp(self, user=None):
self.user.user_permissions.add(permission)
permission_update = Permission.objects.get(name="Can change vacancy")
self.user.user_permissions.add(permission_update)
temp_folder = tempfile.mkdtemp()
self.media_directory = temp_folder
settings.MEDIA_ROOT = self.media_directory
with open("static/vacancy/asesor-vacante.png", "rb") as image_data:
image = SimpleUploadedFile(
"asesor-vacante.png", image_data.read(), content_type="image/png"
Expand Down Expand Up @@ -126,15 +121,8 @@ def test_delete_vacancy(self):
response = self.client.delete(reverse("vacancy-detail", args=[vacancy.id]))
self.assertEqual(response.status_code, 405, response.data)

def tearDown(self):
super().tearDown()
if self.media_directory.startswith("/tmp"):
shutil.rmtree(self.media_directory)
# pass
else:
print(f"Not removing {self.media_directory}")


@override_settings(DEFAULT_FILE_STORAGE="django.core.files.storage.InMemoryStorage")
class ReferenceTest(BaseTestCase):
"""Test for reference."""

Expand Down Expand Up @@ -216,6 +204,7 @@ def test_re_apply_reference(self):
)


@override_settings(DEFAULT_FILE_STORAGE="django.core.files.storage.InMemoryStorage")
class VacancyApplyTest(BaseTestCase):
"""Test for vacancy apply."""

Expand All @@ -241,6 +230,8 @@ def test_vacancy_apply(self):
self.assertEqual(
response.data,
{
"message": 'Correo enviado correctamente a "' + str(self.user.email) + '"',
"message": 'Correo enviado correctamente a "'
+ str(self.user.email)
+ '"',
},
)

0 comments on commit 521454d

Please sign in to comment.