def test_missing_field(self): data = json.dumps({ "user_name": "Harald", "user_email": "*****@*****.**", }) request = self.factory.put('/api/v1/questions/1/answers/', data=data) response = views.answers(request, 1) self.assertEqual(response.status_code, 400)
def test_submit_answer_successfully(self): data = json.dumps({ "content": "Lorem ipsum dolor sit amet", "user_name": "Harald", "user_email": "*****@*****.**", }) request = self.factory.put('/api/v1/questions/1/answers/', data=data) response = views.answers(request, 1) self.assertEqual(response.status_code, 201)
def test_non_string_data(self): data = json.dumps({ "content": ["Lorem ipsum dolor sit amet"], "user_name": "Harald", "user_email": "*****@*****.**", }) request = self.factory.put('/api/v1/questions/1/answers/', data=data) response = views.answers(request, 1) self.assertEqual(response.status_code, 400)
def test_update_answer(self): data = json.dumps({ "content": "Some new content", "user_name": "Harald", "user_email": "*****@*****.**", }) request = self.factory.put('/api/v1/questions/1/answers/2/', data=data) response = views.answers(request, 1, 2) self.assertEqual(response.status_code, 200) self.test_get_one_answer("Harald")
def test_unsupported_request_type(self): request = self.factory.delete('/api/v1/questions/1/answers/') response = views.answers(request, 1) self.assertEqual(response.status_code, 405)
def test_incorrect_json(self): request = self.factory.put('/api/v1/questions/1/answers/') request.data = {'sdfsd'} response = views.answers(request, 1) self.assertEqual(response.status_code, 400)
def test_get_non_existing_answer(self): request = self.factory.get('/api/v1/questions/1/answers/8000/') response = views.answers(request, 1, 8000) self.assertEqual(response.status_code, 404)
def test_get_empty_answers_for_question(self): request = self.factory.get('/api/v1/questions/2/answers/') response = views.answers(request, 2) self.assertEqual(response.status_code, 200) self.assertEqual(json.loads(response.content), [])
def test_get_one_answer(self, user_value="Harry"): request = self.factory.get('/api/v1/questions/1/answers/2/') response = views.answers(request, 1, 2) self.assertEqual(response.status_code, 200) self.assertEqual(json.loads(response.content)['user_name'], user_value)
def test_get_all_answers_for_question(self): request = self.factory.get('/api/v1/questions/1/answers/') response = views.answers(request, 1) self.assertEqual(response.status_code, 200) self.assertEqual(len(json.loads(response.content)), 2)