Skip to content

Commit

Permalink
feat(calendar): add tests about retrievals
Browse files Browse the repository at this point in the history
  • Loading branch information
injoonH committed Jan 3, 2024
1 parent 371b6e9 commit 1a41c8a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 59 deletions.
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def set_admin_client(request):
agree_terms_of_service_at=timezone.now(),
)
client = APIClient()
client.force_authenticate(user=request.cls.user)
client.force_authenticate(user=request.cls.admin)
request.cls.api_client = client


Expand Down
92 changes: 34 additions & 58 deletions tests/test_calendar.py
Original file line number Diff line number Diff line change
@@ -1,71 +1,47 @@
import pytest
from dateutil import parser
from django.utils import timezone
from rest_framework import status

from apps.calendar.models import Calendar
from tests.conftest import RequestSetting, TestCase, Utils
from apps.calendar.models import Event, Tag
from tests.conftest import RequestSetting, TestCase


@pytest.fixture(scope="class", autouse=True)
def set_calendar(request):
request.cls.calendar = Calendar.objects.create(
is_allday=False,
start_at=timezone.now(),
end_at=timezone.now() + timezone.timedelta(hours=1),
ko_title="테스트 캘린더",
en_title="Test Calendar",
def set_event(request):
now = timezone.now()
request.cls.event = Event.objects.create(
is_all_day=False,
start_at=now,
end_at=now + timezone.timedelta(hours=1),
ko_title="한글 제목",
en_title="English Title",
ko_description="한글 설명",
en_description="English Description",
location="테스트 위치",
location="장소",
url="http://example.com/test",
)
request.cls.event.tags.add(
Tag.objects.get_or_create(ko_name="아라", en_name="Ara", color="#ED3A3A")[0],
Tag.objects.get_or_create(ko_name="스팍스", en_name="SPARCS", color="#EBA12A")[0],
)


@pytest.mark.usefixtures("set_user_client", "set_event")
class TestCalendar(TestCase, RequestSetting):
# 모델의 생성 및 필드 확인
def test_calendar_creation(self):
assert self.calendar.is_allday == False
assert self.calendar.start_at < self.calendar.end_at
assert self.calendar.ko_title == "테스트 캘린더"
assert self.calendar.en_title == "Test Calendar"
assert self.calendar.ko_description == "한글 설명"
assert self.calendar.en_description == "English Description"
assert self.calendar.location == "테스트 위치"
assert self.calendar.url == "http://example.com/test"


class TestCalendarAPI(TestCase, RequestSetting, Utils):
def test_create_calendar(self):
tags_data = [
{"name": "Tag1", "color": "#FF0000"},
{"name": "Tag2", "color": "#00FF00"},
]

data = {
"is_allday": False,
"start_at": timezone.now(),
"end_at": timezone.now() + timezone.timedelta(hours=1),
"ko_title": "테스트 캘린더",
"en_title": "Test Calendar",
"ko_description": "한글 설명",
"en_description": "English Description",
"location": "테스트 위치",
"url": "http://example.com/test",
"tags": tags_data,
}
response = self.http_request(self.admin, "post", "calendars/", data=data)
assert response.status_code == 201
assert Calendar.objects.filter(ko_title="테스트 캘린더").exists()

calendar = Calendar.objects.get(ko_title="테스트 캘린더")
assert calendar.tags.count() == len(tags_data)

for tag_data in tags_data:
assert calendar.tags.filter(name=tag_data["name"]).exists()

def test_get_calendar_list(self):
self.create_calendar("Calendar 1")
self.create_calendar("Calendar 2")

response = self.http_request(self.admin, "get", "calendars/")
assert response.status_code == 200
assert len(response.data) == 2
def test_list_count(self) -> None:
res = self.http_request(self.user, "get", "calendar/events")
assert res.status_code == status.HTTP_200_OK
assert res.data.get("num_items") == Event.objects.count()

def test_get(self) -> None:
res = self.http_request(self.user, "get", f"calendar/events/{self.event.id}")
assert res.status_code == status.HTTP_200_OK

for el in res.data:
if el == "tags":
continue
elif el == "start_at" or el == "end_at":
assert parser.parse(res.data.get(el)) == getattr(self.event, el)
else:
assert res.data.get(el) == getattr(self.event, el)

0 comments on commit 1a41c8a

Please sign in to comment.