def test_lesson_QuizManagerAPI_GET(self): # request has invalid params input response = self.app.get('/lessons/quizzes?topic_id=1') # check if status code is correct print('--- check if status code is correct (invalid params input)') self.assertEqual(response.status_code, 400) # request error as dependency record not found response = self.app.get('/lessons/quizzes?topic_id=2&lesson_id=2') # check if status code is correct print( '--- check if status code is correct (dependency record not found)' ) self.assertEqual(response.status_code, 409) # request error as record not found response = self.app.get('/lessons/quizzes?topic_id=1&lesson_id=2') res = res_to_dict(response) # check if status code is correct print('--- check if status code is correct (record not found)') self.assertEqual(response.status_code, 409) # success case response = self.app.get('/lessons/quizzes?topic_id=1&lesson_id=1') res = res_to_dict(response) self.assertEqual(response.status_code, 200) # check if JSON returned is correct print('--- successful, check if JSON returned is correct') self.assertEqual(len(res['quiz']), 2)
def test_QuizOverallAPI_GET(self): # request has invalid params input response = self.app.get('/quizzes/overall?id=') # check if status code is correct print('--- check if status code is correct (invalid params input)') self.assertEqual(response.status_code, 400) # request error as record not found response = self.app.get('/quizzes/overall?id=4') # check if status code is correct print('--- check if status code is correct (record not found)') self.assertEqual(response.status_code, 404) # success case (1) response = self.app.get('/quizzes/overall?id=1') res = res_to_dict(response) # convert response to dictionary # check if status code is correct print('--- successful (2), check if status code is correct') self.assertEqual(response.status_code, 200) # check if JSON returned is correct print('--- successful (1), check if JSON returned is correct') self.assertEqual(res['name'], 'quiz_1') self.assertEqual(len(res['attempts']), 1) # success case (2) response = self.app.get('/quizzes/overall?id=2') res = res_to_dict(response) # convert response to dictionary # check if status code is correct print('--- successful (2), check if status code is correct') self.assertEqual(response.status_code, 200) # check if JSON returned is correct print('--- successful (2), check if JSON returned is correct') self.assertEqual(res['name'], 'quiz_2') self.assertEqual(res['message'], 'No attempts recorded at the moment')
def test_Quiz_QuestionManagerAPI_GET(self): # request has invalid params input response = self.app.get('/quizzes/questions?quiz_id=') # check if status code is correct print('--- check if status code is correct (invalid params input)') self.assertEqual(response.status_code, 400) # request error as record not found response = self.app.get('/quizzes/questions?quiz_id=4') # check if status code is correct print('--- check if status code is correct (record not found)') self.assertEqual(response.status_code, 404) # success case (1) response = self.app.get('/quizzes/questions?quiz_id=1') res = res_to_dict(response) # convert response to dictionary # check if status code is correct print('--- successful (1), check if status code is correct') self.assertEqual(response.status_code, 200) # check if JSON returned is correct print('--- successful (1), check if JSON returned is correct') self.assertEqual(len(res['questions']), 1) # success case (2) response = self.app.get('/quizzes/questions?quiz_id=2') res = res_to_dict(response) # convert response to dictionary # check if status code is correct print('--- successful (2), check if status code is correct') self.assertEqual(response.status_code, 200) # check if JSON returned is correct print('--- successful (2), check if JSON returned is correct') self.assertEqual(len(res['questions']), 0)
def test_Activity_API_GET(self): import datetime date_today = datetime.date.today() date_today_str = date_today.strftime('%Y-%m-%d') date_yesterday = datetime.date.today() - datetime.timedelta(days=10) date_yesterday_str = date_yesterday.strftime('%Y-%m-%d') date_tomorrow = datetime.date.today() + datetime.timedelta(days=10) date_tomorrow_str = date_tomorrow.strftime('%Y-%m-%d') # success case (no record found) response = self.app.get( '/statistics/activity?date_start={date_start}&date_end={date_end}&student_id=1'.format( date_start = date_yesterday_str, date_end = date_yesterday_str ) ) res = res_to_dict(response) # convert response to dictionary # check if status code is correct print('--- successful (no record found) check if status code is correct') self.assertEqual(response.status_code, 200) # check if JSON returned is correct print('--- successful (no record found) check if JSON returned is correct') self.assertEqual(len(res['attempts']), 0) # success case response = self.app.get( '/statistics/activity?date_start={date_start}&date_end={date_end}&student_id=1'.format( date_start = date_yesterday_str, date_end = date_tomorrow_str ) ) res = res_to_dict(response) # convert response to dictionary # check if status code is correct print('--- successful (record found - 1) check if status code is correct') self.assertEqual(response.status_code, 200) # check if JSON returned is correct print('--- successful (record found - 1) check if JSON returned is correct') self.assertEqual(res['attempts'][0][date_today_str], 2) # success case response = self.app.get( '/statistics/activity?date_start={date_start}&date_end={date_end}&student_id=1'.format( date_start = date_today_str, date_end = date_today_str ) ) res = res_to_dict(response) # convert response to dictionary # check if status code is correct print('--- successful (record found - 2) check if status code is correct') self.assertEqual(response.status_code, 200) # check if JSON returned is correct print('--- successful (record found - 2) check if JSON returned is correct') self.assertEqual(res['attempts'][0][date_today_str], 2)
def test_student_CourseManagerAPI_GET(self): # request has invalid params input response = self.app.get('/students/courses?user_email=') # check if status code is correct print('--- check if status code is correct (invalid params input)') self.assertEqual(response.status_code, 400) # request error as record not found response = self.app.get( '/students/[email protected]') # check if status code is correct print('--- check if status code is correct (record not found)') self.assertEqual(response.status_code, 409) # success case response = self.app.get( '/students/[email protected]') res = res_to_dict(response) # convert response to dictionary # check if status code is correct print('--- successful, check if status code is correct') self.assertEqual(response.status_code, 200) # check if JSON returned is correct print('--- successful, check if JSON returned is correct') self.assertEqual(res['name'], 'student_1')
def test_Quiz_CourseManagerAPI_POST(self): # request has invalid params input response = self.app.post('/quizzes/courses?quiz_id=1&course_index=') # check if status code is correct print('--- check if status code is correct (invalid params input)') self.assertEqual(response.status_code, 400) # request error as dependency record not found response = self.app.post('/quizzes/courses?quiz_id=4&course_index=cz1005') # check if status code is correct print('--- check if status code is correct (dependency record not found)') self.assertEqual(response.status_code, 404) # adding courses course = Course('cz1003') db.session.add(course) db.session.commit() # success case response = self.app.post('/quizzes/courses?quiz_id=1&course_index=cz1003') res = res_to_dict(response) # convert response to dictionary # check if status code is correct print('--- successful (2), check if status code is correct') self.assertEqual(response.status_code, 200)
def test_UserAPI_PUT(self): # request provided incorrect password response = self.app.put('/[email protected]&old_password=password1&new_password=password2') # check if status code is correct print('--- check if status code is correct (provided incorrect password)') self.assertEqual(response.status_code, 401) # request has invalid params input response = self.app.put('/[email protected]&old_password=password&new_password='******'--- check if status code is correct (invalid params input)') self.assertEqual(response.status_code, 400) # request error as record not found response = self.app.put('/[email protected]&old_password=password&new_password=password') # check if status code is correct print('--- check if status code is correct (record not found)') self.assertEqual(response.status_code, 409) # success case response = self.app.put('/[email protected]&old_password=password&new_password=password2') res = res_to_dict(response) # convert response to dictionary # check if status code is correct print('--- successful, check if status code is correct') self.assertEqual(response.status_code, 200) # check if JSON returned is correct print('--- successful, check if JSON returned is correct') self.assertTrue(authenticate('password2', res['encrypted_password']))
def test_challengeAPI_GET(self): # request has invalid params input response = self.app.get('/challenges?from_student_id=') # check if status code is correct print('--- check if status code is correct (invalid params input)') self.assertEqual(response.status_code, 400) # request error as record not found response = self.app.get( '/challenges?from_student_id=2&to_student_id=1&quiz_id=2') # check if status code is correct print('--- check if status code is correct (record not found)') self.assertEqual(response.status_code, 200) # success case response = self.app.get( '/challenges?from_student_id=1&to_student_id=2') res = res_to_dict(response) # convert response to dictionary # check if status code is correct print('--- successful, check if status code is correct') self.assertEqual(response.status_code, 200) # check if JSON returned is correct print('--- successful, check if JSON returned is correct') self.assertEqual(len(res), 2)
def test_QuizAPI_PUT(self): # request has invalid params input response = self.app.put('/quizzes?id=1') # check if status code is correct print('--- check if status code is correct (invalid params input)') self.assertEqual(response.status_code, 400) # request error as record not found response = self.app.put('/quizzes?id=4&name=quiz_new') # check if status code is correct print('--- check if status code is correct (record not found)') self.assertEqual(response.status_code, 404) # success case response = self.app.put('/quizzes?id=1&name=quiz_new&date_start=2020-01-01') res = res_to_dict(response) # convert response to dictionary # check if status code is correct print('--- successful, check if status code is correct') self.assertEqual(response.status_code, 200) # check if JSON returned is correct print('--- successful, check if JSON returned is correct') self.assertEqual(res['name'], 'quiz_new') self.assertEqual(res['is_fast'], True) self.assertEqual(res['date_start'], 'Wed, 01 Jan 2020 00:00:00 GMT') self.assertEqual(res['date_end'], 'Tue, 31 Mar 2020 00:00:00 GMT')
def test_staff_CourseManagerAPI_POST(self): # request has invalid params input response = self.app.post('/staffs/[email protected]') # check if status code is correct print('--- check if status code is correct (invalid params input)') self.assertEqual(response.status_code, 400) # request error as dependency record not found response = self.app.post('/staffs/[email protected]&course_index=cz1003') # check if status code is correct print('--- check if status code is correct (dependency record not found)') self.assertEqual(response.status_code, 409) # request error as record already exist response = self.app.post('/staffs/[email protected]&course_index=cz1005') # check if status code is correct print('--- check if status code is correct (record already exist)') self.assertEqual(response.status_code, 409) # adding courses course_cz1003 = Course('cz1003') db.session.add(course_cz1003) db.session.commit() # success case response = self.app.post('/staffs/[email protected]&course_index=cz1003') res = res_to_dict(response) # convert response to dictionary # check if status code is correct print('--- successful, check if status code is correct') self.assertEqual(response.status_code, 200) # check if JSON returned is correct print('--- successful, check if JSON returned is correct') self.assertEqual(res['staff_id'], 3) self.assertEqual(res['course_index'], 'cz1003')
def test_QuestionAttemptListAPI_GET(self): # request has invalid params input response = self.app.get( 'question_attempts/list?student_id=1&question_id=') # check if status code is correct print('--- check if status code is correct (invalid params input)') self.assertEqual(response.status_code, 400) # request error as record not found response = self.app.get( 'question_attempts/list?student_id=2&question_id=1') # check if status code is correct print('--- check if status code is correct (record not found)') self.assertEqual(response.status_code, 409) # success case response = self.app.get( 'question_attempts/list?student_id=1&question_id=1') res = res_to_dict(response) # convert response to dictionary # check if status code is correctha print('--- successful, check if status code is correct') self.assertEqual(response.status_code, 200) # check if JSON returned is correct print('--- successful, check if JSON returned is correct') self.assertEqual(res['list'][0]['duration_ms'], 10) self.assertEqual(len(res['list']), 2)
def test_QuestionChoiceAPI_DELETE(self): # request has invalid params input response = self.app.delete( '/question_choices?question_id=1&questionChoice_id=') # check if status code is correct print('--- check if status code is correct (invalid params input)') self.assertEqual(response.status_code, 400) # request error as record not found response = self.app.delete( '/question_choices?question_id=1&questionChoice_id=4') # check if status code is correct print('--- check if status code is correct (record not found)') self.assertEqual(response.status_code, 404) # success case response = self.app.delete( '/question_choices?question_id=1&questionChoice_id=3') res = res_to_dict(response) # convert response to dictionary # check if status code is correct print('--- successful, check if status code is correct') self.assertEqual(response.status_code, 200) # check if JSON returned is correct print('--- successful, check if JSON returned is correct') self.assertEqual(res['message'], 'Successfully deleted question choice')
def test_QuizAPI_POST(self): # request has invalid params input response = self.app.post('/quizzes?staff_id=3&name=quiz_4&is_fast=true&date_start=2020-01-01&date_end=') # check if status code is correct print('--- check if status code is correct (invalid params input)') self.assertEqual(response.status_code, 400) # request has invalid params input (date_start > date_end) response = self.app.post('/quizzes?staff_id=3&name=quiz_4&is_fast=true&date_start=2020-01-31&date_end=2020-01-01') # check if status code is correct print('--- check if status code is correct (invalid params input - date_start after date_end)') self.assertEqual(response.status_code, 412) # request error as dependency record not found response = self.app.post('/quizzes?staff_id=4&name=quiz_4&is_fast=true&date_start=2020-01-01&date_end=2020-01-31') # check if status code is correct print('--- check if status code is correct (dependency record not found)') self.assertEqual(response.status_code, 404) # success case response = self.app.post('/quizzes?staff_id=3&name=quiz_4&is_fast=true&date_start=2020-01-01&date_end=2020-01-31') res = res_to_dict(response) # convert response to dictionary # check if status code is correct print('--- successful, check if status code is correct') self.assertEqual(response.status_code, 200) # check if JSON returned is correct print('--- successful, check if JSON returned is correct') self.assertEqual(res['name'], 'quiz_4')
def test_QuestionChoiceAPI_POST(self): # request has invalid params input response = self.app.post( '/question_choices?question_id=&description=choice_4&is_correct=true' ) # check if status code is correct print('--- check if status code is correct (invalid params input)') self.assertEqual(response.status_code, 400) # dependencies record not found response = self.app.post( '/question_choices?question_id=6&description=choice_4&is_correct=true' ) # check if status code is correct print('--- check if status code is correct (record not found)') self.assertEqual(response.status_code, 404) # success case response = self.app.post( '/question_choices?question_id=2&description=choice_4&is_correct=true' ) res = res_to_dict(response) # convert response to dictionary # check if status code is correct print('--- successful, check if status code is correct') self.assertEqual(response.status_code, 200) # check if JSON returned is correct print('--- successful, check if JSON returned is correct') self.assertEqual(res['description'], 'choice_4')
def test_StudentAPI_POST(self): # request has invalid params input response = self.app.post( '/[email protected]&password=password&matriculation_number=' ) # check if status code is correct print('--- check if status code is correct (invalid params input)') self.assertEqual(response.status_code, 400) # request error as record already exist response = self.app.post( '/[email protected]&password=password&matriculation_number=U00000000A' ) # check if status code is correct print('--- check if status code is correct (record already exist)') self.assertEqual(response.status_code, 409) # success case response = self.app.post( '/[email protected]&password=password&matriculation_number=U00000000C' ) res = res_to_dict(response) # convert response to dictionary # check if status code is correct print('--- successful, check if status code is correct') self.assertEqual(response.status_code, 200) # check if JSON returned is correct print('--- successful, check if JSON returned is correct') self.assertEqual(res['name'], 'student_3')
def test_QuestionChoiceAPI_PUT(self): # request has invalid params input response = self.app.put( '/question_choices?question_id=1&questionChoice_id=&description=description_new&is_correct=true' ) # check if status code is correct print('--- check if status code is correct (invalid params input)') self.assertEqual(response.status_code, 400) # request error as record not found response = self.app.put( '//question_choices?question_id=1&questionChoice_id=19&description=description_new&is_correct=true' ) # check if status code is correct print('--- check if status code is correct (record not found)') self.assertEqual(response.status_code, 404) # success case response = self.app.put( '/question_choices?question_id=1&questionChoice_id=1&description=description_new' ) res = res_to_dict(response) # convert response to dictionary # check if status code is correct print('--- successful, check if status code is correct') self.assertEqual(response.status_code, 200) # check if JSON returned is correct print('--- successful, check if JSON returned is correct') self.assertEqual(res['description'], 'description_new')
def test_QuestionAPI_POST(self): # request has invalid params input response = self.app.post( '/questions?topic_id=1&lesson_id=&description=description_6') # check if status code is correct print('--- check if status code is correct (invalid params input)') self.assertEqual(response.status_code, 400) # request error as record not found response = self.app.post( '/questions?topic_id=1&lesson_id=5&description=description_6') # check if status code is correct print('--- check if status code is correct (record not found)') self.assertEqual(response.status_code, 404) # success case response = self.app.post( '/questions?topic_id=1&lesson_id=1&description=description_6') res = res_to_dict(response) # convert response to dictionary # check if status code is correct print('--- successful, check if status code is correct') self.assertEqual(response.status_code, 200) # check if JSON returned is correct print('--- successful, check if JSON returned is correct') self.assertEqual(res['description'], 'description_6')
def test_AuthenticationAPI_GET(self): # request provided incorrect password response = self.app.get('/users/[email protected]&password=password1') # check if status code is correct print('--- check if status code is correct (provided incorrect password)') self.assertEqual(response.status_code, 401) # request has invalid params input response = self.app.get('/users/[email protected]&password='******'--- check if status code is correct (invalid params input)') self.assertEqual(response.status_code, 400) # request error as record not found response = self.app.get('/users/[email protected]&password=password') # check if status code is correct print('--- check if status code is correct (record not found)') self.assertEqual(response.status_code, 409) # success case response = self.app.get('/users/[email protected]&password=password') res = res_to_dict(response) # convert response to dictionary # check if status code is correct print('--- successful, check if status code is correct') # check if JSON returned is correct self.assertEqual(response.status_code, 200) print('--- successful, check if JSON returned is correct') self.assertEqual(res['name'], 'staff_1')
def test_TopicAPI_PUT(self): # request has invalid params input (id is empty) response = self.app.put('/topics?id=&name=new_name') # check if status code is correct print( '--- check if status code is correct (invalid params input - empty id)' ) self.assertEqual(response.status_code, 400) # request has invalid params input (name is empty) response = self.app.put('/topics?id=1&name=') # check if status code is correct print( '--- check if status code is correct (invalid params input - empty name)' ) self.assertEqual(response.status_code, 400) # request error as record not found response = self.app.put('/topics?id=3&name=new_name') # check if status code is correct print('--- check if status code is correct (record not found)') self.assertEqual(response.status_code, 409) # success case response = self.app.put('/topics?id=1&name=new_name') res = res_to_dict(response) # convert response to dictionary # check if status code is correct print('--- successful, check if status code is correct') self.assertEqual(response.status_code, 200) # check if JSON returned is correct print('--- successful, check if JSON returned is correct') self.assertEqual(res['name'], 'new_name')
def test_TopicAPI_DELETE(self): # request has invalid params input response = self.app.delete('/topics?id=') # check if status code is correct print('--- check if status code is correct (invalid params input)') self.assertEqual(response.status_code, 400) # request error as record not found response = self.app.delete('/topics?id=3') # check if status code is correct print('--- check if status code is correct (record not found)') self.assertEqual(response.status_code, 409) # request error as record is a dependency response = self.app.delete('/topics?id=2') # check if status code is correct print('--- check if status code is correct (record is a dependency)') self.assertEqual(response.status_code, 409) # removing dependencies rom record lessons = Lesson.query.filter_by(topic_id=2).all() for lesson in lessons: db.session.delete(lesson) db.session.commit() #success case response = self.app.delete('/topics?id=2') res = res_to_dict(response) # convert response to dictionary # check if status code is correct print('--- successful, check if status code is correct') self.assertEqual(response.status_code, 200) # check if JSON returned is correct print('--- successful, check if JSON returned is correct') self.assertEqual(res['message'], 'Successfully deleted topic')
def test_LessonAPI_PUT(self): # request has invalid params input response = self.app.put( '/lessons?topic_id=1&lesson_id=1&col=wrong_value&value=hello') # check if status code is correct print('--- check if status code is correct (invalid params input)') self.assertEqual(response.status_code, 400) # request error as record not found response = self.app.put( '/lessons?topic_id=3&lesson_id=1&col=name&value=lesson_10') # check if status code is correct print('--- check if status code is correct (record not found)') self.assertEqual(response.status_code, 409) # success case response = self.app.put( '/lessons?topic_id=1&lesson_id=1&col=name&value=lesson_10') res = res_to_dict(response) # convert response to dictionary # check if status code is correct print('--- successful (1), check if status code is correct') self.assertEqual(response.status_code, 200) # check if JSON returned is correct print('--- successful (1), check if JSON returned is correct') self.assertEqual(res['name'], 'lesson_10') response = self.app.put( '/lessons?topic_id=1&lesson_id=1&col=content&value=content_new') res = res_to_dict(response) # convert response to dictionary # check if status code is correct print('--- successful (2), check if status code is correct') self.assertEqual(response.status_code, 200) # check if JSON returned is correct print('--- successful (2), check if JSON returned is correct') self.assertEqual(res['content'], 'content_new') response = self.app.put( '/lessons?topic_id=1&lesson_id=1&col=url_link&value=https%3A%2F%2Fwww.facebook.com' ) res = res_to_dict(response) # convert response to dictionary # check if status code is correct print('--- successful (3), check if status code is correct') self.assertEqual(response.status_code, 200) # check if JSON returned is correct print('--- successful (3), check if JSON returned is correct') self.assertEqual(res['url_link'], 'https://www.facebook.com')
def test_StudentScore_API_GET(self): # success case (return all students if no proper params passed) response = self.app.get('/statistics/student_score?student_id=') res = res_to_dict(response) # convert response to dictionary # check if status code is correct print('--- successful (no params passed in) check if status code is correct') self.assertEqual(response.status_code, 200) # check if JSON returned is correct print('--- successful (no params passed in) check if JSON returned is correct') self.assertEqual(len(res['students']), 2) # success case (no student found) response = self.app.get('/statistics/student_score?student_id=3') res = res_to_dict(response) # convert response to dictionary # check if status code is correct print('--- successful (params passed in, but no student found) check if status code is correct') self.assertEqual(response.status_code, 200) # check if JSON returned is correct print('--- successful (params passed in, but no student found) check if JSON returned is correct') self.assertEqual( res, { "students": [] } ) # success case (student found) response = self.app.get('/statistics/student_score?student_id=1') res = res_to_dict(response) # convert response to dictionary # check if status code is correct print('--- successful (params passed in and student found - 1) check if status code is correct') self.assertEqual(response.status_code, 200) # check if JSON returned is correct print('--- successful (params passed in and student found - 1) check if JSON returned is correct') self.assertEqual(res['students'][0]['name'], 'student_1') self.assertEqual(len(res['students'][0]['quizzes']), 2) response = self.app.get('/statistics/student_score?student_id=2') res = res_to_dict(response) # convert response to dictionary # check if status code is correct print('--- successful (params passed in and student found - 2) check if status code is correct') self.assertEqual(response.status_code, 200) # check if JSON returned is correct print('--- successful (params passed in and student found - 2) heck if JSON returned is correct') self.assertEqual(res['students'][0]['name'], 'student_2') self.assertEqual(len(res['students'][0]['quizzes']), 1)
def test_challengeAPI_PUT(self): # request has invalid params input response = self.app.put( '/challenges?from_student_id=1&to_student_id=2&quiz_id=') # check if status code is correct print('--- check if status code is correct (invalid params input)') self.assertEqual(response.status_code, 400) # request error as record already exist response = self.app.put( '/challenges?from_student_id=2&to_student_id=1&quiz_id=2&winner_id=2' ) # check if status code is correct print('--- check if status code is correct (record already exist)') self.assertEqual(response.status_code, 409) # success case (without winner_id) response = self.app.put( '/challenges?from_student_id=1&to_student_id=2&quiz_id=1') res = res_to_dict(response) # convert response to dictionary # check if status code is correct print( '--- successful (no winner yet), check if status code is correct') self.assertEqual(response.status_code, 200) # check if JSON returned is correct print( '--- successful (no winner yet), check if JSON returned is correct' ) self.assertIsNone(res['winner_id']) self.assertTrue(res['is_completed']) # success case (with winner_id) response = self.app.put( '/challenges?from_student_id=1&to_student_id=2&quiz_id=2&winner_id=1' ) res = res_to_dict(response) # convert response to dictionary # check if status code is correct print( '--- successful (winner decided), check if status code is correct') self.assertEqual(response.status_code, 200) # check if JSON returned is correct print( '--- successful (winner decided), check if JSON returned is correct' ) self.assertEqual(res['winner_id'], 1) self.assertTrue(res['is_completed'])
def test_QuestionGetAllAPI_GET(self): # success case response = self.app.get('/questions/all') res = res_to_dict(response) # convert response to dictionary # check if status code is correct print('--- successful, check if status code is correct') self.assertEqual(response.status_code, 200) # check if JSON returned is correct print('--- successful, check if JSON returned is correct') self.assertEqual(len(res['questions']), 5)
def test_LessonAPI_POST(self): # request has invalid params input response = self.app.post('/lessons?topic_id=1&name=new_lesson') # check if status code is correct print('--- check if status code is correct (invalid params input)') self.assertEqual(response.status_code, 400) # request error as record already exist response = self.app.post( '/lessons?topic_id=1&name=lesson_1&content=content&url_link=https%3A%2F%2Fwww.google.com' ) # check if status code is correct print('--- check if status code is correct (record already exist)') self.assertEqual(response.status_code, 409) # success case response = self.app.post( '/lessons?topic_id=2&name=lesson_5&content=content') res = res_to_dict(response) # convert response to dictionary # check if status code is correct print('--- successful (1), check if status code is correct') self.assertEqual(response.status_code, 200) # check if JSON returned is correct print('--- successful (1), check if JSON returned is correct') self.assertEqual(res['name'], 'lesson_5') self.assertEqual(res['url_link'], None) response = self.app.post( '/lessons?topic_id=2&name=lesson_6&content=content&url_link=https%3A%2F%2Fwww.google.com' ) res = res_to_dict(response) # convert response to dictionary # check if status code is correct print('--- successful (2), check if status code is correct') self.assertEqual(response.status_code, 200) # check if JSON returned is correct print('--- successful (2), check if JSON returned is correct') self.assertEqual(res['name'], 'lesson_6') self.assertEqual(res['url_link'], 'https://www.google.com')
def test_userAPI_PUT(self): # invalid params input response = self.app.put('/users?username=user_2&name=') self.assertEqual(response.status_code, 400) # user not found response = self.app.put('/users?username=user_2&name=new_name') self.assertEqual(response.status_code, 409) # success case response = self.app.put('/users?username=adriangohjw&name=new_name') res = res_to_dict(response) self.assertEqual(response.status_code, 200) self.assertEqual('new_name', res['name']) response = self.app.put( '/users?username=adriangohjw&age=24&height=175') res = res_to_dict(response) self.assertEqual(response.status_code, 200) self.assertEqual(24, res['age']) self.assertEqual(175, res['height'])
def test_TopicListAPI_GET(self): # success case response = self.app.get('/topics/all') res = res_to_dict(response) # convert response to dictionary # check if status code is correct print('--- successful, check if status code is correct') self.assertEqual(response.status_code, 200) # check if JSON returned is correct print('--- successful, check if JSON returned is correct') self.assertEqual(len(res['topics']), 2) self.assertEqual(res['topics'][0]['name'], 'topic_1') self.assertEqual(res['topics'][1]['name'], 'topic_2')
def test_userAPI_GET(self): # invalid params input response = self.app.get('/users?username='******'/users?username=adriangohjw_1') self.assertEqual(response.status_code, 409) # success case response = self.app.get('/users?username=adriangohjw') res = res_to_dict(response) self.assertEqual(response.status_code, 200) self.assertEqual(res['name'], 'Adrian Goh')
def test_routeAPI_DELETE(self): # invalid params input response = self.app.delete('/routes?id=') self.assertEqual(response.status_code, 400) # record not found response = self.app.delete('/routes?id=3') self.assertEqual(response.status_code, 409) # success case response = self.app.delete('/routes?id=1') res = res_to_dict(response) self.assertEqual(response.status_code, 200) self.assertEqual(res['message'], 'Successfully deleted route')
def test_routeAPI_PUT(self): # invalid params input response = self.app.put('/routes?id=1&calories=') self.assertEqual(response.status_code, 400) # record not found response = self.app.put('/routes?id=3&calories=100') self.assertEqual(response.status_code, 409) # success case response = self.app.put('/routes?id=1&calories=100') res = res_to_dict(response) self.assertEqual(response.status_code, 200) self.assertEqual(100, res['calories'])