From f56a19596f8e870b43373f912802051dece1b455 Mon Sep 17 00:00:00 2001 From: Hannes Schmidt Date: Fri, 22 Nov 2024 22:11:34 -0800 Subject: [PATCH] Consolidate handling of create-if-missing cans --- test/pfb_test_case.py | 22 +++++++++++++--------- test/service/test_manifest.py | 16 ++++------------ 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/test/pfb_test_case.py b/test/pfb_test_case.py index b15b3bec0..96b942ae3 100644 --- a/test/pfb_test_case.py +++ b/test/pfb_test_case.py @@ -1,4 +1,7 @@ import json +from pathlib import ( + Path, +) import fastavro @@ -15,14 +18,15 @@ def _assert_pfb_schema(self, schema): with self.assertRaises(KeyError): fastavro.parse_schema({'this': 'is not', 'an': 'avro schema'}) - def to_json(records): - return json.dumps(records, indent=4, sort_keys=True) + actual = json.dumps(schema, indent=4, sort_keys=True) + expected = self._data_path('service', 'manifest', 'terra', 'pfb_manifest.schema.json') + self._assert_or_create_json_can(expected, actual) - results_file = self._data_path('service', 'manifest', 'terra', 'pfb_manifest.schema.json') - if results_file.exists(): - with open(results_file, 'r') as f: - expected_records = json.load(f) - self.assertEqual(expected_records, json.loads(to_json(schema))) + def _assert_or_create_json_can(self, expected: Path, actual: str): + if expected.exists(): + with open(expected, 'r') as f: + expected = json.load(f) + self.assertEqual(expected, json.loads(actual)) else: - with open(results_file, 'w') as f: - f.write(to_json(schema)) + with open(expected, 'w') as f: + f.write(actual) diff --git a/test/service/test_manifest.py b/test/service/test_manifest.py index 5ed52e321..8a204a682 100644 --- a/test/service/test_manifest.py +++ b/test/service/test_manifest.py @@ -340,10 +340,6 @@ def test_terra_pfb_manifest(self): shared_file_bundle = self._shared_file_bundle(new_bundle_fqid) self._index_bundle(shared_file_bundle, delete=False) - def to_json(records): - # 'default' is specified to handle the conversion of datetime values - return json.dumps(records, indent=4, sort_keys=True, default=str) - # We write entities differently depending on debug so we test both cases for debug in (1, 0): with self.subTest(debug=debug): @@ -355,14 +351,10 @@ def to_json(records): schema = reader.writer_schema self._assert_pfb_schema(schema) records = list(reader) - results_file = self._canned_manifest_path('terra', 'pfb_manifest.results.json') - if results_file.exists(): - with open(results_file, 'r') as f: - expected_records = json.load(f) - self.assertEqual(expected_records, json.loads(to_json(records))) - else: - with open(results_file, 'w') as f: - f.write(to_json(records)) + expected = self._canned_manifest_path('terra', 'pfb_manifest.results.json') + # 'default' is specified to handle the conversion of datetime values + actual = json.dumps(records, indent=4, sort_keys=True, default=str) + self._assert_or_create_json_can(expected, actual) def _shared_file_bundle(self, bundle): """