|
6 | 6 | # from rest_framework.test import APIClient
|
7 | 7 | #
|
8 | 8 | # from apps.product.models import Product
|
| 9 | +from datetime import timedelta |
| 10 | + |
| 11 | +from django.urls import reverse |
| 12 | +from django.utils import timezone |
| 13 | +from rest_framework import status |
| 14 | +from rest_framework.test import APITestCase |
| 15 | +from rest_framework_simplejwt.tokens import AccessToken |
| 16 | + |
| 17 | +from apps.category.models import Category, Style |
| 18 | +from apps.product.models import Product, RentalHistory |
| 19 | +from apps.user.models import Account |
9 | 20 |
|
10 | 21 | # class ProductListAPITest(TestCase):
|
11 | 22 | # def setUp(self):
|
|
88 | 99 | # self.assertEqual(response.status_code, status.HTTP_200_OK)
|
89 | 100 | # #내가 셋업에 만든 상품이랑 이 뷰에서 조회 하고자 하는 상품의 데이터가 일치 하는 지를 확인해야함
|
90 | 101 | #
|
| 102 | + |
| 103 | + |
| 104 | +class RentalHistoryTestBase(APITestCase): |
| 105 | + def setUp(self) -> None: |
| 106 | + self.borrower = Account.objects.create_user( |
| 107 | + email="[email protected]", nickname="test_borrower", password="password1234@", phone="010-2211-1111" |
| 108 | + ) |
| 109 | + self.lender = Account.objects.create_user( |
| 110 | + email="[email protected]", nickname="test_lender", password="password1234@", phone="010-2211-1112" |
| 111 | + ) |
| 112 | + self.category = Category.objects.create(name="test_category") |
| 113 | + self.styles = Style.objects.create(name="test_style_tag") |
| 114 | + self.product = Product.objects.create( |
| 115 | + name="testproduct", |
| 116 | + lender=self.lender, |
| 117 | + condition="testcondition", |
| 118 | + purchase_date=timezone.now(), |
| 119 | + purchase_price=50000, |
| 120 | + rental_fee=5000, |
| 121 | + size="XL", |
| 122 | + product_category=self.category, |
| 123 | + ) |
| 124 | + self.product.styles.add(self.styles) |
| 125 | + self.token = AccessToken.for_user(self.borrower) |
| 126 | + |
| 127 | + |
| 128 | +class RentalHistoryBorrowerViewTests(RentalHistoryTestBase): |
| 129 | + def setUp(self) -> None: |
| 130 | + super().setUp() |
| 131 | + self.url = reverse("borrowed_rental_history") |
| 132 | + |
| 133 | + def test_대여자가_정상적인_물품_대여신청을_했을때(self) -> None: |
| 134 | + data = { |
| 135 | + "product": self.product.uuid, |
| 136 | + "rental_date": timezone.now(), |
| 137 | + "return_date": timezone.now() + timedelta(days=3), |
| 138 | + } |
| 139 | + response = self.client.post(self.url, data, headers={"Authorization": f"Bearer {self.token}"}) |
| 140 | + self.assertEqual(response.status_code, status.HTTP_201_CREATED) |
| 141 | + self.assertEqual(RentalHistory.objects.count(), 1) |
| 142 | + self.assertEqual(response.data.get("product_info")["uuid"], str(self.product.uuid)) |
| 143 | + self.assertEqual(response.data.get("borrower_nickname"), self.borrower.nickname) |
| 144 | + self.assertEqual(response.data.get("status"), "대여 요청") |
| 145 | + |
| 146 | + def test_이미_대여중인_상품을_대여하려고_시도하는경우_테스트(self) -> None: |
| 147 | + history = RentalHistory.objects.create( |
| 148 | + product=self.product, |
| 149 | + borrower=self.borrower, |
| 150 | + rental_date=timezone.now(), |
| 151 | + return_date=timezone.now() + timedelta(days=3), |
| 152 | + ) |
| 153 | + self.assertEqual(RentalHistory.objects.count(), 1) |
| 154 | + data = { |
| 155 | + "product": history.product.uuid, |
| 156 | + "rental_date": history.rental_date.isoformat(), |
| 157 | + "return_date": history.return_date.isoformat(), # type: ignore |
| 158 | + } |
| 159 | + response = self.client.post(self.url, data, headers={"Authorization": f"Bearer {self.token}"}) |
| 160 | + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) |
| 161 | + |
| 162 | + def test_생성된_대여내역_정상적인_조회_테스트(self) -> None: |
| 163 | + # given |
| 164 | + lender2 = Account.objects.create_user( |
| 165 | + email="[email protected]", nickname="test_lender2", password="password1234@", phone="010-2211-1113" |
| 166 | + ) |
| 167 | + product2 = Product.objects.create( |
| 168 | + name="testproduct2", |
| 169 | + lender=lender2, |
| 170 | + condition="testcondition", |
| 171 | + purchase_date=timezone.now(), |
| 172 | + purchase_price=40000, |
| 173 | + rental_fee=3000, |
| 174 | + size="M", |
| 175 | + product_category=self.category, |
| 176 | + ) |
| 177 | + RentalHistory.objects.create( |
| 178 | + product=self.product, |
| 179 | + borrower=self.borrower, |
| 180 | + rental_date=timezone.now(), |
| 181 | + return_date=timezone.now() + timedelta(days=3), |
| 182 | + ) |
| 183 | + RentalHistory.objects.create( |
| 184 | + product=product2, |
| 185 | + borrower=self.borrower, |
| 186 | + rental_date=timezone.now(), |
| 187 | + return_date=timezone.now() + timedelta(days=3), |
| 188 | + ) |
| 189 | + |
| 190 | + response = self.client.get(self.url, headers={"Authorization": f"Bearer {self.token}"}) |
| 191 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 192 | + self.assertEqual(len(response.data), RentalHistory.objects.count()) |
| 193 | + self.assertEqual(response.data[0]["product_info"]["uuid"], str(self.product.uuid)) |
| 194 | + self.assertEqual(response.data[1]["product_info"]["uuid"], str(product2.uuid)) |
| 195 | + self.assertEqual(response.data[0]["borrower_nickname"], self.borrower.nickname) |
| 196 | + self.assertEqual(response.data[1]["borrower_nickname"], self.borrower.nickname) |
| 197 | + |
| 198 | + |
| 199 | +class RentalHistoryLenderViewTests(RentalHistoryTestBase): |
| 200 | + def setUp(self) -> None: |
| 201 | + super().setUp() |
| 202 | + self.url = reverse("lending_rental_history") |
| 203 | + self.lender2 = Account.objects.create_user( |
| 204 | + email="[email protected]", nickname="test_lender2", password="password1234@", phone="010-2211-1113" |
| 205 | + ) |
| 206 | + self.product2 = Product.objects.create( |
| 207 | + name="testproduct2", |
| 208 | + lender=self.lender2, |
| 209 | + condition="testcondition", |
| 210 | + purchase_date=timezone.now(), |
| 211 | + purchase_price=40000, |
| 212 | + rental_fee=3000, |
| 213 | + size="M", |
| 214 | + product_category=self.category, |
| 215 | + ) |
| 216 | + self.history1 = RentalHistory.objects.create( |
| 217 | + product=self.product, |
| 218 | + borrower=self.borrower, |
| 219 | + rental_date=timezone.now(), |
| 220 | + return_date=timezone.now() + timedelta(days=3), |
| 221 | + ) |
| 222 | + self.history2 = RentalHistory.objects.create( |
| 223 | + product=self.product2, |
| 224 | + borrower=self.borrower, |
| 225 | + rental_date=timezone.now(), |
| 226 | + return_date=timezone.now() + timedelta(days=3), |
| 227 | + ) |
| 228 | + |
| 229 | + def test_lender의_판매내역_조회_테스트(self) -> None: |
| 230 | + self.token = AccessToken.for_user(self.lender) |
| 231 | + |
| 232 | + response = self.client.get(self.url, headers={"Authorization": f"Bearer {self.token}"}) |
| 233 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 234 | + self.assertEqual(response.data[0]["product_info"]["uuid"], str(self.product.uuid)) |
| 235 | + self.assertEqual(response.data[0]["borrower_nickname"], self.borrower.nickname) |
| 236 | + self.assertEqual(response.data[0]["lender_nickname"], self.lender.nickname) |
| 237 | + |
| 238 | + def test_lender2의_판매내역_조회_테스트(self) -> None: |
| 239 | + self.token = AccessToken.for_user(self.lender2) |
| 240 | + |
| 241 | + response = self.client.get(self.url, headers={"Authorization": f"Bearer {self.token}"}) |
| 242 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 243 | + self.assertEqual(response.data[0]["product_info"]["uuid"], str(self.product2.uuid)) |
| 244 | + self.assertEqual(response.data[0]["borrower_nickname"], self.borrower.nickname) |
| 245 | + self.assertEqual(response.data[0]["lender_nickname"], self.lender2.nickname) |
| 246 | + |
| 247 | + |
| 248 | +class RentalHistoryUpdateViewTests(RentalHistoryTestBase): |
| 249 | + def setUp(self) -> None: |
| 250 | + super().setUp() |
| 251 | + self.history = RentalHistory.objects.create( |
| 252 | + product=self.product, |
| 253 | + borrower=self.borrower, |
| 254 | + rental_date=timezone.now(), |
| 255 | + return_date=timezone.now() + timedelta(days=3), |
| 256 | + ) |
| 257 | + self.url = reverse("rental_history_update", kwargs={"pk": self.history.pk}) |
| 258 | + |
| 259 | + def test_대여요청에서_상태_업데이트_테스트(self) -> None: |
| 260 | + data = {"status": "ACCEPT"} |
| 261 | + response = self.client.patch(self.url, data=data, headers={"Authorization": f"Bearer {self.token}"}) |
| 262 | + |
| 263 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 264 | + self.assertEqual(response.data.get("status"), "대여 요청 수락") |
| 265 | + |
| 266 | + def test_대여날짜_변경_테스트(self) -> None: |
| 267 | + data = {"return_date": timezone.now() + timedelta(days=4)} |
| 268 | + |
| 269 | + response = self.client.patch(self.url, data=data, headers={"Authorization": f"Bearer {self.token}"}) |
| 270 | + print(response.data) |
| 271 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 272 | + self.assertEqual(response.data.get("return_date").split("T")[0], data["return_date"].date().isoformat()) |
0 commit comments