summaryrefslogtreecommitdiff
path: root/ishtar_common/tests.py
diff options
context:
space:
mode:
Diffstat (limited to 'ishtar_common/tests.py')
-rw-r--r--ishtar_common/tests.py142
1 files changed, 71 insertions, 71 deletions
diff --git a/ishtar_common/tests.py b/ishtar_common/tests.py
index 7d60deab8..b5e228852 100644
--- a/ishtar_common/tests.py
+++ b/ishtar_common/tests.py
@@ -721,17 +721,18 @@ class GenericSerializationTest:
)
self.documents = []
for idx in range(12):
- self.documents.append(
- models.Document.objects.create(
- title="Test{}".format(idx),
- associated_file=SimpleUploadedFile("test.txt", b"no real content"),
- image=SimpleUploadedFile(
- name="test.png",
- content=open(image_path, "rb").read(),
- content_type="image/png",
- ),
+ with open(image_path, "rb") as img:
+ self.documents.append(
+ models.Document.objects.create(
+ title="Test{}".format(idx),
+ associated_file=SimpleUploadedFile("test.txt", b"no real content"),
+ image=SimpleUploadedFile(
+ name="test.png",
+ content=img.read(),
+ content_type="image/png",
+ ),
+ )
)
- )
def generic_serialization_test(self, serialize, no_test=False, kwargs=None):
if not kwargs:
@@ -2707,8 +2708,10 @@ class BaseImportTest(TestCase):
importer_filename = os.path.join(root, "importer-group.zip")
restore_serialized(importer_filename)
imp_group = models.ImporterGroup.objects.get(slug="chantier-des-depots")
- imp_file = open(os.path.join(root, "importer-group.csv"), "rb")
- imp_media = open(os.path.join(root, "importer-group-media.zip"), "rb")
+ with open(os.path.join(root, "importer-group.csv"), "rb") as imp:
+ imp_file = SimpleUploadedFile(imp.name, imp.read())
+ with open(os.path.join(root, "importer-group-media.zip"), "rb") as imp:
+ imp_media = SimpleUploadedFile(imp.name, imp.read())
return imp_group, imp_file, imp_media
def create_group_import(self, init=True):
@@ -2716,8 +2719,8 @@ class BaseImportTest(TestCase):
create_user()
ishtar_user = models.IshtarUser.objects.all()[0]
file_dict = {
- "imported_file": SimpleUploadedFile(imp_file.name, imp_file.read()),
- "imported_images": SimpleUploadedFile(imp_media.name, imp_media.read())
+ "imported_file": imp_file,
+ "imported_images": imp_media
}
post_dict = {
"importer_type": imp_group.pk,
@@ -2879,13 +2882,12 @@ class ImportTestInterface(BaseImportTest):
def _test_create_import_get_data(self):
csv_path = os.path.join(LIB_BASE_PATH, "ishtar_common", "tests", "insee-test.csv")
+ with open(csv_path, "rb") as f:
+ imported_file = SimpleUploadedFile(name="insee-test.csv", content=f.read())
return {
"name": "Test Name",
"importer_type": self.importer_type.pk,
- "imported_file": SimpleUploadedFile(
- name="insee-test.csv",
- content=open(csv_path, "rb").read(),
- ),
+ "imported_file": imported_file,
"encoding": "utf-8",
"csv_sep": '|',
"skip_lines": 1,
@@ -3030,17 +3032,16 @@ class ImportTestInterface(BaseImportTest):
def test_validation_zip_import_image(self):
# init
image_path = os.path.join(LIB_BASE_PATH, "ishtar_common", "tests", "test.png")
+ with open(image_path, "rb") as f:
+ imported_images = SimpleUploadedFile(name="test.png", content=f.read(),
+ content_type="image/png")
data = {
"name": "Import Zip Not Valid Must Fail",
"importer_type": self.importer_type.pk,
"encoding": "utf-8",
"csv_sep": "|",
"skip_lines": 1,
- "imported_images": SimpleUploadedFile(
- name="test.png",
- content=open(image_path, "rb").read(),
- content_type="image/png",
- ),
+ "imported_images": imported_images,
}
# superuser
@@ -3101,9 +3102,15 @@ class ImportTestInterface(BaseImportTest):
path = os.path.join(
LIB_BASE_PATH, "ishtar_common", "tests", "error-file.csv"
)
- imprt.error_file = SimpleUploadedFile(name="error-file.csv", content=open(path, "rb").read(), content_type="text/csv")
+ with open(path, "rb") as f:
+ error_file = SimpleUploadedFile(name="error-file.csv", content=f.read(),
+ content_type="text/csv")
+ imprt.error_file = error_file
imprt.save()
- imprt2.error_file = SimpleUploadedFile(name="error-file.csv", content=open(path, "rb").read(), content_type="text/csv")
+ with open(path, "rb") as f:
+ error_file = SimpleUploadedFile(name="error-file.csv", content=f.read(),
+ content_type="text/csv")
+ imprt2.error_file = error_file
imprt2.save()
q = models.ImportLineError.objects.filter(import_item=imprt.pk)
@@ -3249,27 +3256,27 @@ class ImportTest(BaseImportTest):
SUB_IMPORT_MATCH_IDX = [1, 2]
with tempfile.TemporaryDirectory() as tmpdir:
- current_zip = zipfile.ZipFile(group_import.archive_file.path, "r")
- name_list = current_zip.namelist()
- self.assertIn("content.json", name_list)
- current_zip.extract("content.json", tmpdir)
- content_name = os.path.join(tmpdir, "content.json")
- with open(content_name, "r") as content:
- files = json.loads(content.read())
- self.assertIn("imported_file", files.keys())
- self.assertIn(files["imported_file"], name_list)
- for idx in range(4):
- self.assertIn(f"sub-{idx}-result_file", files.keys())
- self.assertIn(files[f"sub-{idx}-result_file"], name_list)
- for idx in SUB_IMPORT_MATCH_IDX:
- self.assertIn(f"sub-{idx}-match_file", files.keys())
- self.assertIn(files[f"sub-{idx}-match_file"], name_list)
- for name in name_list:
- current_zip.extract(name, tmpdir)
- if name == files["imported_file"]:
- with open(os.path.join(tmpdir, name), "r") as f:
- result = f.read()
- self.assertEqual(result, csv_content)
+ with zipfile.ZipFile(group_import.archive_file.path, "r") as current_zip:
+ name_list = current_zip.namelist()
+ self.assertIn("content.json", name_list)
+ current_zip.extract("content.json", tmpdir)
+ content_name = os.path.join(tmpdir, "content.json")
+ with open(content_name, "r") as content:
+ files = json.loads(content.read())
+ self.assertIn("imported_file", files.keys())
+ self.assertIn(files["imported_file"], name_list)
+ for idx in range(4):
+ self.assertIn(f"sub-{idx}-result_file", files.keys())
+ self.assertIn(files[f"sub-{idx}-result_file"], name_list)
+ for idx in SUB_IMPORT_MATCH_IDX:
+ self.assertIn(f"sub-{idx}-match_file", files.keys())
+ self.assertIn(files[f"sub-{idx}-match_file"], name_list)
+ for name in name_list:
+ current_zip.extract(name, tmpdir)
+ if name == files["imported_file"]:
+ with open(os.path.join(tmpdir, name), "r") as f:
+ result = f.read()
+ self.assertEqual(result, csv_content)
group_import.unarchive("F")
group_import = models.ImportGroup.objects.get(pk=group_import.pk)
@@ -4627,14 +4634,13 @@ class StorageTest(TestCase):
image_path = os.path.join(
LIB_BASE_PATH, "ishtar_common", "tests", "test.png"
)
+ with open(image_path, "rb") as f:
+ image = SimpleUploadedFile(name="test.png", content=f.read(),
+ content_type="image/png")
doc = models.Document.objects.create(
source_type=self.st1,
title="Operation report",
- image=SimpleUploadedFile(
- name="test.png",
- content=open(image_path, "rb").read(),
- content_type="image/png",
- ),
+ image=image,
)
p = doc.image.path.split(os.sep)
# current save path
@@ -4644,25 +4650,17 @@ class StorageTest(TestCase):
if f.startswith("test"):
os.remove(os.path.join(base_path, f))
doc = models.Document.objects.get(pk=doc.pk)
- doc.image.save(
- "test.png",
- SimpleUploadedFile(
- name="test.png",
- content=open(image_path, "rb").read(),
- content_type="image/png",
- ),
- )
+ with open(image_path, "rb") as f:
+ image = SimpleUploadedFile(name="test.png", content=f.read(),
+ content_type="image/png")
+ doc.image.save("test.png", image)
doc = models.Document.objects.get(pk=doc.pk)
os.remove(doc.image.path)
os.symlink("/tmp/ZZZZZZZZZZZZZZZ", doc.image.path) # bad link
- doc.image.save(
- "test.png",
- SimpleUploadedFile(
- name="test.png",
- content=open(image_path, "rb").read(),
- content_type="image/png",
- ),
- )
+ with open(image_path, "rb") as f:
+ image = SimpleUploadedFile(name="test.png", content=f.read(),
+ content_type="image/png")
+ doc.image.save("test.png", image)
doc.save()
@@ -4851,13 +4849,15 @@ class DocumentTest(TestCase):
pdf_path = os.path.join(
LIB_BASE_PATH, "ishtar_common", "tests", "simple.pdf"
)
- doc = models.Document.objects.create(
- title="Document",
- associated_file=SimpleUploadedFile(
+ with open(pdf_path, "rb") as f:
+ associated_file = SimpleUploadedFile(
name="simple.pdf",
- content=open(pdf_path, "rb").read(),
+ content=f.read(),
content_type="application/pdf",
)
+ doc = models.Document.objects.create(
+ title="Document",
+ associated_file=associated_file
)
doc.operations.add(self.ope1)
doc = models.Document.objects.get(id=doc.pk)