How to unit test operators available only on atlas?

As the title says, how would I achieve this? Say for example, I have this backend api which will eventually query the atlas freetier cluster when it’s deployed to production. This query uses $search to be able to execute full text search on “products” collection. My TDD environment is set up with mongodb in-memory server. This in-memory server doesn’t recognize $search operator.

I’m currently mocking/stubbing the result from the query that I executed manually from MongodbCompass. The process is like, execute the query and check the result. If it’s the one I was expecting then, copy and paste the query to the backend code and use the result as stub.
It doesn’'t seem to be too bad for now but what if db configuration changes, then I’ll always have to replace the stub with latest result from actual db query. I don’t feel like stubbing the result is a good idea.
Unit testing to compare the actual result with what I’m expecting while I already know the result? hmm it’s almost like doing nothing.

The only way I came up with is just connect to dev db on atlas. I was wondering if there’s other way or right way to achieve what I want without using actual cloud db. I prefer in-memory server cause obviously it’s fast and light.

Hi @Polar_swimming,

Could you clarify what you mean by in-memory server? I presume you’re utilsing something like mongodb-memory-server but please correct me if i am wrong here. If this is the case, then it is expected that the $search operator is not recognizable in that instance as it is only available on Atlas.

The only way I came up with is just connect to dev db on atlas. I was wondering if there’s other way or right way to achieve what I want without using actual cloud db.

Unfortunately since the $search operator is Atlas-specific, there is currently no method that I’m aware of that can replicate this functionality locally.

Regards,
Jason

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.