Skip to content

Commit

Permalink
Merge branch 'main' into database
Browse files Browse the repository at this point in the history
  • Loading branch information
dayeonkimm authored Aug 12, 2024
2 parents 32bb6f8 + 56cc71a commit 9498333
Show file tree
Hide file tree
Showing 7 changed files with 442 additions and 7 deletions.
Binary file modified .DS_Store
Binary file not shown.
6 changes: 4 additions & 2 deletions potato_project/githubs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def get_repos(self, username):
else:
return None, response.status_code


# 데이터베이스에 커밋과 날짜를 저장하는 함수
class GitHubDatabaseService:
@staticmethod
Expand Down Expand Up @@ -81,7 +81,7 @@ def get(self, request):
week_ago = today - timedelta(days=7)

# 레포지토리 목록 조회
repos, status_code = github_service.get_repos(user.username)
repos, status_code = github_service.get_repos(user.username)##

if repos is not None:
# 오늘, 7일간 커밋 수 계산
Expand Down Expand Up @@ -157,3 +157,5 @@ def get(self, request):
return JsonResponse(commit_statistics, safe=False)
else:
return Response({"error": "커밋 요청을 실패했습니다."}, status=status_code)


13 changes: 13 additions & 0 deletions potato_project/potato_types/migrations/0004_merge_20240801_0223.py
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 = []
62 changes: 61 additions & 1 deletion potato_project/potatoes/tests.py
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()
76 changes: 75 additions & 1 deletion potato_project/stacks/tests.py
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()
4 changes: 2 additions & 2 deletions potato_project/todos/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def perform_create(self, serializer):
status=status.HTTP_400_BAD_REQUEST,
)

serializer.save(user=self.request.user, date=date)
serializer.save(user=self.request.user, date=date_obj)


# 2. 투두리스트 항목 수정 (UI에서 입력 받은 데이터 + 선택된 날짜로 수정)
Expand All @@ -53,7 +53,7 @@ def perform_update(self, serializer):
status=status.HTTP_400_BAD_REQUEST,
)

serializer.save(date=date)
serializer.save(date=date_obj)


# 3. 투두리스트 항목 삭제
Expand Down
Loading

0 comments on commit 9498333

Please sign in to comment.