Skip to content

Commit

Permalink
Consolidate handling of create-if-missing cans
Browse files Browse the repository at this point in the history
  • Loading branch information
hannes-ucsc committed Nov 23, 2024
1 parent 5b9fd9b commit bf3307a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
22 changes: 13 additions & 9 deletions test/pfb_test_case.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import json
from pathlib import (
Path,
)

import fastavro

Expand All @@ -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)
16 changes: 4 additions & 12 deletions test/service/test_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
"""
Expand Down

0 comments on commit bf3307a

Please sign in to comment.