-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
442 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
potato_project/potato_types/migrations/0004_merge_20240801_0223.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Generated by Django 5.0.7 on 2024-08-01 02:23 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("potato_types", "0002_add_initial_data"), | ||
("potato_types", "0003_remove_potatotype_potato_image"), | ||
] | ||
|
||
operations = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,63 @@ | ||
import unittest | ||
|
||
from django.test import TestCase | ||
from potato_types.models import PotatoType | ||
from potatoes.models import Potato | ||
from potatoes.serializers import PotatoSerializer | ||
from rest_framework import status | ||
from rest_framework.test import APIClient | ||
from users.models import User | ||
|
||
|
||
class MyPotatoDetailTest(TestCase): | ||
def setUp(self): | ||
self.client = APIClient() | ||
self.user = User.objects.create_user( | ||
username="kingtato", | ||
nickname="kingtato", | ||
password="testpassword", | ||
potato_level=1, | ||
potato_exp=1, | ||
) | ||
self.client.force_authenticate(user=self.user) | ||
|
||
self.potato_type1 = PotatoType.objects.create( | ||
potato_name="Type1", potato_description="Type1 Description" | ||
) | ||
self.potato1 = Potato.objects.create( | ||
user=self.user, | ||
potato_type=self.potato_type1, | ||
is_acquired=True, | ||
is_selected=False, | ||
) | ||
|
||
self.url = "/potatoes/collection/" | ||
|
||
def test_get_my_potato_success(self): | ||
response = self.client.get(self.url) | ||
|
||
# 응답 코드 확인 | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
|
||
# 응답 데이터 확인 | ||
response_data = response.json() | ||
self.assertTrue(len(response_data) > 0) | ||
self.assertEqual(response_data[0]["user"], self.user.id) | ||
self.assertEqual(response_data[0]["potato_type"], self.potato_type1.id) | ||
|
||
def test_get_my_potato_not_found(self): | ||
self.client.force_authenticate(user=None) | ||
response = self.client.get(self.url) | ||
|
||
# 응답 코드 확인 | ||
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) | ||
|
||
# 응답 데이터 확인 | ||
response_data = response.json() | ||
self.assertEqual( | ||
response_data["detail"], "Authentication credentials were not provided." | ||
) | ||
|
||
|
||
# Create your tests here. | ||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,77 @@ | ||
import unittest | ||
from unittest.mock import patch | ||
|
||
from django.core.exceptions import ObjectDoesNotExist | ||
from django.db import DatabaseError | ||
from django.test import TestCase | ||
from rest_framework import status | ||
from rest_framework.exceptions import ValidationError | ||
from rest_framework.test import APIClient | ||
from stacks.models import Stack | ||
from stacks.serializers import StackSerializer | ||
from users.models import User | ||
|
||
|
||
class StackListTest(TestCase): | ||
def setUp(self): | ||
self.client = APIClient() | ||
self.user = User.objects.create_user(username="kingtato", nickname="kingtato") | ||
self.client.force_authenticate(user=self.user) | ||
|
||
self.stack1 = Stack.objects.create(name="Python") | ||
self.stack2 = Stack.objects.create(name="Java") | ||
|
||
self.url = "/stacks/all" | ||
|
||
def test_get_stack_list_success(self): | ||
response = self.client.get(self.url) | ||
|
||
# 응답 코드 확인 | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
|
||
# 응답 데이터 확인 | ||
response_data = response.json() | ||
expected_data = StackSerializer([self.stack1, self.stack2], many=True).data | ||
self.assertEqual(response_data, expected_data) | ||
|
||
def test_get_stack_list_database_error(self): | ||
with patch("stacks.views.Stack.objects.all", side_effect=DatabaseError): | ||
response = self.client.get(self.url) | ||
|
||
# 응답 코드 확인 | ||
self.assertEqual( | ||
response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR | ||
) | ||
|
||
# 응답 데이터 확인 | ||
response_data = response.json() | ||
self.assertEqual(response_data["error"], "DB 에러 발생") | ||
|
||
def test_get_stack_list_serializer_error(self): | ||
with patch( | ||
"stacks.views.StackSerializer", | ||
side_effect=ValidationError("Serializer 에러 발생"), | ||
): | ||
response = self.client.get(self.url) | ||
|
||
# 응답 코드 확인 | ||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
|
||
# 응답 데이터 확인 | ||
response_data = response.json() | ||
self.assertEqual(response_data["error"], "Serializer 에러 발생") | ||
|
||
def test_get_stack_list_object_does_not_exist(self): | ||
with patch("stacks.views.Stack.objects.all", side_effect=ObjectDoesNotExist): | ||
response = self.client.get(self.url) | ||
|
||
# 응답 코드 확인 | ||
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) | ||
|
||
# 응답 데이터 확인 | ||
response_data = response.json() | ||
self.assertEqual(response_data["error"], "object가 존재하지 않습니다") | ||
|
||
|
||
# Create your tests here. | ||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.