Repository: mdowds/python-mock-firestore

Issue:
DocumentSnapshot.get() missing
The DocumentSnapshot has a get function, which seems to be missing here. Large sections of our source code uses the get function. It would be great if this could be added.

Discussion:
mikheillomidze: @mdowds Can you please merge this pull request and update package on pypi

Patch:
diff --git a/mockfirestore/document.py b/mockfirestore/document.py
index 24c8a68..ccd3ba7 100644
--- a/mockfirestore/document.py
+++ b/mockfirestore/document.py
@@ -27,9 +27,15 @@ class DocumentSnapshot:
         timestamp = Timestamp.from_now()
         return timestamp
 
+    def get(self, field_path: str) -> Any:
+        if not self.exists:
+            return None
+        else:
+            return reduce(operator.getitem, field_path.split('.'), self._doc)
+
     def _get_by_field_path(self, field_path: str) -> Any:
         try:
-            return reduce(operator.getitem, field_path.split('.'), self._doc)
+            return self.get(field_path)
         except KeyError:
             return None
 


Test patch:
diff --git a/tests/test_document_snapshot.py b/tests/test_document_snapshot.py
index 19cce06..7d60e4b 100644
--- a/tests/test_document_snapshot.py
+++ b/tests/test_document_snapshot.py
@@ -46,3 +46,45 @@ class TestDocumentSnapshot(TestCase):
         doc_snapshot = doc_ref.get()
         self.assertIs(doc_ref, doc_snapshot.reference)
 
+    def test_documentSnapshot_get_by_existing_field_path(self):
+        fs = MockFirestore()
+        fs._data = {'foo': {
+            'first': {'id': 1, 'contact': {
+                'email': 'email@test.com'
+            }}
+        }}
+        doc = fs.collection('foo').document('first').get()
+        self.assertEqual(doc.get('contact.email'), 'email@test.com')
+
+    def test_documentSnapshot_get_by_non_existing_field_path(self):
+        fs = MockFirestore()
+        fs._data = {'foo': {
+            'first': {'id': 1, 'contact': {
+                'email': 'email@test.com'
+            }}
+        }}
+        doc = fs.collection('foo').document('first').get()
+        with self.assertRaises(KeyError):
+            doc.get('contact.phone')
+
+    def test_documentSnapshot_get_in_an_non_existing_document(self):
+        fs = MockFirestore()
+        fs._data = {'foo': {
+            'first': {'id': 1, 'contact': {
+                'email': 'email@test.com'
+            }}
+        }}
+        doc = fs.collection('foo').document('second').get()
+        self.assertIsNone(doc.get('contact.email'))
+
+    def test_documentSnapshot_get_returns_a_copy_of_the_data_stored(self):
+        fs = MockFirestore()
+        fs._data = {'foo': {
+            'first': {'id': 1, 'contact': {
+                'email': 'email@test.com'
+            }}
+        }}
+        doc = fs.collection('foo').document('first').get()
+        self.assertIsNot(
+            doc.get('contact'),fs._data['foo']['first']['contact']
+        )