-
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.
Merge pull request #66 from OZ-Coding-School/feat/database_edit
Feat/database edit
- Loading branch information
Showing
19 changed files
with
287 additions
and
100 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
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,49 +1,65 @@ | ||
from datetime import date | ||
|
||
from attendances.models import Attendance | ||
from django.contrib.auth.models import User | ||
from django.test import TestCase | ||
from django.utils import timezone | ||
from rest_framework import status | ||
from rest_framework.test import APIClient | ||
from users.models import User | ||
|
||
from .models import Attendance | ||
|
||
class AttendanceViewSetTests(TestCase): | ||
|
||
class AttendanceViewSetTests(TestCase): | ||
def setUp(self): | ||
# 테스트에 사용할 사용자 생성 | ||
self.user = User.objects.create_user( | ||
username='testuser', | ||
password='password', | ||
email='testuser@example.com' | ||
username="testuser", password="testpassword" | ||
) | ||
self.client = APIClient() | ||
self.client.login(username="testuser", password="testpassword") | ||
|
||
def test_list_attendance_no_record(self): | ||
# 출석 기록이 없는 경우 | ||
response = self.client.get("/attendances/") | ||
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) | ||
self.assertEqual(response.data, {"출석 기록이 없습니다."}) | ||
|
||
def test_increment_attendance(self): | ||
# 출석 기록을 추가 | ||
response = self.client.post("/attendances/increment/") | ||
self.assertEqual(response.status_code, status.HTTP_201_CREATED) | ||
self.assertEqual(response.data["coin_awarded"], 1) | ||
|
||
def test_increment_attendance_already_exists(self): | ||
# 출석 기록이 이미 있는 경우 | ||
today = timezone.now().date() | ||
Attendance.objects.create(user=self.user, date=today, coin_awarded=1) | ||
|
||
response = self.client.post("/attendances/increment/") | ||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
self.assertEqual(response.data, {"오늘은 출석을 이미 하셨어요!"}) | ||
|
||
def test_decrement_attendance_no_record(self): | ||
# 출석 기록이 없는 경우 코인 차감 시도 | ||
response = self.client.post("/attendances/decrement/", {"value": 1}) | ||
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) | ||
self.assertEqual(response.data, {"message": "출석 기록이 없습니다."}) | ||
|
||
def test_decrement_attendance_insufficient_coins(self): | ||
# 출석 기록이 있지만 코인이 부족한 경우 | ||
Attendance.objects.create( | ||
user=self.user, date=timezone.now().date(), coin_awarded=1 | ||
) | ||
|
||
response = self.client.post("/attendances/decrement/", {"value": 2}) | ||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
self.assertEqual(response.data, {"message": "코인이 모자라요!"}) | ||
|
||
def test_decrement_attendance_success(self): | ||
# 출석 기록이 있고 충분한 코인이 있는 경우 | ||
Attendance.objects.create( | ||
user=self.user, date=timezone.now().date(), coin_awarded=5 | ||
) | ||
|
||
|
||
def test_create_attendance(self): | ||
# 출석 기록 생성 | ||
attendance = Attendance.objects.create(user=self.user, date=date.today(), coin_awarded=10) | ||
self.assertEqual(attendance.user, self.user) | ||
self.assertEqual(attendance.date, date.today()) | ||
self.assertEqual(attendance.coin_awarded, 10) | ||
|
||
def test_read_attendance(self): | ||
# 출석 기록 생성 | ||
Attendance.objects.create(user=self.user, date=date.today(), coin_awarded=10) | ||
attendance = Attendance.objects.get(user=self.user, date=date.today()) | ||
self.assertEqual(attendance.user.username, 'testuser') | ||
self.assertEqual(attendance.coin_awarded, 10) | ||
|
||
def test_update_attendance(self): | ||
# 출석 기록 생성 및 수정 | ||
attendance = Attendance.objects.create(user=self.user, date=date.today(), coin_awarded=10) | ||
attendance.coin_awarded = 20 | ||
attendance.save() | ||
updated_attendance = Attendance.objects.get(user=self.user, date=date.today()) | ||
self.assertEqual(updated_attendance.coin_awarded, 20) | ||
|
||
def test_delete_attendance(self): | ||
# 출석 기록 생성 및 삭제 | ||
attendance = Attendance.objects.create(user=self.user, date=date.today(), coin_awarded=10) | ||
attendance_id = attendance.id | ||
attendance.delete() | ||
with self.assertRaises(Attendance.DoesNotExist): | ||
Attendance.objects.get(id=attendance_id) | ||
response = self.client.post("/attendances/decrement/", {"value": 2}) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(response.data["message"], "물건을 구매했습니다.") | ||
self.assertEqual(response.data["coin_awarded"], 3) |
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
17 changes: 17 additions & 0 deletions
17
potato_project/potato_types/migrations/0002_remove_potatotype_potato_image.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,17 @@ | ||
# Generated by Django 5.0.7 on 2024-07-29 16:12 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("potato_types", "0001_initial"), | ||
] | ||
|
||
operations = [ | ||
migrations.RemoveField( | ||
model_name="potatotype", | ||
name="potato_image", | ||
), | ||
] |
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
18 changes: 18 additions & 0 deletions
18
potato_project/potatoes/migrations/0003_rename_potato_type_id_potato_potato_type.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,18 @@ | ||
# Generated by Django 5.0.7 on 2024-07-29 14:59 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("potatoes", "0002_initial"), | ||
] | ||
|
||
operations = [ | ||
migrations.RenameField( | ||
model_name="potato", | ||
old_name="potato_type_id", | ||
new_name="potato_type", | ||
), | ||
] |
Oops, something went wrong.