import pytest import atlas_um.tokens.claim_values_serializers as sut class TestClaimValuesSerializer: def test_of_BooleanAnd(self): serializer = sut.RegisteredSerializers.of("BooleanAnd") assert isinstance(serializer, sut.BooleanAndClaimValuesSerializer) def test_of_BooleanAndId(self): serializer = sut.RegisteredSerializers.of("BooleanAndId") assert isinstance(serializer, sut.BooleanAndIdClaimValuesSerializer) def test_of_ListAppend(self): serializer = sut.RegisteredSerializers.of("ListAppend") assert isinstance(serializer, sut.ListAppendClaimValuesSerializer) def test_of_unknown(self): with pytest.raises(KeyError): sut.RegisteredSerializers.of("__UnknownSerializer") def test_of_none(self): with pytest.raises(KeyError): sut.RegisteredSerializers.of(None) class TestBooleanAndClaimValuesSerializer: def test_empty_list(self): sval = sut.BooleanAndClaimValuesSerializer().serialize([]) assert sval is None def test_single(self, claim_value_factory): claim_value = claim_value_factory.build(boolean=True) sval = sut.BooleanAndClaimValuesSerializer().serialize([claim_value]) assert sval == claim_value.components def test_all_true(self, claim_value_factory, faker): few = faker.pyint(min_value=2, max_value=10) claim_values = claim_value_factory.build_batch(few, components=True) sval = sut.BooleanAndClaimValuesSerializer().serialize(claim_values) assert sval is True def test_all_false(self, claim_value_factory, faker): few = faker.pyint(min_value=2, max_value=10) claim_values = claim_value_factory.build_batch(few, components=False) sval = sut.BooleanAndClaimValuesSerializer().serialize(claim_values) assert sval is False def test_some_false(self, claim_value_factory, faker): before_count = faker.pyint(min_value=1, max_value=4) before = claim_value_factory.build_batch(before_count, boolean=True) hole = claim_value_factory.build(components=False) after_count = faker.pyint(min_value=1, max_value=4) after = claim_value_factory.build_batch(after_count, boolean=True) claim_values = before + [hole] + after sval = sut.BooleanAndClaimValuesSerializer().serialize(claim_values) assert sval is False class TestBooleanAndIdClaimValuesSerializer: def test_empty_list(self): sval = sut.BooleanAndIdClaimValuesSerializer().serialize([]) assert sval is None def test_single(self, claim_value_factory, faker): id = faker.pystr() claim_value = claim_value_factory.build(external_id=id) sval = sut.BooleanAndIdClaimValuesSerializer().serialize([claim_value]) assert sval == [id] def test_multiple_objects(self, claim_value_factory, faker): few = faker.pyint(min_value=2, max_value=5) ids = [faker.pystr() for i in range(few)] claim_values = [ claim_value_factory.build(external_id=id) for id in ids ] sval = sut.BooleanAndIdClaimValuesSerializer().serialize(claim_values) assert sval == ids[-1:] class TestListAppendClaimValuesSerializer: def test_empty_list(self): sval = sut.ListAppendClaimValuesSerializer().serialize([]) assert sval is None def test_single_string(self, claim_value_factory, faker): string = faker.pystr() claim_value = claim_value_factory.build(components=string) sval = sut.ListAppendClaimValuesSerializer().serialize([claim_value]) assert sval == [string] def test_multiple_strings(self, claim_value_factory, faker): few = faker.pyint(min_value=2, max_value=5) componentss = [faker.pystr() for i in range(few)] claim_values = list( map(lambda a: claim_value_factory.build(components=a), componentss) ) sval = sut.ListAppendClaimValuesSerializer().serialize(claim_values) assert sval == componentss def test_single_object(self, claim_value_factory, faker): components = faker.pydict() claim_value = claim_value_factory.build(components=components) sval = sut.ListAppendClaimValuesSerializer().serialize([claim_value]) assert sval == [components] def test_multiple_objects(self, claim_value_factory, faker): few = faker.pyint(min_value=2, max_value=5) componentss = [faker.pydict() for i in range(few)] claim_values = list( map(lambda a: claim_value_factory.build(components=a), componentss) ) sval = sut.ListAppendClaimValuesSerializer().serialize(claim_values) assert sval == componentss class TestListAppendIdClaimValuesSerializer: def test_empty_list(self): sval = sut.ListAppendIdClaimValuesSerializer().serialize([]) assert sval is None def test_single_object(self, claim_value_factory, faker): string = faker.pystr() claim_value = claim_value_factory.build(external_id=string) sval = sut.ListAppendIdClaimValuesSerializer().serialize([claim_value]) assert sval == [string] def test_multiple_objects(self, claim_value_factory, faker): few = faker.pyint(min_value=2, max_value=5) ids = [faker.pystr() for i in range(few)] claim_values = [ claim_value_factory.build(external_id=id) for id in ids ] sval = sut.ListAppendIdClaimValuesSerializer().serialize(claim_values) assert sval == ids def test_multiple_objects_ints(self, claim_value_factory, faker): few = faker.pyint(min_value=2, max_value=5) ids = list(range(1, few)) claim_values = [ claim_value_factory.build(external_id=id) for id in ids ] sval = sut.ListAppendIdClaimValuesSerializer().serialize(claim_values) assert sval == ids def test_wrong_value(self, claim_value_factory, faker): claim_value = claim_value_factory.build(external_id="") sval = sut.ListAppendIdClaimValuesSerializer().serialize([claim_value]) assert sval == []