def test_fetching_course_by_id(self): """Test fetching courses by id""" res = self.client.post('/courses', data=self.course) course_id = decode_response_to_json(res)['course']['course_id'] rsp = self.client.get('/courses/{}'.format(course_id)) rsp_json = decode_response_to_json(rsp) self.assertEqual(rsp_json['course']['instructor'], 'Canns Bean')
def test_editing_student(self): """test editing student""" result = self.client.post('/students', data=self.student) json_result = decode_response_to_json(result) student_id = json_result['student']['student_id'] response = self.client.put('/students/{}'.format(student_id), data={'first_name': 'Wanjala'}) fetch_response = self.client.get('/students/{}'.format(student_id)) json_fetch_response = decode_response_to_json(fetch_response) self.assertEqual(response.status_code, 200) self.assertEqual(json_fetch_response['student']['first_name'], 'Wanjala')
def test_fetch_all_students(self): """Test fetching all students""" self.client.post('/students', data=self.student) response = self.client.get('/students') json_response = decode_response_to_json(response) self.assertEqual(response.status_code, 200) self.assertTrue(len(json_response['students']) == 1)
def test_empty_params(self): """Test posting course with missing fields""" result = self.client.post('/courses', data={}) json_result = decode_response_to_json(result) self.assertEqual(result.status_code, 400) self.assertEqual(json_result['errors']['course_name'], 'course_name is required')
def test_fetching_all_courses(self): """Test fetching all courses""" self.client.post('/courses', data=self.course) self.course['course_code'] = 'DIS 304' self.client.post('/courses', data=self.course) response = self.client.get('/courses') response_json = decode_response_to_json(response) self.assertEqual(len(response_json['courses']), 2)
def test_deleting_student(self): """test deleting student""" result = self.client.post('/students', data=self.student) json_result = decode_response_to_json(result) student_id = json_result['student']['student_id'] response = self.client.delete('/students/{}'.format(student_id)) self.assertEqual(response.status_code, 204) fetch_response = self.client.get('/students/{}'.format(student_id)) self.assertEqual(fetch_response.status_code, 404)
def test_missing_fields(self): result = self.client.post('/students', data={}) json_result = decode_response_to_json(result) self.assertEqual(result.status_code, 400) self.assertEqual(json_result['errors']['first_name'], 'First name is required')
def test_fetch_student_by_id(self): """test fetching student by id""" result = self.client.post('/students', data=self.student) json_result = decode_response_to_json(result) response = self.client.get('/students/{}'.format(json_result['student']['student_id'])) self.assertEqual(response.status_code, 200)
def test_creating_course(self): """Test creating course""" response = self.client.post('/courses', data=self.course) response_json = decode_response_to_json(response) self.assertEqual(response.status_code, 201) self.assertEqual('DIS 106', response_json['course']['course_code'])