Ejemplo n.º 1
0
    def test_to_repr_query_with_filter(query_filter):
        property_filter = query_filter
        query = Query('foo', property_filter)

        r = query.to_repr()

        assert r['filter'] == property_filter.to_repr()
Ejemplo n.º 2
0
    def test_from_repr_query_with_several_orders():
        orders = [
            PropertyOrder('property1', direction=Direction.ASCENDING),
            PropertyOrder('property2', direction=Direction.DESCENDING)
        ]
        original_query = Query(order=orders)

        data = {
            'kind': [],
            'order': [{
                'property': {
                    'name': orders[0].prop
                },
                'direction': orders[0].direction
            }, {
                'property': {
                    'name': orders[1].prop
                },
                'direction': orders[1].direction
            }]
        }

        output_query = Query.from_repr(data)

        assert output_query == original_query
Ejemplo n.º 3
0
    def test_from_repr_query_without_kind(query_filter):
        original_query = Query(kind='', query_filter=query_filter)
        data = {'kind': [], 'filter': original_query.query_filter.to_repr()}

        output_query = Query.from_repr(data)

        assert output_query == original_query
Ejemplo n.º 4
0
    def test_to_repr_simple_query():
        kind = 'foo'
        query = Query(kind)

        r = query.to_repr()

        assert len(r['kind']) == 1
        assert r['kind'][0]['name'] == kind
Ejemplo n.º 5
0
    def test_to_repr_query_with_several_orders():
        orders = [
            PropertyOrder('property1', direction=Direction.ASCENDING),
            PropertyOrder('property2', direction=Direction.DESCENDING)
        ]
        query = Query(order=orders)

        r = query.to_repr()

        assert len(r['order']) == 2
        assert r['order'][0] == orders[0].to_repr()
        assert r['order'][1] == orders[1].to_repr()
Ejemplo n.º 6
0
def test_query(creds, kind, project):
    # type: (str, str, str) -> None
    with requests.Session() as s:
        ds = Datastore(project=project, service_file=creds, session=s)

        property_filter = PropertyFilter(prop='value',
                                         operator=PropertyFilterOperator.EQUAL,
                                         value=Value(42))
        query = Query(kind=kind, query_filter=Filter(property_filter))

        before = ds.runQuery(query, session=s)
        num_results = len(before.entity_results)

        transaction = ds.beginTransaction(session=s)
        mutations = [
            ds.make_mutation(Operation.INSERT,
                             Key(project, [PathElement(kind)]),
                             properties={'value': 42}),
            ds.make_mutation(Operation.INSERT,
                             Key(project, [PathElement(kind)]),
                             properties={'value': 42}),
        ]
        ds.commit(mutations, transaction=transaction, session=s)

        # TODO: figure out why this is flaky without the sleep
        # Seems to only be flaky intermittently in py2.
        time.sleep(2)

        after = ds.runQuery(query, session=s)
        assert len(after.entity_results) == num_results + 2
Ejemplo n.º 7
0
    def test_from_repr(query):
        original_query = query
        data = {
            'kind': original_query.kind,
            'filter': original_query.query_filter.to_repr()
        }

        output_query = Query.from_repr(data)

        assert output_query == original_query
Ejemplo n.º 8
0
    def test_to_repr_query_without_kind():
        query = Query()

        r = query.to_repr()

        assert not r['kind']
Ejemplo n.º 9
0
 def query(query_filter):
     # type: (Filter) -> Query
     return Query('query_kind', query_filter)