Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ replacements:
from google.cloud.firestore_v1.base_pipeline import SubPipeline
from google.cloud.firestore_v1.base_query import And, FieldFilter, Or
from google.cloud.firestore_v1.batch import WriteBatch
from google.cloud.firestore_v1.bson import BSONObjectId
from google.cloud.firestore_v1.bson import (
BSONMaxKey,
BSONMinKey,
BSONObjectId,
)
from google.cloud.firestore_v1.client import Client
from google.cloud.firestore_v1.collection import CollectionReference
from google.cloud.firestore_v1.document import DocumentReference
Expand Down Expand Up @@ -171,6 +175,8 @@ replacements:
"async_transactional",
"AsyncTransaction",
"AsyncWriteBatch",
"BSONMaxKey",
"BSONMinKey",
"BSONObjectId",
"Client",
"CountAggregation",
Expand Down Expand Up @@ -244,6 +250,8 @@ replacements:
AsyncQuery,
AsyncTransaction,
AsyncWriteBatch,
BSONMaxKey,
BSONMinKey,
BSONObjectId,
Client,
CollectionGroup,
Expand Down Expand Up @@ -302,6 +310,8 @@ replacements:
"async_transactional",
"AsyncTransaction",
"AsyncWriteBatch",
"BSONMaxKey",
"BSONMinKey",
"BSONObjectId",
"Client",
"CountAggregation",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
AsyncQuery,
AsyncTransaction,
AsyncWriteBatch,
BSONMaxKey,
BSONMinKey,
BSONObjectId,
Client,
CollectionGroup,
Expand Down Expand Up @@ -93,6 +95,8 @@
"async_transactional",
"AsyncTransaction",
"AsyncWriteBatch",
"BSONMaxKey",
"BSONMinKey",
"BSONObjectId",
"Client",
"CountAggregation",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@
from google.cloud.firestore_v1.base_pipeline import SubPipeline
from google.cloud.firestore_v1.base_query import And, FieldFilter, Or
from google.cloud.firestore_v1.batch import WriteBatch
from google.cloud.firestore_v1.bson import BSONObjectId
from google.cloud.firestore_v1.bson import (
BSONMaxKey,
BSONMinKey,
BSONObjectId,
)
from google.cloud.firestore_v1.client import Client
from google.cloud.firestore_v1.collection import CollectionReference
from google.cloud.firestore_v1.document import DocumentReference
Expand Down Expand Up @@ -148,6 +152,8 @@
"async_transactional",
"AsyncTransaction",
"AsyncWriteBatch",
"BSONMaxKey",
"BSONMinKey",
"BSONObjectId",
"Client",
"CountAggregation",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

__all__ = [
"BSONObjectId",
"BSONMinKey",
"BSONMaxKey",
]

_OBJECT_ID_BYTES_LEN = 12
Expand Down Expand Up @@ -118,3 +120,39 @@ def __eq__(self, other: Any) -> bool:

def __hash__(self) -> int:
return hash((type(self), self._value))


class BSONMinKey(_BSONType):
"""Represents the BSON MinKey sentinel value for query range boundaries."""

__slots__ = ()

def _to_map_value(self) -> Dict[str, None]:
"""Returns map dictionary representation for wire serialization."""
return {"__min__": None}

def __eq__(self, other: Any) -> bool:
if isinstance(other, BSONMinKey):
return True
return NotImplemented

def __hash__(self) -> int:
return hash(type(self))


class BSONMaxKey(_BSONType):
Comment thread
ohmayr marked this conversation as resolved.
"""Represents the BSON MaxKey sentinel value for query range boundaries."""

__slots__ = ()

def _to_map_value(self) -> Dict[str, None]:
"""Returns map dictionary representation for wire serialization."""
return {"__max__": None}

def __eq__(self, other: Any) -> bool:
if isinstance(other, BSONMaxKey):
return True
return NotImplemented

def __hash__(self) -> int:
return hash(type(self))
10 changes: 8 additions & 2 deletions packages/google-cloud-firestore/tests/system/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
from google.cloud import firestore_v1 as firestore
from google.cloud.firestore_v1.base_query import And, FieldFilter, Or
from google.cloud.firestore_v1.base_vector_query import DistanceMeasure
from google.cloud.firestore_v1.bson import BSONObjectId
from google.cloud.firestore_v1.bson import BSONMaxKey, BSONMinKey, BSONObjectId
from google.cloud.firestore_v1.vector import Vector


Expand Down Expand Up @@ -1284,13 +1284,19 @@ def test_bson_document_writes(client, cleanup, database):

bson_payload = {
"user_id": BSONObjectId("507f191e810c19729de860ea"),
"min_key": BSONMinKey(),
"max_key": BSONMaxKey(),
}

doc_ref.set(bson_payload)

snapshot = doc_ref.get()
assert snapshot.exists
assert snapshot.to_dict() == {"user_id": {"__oid__": "507f191e810c19729de860ea"}}
assert snapshot.to_dict() == {
"user_id": {"__oid__": "507f191e810c19729de860ea"},
"min_key": {"__min__": None},
"max_key": {"__max__": None},
}


@pytest.fixture(scope="module")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
from google.cloud import firestore_v1 as firestore
from google.cloud.firestore_v1.base_query import And, FieldFilter, Or
from google.cloud.firestore_v1.base_vector_query import DistanceMeasure
from google.cloud.firestore_v1.bson import BSONObjectId
from google.cloud.firestore_v1.bson import BSONMaxKey, BSONMinKey, BSONObjectId
from google.cloud.firestore_v1.query_profile import (
ExecutionStats,
ExplainMetrics,
Expand Down Expand Up @@ -1257,13 +1257,19 @@ async def test_async_bson_document_writes(client, cleanup, database):

bson_payload = {
"user_id": BSONObjectId("507f191e810c19729de860ea"),
"min_key": BSONMinKey(),
"max_key": BSONMaxKey(),
}

await doc_ref.set(bson_payload)

snapshot = await doc_ref.get()
assert snapshot.exists
assert snapshot.to_dict() == {"user_id": {"__oid__": "507f191e810c19729de860ea"}}
assert snapshot.to_dict() == {
"user_id": {"__oid__": "507f191e810c19729de860ea"},
"min_key": {"__min__": None},
"max_key": {"__max__": None},
}


@pytest_asyncio.fixture(scope="module")
Expand Down
70 changes: 70 additions & 0 deletions packages/google-cloud-firestore/tests/unit/v1/test_bson.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import pytest

from google.cloud.firestore_v1.bson import (
BSONMaxKey,
BSONMinKey,
BSONObjectId,
_BSONType,
)
Expand Down Expand Up @@ -125,3 +127,71 @@ def test_bson_object_id_pickle():
unpickled = pickle.loads(pickle.dumps(oid))
assert unpickled == oid
assert unpickled.value == oid.value


def test_bson_minkey_to_map_value():
assert BSONMinKey()._to_map_value() == {"__min__": None}


def test_bson_minkey_equality():
key1 = BSONMinKey()
key2 = BSONMinKey()
assert key1 == key2
assert key1 != BSONMaxKey()
assert key1 != "min"


def test_bson_minkey_hash_and_dict_key():
key1 = BSONMinKey()
key2 = BSONMinKey()
assert hash(key1) == hash(key2)
assert len({key1, key2}) == 1


def test_bson_minkey_repr():
assert repr(BSONMinKey()) == "BSONMinKey()"


def test_bson_minkey_copy():
key = BSONMinKey()
assert copy.copy(key) == key
assert copy.deepcopy(key) == key


def test_bson_minkey_pickle():
key = BSONMinKey()
assert pickle.loads(pickle.dumps(key)) == key


def test_bson_maxkey_to_map_value():
assert BSONMaxKey()._to_map_value() == {"__max__": None}


def test_bson_maxkey_equality():
key1 = BSONMaxKey()
key2 = BSONMaxKey()
assert key1 == key2
assert key1 != BSONMinKey()
assert key1 != "max"


def test_bson_maxkey_hash_and_dict_key():
key1 = BSONMaxKey()
key2 = BSONMaxKey()
assert hash(key1) == hash(key2)
assert len({key1, key2}) == 1


def test_bson_maxkey_repr():
assert repr(BSONMaxKey()) == "BSONMaxKey()"


def test_bson_maxkey_copy():
key = BSONMaxKey()
assert copy.copy(key) == key
assert copy.deepcopy(key) == key


def test_bson_maxkey_pickle():
key = BSONMaxKey()
assert pickle.loads(pickle.dumps(key)) == key
Loading