Write unity tests for database operation functions and clean the code #26
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
Assuming that we need tests to have sure about if each piece of code is working we need test each piece of code. So to do that, we need write each class/method separately and that's why i changed a little of the project structure. Also i cleaned the code because functions like
fetchTopParticipants
was also updating thetopParticipants
array andincrementParticipantPoints
was creating a participant when he doesn't exist. By this way each function fit on the single responsibility principleAbout testing database operations, i used the MongoDB Memory Server that uses the last
mongod
binary so it's unnecessary create two databases to test everything.Also i resolved that
TODO
comment about returning just thename
and thepoints
of a viewerChanges
fetchTopParticipants
andincrementParticipantPoints
separately as utils functions.DatabaseClient
class that extends theMongoClient
. To have access toparticipants
collection, we had to access the database then access the collection but we wasn't using the database instance to do anything. Thinking like theRepository Pattern
,DatabaseClient
do the same as creating the MongoClient and then connecting to the database but now we have thegetCollection
method that receives the collection name, the collection type and returns the collection without having to instantiate the database.topParticipants
array fromsrc/app.ts
. I think that it's unnecessary when we already have a functionfetchTopParticipants
that return whattopParticipants
have to store and without it we can handle the data more precisely without side effects. Also on this way when we implement this project using the MVC pattern, we will able to create controllers separately without depending of a global variable