Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Files but having issues, disregard this #107

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
"test:list": "jest --listTests",
"test:specific": "jest path/to/your/tests",
"test:clearCache": "jest --clearCache",
"test:coverage": "jest --coverage",
"dev": "concurrently \"tsc --watch\" \"nodemon -q dist/src/server.js\"",
"build": "rimraf dist && npx tsoa spec-and-routes && npm run tsc",
"start": "node dist/src/server.js",
"tsoa:spec": "npx tsoa spec",
"tsc": "tsc --noEmit --emitDeclarationOnly false --experimentalDecorators",
"tsoa:routes": "npx tsoa routes"
"tsoa:routes": "npx tsoa routes",
"stryker-run": "npx stryker run",
"test:coverage-stryker": "npm run test:coverage && npm run stryker-run"
},
"dependencies": {
"axios": "^1.7.7",
Expand All @@ -40,6 +43,7 @@
"@babel/core": "^7.26.0",
"@babel/preset-env": "^7.26.0",
"@babel/preset-react": "^7.25.9",
"@stryker-mutator/jest-runner": "^8.7.1",
"@types/cookie-parser": "^1.4.7",
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
Expand Down
101 changes: 101 additions & 0 deletions backend/src/Models/Posts/__tests__/NofindUsersByPosts.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// import { findUsersByPosts } from '../post.model';
// import { UserPost } from '../../../../Types/posts';

// // Define individual mock functions for the chained methods
// const mockSelect = jest.fn().mockReturnThis();
// const mockOrderBy = jest.fn().mockReturnThis();
// const mockLimit = jest.fn().mockReturnThis();
// const mockOffset = jest.fn().mockResolvedValue([]);

// // Mock the default export of knex/db
// jest.mock('../../../db/db', () => {
// return {
// __esModule: true,
// default: jest.fn(() => ({
// select: mockSelect,
// orderBy: mockOrderBy,
// limit: mockLimit,
// offset: mockOffset,
// })),
// };
// });

// // Clear all mocks after each test to ensure a clean slate for each test case
// afterEach(() => {
// jest.clearAllMocks();
// });

// describe('findUsersByPosts', () => {
// // Test Case #1: Successfully return posts when the database query is successful
// test('should return posts when the database query is successful', async () => {
// // Explicitly type mockPosts as UserPost[]
// const mockPosts: UserPost[] = [
// { id: 1, user_id: 123, username: 'user1', post_content: 'Post content 1', created_at: new Date(), comments: ["comment1", "comment2"], reactions: 10, locationName: 'Park', latitude: 10, longitude: 20 },
// { id: 2, user_id: 456, username: 'user2', post_content: 'Post content 2', created_at: new Date(), comments: ["comment3", "comment4"], reactions: 8, locationName: 'Mall', latitude: 12, longitude: 22 }
// ];

// // Mock the 'offset' method to return the mock posts
// mockOffset.mockResolvedValueOnce(mockPosts);

// const limit = 5;
// const offset = 0;

// // Call the function under test
// const result = await findUsersByPosts(limit, offset);

// // Verify that the result matches the mock data
// expect(result).toEqual(mockPosts);
// expect(mockSelect).toHaveBeenCalledWith('id', 'user_id', 'username', 'post_content', 'created_at', 'comments', 'reactions', 'locationName', 'latitude', 'longitude');
// expect(mockOrderBy).toHaveBeenCalledWith('created_at', 'desc');
// expect(mockLimit).toHaveBeenCalledWith(limit);
// expect(mockOffset).toHaveBeenCalledWith(offset);
// });

// // Test Case #2: Database query fails
// test('should throw an error if the database query fails', async () => {
// // Mock the 'offset' method to reject with an error
// mockOffset.mockRejectedValueOnce(new Error('Database error'));

// const limit = 5;
// const offset = 0;

// // Verify that the error is thrown
// await expect(findUsersByPosts(limit, offset)).rejects.toThrow('Failed to fetch posts with comments');
// });

// // Test Case #3: No posts found, return an empty array
// test('should return an empty array if no posts are found', async () => {
// const mockPosts: UserPost[] = []; // Empty array for no posts scenario

// // Mock the 'offset' method to return an empty array
// mockOffset.mockResolvedValueOnce(mockPosts);

// const limit = 5;
// const offset = 0;

// // Call the function under test
// const result = await findUsersByPosts(limit, offset);

// // Verify that the result is an empty array
// expect(result).toEqual(mockPosts);
// });

// // Test Case #4: Handle invalid limit and offset values gracefully
// test('should handle invalid limit and offset values gracefully', async () => {
// const mockPosts: UserPost[] = [
// { id: 1, user_id: 123, username: 'user1', post_content: 'Post content 1', created_at: new Date(), comments: ["comment1"], reactions: 10, locationName: 'Park', latitude: 10, longitude: 20 }
// ];

// // Mock the 'offset' method to return the mock posts
// mockOffset.mockResolvedValueOnce(mockPosts);

// const limit = -5; // Invalid limit
// const offset = -1; // Invalid offset

// // Call the function under test and expect it to handle the invalid values
// const result = await findUsersByPosts(limit, offset);

// // Verify that the result is the mock posts despite invalid inputs
// expect(result).toEqual(mockPosts);
// });
// });
6 changes: 3 additions & 3 deletions backend/src/Models/Posts/__tests__/createUserPost.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createUserPost } from '../post.model';
import { UserPost } from '../../../../Types/posts';
import { User } from '../../../../Types/users';

// Mocking the knex object with method chaining
// Mock the db module with correct chaining behavior
const mockInsert = jest.fn().mockReturnThis();
const mockReturning = jest.fn().mockResolvedValue([]);

Expand All @@ -18,7 +18,7 @@ jest.mock('../../../db/db', () => {
};
});

// Clear all mocks after each test to ensure clean slate for each test case
// Clear all mocks after each test to ensure a clean slate for each test case
afterEach(() => {
jest.clearAllMocks();
});
Expand Down Expand Up @@ -47,7 +47,7 @@ describe('createUserPost', () => {
created_at: new Date(),
comment_ids: [],
reactions: 0,
locationName: "",
locationName: '',
latitude: 0,
longitude: 0,
};
Expand Down
92 changes: 92 additions & 0 deletions backend/src/Models/Posts/__tests__/findComments.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { findComments } from '../post.model';
import { UserPost } from '../../../../Types/posts';

// Mock the db module with correct chaining behavior
const mockSelect = jest.fn().mockReturnThis();
const mockWhereIn = jest.fn().mockReturnThis();
const mockOrderBy = jest.fn().mockReturnThis();
const mockLimit = jest.fn().mockReturnThis();
const mockOffset = jest.fn().mockReturnThis();

// Mocking the database module to return the mocked db methods
jest.mock('../../../db/db', () => ({
__esModule: true,
default: jest.fn(() => ({
select: mockSelect,
whereIn: mockWhereIn,
orderBy: mockOrderBy,
limit: mockLimit,
offset: mockOffset,
})),
}));

// Clear all mocks after each test to ensure a clean slate for each test case
afterEach(() => {
jest.clearAllMocks();
});

describe('findComments', () => {
// Test Case #1: Successfully fetch comments
test('should fetch comments successfully', async () => {
const mockComments: UserPost[] = [
{
id: 1,
user_id: 123,
username: 'user1',
post_content: 'First Comment',
created_at: new Date(),
comment_ids: [],
reactions: 0,
locationName: '',
latitude: 0,
longitude: 0,
},
];

// Mock the offset method to resolve with mockComments
mockOffset.mockResolvedValueOnce(mockComments);

const limit = 5;
const offset = 0;
const commentIDs = [1];

// Call the function under test
const result = await findComments(limit, offset, commentIDs);

// Verify the result matches the mock data
expect(result).toEqual(mockComments);

// Verify interactions with the mock db methods
expect(mockSelect).toHaveBeenCalledWith('id', 'user_id', 'username', 'comment_content', 'created_at', 'reactions');
expect(mockWhereIn).toHaveBeenCalledWith('id', commentIDs);
expect(mockOrderBy).toHaveBeenCalledWith('created_at', 'desc');
expect(mockLimit).toHaveBeenCalledWith(limit);
expect(mockOffset).toHaveBeenCalledWith(offset);
});

// Test Case #2: Return empty array if commentIDs is empty
test('should return an empty array if commentIDs is empty', async () => {
const limit = 5;
const offset = 0;
const commentIDs: number[] = [];

// Call the function under test
const result = await findComments(limit, offset, commentIDs);

// Verify that the result is an empty array
expect(result).toEqual([]);
});

// Test Case #3: Handle database error
test('should throw an error if the database query fails', async () => {
// Mock the offset method to reject with an error
mockOffset.mockRejectedValueOnce(new Error('Database error'));

const limit = 5;
const offset = 0;
const commentIDs = [1];

// Verify that the function throws the expected error
await expect(findComments(limit, offset, commentIDs)).rejects.toThrow('Failed to fetch comments');
});
});
73 changes: 73 additions & 0 deletions backend/src/Models/Posts/__tests__/findPosts.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { findPosts } from '../post.model';

// Mock the db module with correct chaining behavior
const mockSelect = jest.fn().mockReturnThis();
const mockOrderBy = jest.fn().mockReturnThis();
const mockLimit = jest.fn().mockReturnThis();
const mockOffset = jest.fn().mockReturnThis();

// Mocking the database module to return the mocked db methods
jest.mock('../../../db/db', () => ({
__esModule: true,
default: jest.fn(() => ({
select: mockSelect,
orderBy: mockOrderBy,
limit: mockLimit,
offset: mockOffset,
})),
}));

// Clear all mocks after each test to ensure a clean slate for each test case
afterEach(() => {
jest.clearAllMocks();
});

describe('findPosts', () => {
// Test Case #1: Successfully fetch posts
test('should fetch posts successfully', async () => {
const mockPosts = [
{
id: 1,
user_id: 123,
username: 'user1',
post_content: 'First Post',
created_at: new Date(),
comment_ids: [],
reactions: 0,
locationName: '',
latitude: 0,
longitude: 0,
},
];

// Mock the offset method to resolve with mockPosts
mockOffset.mockResolvedValueOnce(mockPosts);

const limit = 5;
const offset = 0;

// Call the function under test
const result = await findPosts(limit, offset);

// Verify the result matches the mock data
expect(result).toEqual(mockPosts);

// Verify interactions with the mock db methods
expect(mockSelect).toHaveBeenCalledWith('id', 'user_id', 'username', 'post_content', 'created_at', 'comment_ids', 'reactions', 'locationName', 'latitude', 'longitude');
expect(mockOrderBy).toHaveBeenCalledWith('created_at', 'desc');
expect(mockLimit).toHaveBeenCalledWith(limit);
expect(mockOffset).toHaveBeenCalledWith(offset);
});

// Test Case #2: Handle database error
test('should throw an error if the database query fails', async () => {
// Mock the offset method to reject with an error
mockOffset.mockRejectedValueOnce(new Error('Database error'));

const limit = 5;
const offset = 0;

// Verify that the function throws the expected error
await expect(findPosts(limit, offset)).rejects.toThrow('Failed to fetch posts with comments');
});
});
Loading
Loading