Below is the function for saving data in Mongo.
def _save(data):
db_client = DBConnect().client
try:
db = db_client[os.environ['DB']]
table = db['Table']
table.insert_many([data], ordered=False)
except Exception as err:
log.error(err)
finally:
db_client.close()
I am testing this function.
I wrote test case like this
@pytest.fixture
def client_mock():
yield mongomock.MongoClient()
@pytest.fixture(autouse=False)
def patch_mongo(monkeypatch, client_mock):
mock_client = client_mock
db = mock_client['DB']
col = db['Table']
def fake_mongo():
return mock_client.db
monkeypatch.setattr('address.DBConnect', fake_mongo)
Now the testcase
def test_save(patch_mongo, client_mock):
data = {'1':[1, 2, 3]}
k = mock.patch.dict(os.environ, {'DB': 'DB'})
k.start()
x = _save(data)
k.stop()
the data is successfully inserted, now i want to assert the db contains this data. Can anyone help?