Skip to content

Commit 61c8ec6

Browse files
committed
more code review
1 parent 1c53110 commit 61c8ec6

1 file changed

Lines changed: 51 additions & 19 deletions

File tree

python/tests/test_metadata.py

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -649,8 +649,49 @@ def test_disallow_duplicate_keys(self):
649649
):
650650
tskit.MetadataSchema(schema)
651651

652-
def schema_with_binary(self, num_blobs):
653-
# produces a json+struct schema having some number of integers
652+
def test_round_trip_with_struct_and_json(self):
653+
schema = {
654+
"codec": "json+struct",
655+
"json": {
656+
"type": "object",
657+
"properties": {
658+
"label": {"type": "string"},
659+
"count": {"type": "number"},
660+
"stuff": {
661+
"type": "array",
662+
"arrayLengthFormat": "h",
663+
"items": {"type": "number"},
664+
},
665+
},
666+
"required": ["label"],
667+
},
668+
"struct": {
669+
"type": "object",
670+
"properties": {
671+
"xyz": {
672+
"type": "array",
673+
"arrayLengthFormat": "H",
674+
"items": {"type": "number", "binaryFormat": "i"},
675+
},
676+
},
677+
},
678+
}
679+
ms = tskit.MetadataSchema(schema)
680+
for v in [[], [0, 2, 12], [5] * 1000]:
681+
row = {
682+
"label": "abcdef xyz",
683+
"count": 7,
684+
"b0": 123,
685+
"b1": 0,
686+
"stuff": [1, 3, 2, -1.5],
687+
"xyz": v,
688+
}
689+
encoded = ms.validate_and_encode_row(row)
690+
out = ms.decode_row(encoded)
691+
assert out == row
692+
693+
def schema_with_binary(self, num_binary_ints):
694+
# produces a json+struct schema having num_binary_ints integers
654695
# encoded in binary, labeled b0, ... bX
655696
schema = {
656697
"codec": "json+struct",
@@ -667,28 +708,13 @@ def schema_with_binary(self, num_blobs):
667708
"properties": {},
668709
},
669710
}
670-
for j in range(num_blobs):
711+
for j in range(num_binary_ints):
671712
schema["struct"]["properties"][f"b{j}"] = {
672713
"type": "integer",
673714
"binaryFormat": "i",
674715
}
675716
return tskit.MetadataSchema(schema)
676717

677-
def test_round_trip_with_struct_and_json(self):
678-
schema = dict(self.schema_with_binary(2).schema)
679-
# adds a 'xyz' property to the schema, which stores an array of numbers
680-
schema["struct"]["properties"]["xyz"] = {
681-
"type": "array",
682-
"arrayLengthFormat": "H",
683-
"items": {"type": "number", "binaryFormat": "i"},
684-
}
685-
ms = tskit.MetadataSchema(schema)
686-
for v in [[], [0, 2, 12], [5] * 1000]:
687-
row = {"label": "abcdef xyz", "count": 7, "b0": 123, "b1": 0, "xyz": v}
688-
encoded = ms.validate_and_encode_row(row)
689-
out = ms.decode_row(encoded)
690-
assert out == row
691-
692718
@pytest.mark.parametrize("k", (0, 1, 5, 1001))
693719
def test_byte_alignment(self, k):
694720
# We want to test whether the binary portion begins byte-aligned.
@@ -704,8 +730,14 @@ def test_byte_alignment(self, k):
704730
bytes_per_blob = len(struct.pack("i", 0))
705731
for s in [
706732
"",
733+
"a",
734+
"ab",
707735
"abc",
708-
"superfragilisticexpialodocious",
736+
"abcd",
737+
"abcde",
738+
"abcdef",
739+
"abcdefg",
740+
"abcdefgh",
709741
" " * 1000 + "foo" + " " * 1000,
710742
]:
711743
row = {"label": s, "count": 7}

0 commit comments

Comments
 (0)