Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write unity tests for database operation functions and clean the code #26

Merged
merged 12 commits into from
Apr 30, 2020

Conversation

powilliam
Copy link
Contributor

@powilliam powilliam commented Apr 30, 2020

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 the topParticipants array and incrementParticipantPoints was creating a participant when he doesn't exist. By this way each function fit on the single responsibility principle

About 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 the name and the points of a viewer

Changes

  • Implements fetchTopParticipants and incrementParticipantPoints separately as utils functions.
  • Implements a DatabaseClient class that extends the MongoClient. To have access to participants collection, we had to access the database then access the collection but we wasn't using the database instance to do anything. Thinking like the Repository Pattern, DatabaseClient do the same as creating the MongoClient and then connecting to the database but now we have the getCollection method that receives the collection name, the collection type and returns the collection without having to instantiate the database.
  • Remove the topParticipants array from src/app.ts. I think that it's unnecessary when we already have a function fetchTopParticipants that return what topParticipants 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

Copy link

@d-klotz d-klotz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well done! 🚀

@lucasmontano lucasmontano merged commit 15f1fd1 into lucasmontano:master Apr 30, 2020
@powilliam powilliam deleted the chore/tests branch April 30, 2020 12:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants