Skip to content

Commit

Permalink
Merge pull request #38 from CSSE6400/86
Browse files Browse the repository at this point in the history
test cases covering all of remaining routes in /comments
  • Loading branch information
86LAK authored May 4, 2024
2 parents d034a00 + cbef750 commit 8877b57
Show file tree
Hide file tree
Showing 2 changed files with 225 additions and 12 deletions.
38 changes: 26 additions & 12 deletions backend/src/routes/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ router.patch('/comments/:commentId/upvote', async (req: Request<CommentRoutePara
*
*/

//TODO needs to be tested
// Adds a new comment to the database
router.post('/comments', async (req: Request<any, any, CommentBodyParams>, res: Response) => {
const {
Expand All @@ -251,6 +250,11 @@ router.post('/comments', async (req: Request<any, any, CommentBodyParams>, res:
return;
}

if (!commentText && !commentPNG) {
res.status(400).json('Missing commentText and commentPNG');
return;
}

const { rowCount } = await db.query(`SELECT "questionId" FROM questions WHERE "questionId" = $1`, [questionId]);
if (rowCount === 0) {
res.status(404).json('Question not found');
Expand All @@ -276,7 +280,7 @@ router.post('/comments', async (req: Request<any, any, CommentBodyParams>, res:
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
`, [questionId, parentCommentId, commentText, commentPNG, isCorrect, isEndorsed, upvotes, downvotes]);

res.status(201).json('Comment Added');
res.status(201).json('Comment added');
});

// Adds a new question to the database
Expand Down Expand Up @@ -372,18 +376,21 @@ router.post('/courses', async (req: Request<any, any, CourseBodyParams>, res: Re
*
*/

//TODO needs to be tested
// Gets comment by comment id
router.get('/comments/:commentId', async (req: Request<CommentRouteParams>, res: Response) => {
const { commentId } = req.params;
const { commentId } = req.params;

const { rows } = await db.query<IComment>(`
SELECT "commentId", "questionId", "parentCommentId", "commentText", "commentPNG", "isCorrect", "isEndorsed", "upvotes", "downvotes", "created_at", "updated_at"
FROM comments
WHERE comments."commentId" = $1
`, [commentId]);
const { rows, rowCount } = await db.query<IComment>(`
SELECT "commentId", "questionId", "parentCommentId", "commentText", "commentPNG", "isCorrect", "isEndorsed", "upvotes", "downvotes", "created_at", "updated_at"
FROM comments
WHERE comments."commentId" = $1
`, [commentId]);

if (rowCount === 0) {
return res.status(404).json({ error: 'Comment not found' });
}

res.status(200).json(rows[0]);
res.status(200).json(rows[0]);
});

// Gets all comments by question id
Expand Down Expand Up @@ -585,7 +592,11 @@ router.get('/sketch', async (req: Request, res: Response) => {
(9, 'Question which has a comment to be endorsed', 'Multiple Choice'),
(10, 'Question which has a comment endorsed to be removed', 'Multiple Choice'),
(11, 'Question which has a comment to be upvoted', 'Multiple Choice'),
(12, 'Question which has a comment to be downvoted', 'Multiple Choice');
(12, 'Question which has a comment to be downvoted', 'Multiple Choice'),
(13, 'Question which has no comments. And one will be added', 'Multiple Choice'),
(14, 'Question which has a comment. And one will be added', 'Multiple Choice'),
(15, 'Question which has a comment. And one will be added as nested', 'Multiple Choice'),
(16, 'Question which has a comment. And this is used for error checks on nesting comments with incorrect parent id.', 'Multiple Choice');
INSERT INTO comments ("questionId", "parentCommentId", "commentText", "isCorrect", "isEndorsed", "upvotes", "downvotes")
VALUES
Expand All @@ -608,7 +619,10 @@ router.get('/sketch', async (req: Request, res: Response) => {
(9, NULL, 'This is a comment that will be endorsed', TRUE, TRUE, 100, 1),
(10, NULL, 'This is a comment that will have its endorsement removed', TRUE, TRUE, 100, 1),
(11, NULL, 'This is a comment that will be upvoted', TRUE, TRUE, 100, 1),
(12, NULL, 'This is a comment that will be downvoted', TRUE, TRUE, 100, 1);
(12, NULL, 'This is a comment that will be downvoted', TRUE, TRUE, 100, 1),
(14, NULL, 'This is a comment that will be added', TRUE, TRUE, 100, 1),
(15, NULL, 'This is a comment that a test will add a nested comment to', TRUE, TRUE, 100, 1),
(16, NULL, 'This is a comment.', TRUE, TRUE, 100, 1);
`);
res.status(200).json(`THIS SHIT SKETCH ASF AND WAS LIV'S IDEA!!!`);
});
Expand Down
199 changes: 199 additions & 0 deletions integration_tests/test_comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,207 @@ def test_patch_upvotes_comment_invalid_id(self):
self.assertEqual(404, response.status_code)
self.assertEqual('Comment not found', response.json())

def test_post_comment_success(self):
"""
Checks for a 201 response from the /comments endpoint
Checks for the correct response message
"""
body = {
"questionId": 13,
"parentCommentId": 0,
"commentText": "This is a comment",
"commentPNG": None,
"isCorrect": False,
"isEndorsed": False,
"upvotes": 0,
"downvotes": 0
}
#TODO the above is 13 if when i put 17 it fails. if 17 fails make sure 16 works and then put 13 in.

response = requests.post(self.host() + '/comments', json=body)
self.assertEqual(201, response.status_code)
self.assertEqual('Comment added', response.json())

def test_post_comment_invalid_question_id(self):
"""
Checks for a 404 response from the /comments endpoint
Checks for the correct response message
"""
body = {
"questionId": 868686,
"parentCommentId": 0,
"commentText": "This is a comment",
"commentPNG": None,
"isCorrect": False,
"isEndorsed": False,
"upvotes": 0,
"downvotes": 0
}

response = requests.post(self.host() + '/comments', json=body)
self.assertEqual(404, response.status_code)
self.assertEqual('Question not found', response.json())


def test_post_comment_no_question_id(self):
"""
Checks for a 400 response from the /comments endpoint
Checks for the correct response message
"""
body = {
"questionId": None,
"parentCommentId": 0,
"commentText": "This is a comment",
"commentPNG": None,
"isCorrect": False,
"isEndorsed": False,
"upvotes": 0,
"downvotes": 0
}

response = requests.post(self.host() + '/comments', json=body)
self.assertEqual(400, response.status_code)
self.assertEqual('Missing questionId', response.json())


def test_post_comment_no_comment_text_or_png(self):
"""
Checks for a 400 response from the /comments endpoint
Checks for the correct response message
"""
body = {
"questionId": 13,
"parentCommentId": 0,
"commentText": None,
"commentPNG": None,
"isCorrect": False,
"isEndorsed": False,
"upvotes": 0,
"downvotes": 0
}

response = requests.post(self.host() + '/comments', json=body)
self.assertEqual(400, response.status_code)
self.assertEqual('Missing commentText and commentPNG', response.json())


def test_post_comment_nested_comment(self):
"""
Checks for a 201 response from the /comments endpoint
Checks for the correct response message
"""
body = {
"questionId": 14,
"parentCommentId": 21,
"commentText": "This is a nested comment",
"commentPNG": None,
"isCorrect": False,
"isEndorsed": False,
"upvotes": 0,
"downvotes": 0
}

response = requests.post(self.host() + '/comments', json=body)
self.assertEqual(201, response.status_code)
self.assertEqual('Comment added', response.json())


def test_post_comment_nested_invalid_parentID(self):
"""
Checks for a 404 response from the /comments endpoint
Checks for the correct response message
"""
body = {
"questionId": 14,
"parentCommentId": 868686,
"commentText": "This is a nested comment",
"commentPNG": None,
"isCorrect": False,
"isEndorsed": False,
"upvotes": 0,
"downvotes": 0
}

response = requests.post(self.host() + '/comments', json=body)
self.assertEqual(404, response.status_code)
self.assertEqual('Parent comment not found', response.json())


def test_post_comment_nested_parentId_not_from_same_question(self):
"""
Checks for a 400 response from the /comments endpoint
Checks for the correct response message
"""
body = {
"questionId": 15,
"parentCommentId": 1,
"commentText": "This is a nested comment",
"commentPNG": None,
"isCorrect": False,
"isEndorsed": False,
"upvotes": 0,
"downvotes": 0
}

response = requests.post(self.host() + '/comments', json=body)
self.assertEqual(400, response.status_code)
self.assertEqual('Parent comment is not from the same question', response.json())

def test_get_comment_by_id(self):
"""
Checks for a 200 response from the /comments endpoint
Checks for the correct response message
"""
commentId = 23
expectedResponse = {
"commentId": commentId,
"parentCommentId": None,
"commentText": "This is a comment.",
"commentPNG": None,
"isCorrect": True,
"isEndorsed": True,
"upvotes": 100,
"downvotes": 1,
"questionId": 16,
"created_at": "2001-06-01T09:00:00",
"updated_at": "2001-06-01T09:00:00"
}

response = requests.get(self.host() + '/comments/' + str(commentId))

def update_timestamps(response_dict, created_at, updated_at):
if isinstance(response_dict, list):
for item in response_dict:
update_timestamps(item, created_at, updated_at)
elif isinstance(response_dict, dict):
if 'created_at' in response_dict:
response_dict['created_at'] = created_at
if 'updated_at' in response_dict:
response_dict['updated_at'] = updated_at
for key, value in response_dict.items():
if isinstance(value, (list, dict)):
update_timestamps(value, created_at, updated_at)

update_timestamps(expectedResponse, response.json()['created_at'], response.json()['updated_at'])

self.assertEqual(200, response.status_code)
self.assertEqual(expectedResponse, response.json())


def test_get_comment_by_id_invalid_id(self):
"""
Checks for a 404 response from the /comments endpoint
Checks for the correct response message
"""
commentId = 868686

expectedResponse = {
"error": "Comment not found"
}

response = requests.get(self.host() + '/comments/' + str(commentId))
self.assertEqual(404, response.status_code)
self.assertEqual(expectedResponse, response.json())

if __name__ == '__main__':
unittest.main()

0 comments on commit 8877b57

Please sign in to comment.