Package: mock-firestore-async
Python Mock FirestoreAn in-memory implementation of thePython client libraryfor Google Cloud Firestore, intended for use in tests to replace the real thing. This project is only a partial implementation of the real client library.To install:pip install mock-firestorePython 3.6+ is required for it to work.UsageSyncdb=firestore.Client()mock_db=MockFirestore()# Can be used in the same way as a firestore.Client() object would be, e.g.:db.collection('users').get()mock_db.collection('users').get()Asyncdb=firestore.AsyncClient()mock_db=AsyncMockFirestore()awaitdb.collection('users').get()awaitmock_db.collection('users').get()To reset the store to an empty state, use thereset()method:mock_db=MockFirestore()mock_db.reset()or the equivalent method ofAsyncMockFirestoreSupported operationsSyncmock_db=MockFirestore()# Collectionsmock_db.collections()mock_db.collection('users')mock_db.collection('users').get()mock_db.collection('users').list_documents()mock_db.collection('users').stream()# Documentsmock_db.collection('users').document()mock_db.collection('users').document('alovelace')mock_db.collection('users').document('alovelace').idmock_db.collection('users').document('alovelace').parentmock_db.collection('users').document('alovelace').update_timemock_db.collection('users').document('alovelace').read_timemock_db.collection('users').document('alovelace').get()mock_db.collection('users').document('alovelace').get().existsmock_db.collection('users').document('alovelace').get().to_dict()mock_db.collection('users').document('alovelace').set({'first':'Ada','last':'Lovelace'})mock_db.collection('users').document('alovelace').set({'first':'Augusta Ada'},merge=True)mock_db.collection('users').document('alovelace').update({'born':1815})mock_db.collection('users').document('alovelace').update({'favourite.color':'red'})mock_db.collection('users').document('alovelace').update({'associates':['Charles Babbage','Michael Faraday']})mock_db.collection('users').document('alovelace').collection('friends')mock_db.collection('users').document('alovelace').delete()mock_db.collection('users').document('alovelace').delete()mock_db.collection('users').add({'first':'Ada','last':'Lovelace'},'alovelace')mock_db.get_all([mock_db.collection('users').document('alovelace')])mock_db.document('users/alovelace')mock_db.document('users/alovelace').update({'born':1815})mock_db.collection('users/alovelace/friends')# Queryingmock_db.collection('users').order_by('born').get()mock_db.collection('users').order_by('born',direction='DESCENDING').get()mock_db.collection('users').limit(5).get()mock_db.collection('users').where('born','==',1815).get()mock_db.collection('users').where('born','!=',1815).get()mock_db.collection('users').where('born','<',1815).get()mock_db.collection('users').where('born','>',1815).get()mock_db.collection('users').where('born','<=',1815).get()mock_db.collection('users').where('born','>=',1815).get()mock_db.collection('users').where('born','in',[1815,1900]).stream()mock_db.collection('users').where('born','in',[1815,1900]).stream()mock_db.collection('users').where('associates','array_contains','Charles Babbage').stream()mock_db.collection('users').where('associates','array_contains_any',['Charles Babbage','Michael Faraday']).stream()# Transformsmock_db.collection('users').document('alovelace').update({'likes':firestore.Increment(1)})mock_db.collection('users').document('alovelace').update({'associates':firestore.ArrayUnion(['Andrew Cross','Charles Wheatstone'])})mock_db.collection('users').document('alovelace').update({firestore.DELETE_FIELD:"born"})mock_db.collection('users').document('alovelace').update({'associates':firestore.ArrayRemove(['Andrew Cross'])})# Cursorsmock_db.collection('users').start_after({'id':'alovelace'}).stream()mock_db.collection('users').end_before({'id':'alovelace'}).stream()mock_db.collection('users').end_at({'id':'alovelace'}).stream()mock_db.collection('users').start_after(mock_db.collection('users').document('alovelace')).stream()# Transactionstransaction=mock_db.transaction()transaction.idtransaction.in_progresstransaction.get(mock_db.collection('users').where('born','==',1815))transaction.get(mock_db.collection('users').document('alovelace'))transaction.get_all([mock_db.collection('users').document('alovelace')])transaction.set(mock_db.collection('users').document('alovelace'),{'born':1815})transaction.update(mock_db.collection('users').document('alovelace'),{'born':1815})transaction.delete(mock_db.collection('users').document('alovelace'))transaction.commit()Async(Where usage of those differs from the above)Note: all iterator methods likestreamorlist_documentsin AsyncMockFirestore and its associated async classes
return asynchronous iterators, so when iterating over them,async forsyntax must be used.mock_db=AsyncMockFirestore()# Collectionsawaitmock_db.collection('users').get()# async iterators[doc_refasyncfordoc_refinmock_db.collection('users').list_documents()][doc_snapshotasyncfordoc_snapshotinmock_db.collection('users').stream()]# Documentsawaitmock_db.collection('users').document('alovelace').get()doc_snapshot=awaitmock_db.collection('users').document('alovelace').get()doc_snapshot.existsdoc_snapshot.to_dict()awaitmock_db.collection('users').document('alovelace').set({'first':'Ada','last':'Lovelace'})awaitmock_db.collection('users').document('alovelace').set({'first':'Augusta Ada'},merge=True)awaitmock_db.collection('users').document('alovelace').update({'born':1815})awaitmock_db.collection('users').document('alovelace').update({'favourite.color':'red'})awaitmock_db.collection('users').document('alovelace').update({'associates':['Charles Babbage','Michael Faraday']})awaitmock_db.collection('users').document('alovelace').delete()awaitmock_db.collection('users').document('alovelace').delete()awaitmock_db.collection('users').add({'first':'Ada','last':'Lovelace'},'alovelace')awaitmock_db.get_all([mock_db.collection('users').document('alovelace')])awaitmock_db.document('users/alovelace').update({'born':1815})# Queryingawaitmock_db.collection('users').order_by('born').get()awaitmock_db.collection('users').order_by('born',direction='DESCENDING').get()awaitmock_db.collection('users').limit(5).get()awaitmock_db.collection('users').where('born','==',1815).get()awaitmock_db.collection('users').where('born','!=',1815).get()awaitmock_db.collection('users').where('born','<',1815).get()awaitmock_db.collection('users').where('born','>',1815).get()awaitmock_db.collection('users').where('born','<=',1815).get()awaitmock_db.collection('users').where('born','>=',1815).get()# async iteratorsmock_db.collection('users').where('born','in',[1815,1900]).stream()mock_db.collection('users').where('born','in',[1815,1900]).stream()mock_db.collection('users').where('associates','array_contains','Charles Babbage').stream()mock_db.collection('users').where('associates','array_contains_any',['Charles Babbage','Michael Faraday']).stream()# Transformsawaitmock_db.collection('users').document('alovelace').update({'likes':firestore.Increment(1)})awaitmock_db.collection('users').document('alovelace').update({'associates':firestore.ArrayUnion(['Andrew Cross','Charles Wheatstone'])})awaitmock_db.collection('users').document('alovelace').update({firestore.DELETE_FIELD:"born"})awaitmock_db.collection('users').document('alovelace').update({'associates':firestore.ArrayRemove(['Andrew Cross'])})# Transactionstransaction=mock_db.transaction()transaction.idtransaction.in_progressawaittransaction.get(mock_db.collection('users').where('born','==',1815))awaittransaction.get(mock_db.collection('users').document('alovelace'))awaittransaction.get_all([mock_db.collection('users').document('alovelace')])transaction.set(mock_db.collection('users').document('alovelace'),{'born':1815})transaction.update(mock_db.collection('users').document('alovelace'),{'born':1815})transaction.delete(mock_db.collection('users').document('alovelace'))awaittransaction.commit()Running the testsCreate and activate a virtualenv with a Python version of at least 3.6Install dependencies withpip install -r requirements-dev-minimal.txtRun tests withpython -m unittest discover tests -t /ContributorsMatt DowdsChris TippettAnna MelnikovBen RigglemanSteve Atwellahti123Billcountry MwanikiLucas MouraKamil RomaszkoAnna MelnikovCarl ChipperfieldAaron LooKristof KrennBen PhillipsRene DelgadoklandersonWilliam LiUgo MarchandBryce Thornton