API TEST
Base Test code
- python은 똑같은 코드를 반복적으로 입력하려 결과를 얻는 것보다는 캡슐화를 통해 재사용성을 높이는 것을 선호한다.
- 따라서, 인증, data생성 같은 반복 작업에 대해 먼저 캡슐화를 진행한다.
class TodosAPITestCase(APITestCase) :
def create_todo(self) :
sample_todo={'title':"Hello","desc":"Test"}
response = self.client.post(reverse('todos'),sample_todo)
return response
def authenticate(self) :
self.client.post(reverse("register"),{'username':"username",'password':'password','email':'email@gmail.com'})
response=self.client.post(reverse('login'),{'username':"username",'password':'password','email':'email@gmail.com'})
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {response.data['token']}")
ListCreate Test
- ListCreate Veiw에 대해서 Create와 Read의 다양한 경우를 Test한다.
class TestListCreateTodos(TodosAPITestCase) :
def test_should_not_creates_todo_with_no_auth(self) :
response = self.create_todo()
self.assertEqual(response.status_code,status.HTTP_403_FORBIDDEN)
def test_should_create_todo(self) :
previous_todo_count = Todo.objects.all().count()
self.authenticate()
response = self.create_todo()
self.assertEqual(Todo.objects.all().count(),previous_todo_count+1)
self.assertEqual(response.status_code,status.HTTP_201_CREATED)
self.assertEqual(response.data['title'],'Hello')
self.assertEqual(response.data['desc'],'Test')
def test_retrieves_all_todos(self) :
self.authenticate()
response= self.client.get(reverse('todos'))
self.assertEqual(response.status_code,status.HTTP_200_OK)
self.assertIsInstance(response.data['results'],list)
self.create_todo()
res=self.client.get(reverse('todos'))
self.assertIsInstance(res.data['count'],int)
self.assertEqual(res.data['count'],1)
TodoDetail TEST
- Todo 단일 객체에 대해서 Read, Delete, Update를 테스트한다.
class TestTodoDetailAPIView(TodosAPITestCase) :
def test_retrives_one_item(self) :
self.authenticate()
response=self.create_todo()
res = self.client.get(
reverse('todo',kwargs={'id':response.data['id']}))
self.assertEqual(res.status_code,status.HTTP_200_OK)
todo = Todo.objects.get(id=response.data['id'])
self.assertEqual(todo.title,res.data['title'])
def test_updates_one_item(self) :
self.authenticate()
response=self.create_todo()
res =self.client.patch(reverse('todo',
kwargs={'id':response.data['id']}),
{"title":"New one",'is_complete':True})
self.assertEqual(res.status_code,status.HTTP_200_OK)
updated_todo = Todo.objects.get(id=response.data['id'])
self.assertEqual(updated_todo.is_complete,True)
self.assertEqual(updated_todo.title,"New one")
def test_deletes_one_item(self) :
self.authenticate()
res = self.create_todo()
prev_db_count=Todo.objects.all().count()
self.assertGreater(prev_db_count,0)
self.assertEqual(prev_db_count,1)
response = self.client.delete(
reverse("todo",kwargs={'id':res.data['id']})
)
self.assertEqual(response.status_code,status.HTTP_204_NO_CONTENT)
self.assertEqual(Todo.objects.all().count(),0)