def setUp(self): super(UpholsteryResourceTest, self).setUp() #Create the user self.username = '******' self.password = '******' self.user = User.objects.create_user(self.username, '*****@*****.**', self.password) #Create a model to be used for testing model_data = base_model.copy() del model_data['images'] self.model = Model(**model_data) self.model.save() #Create configuration for testing self.configuration = Configuration(**base_configuration) self.configuration.save() #Strip pillows and make pillows separately uphol_data = base_product.copy() del uphol_data['corner_pillow'] del uphol_data['accent_pillow'] del uphol_data['back_pillow'] del uphol_data['lumbar_pillow'] self.product = Upholstery(**uphol_data) self.product.description = 'AC-1 Sofa' self.product.model = self.model self.product.configuration = self.configuration self.product.save()
class UpholsteryResourceTest(APITestCase): def setUp(self): super(UpholsteryResourceTest, self).setUp() #Create the user self.username = '******' self.password = '******' self.user = User.objects.create_user(self.username, '*****@*****.**', self.password) #Create a model to be used for testing model_data = base_model.copy() del model_data['images'] self.model = Model(**model_data) self.model.save() #Create configuration for testing self.configuration = Configuration(**base_configuration) self.configuration.save() #Strip pillows and make pillows separately uphol_data = base_product.copy() del uphol_data['corner_pillow'] del uphol_data['accent_pillow'] del uphol_data['back_pillow'] del uphol_data['lumbar_pillow'] self.product = Upholstery(**uphol_data) self.product.description = 'AC-1 Sofa' self.product.model = self.model self.product.configuration = self.configuration self.product.save() def get_credentials(self): return None #self.create_basic(username=self.username, password=self.password) def test_get_list(self): """ Test getting a list of models via GET """ resp = self.client.get('/api/v1/upholstery/') #Validate resp self.assertEqual(resp.status_code, 200) #Validate data resp_obj = resp.data self.assertEqual(len(resp_obj['results']), 1) #Validate the first resource upholstery = resp_obj['results'][0] self.assertEqual(upholstery['id'], 1) self.assertEqual(upholstery['type'], 'upholstery') self.assertEqual(upholstery['description'], 'AC-1 Sofa') self.assertEqual(upholstery['model']['id'], 1) self.assertEqual(upholstery['configuration']['id'], 1) self.assertEqual(upholstery['width'], 1000) self.assertEqual(upholstery['depth'], 500) self.assertEqual(upholstery['height'], 400) self.assertEqual(upholstery['manufacture_price'], '50000.00') self.assertEqual(upholstery['price'], '250000.00') #self.assertEqual(upholstery['configuration']['id'], 1) def test_get(self): """ Test retrieving a resource via GET """ resp = self.client.get('/api/v1/upholstery/1/') #Validate resp self.assertEqual(resp.status_code, 200) #Validate the resource configuration = resp.data self.assertEqual(configuration['id'], 1) #Validate the first resource upholstery = resp.data self.assertEqual(upholstery['id'], 1) self.assertEqual(upholstery['type'], 'upholstery') self.assertEqual(upholstery['description'], 'AC-1 Sofa') self.assertEqual(upholstery['model']['id'], 1) self.assertEqual(upholstery['configuration']['id'], 1) self.assertEqual(upholstery['width'], 1000) self.assertEqual(upholstery['depth'], 500) self.assertEqual(upholstery['height'], 400) self.assertEqual(upholstery['manufacture_price'], '50000.00') self.assertEqual(upholstery['price'], '250000.00') def test_post(self): """ Test creating a resource via POST """ #Validate object creation self.assertEqual(Upholstery.objects.count(), 1) resp = self.client.post('/api/v1/upholstery/', format='json', data=base_upholstery) logger.debug(resp) self.assertEqual(Upholstery.objects.count(), 2) #Validate response self.assertEqual(resp.status_code, 201) #Validate the first resource upholstery = resp.data self.assertEqual(upholstery['id'], 2) self.assertEqual(upholstery['type'], 'upholstery') self.assertEqual(upholstery['description'], 'AC-1 Sofa') self.assertEqual(upholstery['model']['id'], 1) self.assertEqual(upholstery['configuration']['id'], 1) self.assertEqual(upholstery['width'], 1000) self.assertEqual(upholstery['depth'], 500) self.assertEqual(upholstery['height'], 400) self.assertEqual(upholstery['manufacture_price'], '50000.00') self.assertEqual(upholstery['price'], '250000.00') def test_put(self): """ Test updating a resource via POST The first part of the test will validate that an object is neither created or deleted """ #Update data updated_uphol = base_upholstery.copy() updated_uphol['price'] = 350000 #Validate object update self.assertEqual(Upholstery.objects.count(), 1) resp = self.client.put('/api/v1/upholstery/1/', format='json', data=updated_uphol, authorization=self.get_credentials()) self.assertEqual(Upholstery.objects.count(), 1) self.assertEqual(resp.status_code, 200) #Validate the first resource upholstery = resp.data self.assertEqual(upholstery['id'], 1) self.assertEqual(upholstery['type'], 'upholstery') self.assertEqual(upholstery['description'], 'AC-1 Sofa') self.assertEqual(upholstery['model']['id'], 1) self.assertEqual(upholstery['configuration']['id'], 1) self.assertEqual(upholstery['width'], 1000) self.assertEqual(upholstery['depth'], 500) self.assertEqual(upholstery['height'], 400) self.assertEqual(upholstery['manufacture_price'], '50000.00') self.assertEqual(upholstery['price'], '350000.00') def test_delete(self): """ Test deleting a resource via DELETE """ #Validate resource deleted self.assertEqual(Upholstery.objects.count(), 1) resp = self.client.delete('/api/v1/upholstery/1/', format='json', authentication=self.get_credentials()) self.assertEqual(Upholstery.objects.count(), 0) #Validate the response self.assertEqual(resp.status_code, 204)
class UpholsteryResourceTest(APITestCase): def setUp(self): super(UpholsteryResourceTest, self).setUp() #Create the user self.username = '******' self.password = '******' self.user = User.objects.create_user(self.username, '*****@*****.**', self.password) #Create a model to be used for testing model_data = base_model.copy() del model_data['images'] self.model = Model(**model_data) self.model.save() #Create configuration for testing self.configuration = Configuration(**base_configuration) self.configuration.save() #Strip pillows and make pillows separately uphol_data = base_product.copy() del uphol_data['corner_pillow'] del uphol_data['accent_pillow'] del uphol_data['back_pillow'] del uphol_data['lumbar_pillow'] self.product = Upholstery(**uphol_data) self.product.description = 'AC-1 Sofa' self.product.model = self.model self.product.configuration = self.configuration self.product.save() def get_credentials(self): return None#self.create_basic(username=self.username, password=self.password) def test_get_list(self): """ Test getting a list of models via GET """ resp = self.client.get('/api/v1/upholstery/') #Validate resp self.assertEqual(resp.status_code, 200) #Validate data resp_obj = resp.data self.assertEqual(len(resp_obj['results']), 1) #Validate the first resource upholstery = resp_obj['results'][0] self.assertEqual(upholstery['id'], 1) self.assertEqual(upholstery['type'], 'upholstery') self.assertEqual(upholstery['description'], 'AC-1 Sofa') self.assertEqual(upholstery['model']['id'], 1) self.assertEqual(upholstery['configuration']['id'], 1) self.assertEqual(upholstery['width'], 1000) self.assertEqual(upholstery['depth'], 500) self.assertEqual(upholstery['height'], 400) self.assertEqual(upholstery['manufacture_price'], '50000.00') self.assertEqual(upholstery['price'], '250000.00') #self.assertEqual(upholstery['configuration']['id'], 1) def test_get(self): """ Test retrieving a resource via GET """ resp = self.client.get('/api/v1/upholstery/1/') #Validate resp self.assertEqual(resp.status_code, 200) #Validate the resource configuration = resp.data self.assertEqual(configuration['id'], 1) #Validate the first resource upholstery = resp.data self.assertEqual(upholstery['id'], 1) self.assertEqual(upholstery['type'], 'upholstery') self.assertEqual(upholstery['description'], 'AC-1 Sofa') self.assertEqual(upholstery['model']['id'], 1) self.assertEqual(upholstery['configuration']['id'], 1) self.assertEqual(upholstery['width'], 1000) self.assertEqual(upholstery['depth'], 500) self.assertEqual(upholstery['height'], 400) self.assertEqual(upholstery['manufacture_price'], '50000.00') self.assertEqual(upholstery['price'], '250000.00') def test_post(self): """ Test creating a resource via POST """ #Validate object creation self.assertEqual(Upholstery.objects.count(), 1) resp = self.client.post('/api/v1/upholstery/', format='json', data=base_upholstery) logger.debug(resp) self.assertEqual(Upholstery.objects.count(), 2) #Validate response self.assertEqual(resp.status_code, 201) #Validate the first resource upholstery = resp.data self.assertEqual(upholstery['id'], 2) self.assertEqual(upholstery['type'], 'upholstery') self.assertEqual(upholstery['description'], 'AC-1 Sofa') self.assertEqual(upholstery['model']['id'], 1) self.assertEqual(upholstery['configuration']['id'], 1) self.assertEqual(upholstery['width'], 1000) self.assertEqual(upholstery['depth'], 500) self.assertEqual(upholstery['height'], 400) self.assertEqual(upholstery['manufacture_price'], '50000.00') self.assertEqual(upholstery['price'], '250000.00') def test_put(self): """ Test updating a resource via POST The first part of the test will validate that an object is neither created or deleted """ #Update data updated_uphol = base_upholstery.copy() updated_uphol['price'] = 350000 #Validate object update self.assertEqual(Upholstery.objects.count(), 1) resp = self.client.put('/api/v1/upholstery/1/', format='json', data=updated_uphol, authorization=self.get_credentials()) self.assertEqual(Upholstery.objects.count(), 1) self.assertEqual(resp.status_code, 200) #Validate the first resource upholstery = resp.data self.assertEqual(upholstery['id'], 1) self.assertEqual(upholstery['type'], 'upholstery') self.assertEqual(upholstery['description'], 'AC-1 Sofa') self.assertEqual(upholstery['model']['id'], 1) self.assertEqual(upholstery['configuration']['id'], 1) self.assertEqual(upholstery['width'], 1000) self.assertEqual(upholstery['depth'], 500) self.assertEqual(upholstery['height'], 400) self.assertEqual(upholstery['manufacture_price'], '50000.00') self.assertEqual(upholstery['price'], '350000.00') def test_delete(self): """ Test deleting a resource via DELETE """ #Validate resource deleted self.assertEqual(Upholstery.objects.count(), 1) resp = self.client.delete('/api/v1/upholstery/1/', format='json', authentication=self.get_credentials()) self.assertEqual(Upholstery.objects.count(), 0) #Validate the response self.assertEqual(resp.status_code, 204)