Badhan-BUET-Zone / badhan-test

Test scripts for Badhan
0 stars 0 forks source link

write test script for GET /log/donations #5

Open mirmahathir1 opened 4 months ago

mirmahathir1 commented 4 months ago

🎯 PR Acceptance Criteria

hmasum52 commented 4 months ago

After calling the endpoint {{url}}/log/donations?startDate=1707237110000&endDate=1717113600000

I got the following response:

{
    "status": "OK",
    "statusCode": 200,
    "message": "Fetched donation count by year and month",
    "countByYearMonth": {
        "2000": {
            "1": 1
        },
        "2021": {
            "1": 1
        },
        "2024": {
            "3": 1,
            "4": 1
        }
    }
}

@mirmahathir1 vai how do I write the schema for this? Shouldn't countByYearMonth be array? Also for each year, the month is also not fixed this must be array too.

mirmahathir1 commented 4 months ago

After calling the endpoint {{url}}/log/donations?startDate=1707237110000&endDate=1717113600000

I got the following response:

{
    "status": "OK",
    "statusCode": 200,
    "message": "Fetched donation count by year and month",
    "countByYearMonth": {
        "2000": {
            "1": 1
        },
        "2021": {
            "1": 1
        },
        "2024": {
            "3": 1,
            "4": 1
        }
    }
}

@mirmahathir1 vai how do I write the schema for this? Shouldn't countByYearMonth be array? Also for each year, the month is also not fixed this must be array too.

I understand your concern. I wanted to do what you said too because these should have been arrays. But I wanted to make this api endpoint as fast as possible because it would take place on the front page of the UI and essentially uses the entire donations collection. Therefore, I pushed down the whole implementation of the logic to the database level here:

https://github.com/Badhan-BUET-Zone/badhan-backend/blob/8de59675f778d481e6435ed568e1ff167f522116/src/db/interfaces/donationInterface.ts#L140-L191

This essentially generates the response you saw, no other postprocessing occurs in the backend. And accessing the years and months through keys became efficient (in my opinion, I may be wrong though) when I populated the data for the chart here on the frontend:

https://github.com/Badhan-BUET-Zone/badhan-web/blob/59ee931a47d67dda1c451a6f779aeea807bda837/src/views/SignInCover.vue#L308

What I want you to do is,

If you feel like you can't do this with jsonschema validation, let me know. I'll give a try on this issue and I will assign you new issues

hmasum52 commented 4 months ago

@mirmahathir1 vai I want to use Supertest for testing the apis. Supertest is a popular library with almost 13k star on github.

Here are some references on how you can test your API using Supertest:

Please share your thoughts on this 💭

mirmahathir1 commented 4 months ago

Go ahead. Write test scripts for this endpoint using supertest. However, make sure to study all the already written tests and make up your mind on how you would maintain a convention to rewrite all the tests (directory structure, cases to be handled etc) incrementally.

TODO: 1) For this issue, just write the test for one single route associated with this issue using supertest by maintaining the convention you want to follow.

2) Modify the npm scripts to make sure that if I run npm run test (or something similar), all the jest and supertest tests run

I also want to point out that jest, the library already being used, is a very popular library that is in the scene for a long time https://github.com/jestjs/jest

hmasum52 commented 4 months ago

On it 👨‍💻