-
Notifications
You must be signed in to change notification settings - Fork 59
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
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: '40.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) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be good to rename the existing There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not deleting any data from the |
||
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 }; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.