def test_model_instances_can_be_equal(): person = Person() assert person == person assert Person(key=Key("Person", 1)) == Person(key=Key("Person", 1)) assert Person(key=Key("Person", 1), first_name="John") == Person(key=Key("Person", 1), first_name="John")
def test_keys_are_hashable(): entities = {} for i in range(10): entities[Key("Person", i)] = i for i in range(10): assert entities[Key("Person", i)] == i
def test_keys_can_be_equal(): key = Key("Foo") assert key == key assert Key("Foo") == Key("Foo") assert Key("Foo", 123) == Key("Foo", 123) assert Key("Foo", 123, parent=Key("Bar", 1)) == Key("Foo", 123, parent=Key("Bar", 1))
def test_restricted_key_properties_can_only_be_assigned_keys_of_that_kind( person): entity = models.ModelWithRestrictedKeyProperty() entity.k = person.key with pytest.raises(ValueError): entity.k = Key("Foo", 1)
def person_in_ns(adapter): person = Person(email="*****@*****.**", first_name="Namespaced", last_name="Person") person.key = Key(Person, namespace="a-namespace") person.put() yield person person.delete()
def person_with_ancestor(person): child = Person( email="*****@*****.**", first_name="Child", last_name="Person", ) child.key = Key(Person, parent=person.key, namespace=person.key.namespace) child.put() yield child child.delete()
def people(adapter): people = put_multi([ Person(key=Key(Person, i), email=f"{i}@example.com", first_name="Person", last_name=str(i)) for i in range(1, 21) ]) yield people delete_multi([person.key for person in people])
def people(adapter): people = [] for i in range(1, 21): person = Person(email=f"{i}@example.com", first_name="Person", last_name=str(i)) person.key = Key(Person, i) people.append(person.put()) yield people delete_multi([person.key for person in people])
def test_keys_can_fail_to_get_single_entities(adapter): assert Key(models.Person, "nonexistent").get() is None
def test_keys_can_be_not_equal(): assert Key("Foo") != Key("Bar") assert Key("Foo", 123) != Key("Foo", 124) assert Key("Foo", 123, parent=Key("Bar", 1)) != Key( "Foo", 123, parent=Key("Bar", 2)) assert Key("Foo", 123, namespace="foos") != Key( "Foo", 123, parent=Key("Bar", 1))
def test_key_delete_deletes_nonexistent_entities(adapter): assert Key("Person", 123).delete() is None
def test_keys_can_be_full(): assert not Key("Person", 123).is_partial
def test_keys_can_be_partial(): assert Key("Person").is_partial
def create(cls, user): session = cls(user=user) session.key = Key(Session, str(uuid4())) return session.put()
def test_partial_keys_dont_have_an_id(): assert Key("Person").id_or_name is None assert Key("Person").int_id is None assert Key("Person").str_id is None
def test_keys_get_multi_fails_unknown_kind(): with pytest.raises(RuntimeError): get_multi([Key("UnknownKind")])
def test_keys_can_lookup_models_by_kind(): assert Key("Person").get_model()
def test_keys_namespace_must_match_parent_namespace(): parent = Key("Organization", 123, namespace="a") with pytest.raises(ValueError): Key("Person", 123, parent=parent, namespace="b")
def test_keys_can_be_assigned_full_keys(): assert props.Key().validate(Key("Person", 12))
def test_key_inherits_parents_namespace(): parent = Key("Organization", 123, namespace="a") assert Key("Person", 123, parent=parent).namespace == parent.namespace
def test_keys_parents_must_not_be_partial(): with pytest.raises(ValueError): Key("Person", 123, parent=Key("Organization"))
def test_keys_are_hierarchical(): assert Key("Person", 123, parent=Key("Organization", 1)).path == ("Organization", 1, "Person", 123)
def test_keys_get_multi_fails_given_partial_keys(): with pytest.raises(RuntimeError): get_multi([Key("Person")])
def test_keys_can_fail_to_lookup_models_by_kind(): with pytest.raises(RuntimeError): Key("UnknownKind").get_model()
def test_model_instances_can_not_be_equal(): assert Person() != {} assert Person(key=Key("Person", 1)) != Person(key=Key("Person", 2)) assert Person(key=Key("Person", 1), first_name="John") != Person( key=Key("Person", 1), first_name="Jane")
def test_keys_reprs_can_be_used_to_rebuild_them(): assert repr(Key("Foo", 123)) == "Key('Foo', 123, parent=None, namespace='')"
def test_keys_can_have_numeric_ids(): assert Key("Person", 123).int_id == 123 assert Key("Person", 123).str_id is None
def test_keys_can_be_constructed_from_models(): assert Key(models.Person) == Key("Person")
def test_keys_cannot_be_assigned_partial_keys(): with pytest.raises(ValueError): key = props.Key() key.validate(Key("Person"))
def test_keys_can_have_string_ids(): assert Key("Person", "Jim").str_id == "Jim" assert Key("Person", "Jim").int_id is None