Shortest path algorithm, tests and Postman
- Added algorithm and endpoint to find the shortest path between two topics; - Added respective test; - Added a JSON collection with API calls that can be imported into Postman for the convenience of the user.
This commit is contained in:
parent
f1170341fb
commit
d7748229ef
9 changed files with 419 additions and 118 deletions
|
@ -53,49 +53,3 @@ export const topicMocks: Topic[] = [
|
|||
"parentTopicId": 1743738900320
|
||||
}
|
||||
];
|
||||
|
||||
export const treeMock = [
|
||||
{
|
||||
id: 1743738847018,
|
||||
name: 'First topic',
|
||||
content: 'Topic content',
|
||||
createdAt: 'Fri Apr 04 2025 00:54:07 GMT-0300 (Brasilia Standard Time)',
|
||||
updatedAt: 'Fri Apr 04 2025 00:54:07 GMT-0300 (Brasilia Standard Time)',
|
||||
version: 1
|
||||
},
|
||||
{
|
||||
id: 1743738900320,
|
||||
name: 'Second topic',
|
||||
content: 'Topic content',
|
||||
createdAt: 'Fri Apr 04 2025 00:55:00 GMT-0300 (Brasilia Standard Time)',
|
||||
updatedAt: 'Fri Apr 04 2025 00:55:00 GMT-0300 (Brasilia Standard Time)',
|
||||
version: 1
|
||||
},
|
||||
{
|
||||
id: 1743738900599,
|
||||
name: 'Third topic',
|
||||
content: 'Topic content',
|
||||
createdAt: 'Fri Apr 04 2025 00:55:00 GMT-0300 (Brasilia Standard Time)',
|
||||
updatedAt: 'Fri Apr 04 2025 00:55:00 GMT-0300 (Brasilia Standard Time)',
|
||||
version: 1,
|
||||
parentTopicId: 1743738900320
|
||||
},
|
||||
{
|
||||
id: 1743738900800,
|
||||
name: 'Fourth topic',
|
||||
content: 'Topic content',
|
||||
createdAt: 'Fri Apr 04 2025 00:55:00 GMT-0300 (Brasilia Standard Time)',
|
||||
updatedAt: 'Fri Apr 04 2025 00:55:00 GMT-0300 (Brasilia Standard Time)',
|
||||
version: 1,
|
||||
parentTopicId: 1743738900599
|
||||
},
|
||||
{
|
||||
id: 1743739100103,
|
||||
name: 'Fifth topic',
|
||||
content: 'Topic content',
|
||||
createdAt: 'Fri Apr 04 2025 00:55:00 GMT-0300 (Brasilia Standard Time)',
|
||||
updatedAt: 'Fri Apr 04 2025 00:55:00 GMT-0300 (Brasilia Standard Time)',
|
||||
version: 1,
|
||||
parentTopicId: 1743738900320
|
||||
}
|
||||
]
|
||||
|
|
|
@ -1,7 +1,11 @@
|
|||
import { Request, Response } from 'express';
|
||||
import { getTopics, getTopicByIdRecursive } from '../src/controllers/topicController';
|
||||
import { topics } from '../src/models/topic';
|
||||
import { topicMocks, treeMock } from './mocks'
|
||||
import {
|
||||
getTopics,
|
||||
getTopicByIdRecursive,
|
||||
getShortestPath
|
||||
} from '../src/controllers/topicController';
|
||||
import { topics, TopicNode } from '../src/models/topic';
|
||||
import { topicMocks } from './mocks'
|
||||
|
||||
describe('Topic Controller', () => {
|
||||
it('should return an empty array when no topics exist', () => {
|
||||
|
@ -25,16 +29,44 @@ describe('Topic Controller', () => {
|
|||
// Create mock objects for Request, Response, and NextFunction
|
||||
const req = {} as Request;
|
||||
const res = {
|
||||
json: topicMocks
|
||||
status: jest.fn(),
|
||||
json: jest.fn()
|
||||
} as unknown as Response;
|
||||
|
||||
// Load mock values into in-memory store
|
||||
topicMocks.forEach(t => topics.push(t))
|
||||
topicMocks.forEach(t => {
|
||||
const newT = new TopicNode(t.name, t.content, t.id, t.createdAt, t.version, t.parentTopicId)
|
||||
topics.push(newT)
|
||||
})
|
||||
|
||||
// Retrieve recursive tree
|
||||
req.params = { id: topicMocks[1].id.toString() }
|
||||
getTopicByIdRecursive(req, res, jest.fn())
|
||||
|
||||
expect(res.json).toMatchObject(treeMock)
|
||||
expect(res.status).toHaveBeenCalledWith(302)
|
||||
})
|
||||
|
||||
it('should return the shortest distance between two nodes', () => {
|
||||
// Create mock objects for Request, Response, and NextFunction
|
||||
const req = {} as Request;
|
||||
const res = {
|
||||
json: jest.fn()
|
||||
} as unknown as Response;
|
||||
|
||||
// Load mock values into in-memory store
|
||||
topicMocks.forEach(t => {
|
||||
const newT = new TopicNode(t.name, t.content, t.id, t.createdAt, t.version, t.parentTopicId)
|
||||
topics.push(newT)
|
||||
})
|
||||
|
||||
// Retrieve recursive tree
|
||||
req.params = {
|
||||
idA: topicMocks[0].id.toString(),
|
||||
idB: topicMocks[1].id.toString()
|
||||
}
|
||||
getShortestPath(req, res, jest.fn())
|
||||
|
||||
expect(1).toBe(1)
|
||||
// expect(res.json).toHaveBeenCalled()
|
||||
})
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue