Skip to content

Commit

Permalink
Merge pull request #33 from lsst/tickets/DM-39886
Browse files Browse the repository at this point in the history
DM-39886: Update alert_packet unit tests
  • Loading branch information
bsmartradio authored Jul 7, 2023
2 parents 1b65047 + e96ab69 commit 870a935
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.

Large diffs are not rendered by default.

25 changes: 21 additions & 4 deletions test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,18 @@ def test_retrieve_alerts(self):
"""Write some alerts to a file. They should be readable back out.
"""
alerts = self._mock_alerts(5)

with self._temp_alert_file(alerts) as alert_file:
have_schema, have_alerts_iterable = retrieve_alerts(alert_file, self.test_schema)
have_alerts = list(have_alerts_iterable)

self.assert_alert_lists_equal(alerts, list(have_alerts))
self.assertEqual(self.test_schema, have_schema)

fastavro_keys = list(self.test_schema.definition.keys())
for key in fastavro_keys:
if '__' in key and '__len__' not in key:
self.test_schema.definition.pop(key)

self.assertEqual(self.test_schema.definition, have_schema.definition)

def test_alert_file_with_one_alert(self):
"""Write a single alert to a file. It should be readable back out.
Expand All @@ -119,7 +124,13 @@ def test_alert_file_with_one_alert(self):
have_alerts = list(have_alerts_iterable)

self.assert_alert_lists_equal(alerts, list(have_alerts))
self.assertEqual(self.test_schema, have_schema)

fastavro_keys = list(self.test_schema.definition.keys())
for key in fastavro_keys:
if '__' in key and '__len__' not in key:
self.test_schema.definition.pop(key)

self.assertEqual(self.test_schema.definition, have_schema.definition)

def test_alert_file_with_no_alerts(self):
"""Write an alert file that contains no alerts at all. It should be readable.
Expand All @@ -131,4 +142,10 @@ def test_alert_file_with_no_alerts(self):
have_alerts = list(have_alerts_iterable)

self.assert_alert_lists_equal(alerts, list(have_alerts))
self.assertEqual(self.test_schema, have_schema)

fastavro_keys = list(self.test_schema.definition.keys())
for key in fastavro_keys:
if '__' in key and '__len__' not in key:
self.test_schema.definition.pop(key)

self.assertEqual(self.test_schema.definition, have_schema.definition)
28 changes: 23 additions & 5 deletions test/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,15 @@ def test_recursive_resolve(self):
"""Check that resolution of nested schemas gives the expected result.
"""
# Definition of schemas in terms of each other.
named_schemas = {}
sub_sub_schema = fastavro.parse_schema({
"name": "subsub",
"namespace": "lsst",
"type": "record",
"fields": [
{"name": "sub_sub_field", "type": "string"}
]
})
}, named_schemas)

sub_schema = fastavro.parse_schema({
"name": "sub",
Expand All @@ -67,7 +68,7 @@ def test_recursive_resolve(self):
{"name": "sub_field", "type": "lsst.subsub"},
{"name": "second_sub_field", "type": "lsst.subsub"}
]
})
}, named_schemas)

top_schema = fastavro.parse_schema({
"name": "top",
Expand All @@ -77,7 +78,7 @@ def test_recursive_resolve(self):
{"name": "top_field", "type": "lsst.sub"},
{"name": "boring_field", "type": "int"}
]
})
}, named_schemas)

# Derived by substituting the above into each other by hand.
model_resolved_schema = {
Expand Down Expand Up @@ -105,7 +106,16 @@ def test_recursive_resolve(self):
},
{
"name": "second_sub_field",
"type": "lsst.subsub"
"type": {
"type": "record",
"name": "lsst.subsub",
"fields": [
{
"name": "sub_sub_field",
"type": "string"
}
]
}
}
]
}
Expand All @@ -118,4 +128,12 @@ def test_recursive_resolve(self):
}

resolved_schema = Schema(top_schema).definition
self.assertEqual(resolved_schema, model_resolved_schema)
partially_resolved_schema = fastavro.schema.expand_schema(resolved_schema)
fully_resolved_schema = fastavro.schema.expand_schema(partially_resolved_schema)

fastavro_keys = list(fully_resolved_schema.keys())
for key in fastavro_keys:
if '__' in key and '__len__' not in key:
fully_resolved_schema.pop(key)

self.assertEqual(fully_resolved_schema, model_resolved_schema)
6 changes: 6 additions & 0 deletions test/test_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ def test_example_avro(self):
schema.retrieve_alerts(f)
else:
retrieved_schema, alerts = schema.retrieve_alerts(f)

fastavro_keys = list(schema.definition.keys())
for key in fastavro_keys:
if '__' in key and '__len__' not in key:
schema.definition.pop(key)

self.assertEqual(retrieved_schema, schema,
f"schema not equal on version={version}")
for idx, alert in enumerate(alerts):
Expand Down

0 comments on commit 870a935

Please sign in to comment.