def test__to_protobuf_select_only(self):
        from google.cloud.firestore_v1beta1.proto import query_pb2

        parent = mock.Mock(id='cat', spec=['id'])
        query1 = self._make_one(parent)
        field_paths = ['a.b', 'a.c', 'd']
        query2 = query1.select(field_paths)

        structured_query_pb = query2._to_protobuf()
        query_kwargs = {
            'from': [
                query_pb2.StructuredQuery.CollectionSelector(
                    collection_id=parent.id,
                ),
            ],
            'select': query_pb2.StructuredQuery.Projection(
                fields=[
                    query_pb2.StructuredQuery.FieldReference(
                        field_path=field_path
                    )
                    for field_path in field_paths
                ],
            ),
        }
        expected_pb = query_pb2.StructuredQuery(**query_kwargs)
        self.assertEqual(structured_query_pb, expected_pb)
    def test__to_protobuf_end_at_only(self):
        # NOTE: "only" is wrong since we must have ``order_by`` as well.
        from google.cloud.firestore_v1beta1.gapic import enums
        from google.cloud.firestore_v1beta1.proto import document_pb2
        from google.cloud.firestore_v1beta1.proto import query_pb2

        parent = mock.Mock(id='ghoti', spec=['id'])
        query1 = self._make_one(parent)
        query2 = query1.end_at({'a': 88})
        query3 = query2.order_by('a')

        structured_query_pb = query3._to_protobuf()
        query_kwargs = {
            'from': [
                query_pb2.StructuredQuery.CollectionSelector(
                    collection_id=parent.id, ),
            ],
            'order_by': [
                _make_order_pb(
                    'a',
                    enums.StructuredQuery.Direction.ASCENDING,
                ),
            ],
            'end_at':
            query_pb2.Cursor(values=[
                document_pb2.Value(integer_value=88),
            ], ),
        }
        expected_pb = query_pb2.StructuredQuery(**query_kwargs)
        self.assertEqual(structured_query_pb, expected_pb)
    def test__to_protobuf_where_only(self):
        from google.cloud.firestore_v1beta1.gapic import enums
        from google.cloud.firestore_v1beta1.proto import document_pb2
        from google.cloud.firestore_v1beta1.proto import query_pb2

        parent = mock.Mock(id='dog', spec=['id'])
        query1 = self._make_one(parent)
        query2 = query1.where('a', '==', u'b')

        structured_query_pb = query2._to_protobuf()
        query_kwargs = {
            'from': [
                query_pb2.StructuredQuery.CollectionSelector(
                    collection_id=parent.id, ),
            ],
            'where':
            query_pb2.StructuredQuery.Filter(
                field_filter=query_pb2.StructuredQuery.FieldFilter(
                    field=query_pb2.StructuredQuery.FieldReference(
                        field_path='a', ),
                    op=enums.StructuredQuery.FieldFilter.Operator.EQUAL,
                    value=document_pb2.Value(string_value=u'b'),
                ), ),
        }
        expected_pb = query_pb2.StructuredQuery(**query_kwargs)
        self.assertEqual(structured_query_pb, expected_pb)
Exemple #4
0
    def _to_protobuf(self):
        """Convert the current query into the equivalent protobuf.

        Returns:
            google.cloud.firestore_v1beta1.types.StructuredQuery: The
            query protobuf.
        """
        query_kwargs = {
            'select':
            self._projection,
            'from': [
                query_pb2.StructuredQuery.CollectionSelector(
                    collection_id=self._parent.id, ),
            ],
            'where':
            self._filters_pb(),
            'order_by':
            self._orders,
            'start_at':
            _cursor_pb(self._start_at, self._orders),
            'end_at':
            _cursor_pb(self._end_at, self._orders),
        }
        if self._offset is not None:
            query_kwargs['offset'] = self._offset
        if self._limit is not None:
            query_kwargs['limit'] = wrappers_pb2.Int32Value(value=self._limit)

        return query_pb2.StructuredQuery(**query_kwargs)
    def _to_protobuf(self):
        """Convert the current query into the equivalent protobuf.

        Returns:
            google.cloud.firestore_v1beta1.types.StructuredQuery: The
            query protobuf.
        """
        projection = self._normalize_projection(self._projection)
        start_at = self._normalize_cursor(self._start_at, self._orders)
        end_at = self._normalize_cursor(self._end_at, self._orders)

        query_kwargs = {
            "select": projection,
            "from": [
                query_pb2.StructuredQuery.CollectionSelector(
                    collection_id=self._parent.id
                )
            ],
            "where": self._filters_pb(),
            "order_by": self._orders,
            "start_at": _cursor_pb(start_at),
            "end_at": _cursor_pb(end_at),
        }
        if self._offset is not None:
            query_kwargs["offset"] = self._offset
        if self._limit is not None:
            query_kwargs["limit"] = wrappers_pb2.Int32Value(value=self._limit)

        return query_pb2.StructuredQuery(**query_kwargs)
    def test__to_protobuf_all_fields(self):
        from google.protobuf import wrappers_pb2
        from google.cloud.firestore_v1beta1.gapic import enums
        from google.cloud.firestore_v1beta1.proto import document_pb2
        from google.cloud.firestore_v1beta1.proto import query_pb2

        parent = mock.Mock(id='cat', spec=['id'])
        query1 = self._make_one(parent)
        query2 = query1.select(['X', 'Y', 'Z'])
        query3 = query2.where('Y', '>', 2.5)
        query4 = query3.order_by('X')
        query5 = query4.limit(17)
        query6 = query5.offset(3)
        query7 = query6.start_at({'X': 10})
        query8 = query7.end_at({'X': 25})

        structured_query_pb = query8._to_protobuf()
        query_kwargs = {
            'from': [
                query_pb2.StructuredQuery.CollectionSelector(
                    collection_id=parent.id, ),
            ],
            'select':
            query_pb2.StructuredQuery.Projection(fields=[
                query_pb2.StructuredQuery.FieldReference(field_path=field_path)
                for field_path in ['X', 'Y', 'Z']
            ], ),
            'where':
            query_pb2.StructuredQuery.Filter(
                field_filter=query_pb2.StructuredQuery.FieldFilter(
                    field=query_pb2.StructuredQuery.FieldReference(
                        field_path='Y', ),
                    op=enums.StructuredQuery.FieldFilter.Operator.GREATER_THAN,
                    value=document_pb2.Value(double_value=2.5),
                ), ),
            'order_by': [
                _make_order_pb(
                    'X',
                    enums.StructuredQuery.Direction.ASCENDING,
                ),
            ],
            'start_at':
            query_pb2.Cursor(
                values=[
                    document_pb2.Value(integer_value=10),
                ],
                before=True,
            ),
            'end_at':
            query_pb2.Cursor(values=[
                document_pb2.Value(integer_value=25),
            ], ),
            'offset':
            3,
            'limit':
            wrappers_pb2.Int32Value(value=17),
        }
        expected_pb = query_pb2.StructuredQuery(**query_kwargs)
        self.assertEqual(structured_query_pb, expected_pb)
Exemple #7
0
    def _to_protobuf(self):
        from google.cloud.firestore_v1beta1.proto import query_pb2

        query_kwargs = {
            "select": None,
            "from": None,
            "where": None,
            "order_by": None,
            "start_at": None,
            "end_at": None,
        }
        return query_pb2.StructuredQuery(**query_kwargs)
    def test__to_protobuf_offset_only(self):
        from google.cloud.firestore_v1beta1.proto import query_pb2

        parent = mock.Mock(id='cartt', spec=['id'])
        query1 = self._make_one(parent)
        offset = 14
        query2 = query1.offset(offset)

        structured_query_pb = query2._to_protobuf()
        query_kwargs = {
            'from': [
                query_pb2.StructuredQuery.CollectionSelector(
                    collection_id=parent.id, ),
            ],
            'offset':
            offset,
        }
        expected_pb = query_pb2.StructuredQuery(**query_kwargs)
        self.assertEqual(structured_query_pb, expected_pb)
Exemple #9
0
    def test__to_protobuf_limit_only(self):
        from google.protobuf import wrappers_pb2
        from google.cloud.firestore_v1beta1.proto import query_pb2

        parent = mock.Mock(id='donut', spec=['id'])
        query1 = self._make_one(parent)
        limit = 31
        query2 = query1.limit(limit)

        structured_query_pb = query2._to_protobuf()
        query_kwargs = {
            'from': [
                query_pb2.StructuredQuery.CollectionSelector(
                    collection_id=parent.id, ),
            ],
            'limit':
            wrappers_pb2.Int32Value(value=limit),
        }
        expected_pb = query_pb2.StructuredQuery(**query_kwargs)
    def test__to_protobuf_order_by_only(self):
        from google.cloud.firestore_v1beta1.gapic import enums
        from google.cloud.firestore_v1beta1.proto import query_pb2

        parent = mock.Mock(id='fish', spec=['id'])
        query1 = self._make_one(parent)
        query2 = query1.order_by('abc')

        structured_query_pb = query2._to_protobuf()
        query_kwargs = {
            'from': [
                query_pb2.StructuredQuery.CollectionSelector(
                    collection_id=parent.id, ),
            ],
            'order_by': [
                _make_order_pb(
                    'abc',
                    enums.StructuredQuery.Direction.ASCENDING,
                ),
            ],
        }
        expected_pb = query_pb2.StructuredQuery(**query_kwargs)
        self.assertEqual(structured_query_pb, expected_pb)