Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for batch method #77

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion mockfirestore/client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Iterable, Sequence
from mockfirestore.collection import CollectionReference
from mockfirestore.document import DocumentReference, DocumentSnapshot
from mockfirestore.transaction import Transaction
from mockfirestore.transaction import Transaction, Batch


class MockFirestore:
Expand Down Expand Up @@ -59,4 +59,7 @@ def get_all(self, references: Iterable[DocumentReference],
def transaction(self, **kwargs) -> Transaction:
return Transaction(self, **kwargs)

def batch(self) -> Batch:
return Batch(self)


9 changes: 8 additions & 1 deletion mockfirestore/transaction.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from functools import partial
import random
from typing import Iterable, Callable

from mockfirestore._helpers import generate_random_string, Timestamp
from mockfirestore.document import DocumentReference, DocumentSnapshot
from mockfirestore.query import Query
Expand All @@ -22,6 +22,7 @@ class Transaction:
This mostly follows the model from
https://googleapis.dev/python/firestore/latest/transaction.html
"""

def __init__(self, client,
max_attempts=MAX_ATTEMPTS, read_only=False):
self._client = client
Expand Down Expand Up @@ -117,3 +118,9 @@ def __enter__(self):
def __exit__(self, exc_type, exc_val, exc_tb):
if exc_type is None:
self.commit()


class Batch(Transaction):
def commit(self):
self._begin() # batch can call commit many times
super(Batch, self).commit()
Loading