Example #1
0
    def test_get_documents_two_queries(self):
        from google.cloud.firestore_bundle import FirestoreBundle
        from google.cloud.firestore_v1 import _helpers

        bundle = FirestoreBundle("test")
        query: query_mod.Query = self._bundled_query_helper()  # type: ignore
        bundle.add_named_query("sweet query", query)

        query: query_mod.Query = self._bundled_query_helper(
            document_ids=["doc-3", "doc-4"])  # type: ignore
        bundle.add_named_query("second query", query)

        docs_iter = _helpers._get_documents_from_bundle(
            bundle, query_name="sweet query")
        doc = next(docs_iter)
        assert doc.id == "doc-1"
        doc = next(docs_iter)
        assert doc.id == "doc-2"

        docs_iter = _helpers._get_documents_from_bundle(
            bundle, query_name="second query")
        doc = next(docs_iter)
        assert doc.id == "doc-3"
        doc = next(docs_iter)
        assert doc.id == "doc-4"
Example #2
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 #3
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 #4
0
    def test_adding_collection_raises_error(self):
        from google.cloud.firestore_bundle import FirestoreBundle

        col = self._bundled_collection_helper()
        bundle = FirestoreBundle("test")
        with pytest.raises(ValueError):
            bundle.add_named_query("asdf", col)
Example #5
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 #6
0
    def test_add_query_twice(self):
        from google.cloud.firestore_bundle import FirestoreBundle

        query = self._bundled_query_helper()
        bundle = FirestoreBundle("test")
        bundle.add_named_query("asdf", query)
        with pytest.raises(ValueError):
            bundle.add_named_query("asdf", query)
Example #7
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 #8
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 #9
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 #10
0
 def test_add_query(self):
     query = self._bundled_query_helper()
     bundle = FirestoreBundle("test")
     bundle.add_named_query("asdf", query)
     self.assertIsNotNone(bundle.named_queries.get("asdf"))
     self.assertIsNotNone(bundle.documents[
         "projects/project-project/databases/(default)/documents/col/doc-1"]
                          )
     self.assertIsNotNone(bundle.documents[
         "projects/project-project/databases/(default)/documents/col/doc-2"]
                          )
Example #11
0
    def test_add_query(self):
        from google.cloud.firestore_bundle import FirestoreBundle

        query = self._bundled_query_helper()
        bundle = FirestoreBundle("test")
        bundle.add_named_query("asdf", query)
        assert bundle.named_queries.get("asdf") is not None
        assert (bundle.documents[
            "projects/project-project/databases/(default)/documents/col/doc-1"]
                is not None)
        assert (bundle.documents[
            "projects/project-project/databases/(default)/documents/col/doc-2"]
                is not None)
Example #12
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 #13
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 #14
0
 def test_async_query(self):
     # Create an async query, but this test does not need to be
     # marked as async by pytest because `bundle.add_named_query()`
     # seemlessly handles accepting async iterables.
     async_query = self._bundled_query_helper()
     bundle = FirestoreBundle("test")
     bundle.add_named_query("asdf", async_query)
     self.assertIsNotNone(bundle.named_queries.get("asdf"))
     self.assertIsNotNone(bundle.documents[
         "projects/project-project/databases/(default)/documents/col/doc-1"]
                          )
     self.assertIsNotNone(bundle.documents[
         "projects/project-project/databases/(default)/documents/col/doc-2"]
                          )
Example #15
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 #16
0
    def test_get_documents(self):
        bundle = FirestoreBundle("test")
        query: Query = self._bundled_query_helper()  # type: ignore
        bundle.add_named_query("sweet query", query)
        docs_iter = _helpers._get_documents_from_bundle(
            bundle, query_name="sweet query")
        doc = next(docs_iter)
        self.assertEqual(doc.id, "doc-1")
        doc = next(docs_iter)
        self.assertEqual(doc.id, "doc-2")

        # Now an empty one
        docs_iter = _helpers._get_documents_from_bundle(
            bundle, query_name="wrong query")
        doc = next(docs_iter, None)
        self.assertIsNone(doc)
Example #17
0
    def test_get_document(self):
        bundle = FirestoreBundle("test")
        query: Query = self._bundled_query_helper()  # type: ignore
        bundle.add_named_query("sweet query", query)

        self.assertIsNotNone(
            _helpers._get_document_from_bundle(
                bundle,
                document_id=
                "projects/project-project/databases/(default)/documents/col/doc-1",
            ), )

        self.assertIsNone(
            _helpers._get_document_from_bundle(
                bundle,
                document_id=
                "projects/project-project/databases/(default)/documents/col/doc-0",
            ), )
Example #18
0
    def test_get_documents(self):
        from google.cloud.firestore_v1 import _helpers
        from google.cloud.firestore_bundle import FirestoreBundle

        bundle = FirestoreBundle("test")
        query: query_mod.Query = self._bundled_query_helper()  # type: ignore
        bundle.add_named_query("sweet query", query)
        docs_iter = _helpers._get_documents_from_bundle(
            bundle, query_name="sweet query")
        doc = next(docs_iter)
        assert doc.id == "doc-1"
        doc = next(docs_iter)
        assert doc.id == "doc-2"

        # Now an empty one
        docs_iter = _helpers._get_documents_from_bundle(
            bundle, query_name="wrong query")
        doc = next(docs_iter, None)
        assert doc is None
Example #19
0
    def test_get_document(self):
        from google.cloud.firestore_bundle import FirestoreBundle
        from google.cloud.firestore_v1 import _helpers

        bundle = FirestoreBundle("test")
        query: query_mod.Query = self._bundled_query_helper()  # type: ignore
        bundle.add_named_query("sweet query", query)

        assert (_helpers._get_document_from_bundle(
            bundle,
            document_id=
            "projects/project-project/databases/(default)/documents/col/doc-1",
        ) is not None)

        assert (_helpers._get_document_from_bundle(
            bundle,
            document_id=
            "projects/project-project/databases/(default)/documents/col/doc-0",
        ) is None)
Example #20
0
    def test_get_documents_two_queries(self):
        bundle = FirestoreBundle("test")
        query: Query = self._bundled_query_helper()  # type: ignore
        bundle.add_named_query("sweet query", query)

        query: Query = self._bundled_query_helper(
            document_ids=["doc-3", "doc-4"])  # type: ignore
        bundle.add_named_query("second query", query)

        docs_iter = _helpers._get_documents_from_bundle(
            bundle, query_name="sweet query")
        doc = next(docs_iter)
        self.assertEqual(doc.id, "doc-1")
        doc = next(docs_iter)
        self.assertEqual(doc.id, "doc-2")

        docs_iter = _helpers._get_documents_from_bundle(
            bundle, query_name="second query")
        doc = next(docs_iter)
        self.assertEqual(doc.id, "doc-3")
        doc = next(docs_iter)
        self.assertEqual(doc.id, "doc-4")
Example #21
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 #22
0
    def test_bundle_build(self):
        from google.cloud.firestore_bundle import FirestoreBundle

        bundle = FirestoreBundle("test")
        bundle.add_named_query("best name", self._bundled_query_helper())
        assert isinstance(bundle.build(), str)
Example #23
0
 def test_bundle_build(self):
     bundle = FirestoreBundle("test")
     bundle.add_named_query("best name", self._bundled_query_helper())
     self.assertIsInstance(bundle.build(), str)
Example #24
0
 def test_add_query_twice(self):
     query = self._bundled_query_helper()
     bundle = FirestoreBundle("test")
     bundle.add_named_query("asdf", query)
     self.assertRaises(ValueError, bundle.add_named_query, "asdf", query)