본문 바로가기

drf9

[Django] DRF - 1차 프로젝트 리팩토링 (4) API Testing 2 1. Cart 이번에는 cart api 테스트 코드를 작성했다. cart의 경우 get, post, put, delete의 모든 기능이 있었기 때문에 모든 부분을 진행했고, cart-detail에 관련된 부분은 reverse 내부에서 args를 설정했다. from django.urls import reverse from django.contrib.auth.models import User from rest_framework.test import APITestCase from rest_framework_simplejwt.tokens import RefreshToken from carts.models import Cart from products.models import * class CartTest(API.. 2022. 6. 15.
[Django] DRF - 1차 프로젝트 리팩토링 (4) API Testing API 부분의 리팩토링이 어느 정도 끝난 후 test code를 작성했다. 1. Category & Product 먼저 category와 product의 detail과 list를 get에 관련한 테스트 코드를 작성했다. # category class CategoryTestCase(APITestCase): def setUp(self): self.category = Category.objects.create( category_name = 'test category', main_description = 'test main description', sub_description = 'test sub description' ) def test_category_list_get(self): response = self.. 2022. 6. 14.
[Django] DRF - 1차 프로젝트 리팩토링 (3) Cart 리팩토링을 진행하면서 cart 부분이 가장 시간이 오래 걸렸다. 1차 프로젝트 당시에 cart 부분을 담당하지 않아서 이해하는데 시간이 걸린 것도 있지만, view 또는 serializer 둘 중 어디서 create 또는 post를 적용시켜야 할지 오랜 고민을 하기도 했다. 몇 번의 시도 끝에 3일 만에 기능을 구현할 수 있었다. 1. CreateAPIView 활용 처음에는 generic views를 사용해서 구현했다. # serializer class CartSerializer(serializers.ModelSerializer): user = serializers.StringRelatedField(read_only=True) product = serializers.StringRelatedField(re.. 2022. 6. 9.
[Django] DRF - 1차 프로젝트 리팩토링 (2) Category detail & list GET 1. Category List GET product GET과 유사하지만, 기존 코드에 limit, offset이 걸려 있어서 이번엔 pagination.py 파일을 따로 만들어 offset limit을 설정했다. # pagination from rest_framework.pagination import LimitOffsetPagination class CategoryLimitOffsetPagination(LimitOffsetPagination): default_limit = 5 # serializer class CategorySerializer(serializers.ModelSerializer): class Meta: model = Category fields = '__all__' # view class.. 2022. 6. 7.