<s> import json <EOL> import pytest <EOL> from rest_framework import status <EOL> from rest_framework . reverse import reverse <EOL> from rest_framework . test import APITestCase <EOL> from silver . models import Customer <EOL> from silver . tests . factories import AdminUserFactory , CustomerFactory <EOL> class TestCustomerEndpoints ( APITestCase ) : <EOL> def setUp ( self ) : <EOL> admin_user = AdminUserFactory . create ( ) <EOL> self . client . force_authenticate ( user = admin_user ) <EOL> self . complete_data = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:email>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:state>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : '<STR_LIT>' , <EOL> "<STR_LIT>" : <NUM_LIT:5> , <EOL> "<STR_LIT>" : False , <EOL> "<STR_LIT>" : { '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT:5>' ] } <EOL> } <EOL> def test_create_post_customer ( self ) : <EOL> url = reverse ( '<STR_LIT>' ) <EOL> response = self . client . post ( url , json . dumps ( self . complete_data ) , <EOL> content_type = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , status . HTTP_201_CREATED ) <EOL> def test_create_post_customer_without_required_field ( self ) : <EOL> url = reverse ( '<STR_LIT>' ) <EOL> required_fields = [ '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> for field in required_fields : <EOL> temp_data = self . complete_data . copy ( ) <EOL> try : <EOL> temp_data . pop ( field ) <EOL> except KeyError : <EOL> pytest . xfail ( '<STR_LIT>' <EOL> '<STR_LIT>' % field ) <EOL> response = self . client . post ( url , json . dumps ( temp_data ) , <EOL> content_type = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , status . HTTP_400_BAD_REQUEST ) <EOL> assert ( response . data == { field : [ '<STR_LIT>' ] } <EOL> or response . data == { field : [ '<STR_LIT>' ] } ) <EOL> def test_get_customer_list ( self ) : <EOL> CustomerFactory . create_batch ( <NUM_LIT> ) <EOL> url = reverse ( '<STR_LIT>' ) <EOL> response = self . client . get ( url ) <EOL> full_url = None <EOL> for field in response . data : <EOL> full_url = field . get ( '<STR_LIT:url>' , None ) <EOL> if full_url : <EOL> break <EOL> if full_url : <EOL> domain = full_url . split ( '<STR_LIT:/>' ) [ <NUM_LIT:2> ] <EOL> full_url = full_url . split ( domain ) [ <NUM_LIT:0> ] + domain + url <EOL> assert response . status_code == status . HTTP_200_OK <EOL> assert response . _headers [ '<STR_LIT>' ] == ( '<STR_LIT>' , '<STR_LIT:<>' + full_url + '<STR_LIT>' + <EOL> '<STR_LIT:<>' + full_url + '<STR_LIT>' + <EOL> '<STR_LIT:<>' + full_url + '<STR_LIT>' ) <EOL> response = self . client . get ( url + '<STR_LIT>' ) <EOL> assert response . status_code == status . HTTP_200_OK <EOL> assert response . _headers [ '<STR_LIT>' ] == ( '<STR_LIT>' , '<STR_LIT:<>' + full_url + '<STR_LIT>' + <EOL> '<STR_LIT:<>' + full_url + '<STR_LIT>' + <EOL> '<STR_LIT:<>' + full_url + '<STR_LIT>' ) <EOL> def test_get_customer_detail ( self ) : <EOL> customer = CustomerFactory . create ( ) <EOL> url = reverse ( '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT>' : customer . pk } ) <EOL> response = self . client . get ( url ) <EOL> self . assertEqual ( response . status_code , status . HTTP_200_OK ) <EOL> self . assertNotEqual ( response . data , [ ] ) <EOL> def test_get_customer_detail_unexisting ( self ) : <EOL> url = reverse ( '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT>' : <NUM_LIT> } ) <EOL> response = self . client . get ( url ) <EOL> self . assertEqual ( response . status_code , status . HTTP_404_NOT_FOUND ) <EOL> self . assertEqual ( response . data , { u'<STR_LIT>' : u'<STR_LIT>' } ) <EOL> def test_delete_customer ( self ) : <EOL> customer = CustomerFactory . create ( ) <EOL> url = reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : customer . pk } ) <EOL> response = self . client . delete ( url ) <EOL> assert response . status_code == status . HTTP_204_NO_CONTENT <EOL> assert Customer . objects . all ( ) . count ( ) == <NUM_LIT:0> <EOL> def test_delete_unexisting_customer ( self ) : <EOL> url = reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : <NUM_LIT> } ) <EOL> response = self . client . delete ( url ) <EOL> assert response . status_code == status . HTTP_404_NOT_FOUND <EOL> def test_edit_put_customer ( self ) : <EOL> CustomerFactory . create ( ) <EOL> changed_data = self . complete_data . copy ( ) <EOL> unchanged_fields = [ '<STR_LIT:email>' , '<STR_LIT>' ] <EOL> for field in unchanged_fields : <EOL> changed_data . pop ( field ) <EOL> url = reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> response = self . client . put ( url , data = json . dumps ( changed_data ) , <EOL> content_type = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , status . HTTP_200_OK ) <EOL> for e in [ '<STR_LIT:url>' , '<STR_LIT:id>' , '<STR_LIT>' ] : <EOL> response . data . pop ( e ) <EOL> for field in response . data : <EOL> if field not in unchanged_fields : <EOL> self . assertEqual ( response . data [ field ] , <EOL> self . complete_data [ field ] ) <EOL> def test_edit_patch_customer ( self ) : <EOL> CustomerFactory . create ( ) <EOL> changed_data = self . complete_data . copy ( ) <EOL> unchanged_fields = [ '<STR_LIT:email>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> for field in unchanged_fields : <EOL> changed_data . pop ( field ) <EOL> url = reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> response = self . client . patch ( url , data = json . dumps ( changed_data ) , <EOL> content_type = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , status . HTTP_200_OK ) <EOL> for e in [ '<STR_LIT:url>' , '<STR_LIT:id>' , '<STR_LIT>' ] : <EOL> response . data . pop ( e ) <EOL> for field in response . data : <EOL> if field not in unchanged_fields : <EOL> self . assertEqual ( response . data [ field ] , <EOL> self . complete_data [ field ] ) </s>
