def setUp(self): super(TableResourceTest, 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(configuration='Coffee Table') self.configuration.save() #Strip pillows and make pillows separately table_data = base_product.copy() del table_data['corner_pillow'] del table_data['accent_pillow'] del table_data['back_pillow'] del table_data['lumbar_pillow'] self.product = Table(**table_data) self.product.description = 'AC-1 Coffee Table' self.product.type = 'table' self.product.model = self.model self.product.configuration = self.configuration self.product.save()
def setUp(self): super(ConfigurationResourceTest, 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 self.configuration = Configuration(**base_configuration) self.configuration.save()
class TableResourceTest(APITestCase): def setUp(self): super(TableResourceTest, 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(configuration='Coffee Table') self.configuration.save() #Strip pillows and make pillows separately table_data = base_product.copy() del table_data['corner_pillow'] del table_data['accent_pillow'] del table_data['back_pillow'] del table_data['lumbar_pillow'] self.product = Table(**table_data) self.product.description = 'AC-1 Coffee Table' self.product.type = 'table' 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/table/', format='json') #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 table = resp_obj['results'][0] self.assertEqual(table['id'], 1) self.assertEqual(table['type'], 'table') self.assertEqual(table['description'], 'AC-1 Coffee Table') self.assertEqual(table['model']['id'], 1) self.assertEqual(table['configuration']['id'], 1) self.assertEqual(table['width'], 1000) self.assertEqual(table['depth'], 500) self.assertEqual(table['height'], 400) self.assertEqual(table['manufacture_price'], '50000.00') def test_get(self): """ Test retrieving a resource via GET """ resp = self.client.get('/api/v1/table/1/', format='json') #Validate resp self.assertEqual(resp.status_code, 200) #Validate the first resource table = resp.data self.assertEqual(table['id'], 1) self.assertEqual(table['type'], 'table') self.assertEqual(table['description'], 'AC-1 Coffee Table') self.assertEqual(table['model']['id'], 1) self.assertEqual(table['configuration']['id'], 1) self.assertEqual(table['width'], 1000) self.assertEqual(table['depth'], 500) self.assertEqual(table['height'], 400) self.assertEqual(table['manufacture_price'], '50000.00') def test_post(self): """ Test creating a resource via POST """ #Validate object creation self.assertEqual(Table.objects.count(), 1) resp = self.client.post('/api/v1/table/', format='json', data=base_table) self.assertEqual(Table.objects.count(), 2) #Validate response self.assertEqual(resp.status_code, 201) #Validate the first resource table = resp.data self.assertEqual(table['id'], 2) self.assertEqual(table['type'], 'table') self.assertEqual(table['description'], 'AC-1 Coffee Table') self.assertEqual(table['model']['id'], 1) self.assertEqual(table['configuration']['id'], 1) self.assertEqual(table['width'], 1000) self.assertEqual(table['depth'], 500) self.assertEqual(table['height'], 400) self.assertEqual(table['manufacture_price'], '50000.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_table = base_table.copy() updated_table['wholesale_price'] = 120000 #Validate object update self.assertEqual(Table.objects.count(), 1) resp = self.client.put('/api/v1/table/1/', format='json', data=updated_table, authorization=self.get_credentials()) self.assertEqual(Table.objects.count(), 1) self.assertEqual(resp.status_code, 200) #Validate the first resource table = resp.data self.assertEqual(table['id'], 1) self.assertEqual(table['type'], 'table') self.assertEqual(table['description'], 'AC-1 Coffee Table') self.assertEqual(table['model']['id'], 1) self.assertEqual(table['configuration']['id'], 1) self.assertEqual(table['width'], 1000) self.assertEqual(table['depth'], 500) self.assertEqual(table['height'], 400) self.assertEqual(table['manufacture_price'], '50000.00') def test_delete(self): """ Test deleting a resource via DELETE """ #Validate resource deleted self.assertEqual(Table.objects.count(), 1) resp = self.client.delete('/api/v1/table/1/', format='json', authentication=self.get_credentials()) self.assertEqual(Table.objects.count(), 0) #Validate the response self.assertEqual(resp.status_code, 204)
class ConfigurationResourceTest(APITestCase): def setUp(self): super(ConfigurationResourceTest, 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 self.configuration = Configuration(**base_configuration) self.configuration.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/configuration/', format='json') #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 configuration = resp_obj['results'][0] self.assertEqual(configuration['id'], 1) self.assertEqual(configuration['configuration'], 'Sofa') def test_get(self): """ Test retrieving a resource via GET """ resp = self.client.get('/api/v1/configuration/1/', format='json') #Validate resp self.assertEqual(resp.status_code, 200) #Validate the resource configuration = resp.data self.assertEqual(configuration['id'], 1) self.assertEqual(configuration['configuration'], 'Sofa') def test_post(self): """ Test creating a resource via POST """ #Validate object creation self.assertEqual(Configuration.objects.count(), 1) resp = self.client.post('/api/v1/configuration/', format='json', data=base_configuration, authorization=self.get_credentials()) self.assertEqual(Configuration.objects.count(), 2) #Validate response self.assertEqual(resp.status_code, 201) #Validate the resource configuration = resp.data self.assertEqual(configuration['id'], 2) self.assertEqual(configuration['configuration'], 'Sofa') 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_config = base_configuration.copy() updated_config['configuration'] = 'Chair' #Validate object update self.assertEqual(Configuration.objects.count(), 1) resp = self.client.put('/api/v1/configuration/1/', format='json', data=updated_config, authorization=self.get_credentials()) #Validate resp self.assertEqual(resp.status_code, 200) self.assertEqual(Configuration.objects.count(), 1) def test_delete(self): """ Test deleting a resource via DELETE """ #Validate resource deleted self.assertEqual(Configuration.objects.count(), 1) resp = self.client.delete('/api/v1/configuration/1/', format='json', authentication=self.get_credentials()) self.assertEqual(Configuration.objects.count(), 0) #Validate the response self.assertEqual(resp.status_code, 204)
class TableResourceTest(APITestCase): def setUp(self): super(TableResourceTest, 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(configuration='Coffee Table') self.configuration.save() #Strip pillows and make pillows separately table_data = base_product.copy() del table_data['corner_pillow'] del table_data['accent_pillow'] del table_data['back_pillow'] del table_data['lumbar_pillow'] self.product = Table(**table_data) self.product.description = 'AC-1 Coffee Table' self.product.type = 'table' 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/table/', format='json') #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 table = resp_obj['results'][0] self.assertEqual(table['id'], 1) self.assertEqual(table['type'], 'table') self.assertEqual(table['description'], 'AC-1 Coffee Table') self.assertEqual(table['model']['id'], 1) self.assertEqual(table['configuration']['id'], 1) self.assertEqual(table['width'], 1000) self.assertEqual(table['depth'], 500) self.assertEqual(table['height'], 400) self.assertEqual(table['manufacture_price'], '50000.00') def test_get(self): """ Test retrieving a resource via GET """ resp = self.client.get('/api/v1/table/1/', format='json') #Validate resp self.assertEqual(resp.status_code, 200) #Validate the first resource table = resp.data self.assertEqual(table['id'], 1) self.assertEqual(table['type'], 'table') self.assertEqual(table['description'], 'AC-1 Coffee Table') self.assertEqual(table['model']['id'], 1) self.assertEqual(table['configuration']['id'], 1) self.assertEqual(table['width'], 1000) self.assertEqual(table['depth'], 500) self.assertEqual(table['height'], 400) self.assertEqual(table['manufacture_price'], '50000.00') def test_post(self): """ Test creating a resource via POST """ #Validate object creation self.assertEqual(Table.objects.count(), 1) resp = self.client.post('/api/v1/table/', format='json', data=base_table) self.assertEqual(Table.objects.count(), 2) #Validate response self.assertEqual(resp.status_code, 201) #Validate the first resource table = resp.data self.assertEqual(table['id'], 2) self.assertEqual(table['type'], 'table') self.assertEqual(table['description'], 'AC-1 Coffee Table') self.assertEqual(table['model']['id'], 1) self.assertEqual(table['configuration']['id'], 1) self.assertEqual(table['width'], 1000) self.assertEqual(table['depth'], 500) self.assertEqual(table['height'], 400) self.assertEqual(table['manufacture_price'], '50000.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_table = base_table.copy() updated_table['wholesale_price'] = 120000 #Validate object update self.assertEqual(Table.objects.count(), 1) resp = self.client.put('/api/v1/table/1/', format='json', data=updated_table, authorization=self.get_credentials()) self.assertEqual(Table.objects.count(), 1) self.assertEqual(resp.status_code, 200) #Validate the first resource table = resp.data self.assertEqual(table['id'], 1) self.assertEqual(table['type'], 'table') self.assertEqual(table['description'], 'AC-1 Coffee Table') self.assertEqual(table['model']['id'], 1) self.assertEqual(table['configuration']['id'], 1) self.assertEqual(table['width'], 1000) self.assertEqual(table['depth'], 500) self.assertEqual(table['height'], 400) self.assertEqual(table['manufacture_price'], '50000.00') def test_delete(self): """ Test deleting a resource via DELETE """ #Validate resource deleted self.assertEqual(Table.objects.count(), 1) resp = self.client.delete('/api/v1/table/1/', format='json', authentication=self.get_credentials()) self.assertEqual(Table.objects.count(), 0) #Validate the response self.assertEqual(resp.status_code, 204)
class ConfigurationResourceTest(APITestCase): def setUp(self): super(ConfigurationResourceTest, 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 self.configuration = Configuration(**base_configuration) self.configuration.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/configuration/', format='json') #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 configuration = resp_obj['results'][0] self.assertEqual(configuration['id'], 1) self.assertEqual(configuration['configuration'], 'Sofa') def test_get(self): """ Test retrieving a resource via GET """ resp = self.client.get('/api/v1/configuration/1/', format='json') #Validate resp self.assertEqual(resp.status_code, 200) #Validate the resource configuration = resp.data self.assertEqual(configuration['id'], 1) self.assertEqual(configuration['configuration'], 'Sofa') def test_post(self): """ Test creating a resource via POST """ #Validate object creation self.assertEqual(Configuration.objects.count(), 1) resp = self.client.post('/api/v1/configuration/', format='json', data=base_configuration, authorization=self.get_credentials()) self.assertEqual(Configuration.objects.count(), 2) #Validate response self.assertEqual(resp.status_code, 201) #Validate the resource configuration = resp.data self.assertEqual(configuration['id'], 2) self.assertEqual(configuration['configuration'], 'Sofa') 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_config = base_configuration.copy() updated_config['configuration'] = 'Chair' #Validate object update self.assertEqual(Configuration.objects.count(), 1) resp = self.client.put('/api/v1/configuration/1/', format='json', data=updated_config, authorization=self.get_credentials()) #Validate resp self.assertEqual(resp.status_code, 200) self.assertEqual(Configuration.objects.count(), 1) def test_delete(self): """ Test deleting a resource via DELETE """ #Validate resource deleted self.assertEqual(Configuration.objects.count(), 1) resp = self.client.delete('/api/v1/configuration/1/', format='json', authentication=self.get_credentials()) self.assertEqual(Configuration.objects.count(), 0) #Validate the response self.assertEqual(resp.status_code, 204)