Hello friends! I’m back again with another story of how I implemented a new feature with TDD in mind and fix some issues from Sonarqube’s inspection in DIGIPUS project. In this second sprint, I implemented Material: Sorting by Comment Count and QA: Fix Bugs Detected By Sonarqube (Part 2). Let’s start walking through how I implement the feature and improve the quality of the codes.
How did I implement Sorting Material by Comment’s Count
The first thing that I did for the [RED]
phase was creating a new test class called DaftarKatalogSortingByJumlahKomentarTest
. I chose that name to follow the other test classes naming styles. After that I wrote the Prefix part of the test that was the setUp()
function. In this function I declared some credetial examples to create new users. I needed that users to create 2 materials with different comment’s count.
Then, I created a new test function called test_sorting_by_jumlah_komentar
. This function would check the Material’s order after the sort by comment’s count endpoint called. The expected result of the test was the Materi 2
placed on the first order because there was a comment in the Materi 2
while the Materi 1
placed on the second order without any comment. Here is the implementation of the test function:
def test_sorting_by_jumlah_komentar(self):
response = self.client.get("/?sort=jumlah_komentar")
self.assertRegex(str(response.content), rf'.*Materi 2.*Materi 1.*')
I pushed the test to the repository and then the pipeline failed. After that, for the [GREEN]
phase I implemented the sort by comment’s count endpoint in the services.py
and updated the HTML templates. I tested it first on the localhost and then pushed it to the repository. The pipeline passed and that’s all that I needed to keep the TDD requirements.
How did I fixed bugs detected by Sonarqube
I checked the quality gate of DIGIPUS project with Sonarqube and it said that the quality gate was failed. There are 15.5% duplicated lines, 9 bugs, and 4 security hotspots. I tried to analyze the bugs that are detected, most of them found on the HTML templates. I fixed the bugs one by one by doing what the Sonarqube recommend. After that, I pushed all of the changes to the repository and checked the Sonarqube again. That time, there was one bug that still detected by the Sonarqube although I fixed it. I asked to one of the professor assistant and he said that it was a false detection. So he reported the false detection bug and then all of the bugs are fixed. This was the screenshot of the Sonarqube right before I fixed all of the bugs, I forgot to take a screenshot again after the bugs are fixed.