Skip to main content

In-memory implementation of Google Cloud Firestore for use in tests

Project description

Fake Firestore

CI PyPI version Python versions License: MIT

Fork of mdowds/mock-firestore, originally mock-firestore on PyPI. This fork focuses on fake semantics and async facade.

An in-memory fake implementation of google-cloud-firestore for use in tests.

Important: This library fakes google-cloud-firestore — the official Google Cloud Firestore client library. It does not support firebase_admin.

Installation

pip install fake-firestore

Python 3.8+ is required.

Usage

from fake_firestore import FakeFirestoreClient

db = FakeFirestoreClient()

# Can be used in the same way as a firestore.Client() object would be, e.g.:
db.collection('users').get()

To reset the store to an empty state, use the reset() method:

db = FakeFirestoreClient()
db.reset()

Note: MockFirestore is still available as a backward compatibility alias for FakeFirestoreClient.

Supported operations

from fake_firestore import FakeFirestoreClient

db = FakeFirestoreClient()

# Collections
db.collections()
db.collection('users')
db.collection('users').get()
db.collection('users').list_documents()
db.collection('users').stream()

# Documents
db.collection('users').document()
db.collection('users').document('alovelace')
db.collection('users').document('alovelace').id
db.collection('users').document('alovelace').parent
db.collection('users').document('alovelace').update_time
db.collection('users').document('alovelace').read_time
db.collection('users').document('alovelace').get()
db.collection('users').document('alovelace').get().exists
db.collection('users').document('alovelace').get().to_dict()
db.collection('users').document('alovelace').set({
    'first': 'Ada',
    'last': 'Lovelace'
})
db.collection('users').document('alovelace').set({'first': 'Augusta Ada'}, merge=True)
db.collection('users').document('alovelace').update({'born': 1815})
db.collection('users').document('alovelace').update({'favourite.color': 'red'})
db.collection('users').document('alovelace').update({'associates': ['Charles Babbage', 'Michael Faraday']})
db.collection('users').document('alovelace').collection('friends')
db.collection('users').document('alovelace').delete()
db.collection('users').document(document_id='alovelace').delete()
db.collection('users').add({'first': 'Ada', 'last': 'Lovelace'}, 'alovelace')
db.get_all([db.collection('users').document('alovelace')])
db.document('users/alovelace')
db.document('users/alovelace').update({'born': 1815})
db.collection('users/alovelace/friends')

# Querying
db.collection('users').order_by('born').get()
db.collection('users').order_by('born', direction='DESCENDING').get()
db.collection('users').limit(5).get()
db.collection('users').where('born', '==', 1815).get()
db.collection('users').where('born', '!=', 1815).get()
db.collection('users').where('born', '<', 1815).get()
db.collection('users').where('born', '>', 1815).get()
db.collection('users').where('born', '<=', 1815).get()
db.collection('users').where('born', '>=', 1815).get()
db.collection('users').where('born', 'in', [1815, 1900]).stream()
db.collection('users').where('associates', 'array_contains', 'Charles Babbage').stream()
db.collection('users').where('associates', 'array_contains_any', ['Charles Babbage', 'Michael Faraday']).stream()

# Transforms
from google.cloud import firestore
db.collection('users').document('alovelace').update({'likes': firestore.Increment(1)})
db.collection('users').document('alovelace').update({'associates': firestore.ArrayUnion(['Andrew Cross', 'Charles Wheatstone'])})
db.collection('users').document('alovelace').update({firestore.DELETE_FIELD: "born"})
db.collection('users').document('alovelace').update({'associates': firestore.ArrayRemove(['Andrew Cross'])})

# Cursors
db.collection('users').start_after({'id': 'alovelace'}).stream()
db.collection('users').end_before({'id': 'alovelace'}).stream()
db.collection('users').end_at({'id': 'alovelace'}).stream()
db.collection('users').start_after(db.collection('users').document('alovelace')).stream()

# Transactions
transaction = db.transaction()
transaction.id
transaction.in_progress
transaction.get(db.collection('users').where('born', '==', 1815))
transaction.get(db.collection('users').document('alovelace'))
transaction.get_all([db.collection('users').document('alovelace')])
transaction.set(db.collection('users').document('alovelace'), {'born': 1815})
transaction.update(db.collection('users').document('alovelace'), {'born': 1815})
transaction.delete(db.collection('users').document('alovelace'))
transaction.commit()

Running the tests

poetry install
poetry run pytest

Contract tests with Firestore emulator

Contract tests live under tests/contract/ and can be run against the Firestore emulator by setting environment variables and choosing the backend:

export FIRESTORE_BACKEND=emulator
export FIRESTORE_EMULATOR_HOST=localhost:8080
export GOOGLE_CLOUD_PROJECT=demo-test
poetry run pytest tests/contract

To run the same contract tests against the in-memory fake:

export FIRESTORE_BACKEND=fake
poetry run pytest tests/contract

You can also use the helper script to run contract tests against the emulator:

chmod +x scripts/run_contract_emulator.sh
export FIRESTORE_EMULATOR_HOST=localhost:8080
export GOOGLE_CLOUD_PROJECT=demo-test
export FIRESTORE_EMULATOR_LOG=/tmp/firestore-emulator.log
./scripts/run_contract_emulator.sh

Original Contributors

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fake_firestore-0.8.3.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fake_firestore-0.8.3-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file fake_firestore-0.8.3.tar.gz.

File metadata

  • Download URL: fake_firestore-0.8.3.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for fake_firestore-0.8.3.tar.gz
Algorithm Hash digest
SHA256 e4c1983ced61eaf676299ab4026bcc27df766ee84753e8ea4021401da78eee95
MD5 6dfd7df4c1f7d160b0c47214aa0f60b0
BLAKE2b-256 11ad7e44c63eb8f9ff0f324399dcff28932f502e39a7fdebd8efb9e32caf8116

See more details on using hashes here.

File details

Details for the file fake_firestore-0.8.3-py3-none-any.whl.

File metadata

  • Download URL: fake_firestore-0.8.3-py3-none-any.whl
  • Upload date:
  • Size: 16.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for fake_firestore-0.8.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2fd44b6e0f2b11245ea0d15c382c073cce68ba72bba1d58a0edf08b6b38e2af7
MD5 9edc9130a3a578e5a4481b67326f4d8d
BLAKE2b-256 f078df44efe9bc9eacee32c93e3c0272f8777ca67fc7e0f00b33bc63d4a0c1d6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page