Skip to content

Commit

Permalink
fix: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kpoke committed Aug 26, 2022
1 parent 69870ec commit 1e6f026
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 23 deletions.
35 changes: 19 additions & 16 deletions api-tests/integration/capture/capture.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ describe('/captures', () => {
await knex('domain_event').insert({ ...domain_event2 });
});

after(async () => {
await knex('capture').del();
await knex('domain_event').del();
});

it('should create a capture', async () => {
const res = await request(app)
.post(`/captures`)
Expand Down Expand Up @@ -98,7 +103,7 @@ describe('/captures', () => {
it('should resend capture created event if it wasnt successful last time and capture already exists', async () => {
await request(app)
.post(`/captures`)
.send({ ...capture2, id: capture1.id })
.send({ ...capture2, reference_id: capture1.reference_id })
.set('Accept', 'application/json')
.expect(200);

Expand All @@ -109,30 +114,31 @@ describe('/captures', () => {
.where({ status: 'sent' });
expect(+numOfEmittedEvents[0].count).to.eql(2);
});

after(async () => {
await knex('capture').del();
await knex('domain_event').del();
});
});

describe('PATCH', () => {
const captureId = uuid.v4();
before(async () => {
await addCapture({
...capture2,
id: captureId,
estimated_geometric_location: 'POINT(50 50)',
updated_at: '2022-01-01T11:11:11.000Z',
attributes: { entries: attributes.attributes },
});
});

after(async () => {
await knex('capture').del();
});

it('should update a capture', async () => {
const updates = {
tree_id: tree1.id,
};

const res = await request(app)
.patch(`/captures/${capture2.id}`)
.patch(`/captures/${captureId}`)
.send(updates)
.set('Accept', 'application/json')
.expect(200);
Expand All @@ -146,13 +152,10 @@ describe('/captures', () => {
tree_id: updates.tree_id,
});
});

after(async () => {
await knex('capture').del();
});
});

describe('GET', () => {
const captureId = uuid.v4();
before(async () => {
await addCapture({
...capture1,
Expand All @@ -162,6 +165,7 @@ describe('/captures', () => {
});
await addCapture({
...capture2,
id: captureId,
estimated_geometric_location: 'POINT(50 50)',
updated_at: '2022-02-01 11:11:11',
attributes: { entries: attributes.attributes },
Expand Down Expand Up @@ -192,14 +196,12 @@ describe('/captures', () => {
.expect(200);
expect(result.body.captures.length).to.eql(1);
expect(result.body.query.count).to.eql(1);
expect(result.body.captures[0].id).to.eql(
'd2c69205-b13f-4ab6-bb5e-33dc504fa0c2',
);
expect(result.body.captures[0].id).to.eql(captureId);
});

it('should delete a capture', async () => {
await request(app)
.patch(`/captures/${capture2.id}`)
.patch(`/captures/${captureId}`)
.send({ status: 'deleted' })
.set('Accept', 'application/json')
.expect(200);
Expand All @@ -214,7 +216,7 @@ describe('/captures', () => {
});

describe('/captures/capture_id/tags', () => {
const captureId = capture2.id;
const captureId = uuid.v4();

before(async () => {
await addCapture({
Expand All @@ -224,6 +226,7 @@ describe('/captures', () => {
});
await addCapture({
...capture2,
id: captureId,
estimated_geometric_location: 'POINT(50 50)',
updated_at: '2022-02-01 11:11:11',
});
Expand Down
2 changes: 1 addition & 1 deletion api-tests/mock/capture1.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
"image_url": "http://xxx",
"planting_organization_id": "3876b7df-0002-4bc7-adfa-097db2080311",
"captured_at": "2022-01-01 11:11:11"
}
}
1 change: 0 additions & 1 deletion api-tests/mock/capture2.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"id": "d2c69205-b13f-4ab6-bb5e-33dc504fa0c2",
"reference_id": 215606,
"session_id": "f97a0338-1d6a-4f19-a067-a7f0b573e3cd",
"image_url": "https://treetracker-dev-images.s3.eu-central-1.amazonaws.com/2020.05.15.16.18.46_37.42138089_-121.87534965_92b10ff4-4d37-43e9-8dd7-ed79e23d1102_IMG_20200511_150539_503676129060555307.jpg",
Expand Down
3 changes: 2 additions & 1 deletion api-tests/mock/domain_event2.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"id": "7cffd8c0-78fb-4cc4-ba9c-1a2856d459d7",
"payload": {
"id": "c02a5ae6-3727-11ec-8d3d-0242ac130003"
"id": "c02a5ae6-3727-11ec-8d3d-0242ac130003",
"reference_id": 1
},
"status": "raised",
"created_at": "2021-05-04 11:24:43",
Expand Down
6 changes: 4 additions & 2 deletions server/models/Capture.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class Capture {
}

static CaptureCreated({
id,
reference_id,
lat,
lon,
Expand All @@ -61,6 +62,7 @@ class Capture {
captured_at,
}) {
return Object.freeze({
id,
reference_id,
approved: true,
type: 'CaptureCreated',
Expand Down Expand Up @@ -184,8 +186,8 @@ class Capture {
newCapture.reference_id,
);
if (existingCapture?.id) {
const domainEvent = await eventRepo.getDomainEvent(
newCapture.reference_id,
const domainEvent = await eventRepo.getCaptureDomainEvent(
existingCapture.reference_id,
);
if (domainEvent.status !== 'sent') {
return {
Expand Down
2 changes: 1 addition & 1 deletion server/models/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class Tree {
});
const [existingTree] = existingTrees;
if (existingTree) {
const domainEvent = await eventRepo.getDomainEvent(newTree.id);
const domainEvent = await eventRepo.getTreeDomainEvent(newTree.id);
if (domainEvent.status !== 'sent') {
return { domainEvent, tree: existingTree, eventRepo, status: 200 };
}
Expand Down
12 changes: 11 additions & 1 deletion server/repositories/EventRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class EventRepository extends BaseRepository {
return super.create(domainEvent);
}

async getDomainEvent(payloadReferenceId) {
async getCaptureDomainEvent(payloadReferenceId) {
const data = await this._session
.getDB()
.raw(`select * from domain_event where payload ->> 'reference_id' = ?;`, [
Expand All @@ -20,6 +20,16 @@ class EventRepository extends BaseRepository {

return data.rows[0];
}

async getTreeDomainEvent(payloadId) {
const data = await this._session
.getDB()
.raw(`select * from domain_event where payload ->> 'id' = ?;`, [
payloadId,
]);

return data.rows[0];
}
}

module.exports = EventRepository;

0 comments on commit 1e6f026

Please sign in to comment.