-
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.
feat(calendar): add tests about retrievals
- Loading branch information
Showing
2 changed files
with
35 additions
and
59 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
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,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) |