Is there more than one way of finding a list of database names?

I’m writing some unit tests which will be run automatically. The function in my program returns a list of local mongo databases and my unit test checks that the list is correct. At the moment, both my program and my unit test use the mongoDB listDatabases() command. My test will also call the program function to confirm that both match. However, I feel that it is not a valid test when both the program and the test use the same call. I can check it visually in Compass, but I am aiming for automated TDD. Any advice gratefully received.

I think that is it. You end up sending the same command if you’ve ever had the opportunity to look at a tcpdump of the operation.

1 Like

If there were two ways you could get the databases listed you would actually be testing that both commands produce the same output rather than that you have the databases you are expecting to have. Even if all your databases got deleted a test that lists databases (even if the command is run in two different ways) would pass because both would still have the same amount of databases.

If you want to test that your cluster contains the databases you are expecting there’s no way around listing the databases you should have somewhere as a hard coded value and then comparing the output of listDatabases to that.

3 Likes

I think what I will do is check that both the list of databases in my test and the list of databases in my program match. Then I’ll add a new database with a randomly generated name and check that both list the database new database with a name that I know. Thank you to @Naomi_Pentrel and @Stennie_X for your prompt responses.

1 Like

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