Skip to content

Commit a69be49

Browse files
committed
fix: Refactor path names usage in tests
1 parent 9a336dd commit a69be49

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

tests/test_unit_tests.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@
2626
warnings.filterwarnings("ignore", category=DeprecationWarning)
2727

2828
PROJECT_PATH = os.getcwd()
29-
30-
testPath = os.path.join(PROJECT_PATH, "tests", "fixtures", "C.jpg")
31-
29+
FIXTURES_DIR = os.path.join(os.path.dirname(__file__), "fixtures")
30+
DEFAULT_TEST_FILE_NAME = "C.jpg"
31+
DEFAULT_TEST_FILE = os.path.join(FIXTURES_DIR, DEFAULT_TEST_FILE_NAME)
32+
INGREDIENT_TEST_FILE = os.path.join(FIXTURES_DIR, "A.jpg")
33+
ALTERNATIVE_INGREDIENT_TEST_FILE = os.path.join(FIXTURES_DIR, "cloud.jpg")
3234

3335
class TestC2paSdk(unittest.TestCase):
3436
def test_sdk_version(self):
@@ -38,32 +40,32 @@ def test_sdk_version(self):
3840
class TestReader(unittest.TestCase):
3941
def setUp(self):
4042
# Use the fixtures_dir fixture to set up paths
41-
self.data_dir = os.path.join(os.path.dirname(__file__), "fixtures")
42-
self.testPath = os.path.join(self.data_dir, "C.jpg")
43+
self.data_dir = FIXTURES_DIR
44+
self.testPath = DEFAULT_TEST_FILE
4345

4446
def test_stream_read(self):
4547
with open(self.testPath, "rb") as file:
4648
reader = Reader("image/jpeg", file)
4749
json_data = reader.json()
48-
self.assertIn("C.jpg", json_data)
50+
self.assertIn(DEFAULT_TEST_FILE_NAME, json_data)
4951

5052
def test_stream_read_and_parse(self):
5153
with open(self.testPath, "rb") as file:
5254
reader = Reader("image/jpeg", file)
5355
manifest_store = json.loads(reader.json())
5456
title = manifest_store["manifests"][manifest_store["active_manifest"]]["title"]
55-
self.assertEqual(title, "C.jpg")
57+
self.assertEqual(title, DEFAULT_TEST_FILE_NAME)
5658

5759
def test_stream_read_string_stream(self):
5860
with Reader("image/jpeg", self.testPath) as reader:
5961
json_data = reader.json()
60-
self.assertIn("C.jpg", json_data)
62+
self.assertIn(DEFAULT_TEST_FILE_NAME, json_data)
6163

6264
def test_stream_read_string_stream_and_parse(self):
6365
with Reader("image/jpeg", self.testPath) as reader:
6466
manifest_store = json.loads(reader.json())
6567
title = manifest_store["manifests"][manifest_store["active_manifest"]]["title"]
66-
self.assertEqual(title, "C.jpg")
68+
self.assertEqual(title, DEFAULT_TEST_FILE_NAME)
6769

6870
def test_reader_bad_format(self):
6971
with self.assertRaises(Error.NotSupported):
@@ -75,7 +77,7 @@ def test_settings_trust(self):
7577
with open(self.testPath, "rb") as file:
7678
reader = Reader("image/jpeg", file)
7779
json_data = reader.json()
78-
self.assertIn("C.jpg", json_data)
80+
self.assertIn(DEFAULT_TEST_FILE_NAME, json_data)
7981

8082
def test_reader_double_close(self):
8183
"""Test that multiple close calls are handled gracefully."""
@@ -170,7 +172,9 @@ def test_read_all_files(self):
170172
class TestBuilder(unittest.TestCase):
171173
def setUp(self):
172174
# Use the fixtures_dir fixture to set up paths
173-
self.data_dir = os.path.join(os.path.dirname(__file__), "fixtures")
175+
self.data_dir = FIXTURES_DIR
176+
self.testPath = DEFAULT_TEST_FILE
177+
self.testPath2 = INGREDIENT_TEST_FILE
174178
with open(os.path.join(self.data_dir, "es256_certs.pem"), "rb") as cert_file:
175179
self.certs = cert_file.read()
176180
with open(os.path.join(self.data_dir, "es256_private.key"), "rb") as key_file:
@@ -185,10 +189,8 @@ def setUp(self):
185189
)
186190
self.signer = Signer.from_info(self.signer_info)
187191

188-
self.testPath = os.path.join(self.data_dir, "C.jpg")
189-
self.testPath2 = os.path.join(self.data_dir, "A.jpg")
190192
self.testPath3 = os.path.join(self.data_dir, "A_thumbnail.jpg")
191-
self.testPath4 = os.path.join(self.data_dir, "cloud.jpg")
193+
self.testPath4 = ALTERNATIVE_INGREDIENT_TEST_FILE
192194

193195
# Define a manifest as a dictionary
194196
self.manifestDefinition = {
@@ -531,7 +533,7 @@ def test_builder_sign_with_multiple_ingredient(self):
531533

532534
# Add second ingredient
533535
ingredient_json2 = '{"title": "Test Ingredient 2"}'
534-
cloud_path = os.path.join(self.data_dir, "cloud.jpg")
536+
cloud_path = ALTERNATIVE_INGREDIENT_TEST_FILE
535537
with open(cloud_path, 'rb') as f:
536538
builder.add_ingredient(ingredient_json2, "image/jpeg", f)
537539

@@ -579,7 +581,7 @@ def test_builder_sign_with_multiple_ingredients_from_stream(self):
579581

580582
# Add second ingredient using stream
581583
ingredient_json2 = '{"title": "Test Ingredient Stream 2"}'
582-
cloud_path = os.path.join(self.data_dir, "cloud.jpg")
584+
cloud_path = ALTERNATIVE_INGREDIENT_TEST_FILE
583585
with open(cloud_path, 'rb') as f:
584586
builder.add_ingredient_from_stream(
585587
ingredient_json2, "image/jpeg", f)
@@ -788,11 +790,9 @@ def setUp(self):
788790
# Filter specific deprecation warnings for legacy API tests
789791
warnings.filterwarnings("ignore", message="The read_file function is deprecated")
790792
warnings.filterwarnings("ignore", message="The sign_file function is deprecated")
791-
792-
self.data_dir = os.path.join(os.path.dirname(__file__), "fixtures")
793793

794-
# That file has C2PA metadata, so there is something to read
795-
self.testPath = os.path.join(self.data_dir, "C.jpg")
794+
self.data_dir = FIXTURES_DIR
795+
self.testPath = DEFAULT_TEST_FILE
796796

797797
# Create temp directory for tests
798798
self.temp_data_dir = os.path.join(self.data_dir, "temp_data")
@@ -819,7 +819,7 @@ def test_read_ingredient_file(self):
819819

820820
# Verify some fields
821821
ingredient_data = json.loads(ingredient_json_with_dir)
822-
self.assertEqual(ingredient_data["title"], "C.jpg")
822+
self.assertEqual(ingredient_data["title"], DEFAULT_TEST_FILE_NAME)
823823
self.assertEqual(ingredient_data["format"], "image/jpeg")
824824
self.assertIn("thumbnail", ingredient_data)
825825

0 commit comments

Comments
 (0)