Skip to content

Commit

Permalink
Merge pull request #7 from ezequielramos/main
Browse files Browse the repository at this point in the history
adding return of face events
  • Loading branch information
ezequielramos authored Jun 11, 2024
2 parents d18e883 + 542f805 commit 7408b42
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 27 deletions.
17 changes: 0 additions & 17 deletions package-lock.json

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

16 changes: 13 additions & 3 deletions src/controllers/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ interface EventFace {
}

let eventId = 0;
const events: { [eventId: number]: EventFace | undefined; } = {};
const events: { [eventId: string]: EventFace | undefined; } = {};

function createEvent({ created_date, camera }: { created_date?: string; camera: number; }) {
eventId++;
Expand Down Expand Up @@ -107,7 +107,7 @@ function createEvent({ created_date, camera }: { created_date?: string; camera:
acknowledged_by: 0,
acknowledged_reaction: "",
cluster_confidence: 0.0,
confidence: 0.0,
confidence: (Math.random() * 0.45) + 0.5,
external_detector: true,
looks_like_confidence: null,
matched_lists: [watchLists[0].id],
Expand Down Expand Up @@ -168,4 +168,14 @@ function getEvent(eventId: number) {
return events[eventId];
}

export { createEvent, getEvent };
function resetEvents() {
for (const eventId of Object.keys(events)) {
delete events[eventId];
}
}

function getEvents() {
return Object.values(events);
}

export { createEvent, getEvent, resetEvents, getEvents };
8 changes: 5 additions & 3 deletions src/routes/events.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Express, Request, Response } from 'express';
import multer from 'multer';
import { validAuthorization } from '../services/route_middlewares';
import { createEvent, getEvent } from '../controllers/events';
import { createEvent, getEvent, getEvents } from '../controllers/events';

const upload = multer({
storage: multer.memoryStorage()
Expand Down Expand Up @@ -111,10 +111,12 @@ function loadEventsRoutes(app: Express) {
});

app.get('/events/faces/', validAuthorization, async (req: Request, res: Response) => {
const events = getEvents();

return res.status(200).json({
"next_page": null,
"count": 0,
"results": []
"count": events.length,
"results": events
});
});
}
Expand Down
28 changes: 24 additions & 4 deletions tests/events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { agent } from 'supertest';
import { readFile } from 'fs/promises';

import { webService } from '../src/services/web-service';
import { resetEvents } from '../src/controllers/events';

const request = agent(webService.app);

describe('Cards Route Testing', async () => {
describe('Events Route Testing', async () => {

let token = "";
let detectionId = "";
Expand Down Expand Up @@ -43,23 +44,29 @@ describe('Cards Route Testing', async () => {
.set('Authorization', 'Token ' + token);

expect(res.statusCode).equals(200);
expect(res.body.objects.face).has.lengthOf(1);
expect(res.body.objects.face[0].id).to.not.be.null;
detectionId = res.body.objects.face[0].id;
});

it('test get events by detectionId', async () => {
resetEvents();
const res = await request.get(`/events/faces/`)
.query({
looks_like: "detection:" + detectionId,
})
.set('Authorization', 'Token ' + token);
expect(res.statusCode).to.be.equal(200);

expect(res.body.next_page).to.be.equal(null);
expect(res.body.count).to.be.equal(0);
});

it('test create event with timestamp', async () => {
resetEvents();
const file = await readFile(__dirname + "/assets/11296869.jpg");

const res = await request.post(`/events/faces/add/`)
let res = await request.post(`/events/faces/add/`)
.attach("fullframe", file, {
filename: 'fullframe.jpg',
contentType: 'image/jpeg'
Expand All @@ -71,7 +78,20 @@ describe('Cards Route Testing', async () => {
.set('Authorization', 'Token ' + token);

expect(res.statusCode).to.be.equal(200);
expect(res.body.id).to.not.be.null;
expect(res.body.events).has.lengthOf(1);

const createdEventId = res.body.events[0];

res = await request.get(`/events/faces/`)
.query({
looks_like: "detection:" + detectionId,
})
.set('Authorization', 'Token ' + token);
expect(res.statusCode).to.be.equal(200);

expect(res.body.next_page).to.be.equal(null);
expect(res.body.count).to.be.equal(1);
expect(res.body.results[0].id).to.be.equal(createdEventId);
});

it('test create event without timestamp', async () => {
Expand All @@ -88,7 +108,7 @@ describe('Cards Route Testing', async () => {
.set('Authorization', 'Token ' + token);

expect(res.statusCode).to.be.equal(200);
expect(res.body.id).to.not.be.null;
expect(res.body.events).has.lengthOf(1);
});

it('test invalid create event - missing picture', async () => {
Expand Down

0 comments on commit 7408b42

Please sign in to comment.