Mocking for unit testing in faust application and mongodb

Alireza Moosavi
2 min readAug 6, 2020

In this story, a stream processing application with faust is going to be tested!

This application lives in this repository, the main intention of developing this app was explaining the steps to do some unit tests on it.

First we know that as it can be seen here, a faust stream process uses python asynchronous functionality, also it process real-time data from kaka topics. So, these two had to be mocked!

About the tests

First even_loop mock is developed in conftest. Then faust app needes to be imported and mocked:

@pytest.yield_fixture(name="event_loop", scope="session")
def fixture_event_loop(request):
loop = asyncio.get_event_loop()
yield loop
loop.close()
@pytest.fixture(name="mock_faust", scope="session")
def fixture_faust_app(event_loop):
f_app.app.finalize()
f_app.app.conf.store = "memory://"
f_app.app.flow_control.resume()
return f_app.app

And finally this faust app saves data into mongodb, so the mongodb allso needs to get mocked to:

@pytest.fixture(name="mock_mongo", scope="session")
def fixture_mock_db():
disconnect()
db = connect("mongoenginetest", host="mongomock://localhost")
print("\n test_mongo: connected!\n")
yield db
db.drop_database("mongoenginetest")
db.close()
print("\n test_mongo: disconnected!")

Now the Tests should be developed:

@pytest.mark.asyncio
async def test_auth_data_processor(mock_mongo, mock_faust):
async with auth_data_processor.test_context() as agent:
await agent.ask(value=true_data[“v1”])
p = Auth.objects(key=true_data[“v1”][“key”])
p_obj = p.first()
counteer = p.count()
assert counteer == 1
assert p_obj.first_name == true_data[“v1”][“first_name”]
assert p_obj.phone == true_data[“v1”][“phone”]

This test will be mocked by faust and mongodb also the asyncio mocks the event_loop.

How to Run

First clone this project and get into it:

Then RUN:

make buildstart

The application will be running…

make create-sample-topic

The topic in kafka for the application will be created.

To run tests:

make run-tests

--

--