def test_missing_field(self): data = json.dumps({ "title": "Why is water wet?", "user_name": "Harald", "user_email": "*****@*****.**", }) request = self.factory.put('/api/v1/questions/', data=data) response = views.questions(request) self.assertEqual(response.status_code, 400)
def test_submit_question_successfully(self): data = json.dumps({ "title": "Why is water wet?", "content": "Eerer erwerwe ewrewre", "user_name": "Harald", "user_email": "*****@*****.**", }) request = self.factory.put('/api/v1/questions/', data=data) response = views.questions(request) self.assertEqual(response.status_code, 201)
def test_update_question(self): data = json.dumps({ "title": "Why is water wet?", "content": "Some new content", "user_name": "Harald", "user_email": "*****@*****.**", }) request = self.factory.put('/api/v1/questions/1/', data=data) response = views.questions(request, 1) self.assertEqual(response.status_code, 200) self.test_get_one_question("Some new content")
def test_unsupported_request_type(self): request = self.factory.delete('/api/v1/questions/') response = views.questions(request) self.assertEqual(response.status_code, 405)
def test_incorrect_json(self): request = self.factory.put('/api/v1/questions/') request.data = {'sdfsd'} response = views.questions(request) self.assertEqual(response.status_code, 400)
def test_get_non_existing_question(self): request = self.factory.get('/api/v1/questions/4000/') response = views.questions(request, 4000) self.assertEqual(response.status_code, 404)
def test_get_one_question(self, content_value="Some content"): request = self.factory.get('/api/v1/questions/1/') response = views.questions(request, 1) self.assertEqual(response.status_code, 200) self.assertEqual( json.loads(response.content)['content'], content_value)
def test_get_all_questions(self): request = self.factory.get('/api/v1/questions/') response = views.questions(request) self.assertEqual(response.status_code, 200) self.assertEqual(len(json.loads(response.content)), 2)