Skip to content

Commit

Permalink
feat: tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
Kpoke committed Oct 12, 2021
1 parent 6b440e9 commit 8215178
Show file tree
Hide file tree
Showing 30 changed files with 827 additions and 30 deletions.
533 changes: 533 additions & 0 deletions api-tests/earnings-api.spec.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions api-tests/earningsFailedTestInvalidHeader.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
earnings_id,worker_id,phone,currency,amount,payment_confirmation_id,payment_id
f3056760-4c36-4cf7-b625-ed656d314794,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,2,jomzy jor jor,2021-10-10T00:00:00.000Z
a69e5cec-945f-4693-8a94-01623ee44187,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,3,jomzy jor jor,2021-10-10T00:00:00.000Z
61714d24-6131-4296-ae36-30a7199cc645,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,4,jomzy jor jor,2021-10-10T00:00:00.000Z
4 changes: 4 additions & 0 deletions api-tests/earningsFailedTestInvalidHeader2.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
earnings_id,worker_id,phone,currency,amount,payment_id,payment_system
f3056760-4c36-4cf7-b625-ed656d314794,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,2,jomzy jor jor,2021-10-10T00:00:00.000Z
a69e5cec-945f-4693-8a94-01623ee44187,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,3,jomzy jor jor,2021-10-10T00:00:00.000Z
61714d24-6131-4296-ae36-30a7199cc645,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,4,jomzy jor jor,2021-10-10T00:00:00.000Z
4 changes: 4 additions & 0 deletions api-tests/earningsFailedTestInvalidHeader3.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
earnings_id,phone,currency,amount,payment_confirmation_id,payment_system
f3056760-4c36-4cf7-b625-ed656d314794,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,2,jomzy jor jor,2021-10-10T00:00:00.000Z
a69e5cec-945f-4693-8a94-01623ee44187,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,3,jomzy jor jor,2021-10-10T00:00:00.000Z
61714d24-6131-4296-ae36-30a7199cc645,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,4,jomzy jor jor,2021-10-10T00:00:00.000Z
4 changes: 4 additions & 0 deletions api-tests/earningsFailedTestInvalidHeader4.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
earnings_id,worker_id,phone,currency,payment_confirmation_id,payment_system
f3056760-4c36-4cf7-b625-ed656d314794,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,2,jomzy jor jor,2021-10-10T00:00:00.000Z
a69e5cec-945f-4693-8a94-01623ee44187,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,3,jomzy jor jor,2021-10-10T00:00:00.000Z
61714d24-6131-4296-ae36-30a7199cc645,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,4,jomzy jor jor,2021-10-10T00:00:00.000Z
4 changes: 4 additions & 0 deletions api-tests/earningsFailedTestInvalidHeader5.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
earnings_id,worker_id,phone,amount,payment_confirmation_id,payment_system
f3056760-4c36-4cf7-b625-ed656d314794,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,2,jomzy jor jor,2021-10-10T00:00:00.000Z
a69e5cec-945f-4693-8a94-01623ee44187,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,3,jomzy jor jor,2021-10-10T00:00:00.000Z
61714d24-6131-4296-ae36-30a7199cc645,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,4,jomzy jor jor,2021-10-10T00:00:00.000Z
4 changes: 4 additions & 0 deletions api-tests/earningsFailedTestInvalidHeader6.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
worker_id,phone,currency,amount,payment_confirmation_id,payment_system
f3056760-4c36-4cf7-b625-ed656d314794,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,2,jomzy jor jor,2021-10-10T00:00:00.000Z
a69e5cec-945f-4693-8a94-01623ee44187,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,3,jomzy jor jor,2021-10-10T00:00:00.000Z
61714d24-6131-4296-ae36-30a7199cc645,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,4,jomzy jor jor,2021-10-10T00:00:00.000Z
4 changes: 4 additions & 0 deletions api-tests/earningsFailedTestInvalidRow.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
earnings_id,worker_id,phone,currency,amount,payment_confirmation_id,payment_system,paid_at
f3056760-4c36-4cf7-b625-ed6,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,2,jomzy jor jor,2021-10-10T00:00:00.000Z
a69e5cec-945f-4693-8a94-01623ee44187,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,3,jomzy jor jor,2021-10-10T00:00:00.000Z
61714d24-6131-4296-ae36-30a7199cc645,Not sure for now,USD,700,4,jomzy jor jor,2021-10-10T00:00:00.000Z
6 changes: 6 additions & 0 deletions api-tests/earningsFailedTestRowWithNotCalculatedStatus.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
earnings_id,worker_id,phone,currency,amount,payment_confirmation_id,payment_system,paid_at
f3056760-4c36-4cf7-b625-ed656d314794,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,2,jomzy jor jor,2021-10-10T00:00:00.000Z
a69e5cec-945f-4693-8a94-01623ee44187,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,3,jomzy jor jor,2021-10-10T00:00:00.000Z
61714d24-6131-4296-ae36-30a7199cc645,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,4,jomzy jor jor,2021-10-10T00:00:00.000Z
9c10e443-4e08-40d4-9b73-5a931886f896,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,4,jomzy jor jor,2021-10-10T00:00:00.000Z
3ab96dfd-274a-4097-8e7d-942e58203784,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,4,jomzy jor jor,2021-10-10T00:00:00.000Z
4 changes: 4 additions & 0 deletions api-tests/earningsSuccessfulTest.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
earnings_id,worker_id,phone,currency,amount,payment_confirmation_id,payment_system,paid_at
f3056760-4c36-4cf7-b625-ed656d314794,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,2,jomzy jor jor,2021-10-10T00:00:00.000Z
a69e5cec-945f-4693-8a94-01623ee44187,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,3,jomzy jor jor,2021-10-10T00:00:00.000Z
61714d24-6131-4296-ae36-30a7199cc645,71be6266-81fe-476f-a563-9bc1c61fc037,Not sure for now,USD,700,4,jomzy jor jor,2021-10-10T00:00:00.000Z
17 changes: 17 additions & 0 deletions api-tests/generic-class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class GenericObject {
constructor(payload) {
this._object = payload;
}

delete_property(property) {
delete this._object[property];
}

change_property(property, value) {
this._object[property] = value;
}
}

module.exports = {
GenericObject,
};
89 changes: 89 additions & 0 deletions api-tests/seed-data-creation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
const { v4: uuid } = require('uuid');
const sinon = require('sinon');
const knex = require('../server/database/knex');
const s3 = require('../server/services/s3');

const workerId = '71be6266-81fe-476f-a563-9bc1c61fc037';
const earningsPaymentObject = {
worker_id: workerId,
amount: 700,
payment_confirmation_id: uuid(),
payment_system: 'cash',
currency: 'USD',
status: 'calculated',
paid_at: new Date().toISOString(),
};
const earningsOne = {
...earningsPaymentObject,
payment_system: 'bike',
id: '915b1c8c-e1d0-44f8-a6a7-7026e2a8f04b',
};
const earningsTwo = {
...earningsPaymentObject,
id: 'f3056760-4c36-4cf7-b625-ed656d314794',
};
const earningsThree = {
...earningsPaymentObject,
id: 'a69e5cec-945f-4693-8a94-01623ee44187',
};
const earningsFour = {
...earningsPaymentObject,
id: '61714d24-6131-4296-ae36-30a7199cc645',
};
const earningsWithPaidStatus = {
...earningsPaymentObject,
id: '9c10e443-4e08-40d4-9b73-5a931886f896',
status: 'paid',
};
const earningsWithCancelledStatus = {
...earningsPaymentObject,
id: '3ab96dfd-274a-4097-8e7d-942e58203784',
status: 'cancelled',
};
let stub;

before(async () => {
stub = sinon.stub(s3, 'putObject').returns({ promise: () => 'a' });
// prettier-ignore
await knex.raw(`
ALTER TABLE earnings ALTER COLUMN batch_id DROP NOT NULL;
INSERT INTO stakeholder.stakeholder(id)
VALUES ('${workerId}');
INSERT INTO public.earnings(
id, worker_id, funder_id, amount, currency, calculated_at, consolidation_id, consolidation_period_start, consolidation_period_end, payment_confirmed_by, payment_confirmation_method, status, active)
VALUES
('${earningsOne.id}','${workerId}', '${uuid()}', '${earningsPaymentObject.amount}', '${earningsPaymentObject.currency}', now(), '${uuid()}', now(), now(), '${uuid()}','single', '${earningsOne.status}', true),
('${earningsTwo.id}','${workerId}', '${uuid()}', '${earningsPaymentObject.amount}', '${earningsPaymentObject.currency}', now(), '${uuid()}', now(), now(), '${uuid()}','single', '${earningsTwo.status}', true),
('${earningsThree.id}','${workerId}', '${uuid()}', '${earningsPaymentObject.amount}', '${earningsPaymentObject.currency}', now(), '${uuid()}', now(), now(), '${uuid()}','single', '${earningsThree.status}', true),
('${earningsFour.id}','${workerId}', '${uuid()}', '${earningsPaymentObject.amount}', '${earningsPaymentObject.currency}', now(), '${uuid()}', now(), now(), '${uuid()}','batch', '${earningsFour.status}', true),
('${earningsWithCancelledStatus.id}','${workerId}', '${uuid()}', '${earningsPaymentObject.amount}', '${earningsPaymentObject.currency}', now(), '${uuid()}', now(), now(), '${uuid()}','single', '${earningsWithCancelledStatus.status}', true),
('${earningsWithPaidStatus.id}','${workerId}', '${uuid()}', '${earningsPaymentObject.amount}', '${earningsPaymentObject.currency}', now(), '${uuid()}', now(), now(), '${uuid()}','single', '${earningsWithPaidStatus.status}', true);
`);
});

after(async () => {
stub.restore();
await knex.raw(`
DELETE FROM public.earnings
WHERE worker_id = '${workerId}';
DELETE FROM stakeholder.stakeholder
WHERE id = '${workerId}';
ALTER TABLE earnings ALTER COLUMN batch_id SET NOT NULL;
`);
});

// should not be in the PATCH request body
const { status, ...earnings } = earningsOne;
const { status: status2, ...earningsPaid } = earningsWithPaidStatus;
const { status: status3, ...earningsCancelled } = earningsWithCancelledStatus;

module.exports = {
earnings,
earningsPaid,
earningsCancelled,
};
54 changes: 54 additions & 0 deletions database/migrations/20211010192212-createBatch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@


let dbm;
let type;
let seed;
const fs = require('fs');
const path = require('path');

let Promise;

/**
* We receive the dbmigrate dependency from dbmigrate initially.
* This enables us to not have to rely on NODE_PATH.
*/
exports.setup = function(options, seedLink) {
dbm = options.dbmigrate;
type = dbm.dataType;
seed = seedLink;
Promise = options.Promise;
};

exports.up = function(db) {
const filePath = path.join(__dirname, 'sqls', '20211010192212-createBatch-up.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
console.log(`received data: ${ data}`);

resolve(data);
});
})
.then(function(data) {
return db.runSql(data);
});
};

exports.down = function(db) {
const filePath = path.join(__dirname, 'sqls', '20211010192212-createBatch-down.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
console.log(`received data: ${ data}`);

resolve(data);
});
})
.then(function(data) {
return db.runSql(data);
});
};

exports._meta = {
"version": 1
};
54 changes: 54 additions & 0 deletions database/migrations/20211010192838-createStakeholder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@


let dbm;
let type;
let seed;
const fs = require('fs');
const path = require('path');

let Promise;

/**
* We receive the dbmigrate dependency from dbmigrate initially.
* This enables us to not have to rely on NODE_PATH.
*/
exports.setup = function(options, seedLink) {
dbm = options.dbmigrate;
type = dbm.dataType;
seed = seedLink;
Promise = options.Promise;
};

exports.up = function(db) {
const filePath = path.join(__dirname, 'sqls', '20211010192838-createStakeholder-up.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
console.log(`received data: ${ data}`);

resolve(data);
});
})
.then(function(data) {
return db.runSql(data);
});
};

exports.down = function(db) {
const filePath = path.join(__dirname, 'sqls', '20211010192838-createStakeholder-down.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
console.log(`received data: ${ data}`);

resolve(data);
});
})
.then(function(data) {
return db.runSql(data);
});
};

exports._meta = {
"version": 1
};
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ exports.setup = function(options, seedLink) {
};

exports.up = function(db) {
const filePath = path.join(__dirname, 'sqls', '20211005092140-createEarnings-up.sql');
const filePath = path.join(__dirname, 'sqls', '20211010193244-createEarnings-up.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
Expand All @@ -35,7 +35,7 @@ exports.up = function(db) {
};

exports.down = function(db) {
const filePath = path.join(__dirname, 'sqls', '20211005092140-createEarnings-down.sql');
const filePath = path.join(__dirname, 'sqls', '20211010193244-createEarnings-down.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ exports.setup = function(options, seedLink) {
};

exports.up = function(db) {
const filePath = path.join(__dirname, 'sqls', '20211008100925-alterCapture-up.sql');
const filePath = path.join(__dirname, 'sqls', '20211010193339-alterCapture-up.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
Expand All @@ -35,7 +35,7 @@ exports.up = function(db) {
};

exports.down = function(db) {
const filePath = path.join(__dirname, 'sqls', '20211008100925-alterCapture-down.sql');
const filePath = path.join(__dirname, 'sqls', '20211010193339-alterCapture-down.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
Expand Down

This file was deleted.

Empty file.
6 changes: 6 additions & 0 deletions database/migrations/sqls/20211010192212-createBatch-up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE IF NOT EXISTS batch (
id uuid NOT NULL PRIMARY KEY,
url varchar NOT NULL,
status varchar NOT NULL,
active boolean NOT NULL
)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* Replace with your SQL commands */
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE SCHEMA IF NOT EXISTS stakeholder;
CREATE TABLE IF NOT EXISTS stakeholder.stakeholder(
id uuid NOT NULL PRIMARY KEY
)
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* Replace with your SQL commands */
DROP TABLE earnings;
DROP TYPE earning_status_enum;
DROP TYPE confirmation_method_enum;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* Replace with your SQL commands */
CREATE TYPE earning_status_enum AS ENUM ('calculated', 'cancelled', 'paid');
CREATE TYPE confirmation_method_enum AS ENUM ('single', 'batch');
CREATE TYPE currency_enum AS ENUM ('USD');
Expand All @@ -16,7 +17,7 @@ CREATE TABLE public.earnings
payment_confirmation_id varchar,
payment_system varchar,
payment_confirmed_by uuid NOT NULL,
payment_confirmation_method uuid NOT NULL,
payment_confirmation_method confirmation_method_enum NOT NULL,
paid_at timestamptz,
status earning_status_enum NOT NULL,
active boolean NOT NULL,
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/* Replace with your SQL commands */
ALTER TABLE treetracker.capture DROP earnings_id
6 changes: 6 additions & 0 deletions database/migrations/sqls/20211010193339-alterCapture-up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* Replace with your SQL commands */
CREATE SCHEMA IF NOT EXISTS treetracker;
CREATE TABLE IF NOT EXISTS treetracker.capture (
id uuid NOT NULL PRIMARY KEY
);
ALTER TABLE treetracker.capture ADD earnings_id uuid REFERENCES public.earnings(id);
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"server-test": "DEBUG=express:* NODE_LOG_LEVEL=debug nodemon server/serverTest.js",
"server": "nodemon server/server.js",
"test-seedDB": "NODE_ENV=test mocha -r dotenv/config dotenv_config_path=.env.test --timeout 10000 --require co-mocha './**/*.spec.js'",
"test-integration-ci": "NODE_ENV=test mocha -r dotenv/config dotenv_config_path=.env.test --exit --timeout 20000 --require co-mocha './__tests__/supertest.js'",
"test-integration-ci": "mocha -r dotenv/config --dotenv_config_path=.env.test --exit --timeout 30000 --require co-mocha './api-tests'",
"test-watch": "NODE_ENV=test NODE_LOG_LEVEL=info mocha -r dotenv/config dotenv_config_path=.env.test --timeout 10000 --require co-mocha -w -b --ignore './server/repositories/**/*.spec.js' './server/setup.js' './server/**/*.spec.js' './__tests__/seed.spec.js' './__tests__/supertest.js'",
"test-watch-debug": "NODE_ENV=test NODE_LOG_LEVEL=debug mocha -r dotenv/config dotenv_config_path=.env.test --timeout 10000 --require co-mocha -w -b --ignore './server/repositories/**/*.spec.js' './server/setup.js' './server/**/*.spec.js' './__tests__/seed.spec.js' './__tests__/supertest.js'",
"prettier-fix": "prettier ./ --write",
Expand Down Expand Up @@ -68,7 +68,7 @@
"mock-knex": "^0.4.9",
"nodemon": "^2.0.4",
"prettier": "^2.1.2",
"sinon": "^9.0.3",
"sinon": "^9.2.4",
"sinon-chai": "^2.14.0",
"supertest": "^4.0.2"
},
Expand Down
Loading

0 comments on commit 8215178

Please sign in to comment.