示例#1
0
    def test_get_not_found(self):
        from google.cloud.firestore_v1beta1.document import DocumentSnapshot

        # Create a minimal fake client with a dummy response.
        read_time = 123
        expected = DocumentSnapshot(None, None, False, read_time, None, None)
        response_iterator = iter([expected])
        client = mock.Mock(_database_string='sprinklez',
                           spec=['_database_string', 'get_all'])
        client.get_all.return_value = response_iterator

        # Actually make a document and call get().
        document = self._make_one('house', 'cowse', client=client)
        field_paths = ['x.y', 'x.z', 't']
        snapshot = document.get(field_paths=field_paths)
        self.assertIsNone(snapshot.reference)
        self.assertIsNone(snapshot._data)
        self.assertFalse(snapshot.exists)
        self.assertEqual(snapshot.read_time, expected.read_time)
        self.assertIsNone(snapshot.create_time)
        self.assertIsNone(snapshot.update_time)

        # Verify the response and the mocks.
        client.get_all.assert_called_once_with([document],
                                               field_paths=field_paths,
                                               transaction=None)
示例#2
0
def _parse_batch_get(get_doc_response, reference_map, client):
    """Parse a `BatchGetDocumentsResponse` protobuf.

    Args:
        get_doc_response (~google.cloud.proto.firestore.v1beta1.\
            firestore_pb2.BatchGetDocumentsResponse): A single response (from
            a stream) containing the "get" response for a document.
        reference_map (Dict[str, .DocumentReference]): A mapping (produced
            by :func:`_reference_info`) of fully-qualified document paths to
            document references.
        client (~.firestore_v1beta1.client.Client): A client that has
            a document factory.

    Returns:
       [.DocumentSnapshot]: The retrieved snapshot.

    Raises:
        ValueError: If the response has a ``result`` field (a oneof) other
            than ``found`` or ``missing``.
    """
    result_type = get_doc_response.WhichOneof("result")
    if result_type == "found":
        reference = _get_reference(get_doc_response.found.name, reference_map)
        data = _helpers.decode_dict(get_doc_response.found.fields, client)
        snapshot = DocumentSnapshot(
            reference,
            data,
            exists=True,
            read_time=get_doc_response.read_time,
            create_time=get_doc_response.found.create_time,
            update_time=get_doc_response.found.update_time,
        )
    elif result_type == "missing":
        snapshot = DocumentSnapshot(
            None,
            None,
            exists=False,
            read_time=get_doc_response.read_time,
            create_time=None,
            update_time=None,
        )
    else:
        raise ValueError(
            "`BatchGetDocumentsResponse.result` (a oneof) had a field other "
            "than `found` or `missing` set, or was unset"
        )
    return snapshot
    def test_end_at(self):
        from google.cloud.firestore_v1beta1.document import DocumentSnapshot

        query1 = self._make_one_all_fields(skip_fields=('orders', ))
        query2 = query1.order_by('hi')

        document_fields3 = {'hi': 'mom'}
        query3 = query2.end_at(document_fields3)
        self.assertIsNot(query3, query2)
        self.assertIsInstance(query3, self._get_target_class())
        self.assertEqual(query3._end_at, (document_fields3, False))
        self._compare_queries(query2, query3, '_end_at')

        # Make sure it overrides.
        query4 = query3.order_by('bye')
        values5 = {'hi': 'zap', 'bye': 88}
        document_fields5 = DocumentSnapshot(None, values5, True, None, None,
                                            None)
        query5 = query4.end_at(document_fields5)
        self.assertIsNot(query5, query4)
        self.assertIsInstance(query5, self._get_target_class())
        self.assertEqual(query5._end_at, (values5, False))
        self._compare_queries(query4, query5, '_end_at')
    def test_end_before(self):
        from google.cloud.firestore_v1beta1.document import DocumentSnapshot

        query1 = self._make_one_all_fields(skip_fields=('orders', ))
        query2 = query1.order_by('down')

        document_fields3 = {'down': 99.75}
        query3 = query2.end_before(document_fields3)
        self.assertIsNot(query3, query2)
        self.assertIsInstance(query3, self._get_target_class())
        self.assertEqual(query3._end_at, (document_fields3, True))
        self._compare_queries(query2, query3, '_end_at')

        # Make sure it overrides.
        query4 = query3.order_by('out')
        values5 = {'down': 100.25, 'out': b'\x00\x01'}
        document_fields5 = DocumentSnapshot(None, values5, True, None, None,
                                            None)
        query5 = query4.end_before(document_fields5)
        self.assertIsNot(query5, query4)
        self.assertIsInstance(query5, self._get_target_class())
        self.assertEqual(query5._end_at, (values5, True))
        self._compare_queries(query4, query5, '_end_at')