def test_bulk_create_entity_with_uuid(token, service): client = Client(token, service, domain="states") an_uuid = uuid.uuid4() entities = [{"uuid": an_uuid, "name": "an entity with a uuid"}] entity = client.bulk_create(entities)[0] assert entity.uuid == an_uuid entity.delete()
def test_best_match_or_create_with_uuid(token, service): client = Client(token, service, domain="states") an_uuid = uuid.uuid4() entity = client.best_match_or_create({"name": "Xanadu"}, create_attrs={"uuid": an_uuid}, threshold=75) assert entity.uuid == an_uuid
def test_create(token, service): client = Client(token, service) domain = client.get_domain("states") client.set_domain(domain) entity = client.create({"name": "District of Columbia"}) assert entity.name == "District of Columbia" entity.delete() # Taxation w/out representation!
def test_get_domain(token, service): client = Client(token, service) domain = client.get_domain("states") assert domain.slug == "states" with pytest.raises(MalformedDomain): client.get_domain(1)
def test_update_entity_by_match_invalid_data_error(token, service): client = Client(token, service, domain="states") with pytest.raises(MalformedUpdateAttributes): client.update_match({"name": "Xanadu"}, {"stuff": { "nested": "too deeply" }})
def test_setup(token, service): client = Client(token, service) client.create_domain("presidents") client.create_domain("politicians") client.bulk_create([{"name": "George W. Bush"}], domain="politicians") client.bulk_create([{"name": "George W. Bush"}], domain="presidents") client.bulk_create([{"name": "George Bush, Jr."}], domain="presidents")
def test_get_entities_with_block_attrs(token, service): client = Client(token, service, domain="states") entities = client.get_entities({"country": "USA"}) assert len(entities) == 51 entities = client.get_entities({"postal_code": "KS"}) assert entities[0].name == "Kansas"
def test_entity_attrs(token, service): client = Client(token, service, domain="presidents") entity = client.match({"name": "George W. Bush"}) assert entity.attrs().sort() == ["uuid", "name", "number"].sort() assert entity.attrs("number") == 43 with pytest.raises(AttributeError): entity.attrs("age")
def validate_block_attrs_nested(token, service): client = Client(token, service) with pytest.raises(MalformedBlockAttributes): client.best_match({"name": "entity"}, block_attrs={"nested": { "too": "deep" }})
def test_bulk_create_entity_with_reserved_attribute_error(token, service): client = Client(token, service, domain="states") entities = [{ "a": "bad entity", "created": "with a reserved key, 'created'" }] with pytest.raises(CreateEntityError): client.bulk_create(entities)
def test_entity_set_superseded_by(token, service): client = Client(token, service) entity = client.best_match({"name": "George W. Bush"}, domain="presidents") superseded = client.best_match( {"name": "George W. Bush"}, domain="politicians" ) superseded.set_superseded_by(entity) assert superseded.superseded_by == entity.uuid
def test_create_or_alias_creates_new(token, service): client = Client(token, service, domain="states") entity = client.alias_or_create( {"name": "Alderaan"}, create_attrs={"galaxy": "Far, far away"}, threshold=90, ) assert entity.name == "Alderaan" and entity.aliased is False
def test_entity_remove_alias_for(token, service): client = Client(token, service, domain="presidents") entity = client.best_match( {"name": "George Bush, Jr."}, return_canonical=False ) assert entity.name == "George Bush, Jr." entity.remove_alias_for() assert entity.alias_for is None
def test_set_threshold_exceptions(token, service): client = Client(token, service) with pytest.raises(MissingThreshold): client.set_threshold() with pytest.raises(MalformedThreshold): client.set_threshold(200) with pytest.raises(MalformedThreshold): client.set_threshold("asd")
def test_entity_attributes_of_various_types(token, service): client = Client(token, service, domain="states") entities = [{"real": False, "number": 53, "listicle": [1, "one"]}] entity = client.bulk_create(entities)[0] assert entity.real is False assert entity.number == 53 assert entity.listicle == [1, "one"] entity.delete()
def test_create_domain_exceptions(token, service): client = Client(token, service) with pytest.raises(MissingDomain): client.create_domain() with pytest.raises(MalformedDomain): client.create_domain("countries", parent="a parent name") with pytest.raises(MalformedDomain): client.create_domain("countries", parent=1)
def test_domain_delete(token, service): client = Client(token, service) domain = client.get_domain("politicians") domain.delete() assert domain.deleted is True domain = client.get_domain("presidents") domain.delete() assert domain.deleted is True
def test_best_match_with_block_attrs(token, service): client = Client(token, service, domain="states") entity = client.best_match({"name": "Arkansas"}, block_attrs={"postal_code": "KS"}) assert entity.name == "Kansas" entity = client.best_match({"name": "Arkansas"}) assert entity.name == "Arkansas"
def test_set_domain(token, service): client = Client(token, service) client.set_domain("sharks") assert client.domain == "sharks" domain = client.create_domain("whales") client.set_domain(domain) assert client.domain == "whales" domain.delete()
def test_update_entity_by_id(token, service): client = Client(token, service, domain="states") entity = client.best_match({"name": "Xanadu"}) entity = client.update_by_id(entity.uuid, { "sacred river": "Alph", "name": "Zanadu" }) assert entity.name == "Zanadu" entity = client.update_by_id(entity.uuid, {"name": "Xanadu"}) assert entity.sacred_river == "Alph" and entity.name == "Xanadu"
def test_bulk_create(token, service): client = Client(token, service) domain = client.get_domain("states") states = [{ "name": s.name, "postal_code": s.abbr, "country": "USA" } for s in us.states.STATES] entities = client.bulk_create(states, domain=domain) assert entities[0].name == states[0]["name"]
def test_get_domains(token, service): client = Client(token, service) domains = client.get_domains() assert len(domains) == 4 states = client.get_domain("states") domains = client.get_domains(parent=states) assert len(domains) == 2 domains = client.get_domains(parent="states") assert len(domains) == 2
def test_delete_domain(token, service): client = Client(token, service) # Client passed slug response = client.delete_domain("cities") assert response is True # Client passed instance domain = client.get_domain("counties") response = client.delete_domain(domain) assert response is True # Instance method domain = client.get_domain("townships") domain.delete() assert domain.deleted is True
def test_empty_domain(token, service): client = Client(token, service) domain = client.create_domain("empty domain") entity = client.best_match({"name": "does not exist"}, domain=domain) assert entity is None entity = client.best_match_or_create({"name": "should create"}, domain=domain) assert entity.created is True assert entity.name == "should create" entity.delete() domain.delete()
def test_match_or_create(token, service): client = Client(token, service, domain="states") entity = client.match_or_create({"name": "Thunderdome"}, create_attrs={"country": "Australia"}) assert entity.created is True and entity.name == "Thunderdome" entity2 = client.match_or_create({"name": "Gas City"}, create_attrs={"country": "Australia"}) assert entity2.created is True and entity2.name == "Gas City" with pytest.raises(UnspecificQueryError): client.match_or_create({"country": "Australia"}) entity.delete() entity2.delete()
def test_update_domain(token, service): client = Client(token, service) # Client passed slug domain = client.update_domain("counties", { "name": "kounties", "parent": None }) assert domain.name == "kounties" and domain.parent is None # Client passed instance domain = client.update_domain(domain, {"name": "counties", "parent": None}) assert domain.name == "counties" # Instance method domain.update({"name": "kounties"}) assert domain.name == "kounties" # Exception with pytest.raises(MalformedDomain): client.update_domain(1, {"name": "counties"})
def test_entity_delete(token, service): client = Client(token, service, domain="presidents") entity = client.best_match({"name": "George W. Bush"}) entity.delete() assert entity.deleted is True entity = client.best_match( {"name": "George Bush, Jr."}, return_canonical=False ) entity.delete() assert entity.deleted is True entity = client.best_match( {"name": "George W. Bush"}, domain="politicians" ) entity.delete() assert entity.deleted is True
def test_entity_cannot_alias_itself(token, service): client = Client(token, service) domain = client.create_domain("temp domain") entity1, entity2 = client.bulk_create([{ "name": "A fake Entity" }, { "name": "A second faker" }], domain=domain) with pytest.raises(BadResponse): entity1.set_alias_for(entity1) assert entity1.alias_for is None entity1.set_alias_for(entity2) assert entity1.alias_for == entity2.uuid entity1.delete() entity2.delete() domain.delete()
def test_create_domain(token, service): client = Client(token, service) states = client.create_domain("states") assert states.name == "states" counties = client.create_domain("counties", parent="states") assert counties.name == "counties" and counties.parent == "states" cities = client.create_domain("cities", parent=states) assert cities.name == "cities" and cities.parent == "states" with pytest.raises(MalformedDomain): client.create_domain(1) with pytest.raises(MalformedDomain): client.create_domain("townships", parent=1) townships = client.create_domain("townships", parent=states) assert townships.name == "townships" and townships.parent == "states"
def test_get_domain_entities(token, service): client = Client(token, service) domain = client.create_domain("temp domain") client.set_domain(domain) client.create({"name": "fake entity 1"}) entities = domain.get_entities() assert entities[0].name == "fake entity 1" client.create({"name": "fake entity 2", "block": "attr"}) entities = domain.get_entities() assert len(entities) == 2 entities = domain.get_entities({"block": "attr"}) assert len(entities) == 1 assert entities[0].name == "fake entity 2" entities = domain.get_entities({"bad": "attr"}) assert len(entities) == 0 for entity in domain.get_entities(): entity.delete() domain.delete()