From 7efe436fd9b794ed585a675bf0cbaf9ff4c8464d Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Wed, 20 Aug 2025 15:42:38 +0200 Subject: 🐛 fix unclosed file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- archaeological_operations/tests.py | 84 ++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 45 deletions(-) (limited to 'archaeological_operations/tests.py') diff --git a/archaeological_operations/tests.py b/archaeological_operations/tests.py index 4add8ea4b..57950ddd2 100644 --- a/archaeological_operations/tests.py +++ b/archaeological_operations/tests.py @@ -182,14 +182,14 @@ class ImportTest(BaseImportTest): def init_ope_import(self, filename="MCC-operations-example.csv", sep=","): mcc_operation = ImporterType.objects.get(name="MCC - Opérations") - mcc_operation_file = open( - settings.LIB_BASE_PATH + "archaeological_operations/tests/" + filename, "rb" - ) - file_dict = { - "imported_file": SimpleUploadedFile( - mcc_operation_file.name, mcc_operation_file.read() - ) - } + with open( + settings.LIB_BASE_PATH + "archaeological_operations/tests/" + filename, + "rb") as mcc_operation_file: + file_dict = { + "imported_file": SimpleUploadedFile( + mcc_operation_file.name, mcc_operation_file.read() + ) + } group, c = TargetKeyGroup.objects.get_or_create(name="My group") post_dict = { "importer_type": mcc_operation.pk, @@ -290,14 +290,13 @@ class ImportTest(BaseImportTest): def init_parcel_import(self): self.init_ope() mcc_parcel = ImporterType.objects.get(name="MCC - Parcelles") - mcc_file = open( - settings.LIB_BASE_PATH + - "archaeological_operations/tests/MCC-parcelles-example.csv", - "rb", - ) - file_dict = { - "imported_file": SimpleUploadedFile(mcc_file.name, mcc_file.read()) - } + with open( + settings.LIB_BASE_PATH + + "archaeological_operations/tests/MCC-parcelles-example.csv", + "rb") as mcc_file: + file_dict = { + "imported_file": SimpleUploadedFile(mcc_file.name, mcc_file.read()) + } post_dict = { "importer_type": mcc_parcel.pk, "skip_lines": 1, @@ -320,14 +319,12 @@ class ImportTest(BaseImportTest): def init_context_record_import(self): self.init_parcel() mcc = ImporterType.objects.get(name="MCC - UE") - mcc_file = open( - settings.LIB_BASE_PATH + "archaeological_context_records/tests/" - "MCC-context-records-example.csv", - "rb", - ) - file_dict = { - "imported_file": SimpleUploadedFile(mcc_file.name, mcc_file.read()) - } + with open( + settings.LIB_BASE_PATH + "archaeological_context_records/tests/" + "MCC-context-records-example.csv", "rb") as mcc_file: + file_dict = { + "imported_file": SimpleUploadedFile(mcc_file.name, mcc_file.read()) + } post_dict = { "importer_type": mcc.pk, "skip_lines": 1, @@ -817,15 +814,14 @@ class ImportDocumentTest(ImportTest, TestCase): target="title", formater_type_id=formater.pk, column_id=col.pk ) - doc_import_file = open( - settings.LIB_BASE_PATH + "archaeological_operations/tests/" + filename, "rb" - ) - - file_dict = { - "imported_file": SimpleUploadedFile( - doc_import_file.name, doc_import_file.read() - ) - } + with open( + settings.LIB_BASE_PATH + "archaeological_operations/tests/" + filename, + "rb") as doc_import_file: + file_dict = { + "imported_file": SimpleUploadedFile( + doc_import_file.name, doc_import_file.read() + ) + } group, c = TargetKeyGroup.objects.get_or_create(name="My group") post_dict = { @@ -4400,12 +4396,11 @@ class RegisterTest(TestCase, OperationInitTest): self.assertEqual(json.loads(response.content.decode())["recordsTotal"], 1) def test_document_generation(self): - tpl = open( - settings.LIB_BASE_PATH + - "archaeological_operations/tests/template_adminact.odt", - "rb", - ) - template = SimpleUploadedFile(tpl.name, tpl.read()) + with open( + settings.LIB_BASE_PATH + + "archaeological_operations/tests/template_adminact.odt", + "rb") as tpl: + template = SimpleUploadedFile(tpl.name, tpl.read()) model, __ = ImporterModel.objects.get_or_create( klass="archaeological_operations.models.AdministrativeAct" ) @@ -4441,12 +4436,11 @@ class RegisterTest(TestCase, OperationInitTest): f.close() def test_document_ref_generation(self): - tpl = open( - settings.LIB_BASE_PATH + - "archaeological_operations/tests/document_reference.odt", - "rb", - ) - template = SimpleUploadedFile(tpl.name, tpl.read()) + with open( + settings.LIB_BASE_PATH + + "archaeological_operations/tests/document_reference.odt", + "rb") as tpl: + template = SimpleUploadedFile(tpl.name, tpl.read()) model, __ = ImporterModel.objects.get_or_create( klass="archaeological_operations.models.AdministrativeAct" ) -- cgit v1.2.3