def test_discover_first(): definition = ("TEST", [("AAAA", Struct())]) collection = SubrecordCollection.from_definition(*definition) discovered = collection.discover([], "AAAA") assert isinstance(discovered, Subrecord) assert discovered.name == "AAAA"
def test_discover_nested_required(): definition = ( "TEST", [("AAAA", Struct()), ("BBBB", [("CCCC", Struct()), ("DDDD", Struct())])], ) collection = SubrecordCollection.from_definition(*definition) with pytest.raises(exceptions.UnexpectedSubrecord): collection.discover(["AAAA"], "DDDD")
def test_discover_nested_first(): definition = ("TEST", [("AAAA", Struct()), ("BBBB", [("CCCC", Struct())])]) collection = SubrecordCollection.from_definition(*definition) discovered = collection.discover(["AAAA"], "CCCC") assert isinstance(discovered, Subrecord) assert discovered.name == "CCCC" definition = ( "TEST", [("AAAA", Struct() * "0"), ("BBBB", [("AAAA", Struct() * "1")])], ) collection = SubrecordCollection.from_definition(*definition) discovered = collection.discover(["AAAA"], "AAAA") assert isinstance(discovered, Subrecord) assert discovered.name == "AAAA" assert discovered.struct.docs == "1"
def test_discover_none(): definition = ("TEST", [("AAAA", Struct()), ("BBBB", Struct())]) collection = SubrecordCollection.from_definition(*definition) with pytest.raises(exceptions.UnexpectedSubrecord): collection.discover(["AAAA", "BBBB"], "AAAA") with pytest.raises(exceptions.UnexpectedSubrecord): collection.discover(["AAAA", "BBBB"], "BBBB")
def test_discover_nested_optional(): definition = ( "TEST", [("AAAA", Struct()), ("BBBB", [("CCCC?", Struct()), ("DDDD", Struct())])], ) collection = SubrecordCollection.from_definition(*definition) discovered = collection.discover(["AAAA"], "DDDD") assert isinstance(discovered, Subrecord) assert discovered.name == "DDDD"
def subrecord_collection( draw, min_count: int = COLLECTION_MIN_COUNT, max_count: int = COLLECTION_MAX_COUNT, level_limit: int = COLLECTION_LEVEL_LIMIT, ): return SubrecordCollection.from_definition(*draw( subrecord_collection_definition(min_count=min_count, max_count=max_count, level_limit=level_limit)))
def test_from_definition(definition): subc = SubrecordCollection.from_definition(*definition) assert isinstance(subc, SubrecordCollection) assert definition[0].startswith(subc.name) assert [item.to_definition() for item in subc.items] == definition[-1] if definition[0][-1] in ("*", "+", "?"): (optional, multiple) = SubrecordCollection.parse_flag(definition[0][-1]) assert subc.optional == optional assert subc.multiple == multiple else: assert not subc.optional assert not subc.multiple
def test_discover_multiple(multiple_scale): definition = ("TEST", [("AAAA+", Struct()), ("BBBB", Struct())]) collection = SubrecordCollection.from_definition(*definition) discovered = collection.discover([], "AAAA") assert isinstance(discovered, Subrecord) assert discovered.name == "AAAA" discovered = collection.discover(["AAAA"] * multiple_scale, "AAAA") assert isinstance(discovered, Subrecord) assert discovered.name == "AAAA" with pytest.raises(exceptions.UnexpectedSubrecord): collection.discover([], "BBBB")
def test_discover_nested_multiple(multiple_scale): definition = ( "TEST", [("AAAA", Struct()), ("BBBB+", [("CCCC", Struct()), ("DDDD", Struct())])], ) collection = SubrecordCollection.from_definition(*definition) discovered = collection.discover(["AAAA"], "CCCC") assert isinstance(discovered, Subrecord) assert discovered.name == "CCCC" discovered = collection.discover( ["AAAA"] + (["CCCC", "DDDD"] * multiple_scale), "CCCC" ) assert isinstance(discovered, Subrecord) assert discovered.name == "CCCC" with pytest.raises(exceptions.UnexpectedSubrecord): collection.discover(["AAAA"], "DDDD")
def test_to_definition(definition): subc = SubrecordCollection.from_definition(*definition) assert isinstance(subc, SubrecordCollection) assert subc.to_definition() == definition