def test_document_update_transformerArrayUnionNested(self):
        fs = MockFirestore()
        fs._data = {
            'foo': {
                'first': {
                    'nested': {
                        'arr': [1]
                    },
                    'other': {
                        'labels': ["a"]
                    },
                }
            }
        }
        fs.collection('foo').document('first').update({
            'nested': {
                'arr': firestore.ArrayUnion([2])
            },
            'other': {
                'labels': firestore.ArrayUnion(["b"]),
                'smoked': 'salmon'
            },
        })

        doc = fs.collection('foo').document('first').get().to_dict()
        self.assertEqual(
            doc, {
                'nested': {
                    'arr': [1, 2]
                },
                'other': {
                    'labels': ["a", "b"],
                    'smoked': 'salmon'
                }
            })
 def test_document_set_mergeNewValue(self):
     fs = MockFirestore()
     fs._data = {'foo': {'first': {'id': 1}}}
     fs.collection('foo').document('first').set({'updated': True},
                                                merge=True)
     doc = fs.collection('foo').document('first').get().to_dict()
     self.assertEqual({'id': 1, 'updated': True}, doc)
 def test_set_document_by_path(self):
     fs = MockFirestore()
     fs._data = {}
     doc_content = {'id': 'bar'}
     fs.document('foo/doc1/bar/doc2').set(doc_content)
     doc = fs.document('foo/doc1/bar/doc2').get().to_dict()
     self.assertEqual(doc_content, doc)
 def test_document_set_setsContentOfDocument(self):
     fs = MockFirestore()
     fs._data = {'foo': {}}
     doc_content = {'id': 'bar'}
     fs.collection('foo').document('bar').set(doc_content)
     doc = fs.collection('foo').document('bar').get().to_dict()
     self.assertEqual(doc_content, doc)
 def test_document_update_transformerArrayUnionBasic(self):
     fs = MockFirestore()
     fs._data = {"foo": {"first": {"arr": [1, 2]}}}
     fs.collection("foo").document("first").update(
         {"arr": firestore.ArrayUnion([3, 4])})
     doc = fs.collection("foo").document("first").get().to_dict()
     self.assertEqual(doc["arr"], [1, 2, 3, 4])
    def test_document_update_transformerIncrementNested(self):
        fs = MockFirestore()
        fs._data = {
            'foo': {
                'first': {
                    'nested': {
                        'count': 1
                    },
                    'other': {
                        'likes': 0
                    },
                }
            }
        }
        fs.collection('foo').document('first').update({
            'nested': {
                'count': firestore.Increment(-1)
            },
            'other': {
                'likes': firestore.Increment(1),
                'smoked': 'salmon'
            },
        })

        doc = fs.collection('foo').document('first').get().to_dict()
        self.assertEqual(doc, {
            'nested': {
                'count': 0
            },
            'other': {
                'likes': 1,
                'smoked': 'salmon'
            }
        })
    def test_collection_whereArrayContains(self):
        fs = MockFirestore()
        fs._data = {
            'foo': {
                'first': {
                    'field': ['val4']
                },
                'second': {
                    'field': ['val3', 'val2']
                },
                'third': {
                    'field': ['val3', 'val2', 'val1']
                }
            }
        }

        docs = list(
            fs.collection('foo').where('field', 'array_contains',
                                       'val1').stream())
        self.assertEqual(len(docs), 1)
        self.assertEqual(docs[0].to_dict(),
                         {'field': ['val3', 'val2', 'val1']})
Exemple #8
0
def test_post_updates(get_database_client, app):
    mock_db = MockFirestore()
    get_database_client.return_value = mock_db

    mock_db.collection('items').document('item1').set({'quantity': 1})
    expected = {'quantity': 4}
    with app.test_request_context(
            json={
                "item": "item1",
                "quantity": 4
            },
            content_type="application/json",
            method='POST',
            headers={'Origin': 'https://inventoryapp-276220.web.app'}):
        res = main.post(flask.request)
        assert res.status_code == 200
        assert expected == res.get_json()
        doc = mock_db.collection('items').document('item1').get()
        assert doc.to_dict() == expected
        assert 'https://inventoryapp-276220.web.app' == res.headers[
            'Access-Control-Allow-Origin']
        assert 'true' == res.headers['Access-Control-Allow-Credentials']
class TestStream(TestCase):
    def setUp(self):
        self.store = MockFirestore()
        self.store._data = {
            'foo': {
                'first': {'id': 1, 'value': 'number one'},
                'second': {'id': 2},
            }
        }

    def test_basic(self):
        results = list(self.store.collection('foo').where('id', '==', 1).stream())
        assert len(results) == 1
        assert results[0].to_dict() == {'id': 1, 'value': 'number one'}

    def test_handles_non_equality_mismatch(self):
        # NOTE: This is the closest to non-equality we can do for firestore
        # (source: https://stackoverflow.com/a/48481812/13340678), and is essentially conducting
        # a presence test.
        results = list(self.store.collection('foo').where('value', '>', '').stream())
        assert len(results) == 1
        assert results[0].to_dict() == {'id': 1, 'value': 'number one'}
Exemple #10
0
def test_database_get():
    """
    Tests that database library can accurately fetch from database given ID.
    """
    mock_db = MockFirestore()

    test_data = {"name": "Leroy", "id": 1337}

    test_collection_name = "users"

    mock_db.collection(test_collection_name).document(test_data["id"]).set(test_data)

    test_db = Database(test_collection_name, firestore_client=mock_db)

    doc = test_db.get(test_data["id"])

    assert doc.exists

    doc_json = doc.to_dict()

    assert doc_json["name"] == test_data["name"]
    assert doc_json["id"] == test_data["id"]
    def test_collection_orderBy_descending(self):
        fs = MockFirestore()
        fs._data = {
            'foo': {
                'first': {
                    'order': 2
                },
                'second': {
                    'order': 3
                },
                'third': {
                    'order': 1
                }
            }
        }

        docs = list(
            fs.collection('foo').order_by('order',
                                          direction="DESCENDING").stream())
        self.assertEqual({'order': 3}, docs[0].to_dict())
        self.assertEqual({'order': 2}, docs[1].to_dict())
        self.assertEqual({'order': 1}, docs[2].to_dict())
 def test_document_set_isolation(self):
     fs = MockFirestore()
     fs._data = {'foo': {}}
     doc_content = {'id': 'bar'}
     fs.collection('foo').document('bar').set(doc_content)
     doc_content['id'] = 'new value'
     doc = fs.collection('foo').document('bar').get().to_dict()
     self.assertEqual({'id': 'bar'}, doc)
 def test_document_delete_documentDoesNotExistAfterDelete(self):
     fs = MockFirestore()
     fs._data = {'foo': {
         'first': {'id': 1}
     }}
     fs.collection('foo').document('first').delete()
     doc = fs.collection('foo').document('first').get()
     self.assertEqual(False, doc.exists)
    def test_document_update_transformerIncrementNonExistent(self):
        fs = MockFirestore()
        fs._data = {'foo': {'first': {'spicy': 'tuna'}}}
        fs.collection('foo').document('first').update(
            {'count': firestore.Increment(1)})

        doc = fs.collection('foo').document('first').get().to_dict()
        self.assertEqual(doc, {'count': 1, 'spicy': 'tuna'})
    def test_document_update_transformerIncrementBasic(self):
        fs = MockFirestore()
        fs._data = {'foo': {'first': {'count': 1}}}
        fs.collection('foo').document('first').update(
            {'count': firestore.Increment(2)})

        doc = fs.collection('foo').document('first').get().to_dict()
        self.assertEqual(doc, {'count': 3})
 def test_document_update_isolation(self):
     fs = MockFirestore()
     fs._data = {'foo': {'first': {'nested': {'id': 1}}}}
     update_doc = {'nested': {'id': 2}}
     fs.collection('foo').document('first').update(update_doc)
     update_doc['nested']['id'] = 3
     doc = fs.collection('foo').document('first').get().to_dict()
     self.assertEqual({'nested': {'id': 2}}, doc)
    def test_document_update_transformerArrayUnionNonExistent(self):
        fs = MockFirestore()
        fs._data = {'foo': {'first': {'spicy': 'tuna'}}}
        fs.collection('foo').document('first').update(
            {'arr': firestore.ArrayUnion([1])})

        doc = fs.collection('foo').document('first').get().to_dict()
        self.assertEqual(doc, {'arr': [1], 'spicy': 'tuna'})
 def test_document_update_changeExistingValue(self):
     fs = MockFirestore()
     fs._data = {'foo': {
         'first': {'id': 1}
     }}
     fs.collection('foo').document('first').update({'id': 2})
     doc = fs.collection('foo').document('first').get().to_dict()
     self.assertEqual({'id': 2}, doc)
 def test_document_set_overwriteValue(self):
     fs = MockFirestore()
     fs._data = {'foo': {
         'first': {'id': 1}
     }}
     fs.collection('foo').document('first').set({'new_id': 1}, merge=False)
     doc = fs.collection('foo').document('first').get().to_dict()
     self.assertEqual({'new_id': 1}, doc)
    def test_collection_whereArrayContainsAny(self):
        fs = MockFirestore()
        fs._data = {
            'foo': {
                'first': {
                    'field': ['val4']
                },
                'second': {
                    'field': ['val3', 'val2']
                },
                'third': {
                    'field': ['val3', 'val2', 'val1']
                }
            }
        }

        contains_any_docs = list(
            fs.collection('foo').where('field', 'array_contains_any',
                                       ['val1', 'val4']).stream())
        self.assertEqual(len(contains_any_docs), 2)
        self.assertEqual({'field': ['val4']}, contains_any_docs[0].to_dict())
        self.assertEqual({'field': ['val3', 'val2', 'val1']},
                         contains_any_docs[1].to_dict())
Exemple #21
0
def test_post_not_existing(get_database_client, app):
    mock_db = MockFirestore()
    get_database_client.return_value = mock_db

    with app.test_request_context(
            json={
                "item": "item1",
                "quantity": 4
            },
            content_type="application/json",
            method='POST',
            headers={'Origin': 'https://inventoryapp-276220.web.app'}):
        res = main.post(flask.request)
        assert res.status_code == 404
Exemple #22
0
def test_database_add():
    """
    Tests that database library can accurately add to database.
    """
    mock_db = MockFirestore()

    test_data = {"name": "Leroy", "id": 1337}

    test_collection_name = "users"

    test_db = Database(test_collection_name, firestore_client=mock_db)

    test_db.add(test_data, id=test_data["id"])

    doc_ref = mock_db.collection(test_collection_name).document(test_data["id"])
    doc = doc_ref.get()

    assert doc.exists

    doc_json = doc.to_dict()

    assert doc_json["name"] == test_data["name"]
    assert doc_json["id"] == test_data["id"]
    def test_collection_addDocument(self):
        fs = MockFirestore()
        fs._data = {'foo': {}}
        doc_id = 'bar'
        doc_content = {'id': doc_id, 'xy': 'z'}
        timestamp, doc_ref = fs.collection('foo').add(doc_content)
        self.assertEqual(doc_content, doc_ref.get().to_dict())

        doc = fs.collection('foo').document(doc_id).get().to_dict()
        self.assertEqual(doc_content, doc)

        with self.assertRaises(AlreadyExists):
            fs.collection('foo').add(doc_content)
Exemple #24
0
def test_delete_not_existing(get_database_client, app):
    mock_db = MockFirestore()
    get_database_client.return_value = mock_db

    with app.test_request_context(
            json={"item": "item1"},
            content_type="application/json",
            method='DELETE',
            headers={'Origin': 'https://inventoryapp-276220.web.app'}):
        res = main.delete(flask.request)
        print("%s" % str(res))
        assert res.status_code == 200
        assert 'https://inventoryapp-276220.web.app' == res.headers[
            'Access-Control-Allow-Origin']
        assert 'true' == res.headers['Access-Control-Allow-Credentials']
    def test_collection_whereIn(self):
        fs = MockFirestore()
        fs._data = {
            'foo': {
                'first': {
                    'field': 'a1'
                },
                'second': {
                    'field': 'a2'
                },
                'third': {
                    'field': 'a3'
                },
                'fourth': {
                    'field': 'a4'
                },
            }
        }

        docs = list(
            fs.collection('foo').where('field', 'in', ['a1', 'a3']).stream())
        self.assertEqual(len(docs), 2)
        self.assertEqual({'field': 'a1'}, docs[0].to_dict())
        self.assertEqual({'field': 'a3'}, docs[1].to_dict())
Exemple #26
0
def test_get_all(get_database_client, app):
    mock_db = MockFirestore()
    get_database_client.return_value = mock_db
    mock_db.collection('items').document('test').set({'quantity': 1})
    mock_db.collection('items').document('test2').set({'quantity': 2})

    expected = [{"test": {'quantity': 1}}, {"test2": {'quantity': 2}}]
    with app.test_request_context(
            headers={'Origin': 'https://inventoryapp-276220.web.app'}):
        res = main.get(flask.request)
        assert res.status_code == 200
        assert expected == res.get_json()
        assert 'https://inventoryapp-276220.web.app' == res.headers[
            'Access-Control-Allow-Origin']
        assert 'true' == res.headers['Access-Control-Allow-Credentials']
Exemple #27
0
def test_database_query():
    """
    Tests that database library can accurately query database.
    """
    mock_db = MockFirestore()

    test_data_1 = {"name": "Leroy", "id": 1337}
    test_data_2 = {"name": "Julia", "id": 7}
    test_data_3 = {"name": "Adrian", "id": 2000}

    test_collection_name = "users"

    mock_db.collection(test_collection_name).document(test_data_1["id"]).set(
        test_data_1
    )
    mock_db.collection(test_collection_name).document(test_data_2["id"]).set(
        test_data_2
    )
    mock_db.collection(test_collection_name).document(test_data_3["id"]).set(
        test_data_3
    )

    test_db = Database(test_collection_name, firestore_client=mock_db)

    query_results = test_db.query("id", "==", test_data_1["id"])

    assert len(query_results) == 1

    doc = query_results[0]

    assert doc.exists

    doc_json = doc.to_dict()

    assert doc_json["name"] == test_data_1["name"]
    assert doc_json["id"] == test_data_1["id"]
Exemple #28
0
 def test_document_delete_MacAddress_NotExist_After_Delete(self):
     fs = MockFirestore()
     fs._data = {
         'user power supply units': {
             '84:CC:A8:84:B8:00': {
                 'ScreenState': 'ON'
             },
             '84:CC:A8:84:B8:20': {
                 'ScreenState': 'OFF'
             },
             '84:CC:A8:84:B8:40': {
                 'ScreenState': 'ON'
             },
         }
     }
     fs.collection('user power supply units').document(
         '84:CC:A8:84:B8:20').delete()
     doc = fs.collection('user power supply units').document(
         '84:CC:A8:84:B8:20').get()
     self.assertEqual(False, doc.exists)
Exemple #29
0
 def test_document_get_returnsDocument(self):
     fs = MockFirestore()
     fs._data = {'foo': {'first': {'id': 1}}}
     doc = fs.collection('foo').document('first').get().to_dict()
     self.assertEqual({'id': 1}, doc)
Exemple #30
0
 def test_document_get_documentDoesNotExist(self):
     fs = MockFirestore()
     fs._data = {'foo': {}}
     doc = fs.collection('foo').document('bar').get().to_dict()
     self.assertEqual({}, doc)