예제 #1
0
def test_put_model_matches_point_query() -> None:
    model = SimpleModel(id="test", str_prop="asdf")
    key = model.put()

    get_resp = SimpleModel.get_by_id("test")
    assert get_resp == model
    assert model.key == key
예제 #2
0
def test_query_count_async_not_found(
    ndb_stub: datastore_stub.LocalDatastoreStub, ) -> None:
    model = SimpleModel(id="test", str_prop="asdf")
    model.put()

    count = SimpleModel.query().filter(
        SimpleModel.str_prop == "foo").count_async()
    assert count.get_result() == 0
예제 #3
0
def test_get_existing_by_multi_field(
    ndb_stub: datastore_stub.LocalDatastoreStub, ) -> None:
    model = SimpleModel(id="test", str_prop="asdf", int_prop=42)
    ndb_stub._insert_model(model)

    query_res = SimpleModel.query(SimpleModel.str_prop == "asdf",
                                  SimpleModel.int_prop == 42).get()
    assert query_res == model
예제 #4
0
def test_query_keys_only() -> None:
    stored_keys = ndb.put_multi(
        [SimpleModel(id=f"test{i}", int_prop=i) for i in range(10)])
    assert len(stored_keys) == 10

    resp = SimpleModel.query().fetch(keys_only=True)
    assert len(resp) == 10
    assert all(isinstance(i, ndb.Key) for i in resp)
예제 #5
0
def test_query_all() -> None:
    stored_keys = ndb.put_multi(
        [SimpleModel(id=f"test{i}", int_prop=i) for i in range(10)])
    assert len(stored_keys) == 10

    query_all = SimpleModel.query().order(SimpleModel.int_prop).fetch()
    assert len(query_all) == 10
    for i, model in enumerate(query_all):
        assert model.int_prop == i
예제 #6
0
def test_get_or_insert_then_update() -> None:
    model = SimpleModel.get_or_insert("test", int_prop=10)
    assert model is not None

    model.int_prop = 20
    model.put()

    sanity_check = SimpleModel.get_by_id("test")
    assert sanity_check == model
예제 #7
0
def test_count_existing_by_field(
        ndb_stub: datastore_stub.LocalDatastoreStub) -> None:
    model = SimpleModel(
        id="test",
        str_prop="asdf",
    )
    ndb_stub._insert_model(model)

    count = SimpleModel.query(SimpleModel.str_prop == "asdf").count()
    assert count == 1
예제 #8
0
def test_get_existing_by_id(
        ndb_stub: datastore_stub.LocalDatastoreStub) -> None:
    model = SimpleModel(
        id="test",
        str_prop="asdf",
    )
    ndb_stub._insert_model(model)

    model_res = SimpleModel.get_by_id("test")
    assert model_res == model
예제 #9
0
def test_get_existing_by_field_not_found(
    ndb_stub: datastore_stub.LocalDatastoreStub, ) -> None:
    model = SimpleModel(
        id="test",
        str_prop="asdf",
    )
    ndb_stub._insert_model(model)

    query_res = SimpleModel.query(SimpleModel.str_prop == "foo").get()
    assert query_res is None
예제 #10
0
def test_query_in_range() -> None:
    stored_keys = ndb.put_multi(
        [SimpleModel(id=f"test{i}", int_prop=i) for i in range(10)])
    assert len(stored_keys) == 10

    resp = (SimpleModel.query(SimpleModel.int_prop >= 1,
                              SimpleModel.int_prop < 5).order(
                                  SimpleModel.int_prop).fetch())
    assert len(resp) == 4
    for i, model in enumerate(resp):
        assert model.int_prop == i + 1
예제 #11
0
def test_delete_model(ndb_stub: datastore_stub.LocalDatastoreStub) -> None:
    model = SimpleModel(
        id="test",
        str_prop="asdf",
    )
    ndb_stub._insert_model(model)

    key = ndb.Key(SimpleModel, "test")
    key.delete()

    assert SimpleModel.get_by_id("test") is None
예제 #12
0
def test_query_for_key_prop_filter() -> None:
    SimpleModel(id="test").put()
    SimpleModel(id="test1").put()
    SimpleModel(id="test2").put()
    KeyPropertyModel(id="test", model_ref=ndb.Key(SimpleModel, "test")).put()

    resp = KeyPropertyModel.query(
        KeyPropertyModel.model_ref == ndb.Key(SimpleModel, "test")).fetch()
    assert len(resp) == 1
    assert resp[0] == KeyPropertyModel.get_by_id("test")
    assert resp[0].model_ref.get() == SimpleModel.get_by_id("test")
예제 #13
0
def test_ancestor_query() -> None:
    parent_model = SimpleModel(id="parent", str_prop="parent_model")
    parent_model.put()

    child_model = ChildModel(id="child",
                             parent=parent_model.key,
                             str_prop="child_model")
    child_model.put()

    child_query = ChildModel.query(ancestor=parent_model.key).fetch()
    assert child_query == [child_model]
예제 #14
0
def test_query_projection() -> None:
    stored_keys = ndb.put_multi([
        SimpleModel(id=f"test{i}", int_prop=i, str_prop=f"{i}")
        for i in range(10)
    ])
    assert len(stored_keys) == 10

    resp = SimpleModel.query().fetch(projection=[SimpleModel.int_prop])
    assert len(resp) == 10
    assert all(i.int_prop is not None for i in resp)
    with pytest.raises(ndb.model.UnprojectedPropertyError):
        assert all(i.str_prop is None for i in resp)
예제 #15
0
def test_fetch_existing_by_field_multiple_with_order(
    ndb_stub: datastore_stub.LocalDatastoreStub, ) -> None:
    model1 = SimpleModel(id="test", str_prop="asdf", int_prop=10)
    model2 = SimpleModel(id="test2", str_prop="asdf", int_prop=20)
    ndb_stub._insert_model(model1)
    ndb_stub._insert_model(model2)

    # We don't pass an ordering here, so the order is not deterministic
    query_res = (SimpleModel.query(SimpleModel.str_prop == "asdf").order(
        SimpleModel.int_prop).fetch(limit=2))
    assert len(query_res) == 2
    assert query_res == [model1, model2]
예제 #16
0
def test_query_with_None() -> None:
    m1 = SimpleModel(str_prop="asdf", int_prop=10)
    m2 = SimpleModel(str_prop="aaaa", int_prop=None)
    m1.put()
    m2.put()

    resp1 = SimpleModel.query(
        SimpleModel.int_prop != None).fetch()  # noqa: E711
    assert resp1 == [m1]

    resp2 = SimpleModel.query(
        SimpleModel.int_prop == None).fetch()  # noqa: E711
    assert resp2 == [m2]
예제 #17
0
def test_put_model_no_id() -> None:
    model1 = SimpleModel(str_prop="asdf")
    model2 = SimpleModel(str_prop="hjkl")
    model1.put()
    model2.put()

    stored = SimpleModel.query().fetch()
    assert stored == [model1, model2]
    assert stored[0].key.id() == 1
    assert stored[1].key.id() == 2
예제 #18
0
 def test_unique_within_transaction(self):
     with transaction.atomic():
         a = SimpleModel()
         a.save()
         b = SimpleModel()
         b.save()
     assert [1, 2] == list(
         SimpleModel.objects.values_list('sequence', flat=True))
예제 #19
0
def test_fetch_existing_by_le(
        ndb_stub: datastore_stub.LocalDatastoreStub) -> None:
    model1 = SimpleModel(
        id="test",
        int_prop=42,
    )
    model2 = SimpleModel(
        id="test2",
        int_prop=43,
    )
    ndb_stub._insert_model(model1)
    ndb_stub._insert_model(model2)

    query_res = SimpleModel.query(SimpleModel.int_prop <= 43).fetch(limit=5)
    assert len(query_res) == 2
예제 #20
0
def test_fetch_existing_by_field_with_limit(
    ndb_stub: datastore_stub.LocalDatastoreStub, ) -> None:
    model1 = SimpleModel(
        id="test",
        str_prop="asdf",
    )
    model2 = SimpleModel(
        id="test2",
        str_prop="asdf",
    )
    ndb_stub._insert_model(model1)
    ndb_stub._insert_model(model2)

    # Since we don't pass an order by, it's non deterministic which we get
    query_res = SimpleModel.query(SimpleModel.str_prop == "asdf").fetch(
        limit=1)
    assert len(query_res) == 1
    assert query_res[0].str_prop == "asdf"
예제 #21
0
def test_fetch_existing_by_field_multiple(
    ndb_stub: datastore_stub.LocalDatastoreStub, ) -> None:
    model1 = SimpleModel(
        id="test",
        str_prop="asdf",
    )
    model2 = SimpleModel(
        id="test2",
        str_prop="asdf",
    )
    ndb_stub._insert_model(model1)
    ndb_stub._insert_model(model2)

    # We don't pass an ordering here, so the order is not deterministic
    query_res = SimpleModel.query(SimpleModel.str_prop == "asdf").fetch(
        limit=2)
    assert len(query_res) == 2
    query_res.sort(key=lambda m: m.key.id())
    assert query_res == [model1, model2]
예제 #22
0
 def test_lock_table(self):
     with lock_table(SimpleModel):
         a = SimpleModel()
         a.save()
         b = SimpleModel()
         b.save()
예제 #23
0
def test_get_nonexistent_key() -> None:
    model = SimpleModel.get_by_id("notfound")
    assert model is None
예제 #24
0
def test_put_model() -> None:
    model = SimpleModel(id="test", str_prop="asdf")
    key = model.put()

    assert key == model.key
예제 #25
0
def test_put_model_matches_key_get() -> None:
    model = SimpleModel(id="test", str_prop="asdf")
    key = model.put()

    get_resp = key.get()
    assert get_resp == model
예제 #26
0
    def test_simple_model(self):
        a = SimpleModel()
        a.save()
        assert 1 == a.sequence
        a.save()
        assert 1 == a.sequence

        b = SimpleModel()
        b.save()
        assert 2 == b.sequence
        b.save()
        assert 2 == b.sequence
예제 #27
0
    def test_delete_simple_model(self):
        a = SimpleModel()
        a.save()

        b = SimpleModel()
        b.save()
        b.delete()

        c = SimpleModel()
        c.save()
        assert 2 == c.sequence
        c.save()
        assert 2 == c.sequence
예제 #28
0
def test_get_or_insert_doesnt_exist() -> None:
    model_res = SimpleModel.get_or_insert("test")
    assert model_res is not None