[Django] 쿼리스트링 unittest

dhkim·2020년 8월 7일

Django

목록 보기
10/10

전번 쿼리스트링에 대한 유닛테스트를 만들어보았다

from django.test        import TestCase,Client
from django.db.models   import Q
import unittest
from .models            import(
    Photo,
    HashTag,
    PhotoHashTag,
    PhotoCollection,
    BackGroundColor
)
from account.models     import(
    User,
    Like,
    Interest,
    UserInterest,
    Follow,
    Collection
)

class PhotoViewTest_Seccess(TestCase):
    def setUp(self):
        client = Client()
        user = User.objects.create(
            id =1,
            first_name = 'we',
            last_name = 'plash',
            profile_image = 'url',
            user_name = 'weplash',
            email = 'weplash@weplash.com',
            password = '123456',
        )
        bc = BackGroundColor.objects.create(
            id = 1,
            name = '#C0C5CC'
        )
        photo = Photo.objects.create(
            id=1,
            user_id=user.id,
            image = 'url',
            location = 'Zermatt, Schweiz',
            width = 1000,
            height = 667,
            background_color_id = bc.id
        )
        collection = Collection.objects.create(
            id = 1,
            user = user,
            name = 'Nature'
        )
        PhotoCollection.objects.create(
            id = 1,
            photo_id = photo.id,
            collection_id = collection.id
        )
    
    def teardown(self):
        Photo.objects.all().delete()
        User.objects.all().delete()
        BackGroundColor.objects.all().delete()
        Collection.objects.all().delete()
        PhotoCollection.objects.all().delete()

    
    def test_PhotoView_success(self):
        response = self.client.get('/photo?user=weplash&user_category=Nature&offset=0&limit=20')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.json(),
        {'data':[{'height': 667,
                  'id': 1,
                  'image': 'url',
                  'location': 'Zermatt, Schweiz',
                  'user_first_name': 'we',
                  'user_last_name': 'plash',
                  'user_name': 'weplash',
                  'user_profile_image': 'url',
                  'width': 1000}]})

class PhotoViewTest_404_Error(TestCase):
    def setUp(self):
        client = Client()
        user = User.objects.create(
            id =1,
            first_name = 'we',
            last_name = 'plash',
            profile_image = 'url',
            user_name = 'weplash',
            email = 'weplash@weplash.com',
            password = '123456',
        )
        bc = BackGroundColor.objects.create(
            id = 1,
            name = '#C0C5CC'
        )
        photo = Photo.objects.create(
            id=1,
            user_id=user.id,
            image = 'url',
            location = 'Zermatt, Schweiz',
            width = 1000,
            height = 667,
            background_color_id = bc.id
        )
        collection = Collection.objects.create(
            id = 1,
            user = user,
            name = 'Nature'
        )
        PhotoCollection.objects.create(
            id = 1,
            photo_id = photo.id,
            collection_id = collection.id
        )
    
    def teardown(self):
        Photo.objects.all().delete()
        User.objects.all().delete()
        BackGroundColor.objects.all().delete()
        Collection.objects.all().delete()
        PhotoCollection.objects.all().delete()

    
    def test_PhotoView_success(self):
        response = self.client.get('/photouser=weplash&user_category=Nature&offset=0&limit=20')
        self.assertEqual(response.status_code, 404)

class PhotoViewTest_Value_Error(TestCase):
    def setUp(self):
        client = Client()
        user = User.objects.create(
            id =1,
            first_name = 'we',
            last_name = 'plash',
            profile_image = 'url',
            user_name = 'weplash',
            email = 'weplash@weplash.com',
            password = '123456',
        )
        bc = BackGroundColor.objects.create(
            id = 1,
            name = '#C0C5CC'
        )
        photo = Photo.objects.create(
            id=1,
            user_id=user.id,
            image = 'url',
            location = 'Zermatt, Schweiz',
            width = 1000,
            height = 667,
            background_color_id = bc.id
        )
        collection = Collection.objects.create(
            id = 1,
            user = user,
            name = 'Nature'
        )
        PhotoCollection.objects.create(
            id = 1,
            photo_id = photo.id,
            collection_id = collection.id
        )
    
    def teardown(self):
        Photo.objects.all().delete()
        User.objects.all().delete()
        BackGroundColor.objects.all().delete()
        Collection.objects.all().delete()
        PhotoCollection.objects.all().delete()

    
    def test_PhotoView_success(self):
        response = self.client.get('/photo?user=weplash&user_category=Nature&offset=0&limit=20')
        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.json(),
        {'data':[{"message":"VALUE_ERROR"})

class BackgraundView_Seccess(TestCase):
    def setUp(self):
        client = Client()
        user = User.objects.create(
        id =1,
        first_name = 'we',
        last_name = 'plash',
        profile_image = 'url',
        user_name = 'weplasha',
        email = 'weplash@weplash.com',
        password = '123456',
        )
        bc = BackGroundColor.objects.create(
            id = 1,
            name = '#C0C5CC'
        )
        photo = Photo.objects.create(
            id=1,
            user_id=user.id,
            image = 'url',
            location = 'Zermatt, Schweiz',
            width = 1000,
            height = 667,
            background_color_id = bc.id
        )
        collection = Collection.objects.create(
            id = 1,
            user = user,
            name = 'Nature'
        )
        PhotoCollection.objects.create(
            id = 1,
            photo_id = photo.id,
            collection_id = collection.id
        )
    def teardown(self):
        Photo.objects.all().delete()
        User.objects.all().delete()
        BackGroundColor.objects.all().delete()
        Collection.objects.all().delete()
        PhotoCollection.objects.all().delete()

    def test_PhotoView_success(self):
        response = self.client.get('/photo/back/Nature')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.json(),
        {'data': [{'background_color': '#C0C5CC',
                   'height': 667,
                   'id': 1,
                   'width': 1000}]})

class BackgraundView_404_Error(TestCase):
    def setUp(self):
        client = Client()
        user = User.objects.create(
        id =1,
        first_name = 'we',
        last_name = 'plash',
        profile_image = 'url',
        user_name = 'weplasha',
        email = 'weplash@weplash.com',
        password = '123456',
        )
        bc = BackGroundColor.objects.create(
            id = 1,
            name = '#C0C5CC'
        )
        photo = Photo.objects.create(
            id=1,
            user_id=user.id,
            image = 'url',
            location = 'Zermatt, Schweiz',
            width = 1000,
            height = 667,
            background_color_id = bc.id
        )
        collection = Collection.objects.create(
            id = 1,
            user = user,
            name = 'Nature'
        )
        PhotoCollection.objects.create(
            id = 1,
            photo_id = photo.id,
            collection_id = collection.id
        )
    def teardown(self):
        Photo.objects.all().delete()
        User.objects.all().delete()
        BackGroundColor.objects.all().delete()
        Collection.objects.all().delete()
        PhotoCollection.objects.all().delete()

    def test_PhotoView_success(self):
        response = self.client.get('/phot1/back/Nature')
        self.assertEqual(response.status_code, 404)






# Create your tests here.

0개의 댓글