Skip to content

Commit 5a1f118

Browse files
committed
Fix failing tests #241
Signed-off-by: tdruez <[email protected]>
1 parent c845968 commit 5a1f118

File tree

2 files changed

+44
-15
lines changed

2 files changed

+44
-15
lines changed

dje/tests/__init__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from django.conf import settings
1515
from django.contrib.auth import get_user_model
1616
from django.contrib.auth.models import Permission
17+
from django.core.files.uploadedfile import TemporaryUploadedFile
1718
from django.db import DEFAULT_DB_ALIAS
1819
from django.db import connections
1920
from django.test.runner import DiscoverRunner
@@ -124,3 +125,20 @@ def assertMaxQueries(self, num, func=None, *args, using=DEFAULT_DB_ALIAS, **kwar
124125

125126
with context:
126127
func(*args, **kwargs)
128+
129+
130+
def wrap_as_temp_uploaded_file(file_path):
131+
"""Wrap an existing file as a Django TemporaryUploadedFile"""
132+
temp_file = TemporaryUploadedFile(
133+
name=file_path.name,
134+
content_type="application/json",
135+
size=file_path.stat().st_size,
136+
charset="utf-8",
137+
)
138+
139+
# Read and copy file content into the temporary file
140+
with open(file_path, "rb") as f:
141+
temp_file.write(f.read())
142+
143+
temp_file.seek(0) # Reset pointer after writing
144+
return temp_file

product_portfolio/tests/test_importers.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from dje.models import Dataspace
2323
from dje.tests import create_admin
2424
from dje.tests import create_superuser
25+
from dje.tests import wrap_as_temp_uploaded_file
2526
from license_library.models import License
2627
from license_library.models import LicenseChoice
2728
from organization.models import Owner
@@ -714,7 +715,8 @@ def setUp(self):
714715

715716
def test_product_portfolio_product_import_from_scan_proper(self):
716717
scan_input_location = self.testfiles_path / "import_from_scan.json"
717-
importer = ImportFromScan(self.product1, self.super_user, scan_input_location)
718+
upload_file = wrap_as_temp_uploaded_file(scan_input_location)
719+
importer = ImportFromScan(self.product1, self.super_user, upload_file)
718720
warnings, created_counts = importer.save()
719721
self.assertEqual([], warnings)
720722
self.assertEqual(
@@ -750,14 +752,16 @@ def test_product_portfolio_product_import_from_scan_proper(self):
750752
self.assertDictEqual(expected_details, resource.additional_details)
751753

752754
# Make sure we do not create duplicates on re-importing
753-
importer = ImportFromScan(self.product1, self.super_user, scan_input_location)
755+
upload_file = wrap_as_temp_uploaded_file(scan_input_location)
756+
importer = ImportFromScan(self.product1, self.super_user, upload_file)
754757
warnings, created_counts = importer.save()
755758
self.assertEqual([], warnings)
756759
self.assertEqual({}, created_counts)
757760

758761
def test_product_portfolio_product_import_from_scan_scanpipe_results(self):
759762
scan_input_location = self.testfiles_path / "scancodeio_scan_codebase_results.json"
760-
importer = ImportFromScan(self.product1, self.super_user, scan_input_location)
763+
upload_file = wrap_as_temp_uploaded_file(scan_input_location)
764+
importer = ImportFromScan(self.product1, self.super_user, upload_file)
761765
warnings, created_counts = importer.save()
762766
self.assertEqual([], warnings)
763767
self.assertEqual(
@@ -790,21 +794,24 @@ def test_product_portfolio_product_import_from_scan_scanpipe_results(self):
790794
self.assertDictEqual(expected_details, resource.additional_details)
791795

792796
# Make sure we do not create duplicates on re-importing
793-
importer = ImportFromScan(self.product1, self.super_user, scan_input_location)
797+
upload_file = wrap_as_temp_uploaded_file(scan_input_location)
798+
importer = ImportFromScan(self.product1, self.super_user, upload_file)
794799
warnings, created_counts = importer.save()
795800
self.assertEqual([], warnings)
796801
self.assertEqual({}, created_counts)
797802

798803
def test_product_portfolio_product_import_from_scan_package_without_purl(self):
799804
scan_input_location = self.testfiles_path / "package_without_purl.json"
800-
importer = ImportFromScan(self.product1, self.super_user, scan_input_location)
805+
upload_file = wrap_as_temp_uploaded_file(scan_input_location)
806+
importer = ImportFromScan(self.product1, self.super_user, upload_file)
801807
warnings, created_counts = importer.save()
802808
self.assertEqual([], warnings)
803809
self.assertEqual({}, created_counts)
804810

805811
def test_product_portfolio_product_import_from_scan_duplicated_purl_are_imported_once(self):
806812
scan_input_location = self.testfiles_path / "duplicated_purl.json"
807-
importer = ImportFromScan(self.product1, self.super_user, scan_input_location)
813+
upload_file = wrap_as_temp_uploaded_file(scan_input_location)
814+
importer = ImportFromScan(self.product1, self.super_user, upload_file)
808815
warnings, created_counts = importer.save()
809816

810817
self.assertEqual([], warnings)
@@ -828,28 +835,32 @@ def test_product_portfolio_product_import_from_scan_duplicated_purl_are_imported
828835
self.assertIn("artifacts/six-1.14.0/setup.py", codebase_resources)
829836

830837
def test_product_portfolio_product_import_from_scan_input_file_errors(self):
831-
expected = "The file content is not proper JSON."
838+
expected = "Invalid JSON file: Expecting value: line 1 column 1 (char 0)"
832839
scan_input_location = Path(__file__)
833-
importer = ImportFromScan(self.product1, self.super_user, scan_input_location)
840+
upload_file = wrap_as_temp_uploaded_file(scan_input_location)
841+
importer = ImportFromScan(self.product1, self.super_user, upload_file)
834842
with self.assertRaisesMessage(ValidationError, expected):
835843
importer.save()
836844

837845
expected = "The uploaded file is not a proper ScanCode output results."
838846
scan_input_location = self.testfiles_path / "json_but_not_scan_results.json"
839-
importer = ImportFromScan(self.product1, self.super_user, scan_input_location)
847+
upload_file = wrap_as_temp_uploaded_file(scan_input_location)
848+
importer = ImportFromScan(self.product1, self.super_user, upload_file)
840849
with self.assertRaisesMessage(ValidationError, expected):
841850
importer.save()
842851

843852
options_str = "--copyright --package"
844853
expected = f"The Scan run is missing those required options: {options_str}"
845854
scan_input_location = self.testfiles_path / "missing_scancode_options.json"
846-
importer = ImportFromScan(self.product1, self.super_user, scan_input_location)
855+
upload_file = wrap_as_temp_uploaded_file(scan_input_location)
856+
importer = ImportFromScan(self.product1, self.super_user, upload_file)
847857
with self.assertRaisesMessage(ValidationError, expected):
848858
importer.save()
849859

850860
expected = "'This ScanCode.io output does not include packages nor dependencies data."
851861
scan_input_location = self.testfiles_path / "missing_correct_pipeline.json"
852-
importer = ImportFromScan(self.product1, self.super_user, scan_input_location)
862+
upload_file = wrap_as_temp_uploaded_file(scan_input_location)
863+
importer = ImportFromScan(self.product1, self.super_user, upload_file)
853864
with self.assertRaisesMessage(ValidationError, expected):
854865
importer.save()
855866

@@ -859,13 +870,13 @@ def test_product_portfolio_product_import_from_scan_input_data_validation_errors
859870
"Ensure this value has at most 1024 characters (it has 3466)."
860871
)
861872
scan_input_location = self.testfiles_path / "package_data_validation_error.json"
862-
importer = ImportFromScan(
863-
self.product1, self.super_user, scan_input_location, stop_on_error=True
864-
)
873+
upload_file = wrap_as_temp_uploaded_file(scan_input_location)
874+
importer = ImportFromScan(self.product1, self.super_user, upload_file, stop_on_error=True)
865875
with self.assertRaisesMessage(ValidationError, expected):
866876
importer.save()
867877

868-
importer = ImportFromScan(self.product1, self.super_user, scan_input_location)
878+
upload_file = wrap_as_temp_uploaded_file(scan_input_location)
879+
importer = ImportFromScan(self.product1, self.super_user, upload_file)
869880
warnings, created_counts = importer.save()
870881
expected = [
871882
"pkg:pypi/six?uuid=bbe2794a-d81a-4728-a66d-5700a3735977 license_expression: "

0 commit comments

Comments
 (0)