Skip to content

Commit

Permalink
fix: linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Kpoke committed Oct 13, 2021
1 parent c3fb5f4 commit 3b11f63
Show file tree
Hide file tree
Showing 14 changed files with 244 additions and 240 deletions.
2 changes: 1 addition & 1 deletion seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ async function seed() {
uuid: token.uuid,
});

await knex('token').insert(tokenB);
// await knex('token').insert(tokenB);
}

async function clear() {
Expand Down
4 changes: 3 additions & 1 deletion seed.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ describe('Seed data into DB', () => {

before(async () => {
expect(seed.token).to.have.property('id');
r = await pool.query(`select * from token where id = '${seed.token.id}'`);
const r = await pool.query(
`select * from token where id = '${seed.token.id}'`,
);
expect(r).to.have.property('rows').to.have.lengthOf(1);
token = r.rows[0];
});
Expand Down
6 changes: 2 additions & 4 deletions server/handlers/captureHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ const { dispatch } = require('../models/DomainEvent');
const Session = require('../infra/database/Session');
const { publishMessage } = require('../infra/messaging/RabbitMQMessaging');

const {
CaptureRepository,
EventRepository,
} = require('../infra/database/PgRepositories');
const CaptureRepository = require('../infra/database/CaptureRepository');
const EventRepository = require('../infra/database/EventRepository');

const captureHandlerGet = async function (req, res) {
console.log('CAPTURE ROUTER get', req.query);
Expand Down
1 change: 1 addition & 0 deletions server/handlers/mocks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// MOCK QUERIES
const { v4: uuidv4 } = require('uuid');

const randIndex = (arr) => {
return Math.round(Math.random() * arr.length);
Expand Down
8 changes: 3 additions & 5 deletions server/handlers/treeHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ const { dispatch } = require('../models/DomainEvent');
const Session = require('../infra/database/Session');
const { publishMessage } = require('../infra/messaging/RabbitMQMessaging');

const {
TreeRepository,
CaptureRepository,
EventRepository,
} = require('../infra/database/PgRepositories');
const EventRepository = require('../infra/database/EventRepository');
const TreeRepository = require('../infra/database/TreeRepository');

const treeHandlerGet = async function (req, res) {
const session = new Session(false);
// todo
const result = {};
res.send(result);
res.end();
};
Expand Down
48 changes: 48 additions & 0 deletions server/infra/database/CaptureRepository.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const BaseRepository = require('./BaseRepository');

class CaptureRepository extends BaseRepository {
constructor(session) {
super('capture', session);
this._tableName = 'capture';
this._session = session;
}

async getByFilter(filterCriteria, options) {
console.log('PG REPOSITORY DB getByFilter', filterCriteria, options);
const query = Object.keys(filterCriteria).length
? filterCriteria
: `id` > 10;
return await this._session
.getDB()
.where(filterCriteria)
.select(
'id',
'reference_id',
'image_url',
'lat',
'lon',
'gps_accuracy',
'planter_id',
'planter_photo_url',
'planter_username',
'device_identifier',
'note',
'morphology',
'age',
'attributes',
'status',
'created_at',
'updated_at',
)
.from('treetracker.capture')
.orderBy('created_at', 'desc')
.limit(options.limit)
.offset(options.offset);
}

async add(capture) {
return await super.create(capture);
}
}

module.exports = CaptureRepository;
15 changes: 15 additions & 0 deletions server/infra/database/EventRepository.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const BaseRepository = require('./BaseRepository');

class EventRepository extends BaseRepository {
constructor(session) {
super('domain_event', session);
this._tableName = 'domain_event';
this._session = session;
}

async add(domainEvent) {
return await super.create(domainEvent);
}
}

module.exports = EventRepository;
98 changes: 0 additions & 98 deletions server/infra/database/PgRepositories.js

This file was deleted.

7 changes: 3 additions & 4 deletions server/infra/database/Session.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ class Session {
console.log('SESSION getDB');
if (this.thx) {
return this.thx;
}
return knex;

}
return knex;
}

isTransactionInProgress() {
return this.thx != undefined;
return this.thx !== undefined;
}

async beginTransaction() {
Expand Down
37 changes: 37 additions & 0 deletions server/infra/database/TreeRepository.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const BaseRepository = require('./BaseRepository');

class TreeRepository extends BaseRepository {
constructor(session) {
super('tree', session);
this._tableName = 'tree';
this._session = session;
}

async add(tree) {
const wellKnownText = `POINT(${tree.lon} ${tree.lat})`;
const result = await this._session.getDB().raw(
`insert into treetracker.tree (
id, lat, lon, location, latest_capture_id, image_url, species_id, age, morphology,
status, created_at, updated_at)
values(?, ?, ?, ST_PointFromText(?, 4326), ?, ?, ?, ?, ?, ?, ?, ?)
returning id`,
[
tree.id,
tree.lat,
tree.lon,
wellKnownText,
tree.latest_capture_id,
tree.image_url,
tree.species_id,
tree.age,
tree.morphology,
tree.status,
tree.created_at,
tree.updated_at,
],
);
return result.rows[0];
}
}

module.exports = TreeRepository;
4 changes: 3 additions & 1 deletion server/infra/messaging/RabbitMQMessaging.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Broker = require('rascal').BrokerAsPromised;
const {config} = require('./config');
const { config } = require('./config');

const publishMessage = async (publicationName, payload, resultHandler) => {
const broker = await Broker.create(config);
Expand All @@ -20,6 +20,8 @@ const publishMessage = async (publicationName, payload, resultHandler) => {

// not used currently, but saving for future use
const subscribe = async (subscriptionName, eventHandler) => {
// Not Sure where this is to come from
const queueName = '';
const broker = await Broker.create(config);
try {
const subscription = await broker.subscribe(subscriptionName);
Expand Down
92 changes: 50 additions & 42 deletions server/models/tree.spec.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,58 @@
const sinon = require('sinon')
const chai = require("chai");
const sinon = require('sinon');
const chai = require('chai');
const assertArrays = require('chai-arrays');
const sinonChai = require("sinon-chai");
const sinonChai = require('sinon-chai');

chai.use(sinonChai);
chai.use(assertArrays);
const {expect} = chai;
const { expect } = chai;

const { treeFromRequest, createTree } = require('./tree.js');
const { TreeRepository } = require('../infra/database/PgRepositories.js');

describe("executing treeFromRequest function", () => {
const tree = treeFromRequest({
capture_id: 'fde165f6-5ebe-4ef6-aa46-fc0e391c1b78',
image_url: 'http://test',
lat: 10.15,
lon: 15.03
});

it("should return an immutable tree object", () => {
tree.arandomNewAttribute = 1;
expect(tree.arandomNewAttribute).to.equal(undefined);
});

it("should return an object with the required parameters", function() {
expect(Object.keys(tree)).to.equalTo(
['id', 'latest_capture_id', 'image_url', 'lat', 'lon', 'species_id', 'morphology', 'age', 'status']
);
});
const TreeRepository = require('../infra/database/TreeRepository');

describe('executing treeFromRequest function', () => {
const tree = treeFromRequest({
capture_id: 'fde165f6-5ebe-4ef6-aa46-fc0e391c1b78',
image_url: 'http://test',
lat: 10.15,
lon: 15.03,
});

it('should return an immutable tree object', () => {
tree.arandomNewAttribute = 1;
expect(tree.arandomNewAttribute).to.equal(undefined);
});

it('should return an object with the required parameters', function () {
expect(Object.keys(tree)).to.equalTo([
'id',
'latest_capture_id',
'image_url',
'lat',
'lon',
'species_id',
'morphology',
'age',
'status',
]);
});
});

describe("executing createTree function", () => {
const tree = treeFromRequest({
capture_id: 'fde165f6-5ebe-4ef6-aa46-fc0e391c1b78',
image_url: 'http://test',
lat: 10.15,
lon: 15.03
});

const repository = new TreeRepository();
const stub = sinon.stub(repository, "add");
const executeCreateTree = createTree(repository);

it("should add tree to the repository", async () => {
await executeCreateTree(tree);
expect(stub).calledWith(tree);
stub.restore();
})
})
describe('executing createTree function', () => {
const tree = treeFromRequest({
capture_id: 'fde165f6-5ebe-4ef6-aa46-fc0e391c1b78',
image_url: 'http://test',
lat: 10.15,
lon: 15.03,
});

const repository = new TreeRepository();
const stub = sinon.stub(repository, 'add');
const executeCreateTree = createTree(repository);

it('should add tree to the repository', async () => {
await executeCreateTree(tree);
expect(stub).calledWith(tree);
stub.restore();
});
});
Loading

0 comments on commit 3b11f63

Please sign in to comment.