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

feat: implement v2 for get /countries/leaderboard #97

Merged
merged 3 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions __tests__/e2e/countries.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import supertest from 'supertest';
import app from '../../server/app';
import seed from '../seed';

describe('', () => {
it('countries/6632544', async () => {
Expand Down Expand Up @@ -58,4 +59,25 @@ describe('', () => {
},
1000 * 60,
);

it('countries/v2/leaderboard', async () => {
const response = await seed
.clear()
.then(
async () =>
seed
.seed()
.then(
async () => supertest(app).get('/countries/v2/leaderboard'),
),
);
expect(response.status).toBe(200);
expect(response.body.countries[0]).toMatchObject({
Copy link
Contributor

Choose a reason for hiding this comment

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

For the leaderboard test, it might be beneficial to check which specific country is first in the array. I believe tree 1 is located in USA, tree 2 is located in Canada, so you could expect either Canada or USA. Maybe include more trees in your seed file to definitively have 1 country appear in the first array.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have added another tree planted in the US to make the top country specific. Now it checks if the country is US and if the number of trees planted is 2.

id: expect.any(Number),
name: expect.any(String),
planted: expect.any(String),
centroid: expect.stringMatching(/coordinates/),
});
await seed.clear();
});
});
58 changes: 58 additions & 0 deletions __tests__/seed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { knex } from 'knex';
import log from 'loglevel';
const uuid = require('uuid');

const connection = process.env.DATABASE_URL;

!connection && log.warn('env var DATABASE_URL not set');

const knexConfig = {
client: 'pg',
// debug: process.env.NODE_LOG_LEVEL === 'debug',
debug: true,
connection,
pool: { min: 0, max: 10 },
};

const dataRawCaptureFeature = [
{
id: uuid.v4(),
lat: '41.50414585511928',
lon: '-75.66275380279951',
location: '0101000020E6100000B514ED8E6AEA52C05D13F4D987C04440',
field_user_id: 5127,
field_username: 'test',
created_at: new Date().toUTCString(),
updated_at: new Date().toUTCString(),
},
{
id: uuid.v4(),
lat: '57.57641356164619',
lon: '-113.11416324692146',
location: '0101000020E6100000B4FB5C734E475CC0E21E6AEBC7C94C40',
field_user_id: 5127,
field_username: 'test',
created_at: new Date().toUTCString(),
updated_at: new Date().toUTCString(),
},
];

async function seed() {
knexConfig.searchPath = [process.env.DATABASE_SCHEMA, 'webmap'];
const serverCon = knex(knexConfig);
const response = await serverCon.transaction(async (trx) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be good to rename the existing raw_capture_feature table to another name (say temp_raw_capture_feature), then fill a newly created table with your seed data and name it to raw_capture_feature for your test. The test can proceed to completion, then you can delete the created table and rename temp_raw_capture_feature back to raw_capture_feature. This way will avoid clearing actual test data once ETL scripts begin to populate raw_capture_feature, and you won't have to rewrite leaderboard tests to account for the the new data in the table. You can use this knex documentation.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, yes, I know this is annoying, but, just don't touch the tables in public , you all can freely delete data under webmap schema, that's totally fine, because we didn't use that table for web map presentation.

And the next step would be use dedicated database for test, so we can do anything safely, there already are good practice in other our microservice for this, so we can learn what they are doing

Copy link
Contributor

Choose a reason for hiding this comment

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

For me, I run a docker image of postgresql, and run db-migrate to set up the database, then I can run the test fastly, for other API service, like: earnings-api

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not deleting any data from the public schema. The data addition and deletion is all in the webmap schema. So I don't see the need to create new tables because the intention is to finally add a single set of data that would be used to do all the tests on the webmap schema.

return await trx.insert(dataRawCaptureFeature).into('raw_capture_feature');
});
serverCon.destroy();
return response;
}

async function clear() {
knexConfig.searchPath = [process.env.DATABASE_SCHEMA, 'webmap'];
const serverCon = knex(knexConfig);
const response = await serverCon('raw_capture_feature').del();
serverCon.destroy();
return response;
}

export default { clear, seed };
23 changes: 23 additions & 0 deletions server/infra/database/CountryRepositoryV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,27 @@ export default class CountryRepositoryV2 extends BaseRepository<Country> {
}
return object.rows;
}


async getLeaderBoard(top = 10) {
const sql = `
select r.*, public.region.name, ST_AsGeoJSON(public.region.centroid) as centroid from (
select count(public.region.id) as planted, public.region.id
from webmap.raw_capture_feature
LEFT JOIN public.region
on ST_WITHIN(webmap.raw_capture_feature.location, public.region.geom)
left join public.region_type
on public.region.type_id = public.region_type.id
where
public.region_type.type = 'country'
group by public.region.id
order by count(public.region.id) desc
limit ${top}
) r left join public.region
on r.id = public.region.id
;
`;
const object = await this.session.getDB().raw(sql);
return object.rows;
}
}
3 changes: 3 additions & 0 deletions server/models/CountryV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ export default {
getCountries,
getById: delegateRepository<CountryRepositoryV2, Country>('getById'),
getByFilter: delegateRepository<CountryRepositoryV2, Country>('getByFilter'),
getLeaderBoard: delegateRepository<CountryRepositoryV2, Country>(
'getLeaderBoard',
),
};
13 changes: 13 additions & 0 deletions server/routers/countriesRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ import CountryModelV2 from '../models/CountryV2';

const router = express.Router();

router.get(
'/v2/leaderboard',
handlerWrapper(async (req, res) => {
const repo = new CountryRepositoryV2(new Session());
const exe = CountryModelV2.getLeaderBoard(repo);
const result = await exe(req.params.id);
res.send({
countries: result,
});
res.end();
}),
);

router.get(
'/v2/:id',
handlerWrapper(async (req, res) => {
Expand Down