def test_filter_pb():
        foo = model.StringProperty("foo")
        query = query_module.QueryOptions(kind="Foo", filters=(foo == "bar"))
        query_pb = _datastore_query._query_to_protobuf(query)

        filter_pb = query_pb2.PropertyFilter(
            property=query_pb2.PropertyReference(name="foo"),
            op=query_pb2.PropertyFilter.EQUAL,
            value=entity_pb2.Value(string_value="bar"),
        )
        expected_pb = query_pb2.Query(
            kind=[query_pb2.KindExpression(name="Foo")],
            filter=query_pb2.Filter(property_filter=filter_pb),
        )
        assert query_pb == expected_pb
 def test_order_by():
     query = query_module.QueryOptions(order_by=[
         query_module.PropertyOrder("a"),
         query_module.PropertyOrder("b", reverse=True),
     ])
     expected_pb = query_pb2.Query(order=[
         query_pb2.PropertyOrder(
             property=query_pb2.PropertyReference(name="a"),
             direction=query_pb2.PropertyOrder.ASCENDING,
         ),
         query_pb2.PropertyOrder(
             property=query_pb2.PropertyReference(name="b"),
             direction=query_pb2.PropertyOrder.DESCENDING,
         ),
     ])
     assert _datastore_query._query_to_protobuf(query) == expected_pb
 def test_it(_datastore_api):
     query = query_module.QueryOptions(project="testing", namespace="")
     query_pb = _datastore_query._query_to_protobuf(query)
     _datastore_api.make_call.return_value = utils.future_result("foo")
     read_options = datastore_pb2.ReadOptions()
     request = datastore_pb2.RunQueryRequest(
         project_id="testing",
         partition_id=entity_pb2.PartitionId(project_id="testing",
                                             namespace_id=""),
         query=query_pb,
         read_options=read_options,
     )
     _datastore_api.get_read_options.return_value = read_options
     assert _datastore_query._datastore_run_query(query).result() == "foo"
     _datastore_api.make_call.assert_called_once_with("RunQuery",
                                                      request,
                                                      timeout=None)
     _datastore_api.get_read_options.assert_called_once_with(
         query, default_read_consistency=_datastore_api.EVENTUAL)
Example #4
0
    def test_ancestor_with_composite_filter():
        key = key_module.Key("Foo", 123)
        foo = model.StringProperty("foo")
        food = model.StringProperty("food")
        query = query_module.QueryOptions(
            ancestor=key,
            filters=query_module.AND(foo == "bar", food == "barn"),
        )
        query_pb = _datastore_query._query_to_protobuf(query)

        filter_pb1 = query_pb2.PropertyFilter(
            property=query_pb2.PropertyReference(name="foo"),
            op=query_pb2.PropertyFilter.EQUAL,
            value=entity_pb2.Value(string_value="bar"),
        )
        filter_pb2 = query_pb2.PropertyFilter(
            property=query_pb2.PropertyReference(name="food"),
            op=query_pb2.PropertyFilter.EQUAL,
            value=entity_pb2.Value(string_value="barn"),
        )
        ancestor_pb = query_pb2.PropertyFilter(
            property=query_pb2.PropertyReference(name="__key__"),
            op=query_pb2.PropertyFilter.HAS_ANCESTOR,
        )
        ancestor_pb.value.key_value.CopyFrom(key._key.to_protobuf())
        expected_pb = query_pb2.Query(
            filter=query_pb2.Filter(
                composite_filter=query_pb2.CompositeFilter(
                    op=query_pb2.CompositeFilter.AND,
                    filters=[
                        query_pb2.Filter(property_filter=filter_pb1),
                        query_pb2.Filter(property_filter=filter_pb2),
                        query_pb2.Filter(property_filter=ancestor_pb),
                    ],
                )
            )
        )
        assert query_pb == expected_pb
Example #5
0
 def test_limit():
     query = query_module.QueryOptions(limit=20)
     expected_pb = query_pb2.Query()
     expected_pb.limit.value = 20
     assert _datastore_query._query_to_protobuf(query) == expected_pb
Example #6
0
 def test_offset():
     query = query_module.QueryOptions(offset=20)
     assert _datastore_query._query_to_protobuf(query) == query_pb2.Query(
         offset=20
     )
Example #7
0
 def test_kind():
     query = query_module.QueryOptions(kind="Foo")
     assert _datastore_query._query_to_protobuf(query) == query_pb2.Query(
         kind=[query_pb2.KindExpression(name="Foo")]
     )
Example #8
0
 def test_no_args():
     query = query_module.QueryOptions()
     assert _datastore_query._query_to_protobuf(query) == query_pb2.Query()
 def test_end_cursor():
     query = query_module.QueryOptions(
         end_cursor=_datastore_query.Cursor(b"abc"))
     assert _datastore_query._query_to_protobuf(query) == query_pb2.Query(
         end_cursor=b"abc")