def test_invalid_items(name, items, optional, multiple): if isinstance(items, list) and len(items) <= 0: with pytest.raises(ValueError): SubrecordCollection(name, items, optional, multiple) else: with pytest.raises(TypeError): SubrecordCollection(name, items, optional, multiple)
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_from_dict(dictionary): subc = SubrecordCollection.from_dict(dictionary) assert isinstance(subc, SubrecordCollection) assert subc.name == dictionary["name"] assert subc.items == [ SubrecordCollection.from_dict(item) if "items" in item else Subrecord.from_dict(item) for item in dictionary["items"] ] assert subc.optional == dictionary["optional"] assert subc.multiple == dictionary["multiple"]
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_init(name, items, optional, multiple): subc = SubrecordCollection(name, items, optional=optional, multiple=multiple) assert isinstance(subc, SubrecordCollection) assert subc.name == name assert subc.items == items assert subc.optional == optional assert subc.multiple == multiple
def test_be(collection, flag): (optional, multiple) = SubrecordCollection.parse_flag(flag) subc = collection.be(flag) assert isinstance(subc, SubrecordCollection) assert subc.name == collection.name assert subc.items == collection.items assert subc.optional == optional assert subc.multiple == multiple
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 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_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 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_invalid_optional(name, items, optional, multiple): with pytest.raises(TypeError): SubrecordCollection(name, items, optional, multiple)
def test_to_definition(definition): subc = SubrecordCollection.from_definition(*definition) assert isinstance(subc, SubrecordCollection) assert subc.to_definition() == definition
def test_invalid_name(name, items, optional, multiple): if not isinstance(name, str): with pytest.raises(TypeError): SubrecordCollection(name, items, optional, multiple)
def test_to_dict(dictionary): subc = SubrecordCollection.from_dict(dictionary) assert isinstance(subc, SubrecordCollection) assert subc.to_dict() == dictionary