Example #1
0
    def test_not_metadata_first(self, fnc):
        from google.cloud.firestore_v1 import _helpers

        client = _test_helpers.make_client()
        fnc.return_value = iter([{"document": {}}])
        with pytest.raises(ValueError):
            _helpers.deserialize_bundle("does not matter", client)
Example #2
0
    def test_invalid_json(self, fnc):
        from google.cloud.firestore_v1 import _helpers

        client = _test_helpers.make_client()
        fnc.return_value = iter([{}])
        with pytest.raises(ValueError):
            _helpers.deserialize_bundle("does not matter", client)
Example #3
0
    def test_invalid_bundle_element_type(self, fnc, _):
        from google.cloud.firestore_v1 import _helpers

        client = _test_helpers.make_client()
        # invalid bc `wtfisthis?` is obviously invalid
        fnc.return_value = [{"metadata": {"id": "asdf"}}, {"wtfisthis?": {}}]
        with pytest.raises(ValueError):
            _helpers.deserialize_bundle("does not matter", client)
Example #4
0
    def test_invalid_bundle_start(self, fnc, _):
        from google.cloud.firestore_v1 import _helpers

        client = _test_helpers.make_client()
        # invalid bc first element must be of key `metadata`
        fnc.return_value = [{"document": {}}]
        with pytest.raises(ValueError):
            _helpers.deserialize_bundle("does not matter", client)
Example #5
0
    def test_invalid_bundle(self, fnc, _):
        from google.cloud.firestore_v1 import _helpers

        client = _test_helpers.make_client()
        # invalid bc `document` must follow `document_metadata`
        fnc.return_value = [{"metadata": {"id": "asdf"}}, {"document": {}}]
        with pytest.raises(ValueError):
            _helpers.deserialize_bundle("does not matter", client)
Example #6
0
    def test_build_round_trip_more_unicode(self):
        from google.cloud.firestore_bundle import FirestoreBundle
        from google.cloud.firestore_v1 import _helpers

        bano = "baño"
        chinese_characters = "殷周金文集成引得"
        query = self._bundled_query_helper(data=[{
            "bano": bano
        }, {
            "international":
            chinese_characters
        }], )
        bundle = FirestoreBundle("test")
        bundle.add_named_query("asdf", query)
        serialized = bundle.build()
        reserialized_bundle = _helpers.deserialize_bundle(
            serialized, query._client)

        assert (bundle.documents[
            "projects/project-project/databases/(default)/documents/col/doc-1"]
                .snapshot._data["bano"] == bano)
        assert (bundle.documents[
            "projects/project-project/databases/(default)/documents/col/doc-2"]
                .snapshot._data["international"] == chinese_characters)
        assert serialized == reserialized_bundle.build()
Example #7
0
 def test_valid_passes(self, fnc, _):
     client = _test_helpers.make_client()
     fnc.return_value = [
         {
             "metadata": {
                 "id": "asdf"
             }
         },
         {
             "documentMetadata": {}
         },
         {
             "document": {}
         },
     ]
     _helpers.deserialize_bundle("does not matter", client)
Example #8
0
    def test_build_round_trip_emojis(self):
        smile = "😂"
        mermaid = "🧜🏿‍♀️"
        query = self._bundled_query_helper(data=[{
            "smile": smile
        }, {
            "compound": mermaid
        }], )
        bundle = FirestoreBundle("test")
        bundle.add_named_query("asdf", query)
        serialized = bundle.build()
        reserialized_bundle = _helpers.deserialize_bundle(
            serialized, query._client)

        self.assertEqual(
            bundle.documents[
                "projects/project-project/databases/(default)/documents/col/doc-1"]
            .snapshot._data["smile"],
            smile,
        )
        self.assertEqual(
            bundle.documents[
                "projects/project-project/databases/(default)/documents/col/doc-2"]
            .snapshot._data["compound"],
            mermaid,
        )
        self.assertEqual(
            serialized,
            reserialized_bundle.build(),
        )
Example #9
0
    def test_build_round_trip_more_unicode(self):
        bano = "baño"
        chinese_characters = "殷周金文集成引得"
        query = self._bundled_query_helper(data=[{
            "bano": bano
        }, {
            "international":
            chinese_characters
        }], )
        bundle = FirestoreBundle("test")
        bundle.add_named_query("asdf", query)
        serialized = bundle.build()
        reserialized_bundle = _helpers.deserialize_bundle(
            serialized, query._client)

        self.assertEqual(
            bundle.documents[
                "projects/project-project/databases/(default)/documents/col/doc-1"]
            .snapshot._data["bano"],
            bano,
        )
        self.assertEqual(
            bundle.documents[
                "projects/project-project/databases/(default)/documents/col/doc-2"]
            .snapshot._data["international"],
            chinese_characters,
        )
        self.assertEqual(
            serialized,
            reserialized_bundle.build(),
        )
Example #10
0
    def test_valid_passes(self, fnc, _):
        from google.cloud.firestore_v1 import _helpers

        client = _test_helpers.make_client()
        fnc.return_value = [
            {
                "metadata": {
                    "id": "asdf"
                }
            },
            {
                "documentMetadata": {}
            },
            {
                "document": {}
            },
        ]
        _helpers.deserialize_bundle("does not matter", client)
Example #11
0
 def test_build_round_trip(self):
     query = self._bundled_query_helper()
     bundle = FirestoreBundle("test")
     bundle.add_named_query("asdf", query)
     serialized = bundle.build()
     self.assertEqual(
         serialized,
         _helpers.deserialize_bundle(serialized, query._client).build(),
     )
Example #12
0
    def test_build_round_trip(self):
        from google.cloud.firestore_bundle import FirestoreBundle
        from google.cloud.firestore_v1 import _helpers

        query = self._bundled_query_helper()
        bundle = FirestoreBundle("test")
        bundle.add_named_query("asdf", query)
        serialized = bundle.build()
        assert (serialized == _helpers.deserialize_bundle(
            serialized, query._client).build())
Example #13
0
 def test_deserialized_bundle_cached_metadata(self):
     query = self._bundled_query_helper()
     bundle = FirestoreBundle("test")
     bundle.add_named_query("asdf", query)
     bundle_copy = _helpers.deserialize_bundle(bundle.build(),
                                               query._client)
     self.assertIsInstance(bundle_copy, FirestoreBundle)
     self.assertIsNotNone(bundle_copy._deserialized_metadata)
     bundle_copy.add_named_query("second query", query)
     self.assertIsNone(bundle_copy._deserialized_metadata)
Example #14
0
    def test_deserialize_from_seconds_nanos(self):
        """Some SDKs (Node) serialize Timestamp values to
        '{"seconds": 123, "nanos": 456}', instead of an ISO-formatted string.
        This tests deserialization from that format."""
        from google.protobuf.json_format import ParseError
        from google.cloud.firestore_v1 import _helpers

        client = _test_helpers.make_client(project_name="fir-bundles-test")

        _serialized: str = (
            '139{"metadata":{"id":"test-bundle","createTime":' +
            '{"seconds":"1616434660","nanos":913764000},"version":1,"totalDocuments"'
            +
            ':1,"totalBytes":"829"}}224{"namedQuery":{"name":"self","bundledQuery":'
            +
            '{"parent":"projects/fir-bundles-test/databases/(default)/documents",'
            +
            '"structuredQuery":{"from":[{"collectionId":"bundles"}]}},"readTime":'
            +
            '{"seconds":"1616434660","nanos":913764000}}}194{"documentMetadata":'
            +
            '{"name":"projects/fir-bundles-test/databases/(default)/documents/'
            +
            'bundles/test-bundle","readTime":{"seconds":"1616434660","nanos":'
            +
            '913764000},"exists":true,"queries":["self"]}}402{"document":{"name":'
            +
            '"projects/fir-bundles-test/databases/(default)/documents/bundles/'
            + 'test-bundle","fields":{"clientCache":{"stringValue":"1200"},' +
            '"serverCache":{"stringValue":"600"},"queries":{"mapValue":{"fields":'
            +
            '{"self":{"mapValue":{"fields":{"collection":{"stringValue":"bundles"'
            +
            '}}}}}}}},"createTime":{"seconds":"1615488796","nanos":163327000},'
            + '"updateTime":{"seconds":"1615492486","nanos":34157000}}}')

        with pytest.raises(
            (ValueError, ParseError)):  # protobuf 3.18.0 raises ParseError
            _helpers.deserialize_bundle(_serialized, client=client)
Example #15
0
    def test_deserialized_bundle_cached_metadata(self):
        from google.cloud.firestore_bundle import FirestoreBundle
        from google.cloud.firestore_v1 import _helpers

        query = self._bundled_query_helper()
        bundle = FirestoreBundle("test")
        bundle.add_named_query("asdf", query)
        bundle_copy = _helpers.deserialize_bundle(bundle.build(),
                                                  query._client)
        assert isinstance(bundle_copy, FirestoreBundle)
        assert bundle_copy._deserialized_metadata is not None
        bundle_copy.add_named_query("second query", query)
        assert bundle_copy._deserialized_metadata is None
Example #16
0
 def test_roundtrip_binary_data(self):
     query = self._bundled_query_helper(data=[{"binary_data": b"\x0f"}], )
     bundle = FirestoreBundle("test")
     bundle.add_named_query("asdf", query)
     serialized = bundle.build()
     reserialized_bundle = _helpers.deserialize_bundle(
         serialized, query._client)
     gen = _helpers._get_documents_from_bundle(reserialized_bundle)
     snapshot = next(gen)
     self.assertEqual(
         int.from_bytes(snapshot._data["binary_data"],
                        byteorder=sys.byteorder),
         15,
     )
Example #17
0
    def test_roundtrip_binary_data(self):
        import sys
        from google.cloud.firestore_bundle import FirestoreBundle
        from google.cloud.firestore_v1 import _helpers

        query = self._bundled_query_helper(data=[{"binary_data": b"\x0f"}], )
        bundle = FirestoreBundle("test")
        bundle.add_named_query("asdf", query)
        serialized = bundle.build()
        reserialized_bundle = _helpers.deserialize_bundle(
            serialized, query._client)
        gen = _helpers._get_documents_from_bundle(reserialized_bundle)
        snapshot = next(gen)
        assert (int.from_bytes(snapshot._data["binary_data"],
                               byteorder=sys.byteorder) == 15)
Example #18
0
    def test_build_round_trip_emojis(self):
        from google.cloud.firestore_bundle import FirestoreBundle
        from google.cloud.firestore_v1 import _helpers

        smile = "😂"
        mermaid = "🧜🏿‍♀️"
        query = self._bundled_query_helper(data=[{
            "smile": smile
        }, {
            "compound": mermaid
        }], )
        bundle = FirestoreBundle("test")
        bundle.add_named_query("asdf", query)
        serialized = bundle.build()
        reserialized_bundle = _helpers.deserialize_bundle(
            serialized, query._client)

        assert (bundle.documents[
            "projects/project-project/databases/(default)/documents/col/doc-1"]
                .snapshot._data["smile"] == smile)
        assert (bundle.documents[
            "projects/project-project/databases/(default)/documents/col/doc-2"]
                .snapshot._data["compound"] == mermaid)
        assert serialized == reserialized_bundle.build()
Example #19
0
    def test_not_actually_a_bundle_at_all(self):
        from google.cloud.firestore_v1 import _helpers

        client = _test_helpers.make_client()
        with pytest.raises(ValueError):
            _helpers.deserialize_bundle("{}", client)