Skip to content

Commit

Permalink
Merge pull request #32 from CSSE6400/86
Browse files Browse the repository at this point in the history
test cases for /exam routes
  • Loading branch information
86LAK authored May 2, 2024
2 parents 51e0639 + 2fdf23e commit 1ac342a
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 23 deletions.
51 changes: 31 additions & 20 deletions backend/src/routes/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ router.post('/questions', async (req: Request<any, any, QuestionBodyParams>, res
return;
}

const { rowCount } = await db.query(`SELECT "examId" exams WHERE "examId" = $1`, [examId]);
const { rowCount } = await db.query(`SELECT "examId" FROM exams WHERE "examId" = $1`, [examId]);
if (rowCount === 0) {
res.status(404).json('Exam not found!');
return;
Expand All @@ -267,7 +267,7 @@ router.post('/questions', async (req: Request<any, any, QuestionBodyParams>, res
res.status(201).json('Question Added!');
});

// Adds a new exam to the databasecustomersscustomerss
// Adds a new exam to the database
router.post('/exams', async (req: Request<any, any, ExamBodyParams>, res: Response) => {
const {
examYear,
Expand All @@ -278,13 +278,13 @@ router.post('/exams', async (req: Request<any, any, ExamBodyParams>, res: Respon

// Check key
if (!courseCode) {
res.status(400).json('Missing courseCode!');
res.status(400).json('Missing courseCode');
return;
}

const { rowCount } = await db.query(`SELECT "courseCode" courses WHERE "courseCode" = $1`, [courseCode]);
const { rowCount } = await db.query(`SELECT "courseCode" FROM courses WHERE "courseCode" = $1`, [courseCode]);
if (rowCount === 0) {
res.status(404).json('Exam not found!');
res.status(404).json('Course not found');
return;
}

Expand All @@ -293,7 +293,7 @@ router.post('/exams', async (req: Request<any, any, ExamBodyParams>, res: Respon
VALUES ($1, $2, $3, $4)
`, [examYear, examSemester, examType, courseCode]);

res.status(201).json('Exam Added!');
res.status(201).json('Exam created');
});

// Adds a new Course to the database
Expand Down Expand Up @@ -372,28 +372,39 @@ router.get('/questions/:questionId', async (req: Request<QuestionRouteParams>, r

// Exam questions by exam ID
router.get('/exams/:examId/questions', async (req: Request<ExamRouteParams>, res: Response) => {
const { examId } = req.params;
const { examId } = req.params;

const { rows } = await db.query<Question>(`
SELECT "questionId", "questionText", "questionType", "questionPNG"
FROM questions
WHERE questions."examId" = $1
`, [examId]);
const { rows } = await db.query<Question>(`
SELECT "questionId", "questionText", "questionType", "questionPNG"
FROM questions
WHERE questions."examId" = $1
`, [examId]);

res.status(200).json(rows);
if (rows.length === 0) {
res.status(404).json('No questions found for this exam');
return;
}

res.status(200).json(rows);
});


// Exam by ID
router.get('/exams/:examId', async (req: Request<ExamRouteParams>, res: Response) => {
const { examId } = req.params;
const { examId } = req.params;

const { rows } = await db.query<Exam>(`
SELECT "examId", "examYear", "examSemester", "examType"
FROM exams
WHERE exams."examId" = $1
`, [examId]);
const { rows } = await db.query<Exam>(`
SELECT "examId", "examYear", "examSemester", "examType"
FROM exams
WHERE exams."examId" = $1
`, [examId]);

res.status(200).json(rows[0]);
if (rows.length === 0) {
res.status(404).json('Exam not found');
return;
}

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

// A course's exams by code
Expand Down
103 changes: 100 additions & 3 deletions integration_tests/test_exams.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,110 @@


class TestUser(BaseCase):
def test_evan(self):
def test_exam_post(self):
"""
Checks for a 200 response from the evan endpoint
Checks for a 201 response from the /exam endpoint
Checks for the correct response message
"""
response = requests.get(self.host() + '/evan', headers={'Accept': 'application/json'})
body = {
"examYear": 2021,
"examSemester": "1",
"examType": "Final",
"courseCode": "CSSE6400"
}

response = requests.post(self.host() + '/exams', json=body)
self.assertEqual(201, response.status_code)
self.assertEqual('Exam created', response.json())


def test_exam_post_missing_courseCode(self):
"""
Checks for a 400 response from the /exam endpoint
Checks for the correct response message
"""
body = {
"examYear": 2021,
"examSemester": "1",
"examType": "Final"
}

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


def test_exam_post_exam_not_found(self):
"""
Checks for a 404 response from the /exam endpoint
Checks for the correct response message
"""
body = {
"examYear": 2021,
"examSemester": "1",
"examType": "Final",
"courseCode": "TOYO8686"
}

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


def test_exam_get_questions_by_exam_id(self):
"""
Checks for a 200 response from the /exam endpoint
Checks for the correct response message
"""
examId = 1
expectedBody = [{
"questionId": 1,
"questionText": "Who is the best tutor at UQ?",
"questionType": "Multiple Choice",
"questionPNG": None
}]
response = requests.get(self.host() + f'/exams/{examId}/questions')
self.assertEqual(200, response.status_code)
self.assertEqual(expectedBody, response.json())


def test_exam_get_questions_by_examId_not_found(self):
"""
Checks for a 404 response from the /exam endpoint
Checks for the correct response message
"""
examId = 8686
response = requests.get(self.host() + f'/exams/{examId}/questions')
self.assertEqual(404, response.status_code)
self.assertEqual('No questions found for this exam', response.json())


def test_exam_get_exam_by_id(self):
"""
Checks for a 200 response from the /exam endpoint
Checks for the correct response message
"""
examId = 1
expectedBody = {
"examId": 1,
"examYear": 2021,
"examSemester": 1,
"examType": "Final"
}
response = requests.get(self.host() + f'/exams/{examId}')
self.assertEqual(200, response.status_code)
self.assertEqual(expectedBody, response.json())


def test_exam_get_exam_by_id_not_found(self):
"""
Checks for a 404 response from the /exam endpoint
Checks for the correct response message
"""
examId = 8686
response = requests.get(self.host() + f'/exams/{examId}')
self.assertEqual(404, response.status_code)
self.assertEqual('Exam not found', response.json())


if __name__ == '__main__':
Expand Down

0 comments on commit 1ac342a

Please sign in to comment.