def setUp(self): """ Sets up environment for tests """ super(TestItemResource, self).setUp() self.create_user() self.client.login(username='******', password='******') #Create supplier, customer and addrss self.customer = Customer(**base_customer) self.customer.save() self.supplier = Supplier(**base_supplier) self.supplier.save() self.address = Address(address1="Jiggle", contact=self.customer) self.address.save() #Create a product to add self.product = Product.create(self.user, **base_product) self.product.save() self.fabric = Fabric.create(**base_fabric) f_data = base_fabric.copy() f_data["pattern"] = "Stripe" self.fabric2 = Fabric.create(**f_data) #Create acknowledgement ack_data = base_ack.copy() del ack_data['customer'] del ack_data['items'] del ack_data['employee'] del ack_data['project'] self.ack = Estimate(**ack_data) self.ack.customer = self.customer self.ack.employee = self.user self.ack.save() #Create an item self.item_data = { 'id': 1, 'quantity': 1, 'is_custom_size': True, 'width': 1500, "fabric": { "id": 1 } } self.item = Item.create(acknowledgement=self.ack, **self.item_data)
def setUp(self): super(CustomerResourceTest, self).setUp() #Create the user self.username = '******' self.password = '******' self.user = User.objects.create_user(self.username, '*****@*****.**', self.password) self.client.force_authenticate(self.user) self.customer = Customer(**base_contact) self.customer.is_customer = True self.customer.save() self.address = Address(**base_address) self.address.contact = self.customer self.address.save()
def sync_customers(request): service = Customer.get_google_contacts_service(request.user) query = gdata.contacts.client.ContactsQuery() query.max_results = 10000 feed = gd_client.GetContacts(q = query) print len(feed.entry)
def setUp(self): """ Sets up environment for tests """ super(TestItemResource, self).setUp() self.create_user() self.client.login(username='******', password='******') #Create supplier, customer and addrss self.customer = Customer(**base_customer) self.customer.save() self.supplier = Supplier(**base_supplier) self.supplier.save() self.address = Address(address1="Jiggle", contact=self.customer) self.address.save() #Create a product to add self.product = Product.create(self.user, **base_product) self.product.save() self.fabric = Fabric.create(**base_fabric) f_data = base_fabric.copy() f_data["pattern"] = "Stripe" self.fabric2 = Fabric.create(**f_data) #Create acknowledgement ack_data = base_ack.copy() del ack_data['customer'] del ack_data['items'] del ack_data['employee'] del ack_data['project'] self.ack = Estimate(**ack_data) self.ack.customer = self.customer self.ack.employee = self.user self.ack.save() #Create an item self.item_data = {'id': 1, 'quantity': 1, 'is_custom_size': True, 'width': 1500, "fabric": {"id":1}} self.item = Item.create(acknowledgement=self.ack, **self.item_data)
def setUp(self): """ Set up for the Estimate Test Objects created: -User -Customer -Supplier -Address -product -2 fabrics After Creating all the needed objects for the Estimate, test that all the objects have been made. """ super(EstimateResourceTest, self).setUp() # Set Base URL self.base_url = '{0}'.format('/api/v1/estimate/') self.ct = ContentType(app_label="estimates") self.ct.save() #Create the user self.username = '******' self.password = '******' self.user = User.objects.create_user(self.username, '*****@*****.**', self.password) p = Permission(content_type=self.ct, codename="change_estimate") p.save() p2 = Permission(content_type=self.ct, codename="add_estimate") p2.save() self.user.user_permissions.add(p) self.user.user_permissions.add(p2) self.user.save() self.setup_client() #Create supplier, customer and addrss customer = copy.deepcopy(base_customer) del customer['id'] self.customer = Customer(**customer) self.customer.save() self.supplier = Supplier(**base_supplier) self.supplier.save() self.address = Address(address1="Jiggle", contact=self.customer) self.address.save() #Create a product to add self.product = Product.create(self.user, **base_product) self.product.save() #Create custom product self.custom_product = Product() self.custom_product.id = 10436 self.custom_product.save() self.fabric = Fabric.create(**base_fabric) self.fabric.quantity = 26 self.fabric.save() f_data = base_fabric.copy() f_data["pattern"] = "Stripe" self.fabric2 = Fabric.create(**f_data) #Create custom product self.custom_product = Product.create(self.user, description="Custom Custom", id=10436, width=0, depth=0, height=0, price=0, wholesale_price=0, retail_price=0) self.custom_product.id = 10436 self.custom_product.save() self.image = S3Object(key='test', bucket='test') self.image.save() #Create acknowledgement ack_data = base_ack.copy() del ack_data['customer'] del ack_data['items'] del ack_data['employee'] del ack_data['project'] self.ack = Estimate(**ack_data) self.ack.customer = self.customer self.ack.employee = self.user self.ack.save() #Create an item item_data = { 'id': 1, 'quantity': 1, 'is_custom_size': True, 'width': 1500, "fabric": { "id": 1 } } self.item = Item.create(estimate=self.ack, **item_data) item_data = { 'is_custom': True, 'description': 'F-04 Sofa', 'quantity': 3 } self.item2 = Item.create(estimate=self.ack, **item_data) #Create fake S3Objects to test files attached to acknowledgements self.file1 = S3Object(key='test1', bucket='test') self.file2 = S3Object(key='test2', bucket='test') self.file1.save() self.file2.save()
class EstimateResourceTest(APITestCase): """" This tests the api acknowledgements: GET list: -get a list of objects -objects have items and items have pillows GET: -the acknowledgement has delivery date, order date customer, status, grand_total, vat, employee, discount -the acknowledgement has a list of items. -The items have pillows and fabrics -pillows have fabrics -the items has dimensions, pillows, fabric, comments price per item POST: -create an acknowledgement that has delivery date, order date customer, status, grand_total, vat, employee, discount, items -the items should have fabrics and pillows where appropriate """ def setUp(self): """ Set up for the Estimate Test Objects created: -User -Customer -Supplier -Address -product -2 fabrics After Creating all the needed objects for the Estimate, test that all the objects have been made. """ super(EstimateResourceTest, self).setUp() # Set Base URL self.base_url = '{0}'.format('/api/v1/estimate/') self.ct = ContentType(app_label="estimates") self.ct.save() #Create the user self.username = '******' self.password = '******' self.user = User.objects.create_user(self.username, '*****@*****.**', self.password) p = Permission(content_type=self.ct, codename="change_estimate") p.save() p2 = Permission(content_type=self.ct, codename="add_estimate") p2.save() self.user.user_permissions.add(p) self.user.user_permissions.add(p2) self.user.save() self.setup_client() #Create supplier, customer and addrss customer = copy.deepcopy(base_customer) del customer['id'] self.customer = Customer(**customer) self.customer.save() self.supplier = Supplier(**base_supplier) self.supplier.save() self.address = Address(address1="Jiggle", contact=self.customer) self.address.save() #Create a product to add self.product = Product.create(self.user, **base_product) self.product.save() #Create custom product self.custom_product = Product() self.custom_product.id = 10436 self.custom_product.save() self.fabric = Fabric.create(**base_fabric) self.fabric.quantity = 26 self.fabric.save() f_data = base_fabric.copy() f_data["pattern"] = "Stripe" self.fabric2 = Fabric.create(**f_data) #Create custom product self.custom_product = Product.create(self.user, description="Custom Custom", id=10436, width=0, depth=0, height=0, price=0, wholesale_price=0, retail_price=0) self.custom_product.id = 10436 self.custom_product.save() self.image = S3Object(key='test', bucket='test') self.image.save() #Create acknowledgement ack_data = base_ack.copy() del ack_data['customer'] del ack_data['items'] del ack_data['employee'] del ack_data['project'] self.ack = Estimate(**ack_data) self.ack.customer = self.customer self.ack.employee = self.user self.ack.save() #Create an item item_data = { 'id': 1, 'quantity': 1, 'is_custom_size': True, 'width': 1500, "fabric": { "id": 1 } } self.item = Item.create(estimate=self.ack, **item_data) item_data = { 'is_custom': True, 'description': 'F-04 Sofa', 'quantity': 3 } self.item2 = Item.create(estimate=self.ack, **item_data) #Create fake S3Objects to test files attached to acknowledgements self.file1 = S3Object(key='test1', bucket='test') self.file2 = S3Object(key='test2', bucket='test') self.file1.save() self.file2.save() def setup_client(self): # Login the Client # APIClient #self.client = APIClient(enforce_csrf_checks=False) #self.client.login(username=self.username, password=self.password) self.client.force_authenticate(self.user) # RequestsClient """ logger.debug(self) self.client = RequestsClient() self.client.auth = HTTPBasicAuth(self.username, self.password) logger.debug(self.client.__dict__) # Obtain a CSRF token. response = self.client.post("{0}{1}".format(self.live_server_url,'/login'), data={'username': self.username, 'password': self.password}) logger.debug(response.cookies['csrftoken']) assert response.status_code == 200 csrftoken = response.cookies['csrftoken'] self.client.headers.update({'X-CSRFTOKEN': csrftoken}) logger.debug(self.client.__dict__) """ def get_credentials(self): return self.user def open_url(self, url): pass #webbrowser.open(url) def test_get_list(self): """ Tests getting the list of acknowledgements """ #Get and verify the resp resp = self.client.get(self.base_url) self.assertEqual(resp.status_code, 200, msg=resp) logger.debug(resp) #Verify the data sent quotations = resp.data self.assertIsNotNone(quotations) self.assertEqual(len(quotations), 1) self.assertEqual(len(quotations[0]['items']), 2) def test_get(self): """ Tests getting the acknowledgement """ #Get and verify the resp resp = self.client.get("{0}1/".format(self.base_url)) self.assertEqual(resp.status_code, 200, msg=resp) #Verify the data sent ack = resp.data self.assertIsNotNone(ack) self.assertEqual(ack['id'], 1) self.assertEqual(ack['customer']['id'], 1) self.assertEqual(dateutil.parser.parse(ack['delivery_date']), base_delivery_date) self.assertEqual(Decimal(ack['vat']), 0) self.assertEqual(Decimal(ack['grand_total']), Decimal(0)) def xtest_post_dr_vs_pci(self): """ Test POSTING ack with company as 'Dellarobbia Thailand' vs 'Pacific Carpet' """ logger.debug( "\n\n Testing creating acknowledgement with diferring companies\n") ack1_data = copy.deepcopy(base_ack) ack1_data['company'] = 'Dellarobbia Thailand' ack def test_post_with_discount_on_data(self): """ Testing POSTing data to the api """ logger.debug( "\n\n Testing creating acknowledgement with a discount \n") modified_data = copy.deepcopy(base_ack) modified_data['discount'] = 50 modified_data['items'][-1]['fabric'] = {'id': 1, 'image': {'id': 1}} modified_data['items'][-1]['fabric_quantity'] = 8 modified_data['files'] = [{'id': 1}, {'id': 2}] #POST and verify the response self.assertEqual(Estimate.objects.count(), 1) resp = self.client.post('/api/v1/estimate/', data=modified_data, format='json') #Verify that http response is appropriate self.assertEqual(resp.status_code, 201, msg=resp) #Verify that an acknowledgement is created in the system self.assertEqual(Estimate.objects.count(), 2) #Verify the resulting acknowledgement #that is returned from the post data ack = resp.data self.assertIsNotNone(ack) self.assertEqual(ack['id'], 2) self.assertEqual(ack['customer']['id'], 1) self.assertEqual(ack['employee']['id'], 1) self.assertEqual(Decimal(Decimal(ack['vat'])), Decimal(0)) self.assertEqual(Decimal(ack['subtotal']), Decimal(300000)) self.assertEqual(Decimal(ack['total']), Decimal(150000)) self.assertEqual(Decimal(ack['grand_total']), Decimal(150000)) self.assertEqual(len(ack['items']), 3) self.assertIn('project', ack) self.assertEqual(ack['project']['id'], 1) self.assertEqual(ack['project']['codename'], 'Ladawan1') #self.assertIsInstance(ack['files'], list) #self.assertEqual(len(ack['files']), 2) #Test standard sized item item1 = ack['items'][0] self.assertEqual(item1['id'], 3) self.assertEqual(item1['description'], 'Test Sofa Max') self.assertEqual(Decimal(item1['quantity']), Decimal('2.00')) self.assertEqual(item1['width'], 1000) self.assertEqual(item1['height'], 320) self.assertEqual(item1['depth'], 760) self.assertEqual(item1['fabric']['id'], 1) self.assertEqual(len(item1['pillows']), 4) self.assertEqual(Decimal(item1['unit_price']), Decimal(100000)) self.assertIn('total', item1) self.assertEqual(Decimal(item1['total']), Decimal(200000)) #Test custom sized item item2 = ack['items'][1] self.assertEqual(item2['id'], 4) self.assertEqual(item2['description'], 'High Gloss Table') self.assertEqual(Decimal(item2['quantity']), Decimal('1.00')) self.assertEqual(item2['width'], 1500) self.assertEqual(item2['height'], 320) self.assertEqual(item2['depth'], 760) self.assertEqual(item2['fabric']['id'], 1) self.assertEqual(Decimal(item2['unit_price']), Decimal(100000)) self.assertEqual(Decimal(item2['total']), Decimal(100000)) #Test custom item with width item3 = ack['items'][2] self.assertEqual(item3['width'], 1) self.assertEqual(item3['description'], 'test custom item') self.assertEqual(Decimal(item3['quantity']), Decimal('1.00')) self.assertEqual(Decimal(item3['unit_price']), 0) self.assertEqual(item3['fabric']['id'], 1) #Tests links to document """ self.assertIsNotNone(ack['pdf']) self.assertIsNotNone(ack['pdf']['acknowledgement']) self.assertIsNotNone(ack['pdf']['production']) self.assertIsNotNone(ack['pdf']['confirmation']) """ #Tests the acknowledgement in the database root_ack = Estimate.objects.get(pk=2) logger.debug(root_ack.project) self.assertEqual(root_ack.id, 2) self.assertEqual(root_ack.items.count(), 3) self.assertIsInstance(root_ack.project, Project) self.assertEqual(root_ack.project.id, 1) self.assertEqual(root_ack.project.codename, "Ladawan1") root_ack_items = root_ack.items.all() item1 = root_ack_items[0] item2 = root_ack_items[1] item3 = root_ack_items[2] self.assertEqual(item1.estimate.id, 2) self.assertEqual(item1.description, 'Test Sofa Max') self.assertEqual(item1.quantity, 2) self.assertEqual(item1.width, 1000) self.assertEqual(item1.height, 320) self.assertEqual(item1.depth, 760) self.assertEqual(item2.estimate.id, 2) self.assertEqual(item2.description, 'High Gloss Table') self.assertEqual(item2.width, 1500) self.assertEqual(item2.height, 320) self.assertEqual(item2.depth, 760) self.assertEqual(item3.estimate.id, 2) self.assertEqual(item3.description, 'test custom item') self.assertEqual(item3.width, 1) self.assertEqual(item3.quantity, 1) #Tests files attached to acknowledgements """ self.assertEqual(root_ack.files.all().count(), 2) file1 = root_ack.files.all()[0] self.assertEqual(file1.id, 1) file2 = root_ack.files.all()[1] self.assertEqual(file2.id, 2) #Test Fabric Log self.assertEqual(Log.objects.filter(acknowledgement_id=root_ack.id).count(), 1) log = Log.objects.get(acknowledgement_id=root_ack.id) self.assertEqual(log.quantity, Decimal('33')) self.assertEqual(log.action, 'RESERVE') self.assertEqual(log.acknowledgement_id, '2') self.assertEqual(log.message, 'Reserve 33m of Pattern: Max, Col: charcoal for Ack#2') self.assertEqual(Fabric.objects.get(id=1).quantity, Decimal('-7')) """ self.open_url(ack['files'][0]['url']) def test_post_with_custom_image(self): """ Testing POSTing data to the api with custom item with custom image """ logger.debug( "\n\n Testing creating acknowledgement with a custom image \n") #Apply a discount to the customer ack = copy.deepcopy(base_ack) ack['items'][2]['image'] = {'id': 1} #POST and verify the response self.assertEqual(Estimate.objects.count(), 1) resp = self.client.post('/api/v1/estimate/', data=ack, format='json') #Verify that http response is appropriate self.assertEqual(resp.status_code, 201, msg=resp) #Verify that an acknowledgement is created in the system self.assertEqual(Estimate.objects.count(), 2) #Verify the resulting acknowledgement #that is returned from the post data ack = resp.data self.assertIsNotNone(ack) self.assertEqual(ack['id'], 2) self.assertEqual(ack['customer']['id'], 1) self.assertEqual(ack['employee']['id'], 1) self.assertEqual(Decimal(ack['vat']), 0) self.assertEqual(Decimal(ack['grand_total']), Decimal(300000)) self.assertEqual(len(ack['items']), 3) self.assertIn('project', ack) self.assertEqual(ack['project']['id'], 1) self.assertEqual(ack['project']['codename'], 'Ladawan1') #Test standard sized item item1 = ack['items'][0] self.assertEqual(item1['id'], 3) self.assertEqual(item1['description'], 'Test Sofa Max') self.assertEqual(Decimal(item1['quantity']), Decimal('2.00')) self.assertEqual(item1['width'], 1000) self.assertEqual(item1['height'], 320) self.assertEqual(item1['depth'], 760) self.assertEqual(item1['fabric']['id'], 1) self.assertEqual(len(item1['pillows']), 4) self.assertEqual(Decimal(item1['unit_price']), Decimal(100000)) self.assertEqual(Decimal(item1['total']), Decimal(200000)) #Test custom sized item item2 = ack['items'][1] self.assertEqual(item2['id'], 4) self.assertEqual(item2['description'], 'High Gloss Table') self.assertEqual(Decimal(item2['quantity']), Decimal('1.00')) self.assertEqual(item2['width'], 1500) self.assertEqual(item2['height'], 320) self.assertEqual(item2['depth'], 760) self.assertEqual(item2['fabric']['id'], 1) self.assertEqual(Decimal(item2['unit_price']), Decimal(100000)) self.assertEqual(Decimal(item2['total']), Decimal(100000)) #Test custom item with width item3 = ack['items'][2] self.assertEqual(item3['width'], 1) self.assertEqual(item3['description'], 'test custom item') self.assertEqual(Decimal(item3['quantity']), Decimal('1.00')) self.assertEqual(Decimal(item3['unit_price']), 0) self.assertIsNotNone(item3['image']) self.assertIn('url', item3['image']) #Tests links to document #self.assertIsNotNone(ack['pdf']) #self.assertIsNotNone(ack['pdf']['acknowledgement']) #self.assertIsNotNone(ack['pdf']['production']) #self.assertIsNotNone(ack['pdf']['confirmation']) #logger.debug(ack['pdf']['confirmation']) #print "\n\n\n" #Tests the acknowledgement in the database root_ack = Estimate.objects.get(pk=2) logger.debug(root_ack.project) self.assertEqual(root_ack.id, 2) self.assertEqual(root_ack.items.count(), 3) self.assertIsInstance(root_ack.project, Project) self.assertEqual(root_ack.project.id, 1) self.assertEqual(root_ack.project.codename, "Ladawan1") root_ack_items = root_ack.items.all() item1 = root_ack_items[0] item2 = root_ack_items[1] item3 = root_ack_items[2] self.assertEqual(item1.estimate.id, 2) self.assertEqual(item1.description, 'Test Sofa Max') self.assertEqual(item1.quantity, 2) self.assertEqual(item1.width, 1000) self.assertEqual(item1.height, 320) self.assertEqual(item1.depth, 760) self.assertEqual(item2.estimate.id, 2) self.assertEqual(item2.description, 'High Gloss Table') self.assertEqual(item2.width, 1500) self.assertEqual(item2.height, 320) self.assertEqual(item2.depth, 760) self.assertEqual(item3.estimate.id, 2) self.assertEqual(item3.description, 'test custom item') self.assertEqual(item3.width, 1) self.assertEqual(item3.quantity, 1) def test_post_without_vat(self): """ Testing POSTing data to the api """ logger.debug("\n\n Testing creating acknowledgement without vat \n") #POST and verify the response self.assertEqual(Estimate.objects.count(), 1) resp = self.client.post('/api/v1/estimate/', format='json', data=base_ack, authentication=self.get_credentials()) self.assertEqual(resp.status_code, 201, msg=resp) self.assertEqual(Estimate.objects.count(), 2) #Verify the resulting acknowledgement #that is returned from the post data ack = resp.data self.assertIsNotNone(ack) self.assertEqual(ack['id'], 2) self.assertEqual(ack['customer']['id'], 1) self.assertEqual(ack['employee']['id'], 1) self.assertEqual(Decimal(ack['vat']), 0) self.assertEqual(Decimal(ack['grand_total']), Decimal('300000')) self.assertEqual(len(ack['items']), 3) self.assertIn('project', ack) self.assertEqual(ack['project']['id'], 1) self.assertEqual(ack['project']['codename'], 'Ladawan1') #Test standard sized item item1 = ack['items'][0] self.assertEqual(item1['id'], 3) self.assertEqual(item1['description'], 'Test Sofa Max') self.assertEqual(Decimal(item1['quantity']), Decimal('2.00')) self.assertEqual(item1['width'], 1000) self.assertEqual(item1['height'], 320) self.assertEqual(item1['depth'], 760) self.assertEqual(item1['fabric']['id'], 1) self.assertEqual(len(item1['pillows']), 4) self.assertEqual(Decimal(item1['unit_price']), Decimal(100000)) self.assertEqual(Decimal(item1['total']), Decimal(200000)) #Test custom sized item item2 = ack['items'][1] self.assertEqual(item2['id'], 4) self.assertEqual(item2['description'], 'High Gloss Table') self.assertEqual(Decimal(item2['quantity']), Decimal('1.00')) self.assertEqual(item2['width'], 1500) self.assertEqual(item2['height'], 320) self.assertEqual(item2['depth'], 760) self.assertEqual(item2['fabric']['id'], 1) self.assertEqual(Decimal(item2['unit_price']), Decimal(100000)) self.assertEqual(Decimal(item2['total']), Decimal(100000)) #Test custom item with width item3 = ack['items'][2] self.assertEqual(item3['width'], 1) self.assertEqual(item3['description'], 'test custom item') self.assertEqual(Decimal(item3['quantity']), Decimal('1.00')) self.assertEqual(Decimal(item3['unit_price']), 0) #Tests links to document #self.assertIsNotNone(ack['pdf']) #?self.assertIsNotNone(ack['pdf']['acknowledgement']) #self.assertIsNotNone(ack['pdf']['production']) #self.assertIsNotNone(ack['pdf']['confirmation']) self.open_url(ack['files'][0]['url']) def test_post_with_vat(self): """ Testing POSTing data to the api if there is vat """ logger.debug("\n\n Testing creating acknowledgement with vat \n") #Altering replication of base ack data ack_data = base_ack.copy() ack_data['vat'] = 7 #Verifying current number of acknowledgements in database self.assertEqual(Estimate.objects.count(), 1) resp = self.client.post('/api/v1/estimate/', format='json', data=ack_data, authentication=self.get_credentials()) self.assertEqual(resp.status_code, 201, msg=resp) self.assertEqual(Estimate.objects.count(), 2) #Verify the resulting acknowledgement #that is returned from the post data ack = resp.data self.assertIsNotNone(ack) self.assertEqual(ack['id'], 2) self.assertEqual(ack['customer']['id'], 1) self.assertEqual(ack['employee']['id'], 1) self.assertEqual(Decimal(ack['vat']), 7) self.assertEqual(Decimal(ack['grand_total']), Decimal(321000.00)) self.assertEqual(len(ack['items']), 3) self.assertIn('project', ack) self.assertEqual(ack['project']['id'], 1) self.assertEqual(ack['project']['codename'], 'Ladawan1') #Test standard sized item item1 = ack['items'][0] self.assertEqual(item1['id'], 3) self.assertEqual(item1['description'], 'Test Sofa Max') self.assertEqual(Decimal(item1['quantity']), Decimal('2')) self.assertEqual(item1['width'], 1000) self.assertEqual(item1['height'], 320) self.assertEqual(item1['depth'], 760) self.assertEqual(item1['fabric']['id'], 1) self.assertEqual(len(item1['pillows']), 4) self.assertEqual(Decimal(item1['unit_price']), Decimal(100000)) self.assertEqual(Decimal(item1['total']), Decimal(200000)) #Test custom sized item item2 = ack['items'][1] self.assertEqual(item2['id'], 4) self.assertEqual(item2['description'], 'High Gloss Table') self.assertEqual(Decimal(item2['quantity']), Decimal('1.00')) self.assertEqual(item2['width'], 1500) self.assertEqual(item2['height'], 320) self.assertEqual(item2['depth'], 760) self.assertEqual(item2['fabric']['id'], 1) self.assertEqual(Decimal(item2['unit_price']), Decimal(100000)) self.assertEqual(Decimal(item2['total']), Decimal(100000)) #Test custom item with width item3 = ack['items'][2] self.assertEqual(item3['width'], 1) self.assertEqual(item3['description'], 'test custom item') self.assertEqual(Decimal(item3['quantity']), Decimal('1.00')) self.assertEqual(Decimal(item3['unit_price']), 0) #Tests links to document #self.assertIsNotNone(ack['pdf']) #self.assertIsNotNone(ack['pdf']['acknowledgement']) #self.assertIsNotNone(ack['pdf']['production']) #self.assertIsNotNone(ack['pdf']['confirmation']) self.open_url(ack['files'][0]['url']) def test_post_with_vat_and_discount(self): """ Testing POSTing data to the api if there is vat """ logger.debug( "\n\n Testing creating acknowledgement with a discount and vat \n") #POST and verify the response ack_data = base_ack.copy() ack_data['vat'] = 7 ack_data['discount'] = 50 self.assertEqual(Estimate.objects.count(), 1) resp = self.client.post('/api/v1/estimate/', format='json', data=ack_data, authentication=self.get_credentials()) self.assertEqual(resp.status_code, 201, msg=resp) self.assertEqual(Estimate.objects.count(), 2) #Verify the resulting acknowledgement #that is returned from the post data ack = resp.data self.assertIsNotNone(ack) self.assertEqual(ack['id'], 2) self.assertEqual(ack['customer']['id'], 1) self.assertEqual(ack['employee']['id'], 1) self.assertEqual(Decimal(ack['vat']), Decimal(7)) self.assertEqual(Decimal(ack['subtotal']), Decimal('300000')) self.assertEqual(Decimal(ack['discount_amount']), Decimal('150000')) self.assertEqual(Decimal(ack['post_discount_total']), Decimal('150000')) self.assertEqual(Decimal(ack['total']), Decimal('150000')) self.assertEqual(Decimal(ack['vat_amount']), Decimal('10500')) self.assertEqual(Decimal(ack['grand_total']), Decimal('160500.000')) self.assertEqual(len(ack['items']), 3) #Test standard sized item item1 = ack['items'][0] self.assertEqual(item1['id'], 3) self.assertEqual(item1['description'], 'Test Sofa Max') self.assertEqual(Decimal(item1['quantity']), 2) self.assertEqual(item1['width'], 1000) self.assertEqual(item1['height'], 320) self.assertEqual(item1['depth'], 760) self.assertEqual(item1['fabric']['id'], 1) self.assertEqual(len(item1['pillows']), 4) self.assertEqual(Decimal(item1['unit_price']), Decimal(100000)) self.assertIn('total', item1, item1) self.assertEqual(Decimal(item1['total']), Decimal(200000)) #Test custom sized item item2 = ack['items'][1] self.assertEqual(item2['id'], 4) self.assertEqual(item2['description'], 'High Gloss Table') self.assertEqual(Decimal(item2['quantity']), Decimal('1.00')) self.assertEqual(item2['width'], 1500) self.assertEqual(item2['height'], 320) self.assertEqual(item2['depth'], 760) self.assertEqual(item2['fabric']['id'], 1) self.assertEqual(Decimal(item2['unit_price']), Decimal(100000)) # No longer Caculates Custom Size self.assertEqual(Decimal(item2['total']), Decimal(100000)) #Test custom item with width item3 = ack['items'][2] self.assertEqual(item3['width'], 1) self.assertEqual(item3['description'], 'test custom item') self.assertEqual(Decimal(item3['quantity']), Decimal('1.00')) self.assertEqual(Decimal(item3['unit_price']), 0) #Tests links to document #self.assertIsNotNone(ack['pdf']) #self.assertIsNotNone(ack['pdf']['acknowledgement']) #self.assertIsNotNone(ack['pdf']['production']) #self.assertIsNotNone(ack['pdf']['confirmation']) self.open_url(ack['files'][0]['url']) def test_post_with_vat_and_both_discounts(self): """ Testing POSTing data to the api if there is vat """ logger.debug( "\n\n Testing creating acknowledgement with both discounts and vat \n" ) #POST and verify the response ack_data = base_ack.copy() ack_data['vat'] = 7 ack_data['discount'] = 50 ack_data['second_discount'] = 10 self.assertEqual(Estimate.objects.count(), 1) resp = self.client.post('/api/v1/estimate/', format='json', data=ack_data, authentication=self.get_credentials()) self.assertEqual(resp.status_code, 201, msg=resp) self.assertEqual(Estimate.objects.count(), 2) #Verify the resulting acknowledgement #that is returned from the post data ack = resp.data self.assertIsNotNone(ack) self.assertEqual(ack['id'], 2) self.assertEqual(ack['customer']['id'], 1) self.assertEqual(ack['employee']['id'], 1) self.assertEqual(Decimal(ack['vat']), Decimal(7)) self.assertEqual(Decimal(ack['subtotal']), Decimal('300000')) self.assertEqual(Decimal(ack['discount_amount']), Decimal('150000')) self.assertEqual(Decimal(ack['post_discount_total']), Decimal('150000')) self.assertEqual(Decimal(ack['second_discount_amount']), Decimal('15000')) self.assertEqual(Decimal(ack['total']), Decimal('135000')) self.assertEqual(Decimal(ack['vat_amount']), Decimal('9450')) self.assertEqual(Decimal(ack['grand_total']), Decimal('144450.000')) self.assertEqual(len(ack['items']), 3) #Test standard sized item item1 = ack['items'][0] self.assertEqual(item1['id'], 3) self.assertEqual(item1['description'], 'Test Sofa Max') self.assertEqual(Decimal(item1['quantity']), 2) self.assertEqual(item1['width'], 1000) self.assertEqual(item1['height'], 320) self.assertEqual(item1['depth'], 760) self.assertEqual(item1['fabric']['id'], 1) self.assertEqual(len(item1['pillows']), 4) self.assertEqual(Decimal(item1['unit_price']), Decimal(100000)) self.assertEqual(Decimal(item1['total']), Decimal(200000)) #Test custom sized item item2 = ack['items'][1] self.assertEqual(item2['id'], 4) self.assertEqual(item2['description'], 'High Gloss Table') self.assertEqual(Decimal(item2['quantity']), Decimal('1.00')) self.assertEqual(item2['width'], 1500) self.assertEqual(item2['height'], 320) self.assertEqual(item2['depth'], 760) self.assertEqual(item2['fabric']['id'], 1) self.assertEqual(Decimal(item2['unit_price']), Decimal(100000)) # No longer Caculates Custom Size self.assertEqual(Decimal(item2['total']), Decimal(100000)) #Test custom item with width item3 = ack['items'][2] self.assertEqual(item3['width'], 1) self.assertEqual(item3['description'], 'test custom item') self.assertEqual(Decimal(item3['quantity']), Decimal('1.00')) self.assertEqual(Decimal(item3['unit_price']), 0) #Tests links to document #self.assertIsNotNone(ack['pdf']) #self.assertIsNotNone(ack['pdf']['acknowledgement']) #self.assertIsNotNone(ack['pdf']['production']) #self.assertIsNotNone(ack['pdf']['confirmation']) self.open_url(ack['files'][0]['url']) def test_post_with_custom_price(self): """ Test creating a custom price for all three item types """ logger.debug( "\n\n Testing creating acknowledgement with custom prices for all items \n" ) #POST and verify the response ack_data = copy.deepcopy(base_ack) ack_data['items'][0]['unit_price'] = 100 ack_data['items'][1]['unit_price'] = 200 ack_data['items'][2]['unit_price'] = 300 self.assertEqual(Estimate.objects.count(), 1) resp = self.client.post('/api/v1/estimate/', format='json', data=ack_data, authentication=self.get_credentials()) self.assertEqual(resp.status_code, 201, msg=resp) self.assertEqual(Estimate.objects.count(), 2) #Verify the resulting acknowledgement #that is returned from the post data ack = resp.data self.assertIsNotNone(ack) self.assertEqual(ack['id'], 2) self.assertEqual(ack['customer']['id'], 1) self.assertEqual(ack['employee']['id'], 1) self.assertEqual(Decimal(ack['vat']), 0) self.assertEqual(Decimal(ack['grand_total']), Decimal('700.00')) self.assertEqual(len(ack['items']), 3) #Test standard sized item item1 = ack['items'][0] self.assertEqual(item1['id'], 3) self.assertEqual(item1['description'], 'Test Sofa Max') self.assertEqual(Decimal(item1['quantity']), 2) self.assertEqual(item1['width'], 1000) self.assertEqual(item1['height'], 320) self.assertEqual(item1['depth'], 760) self.assertEqual(item1['fabric']['id'], 1) self.assertEqual(len(item1['pillows']), 4) self.assertEqual(Decimal(item1['unit_price']), Decimal('100')) self.assertEqual(Decimal(item1['total']), Decimal('200')) #Test custom sized item item2 = ack['items'][1] self.assertEqual(item2['id'], 4) self.assertEqual(item2['description'], 'High Gloss Table') self.assertEqual(Decimal(item2['quantity']), Decimal('1.00')) self.assertEqual(item2['width'], 1500) self.assertEqual(item2['height'], 320) self.assertEqual(item2['depth'], 760) self.assertEqual(item2['fabric']['id'], 1) self.assertEqual(Decimal(item2['unit_price']), Decimal('200')) self.assertEqual(Decimal(item2['total']), Decimal('200')) #Test custom item with width item3 = ack['items'][2] self.assertEqual(item3['width'], 1) self.assertEqual(item3['description'], 'test custom item') self.assertEqual(Decimal(item3['quantity']), Decimal('1.00')) self.assertEqual(Decimal(item3['unit_price']), Decimal('300')) #Tests links to document #self.assertIsNotNone(ack['pdf']) #self.assertIsNotNone(ack['pdf']['acknowledgement']) #self.assertIsNotNone(ack['pdf']['production']) #self.assertIsNotNone(ack['pdf']['confirmation']) def test_post_where_first_item_has_no_fabric(self): """ Test creating a custom price for all three item types """ logger.debug( "\n\n Testing creating acknowledgement with custom prices for all items \n" ) #POST and verify the response ack_data = copy.deepcopy(base_ack) del ack_data['items'][0]['fabric'] del ack_data['items'][0]['pillows'][0]['fabric'] self.assertEqual(Estimate.objects.count(), 1) resp = self.client.post('/api/v1/estimate/', format='json', data=ack_data, authentication=self.get_credentials()) self.assertEqual(resp.status_code, 201, msg=resp) self.assertEqual(Estimate.objects.count(), 2) #Verify the resulting acknowledgement #that is returned from the post data ack = resp.data self.assertIsNotNone(ack) self.assertEqual(ack['id'], 2) self.assertEqual(ack['customer']['id'], 1) self.assertEqual(ack['employee']['id'], 1) self.assertEqual(Decimal(ack['vat']), 0) self.assertEqual(Decimal(ack['grand_total']), Decimal('300000')) self.assertEqual(len(ack['items']), 3) #Test standard sized item item1 = ack['items'][0] self.assertEqual(item1['id'], 3) self.assertEqual(item1['description'], 'Test Sofa Max') self.assertEqual(Decimal(item1['quantity']), Decimal('2.00')) self.assertEqual(item1['width'], 1000) self.assertEqual(item1['height'], 320) self.assertEqual(item1['depth'], 760) self.assertIsNone(item1['fabric']) self.assertEqual(len(item1['pillows']), 5) self.assertEqual(Decimal(item1['unit_price']), Decimal('100000')) self.assertEqual(Decimal(item1['total']), Decimal('200000')) #Test custom sized item item2 = ack['items'][1] self.assertEqual(item2['id'], 4) self.assertEqual(item2['description'], 'High Gloss Table') self.assertEqual(Decimal(item2['quantity']), Decimal('1.00')) self.assertEqual(item2['width'], 1500) self.assertEqual(item2['height'], 320) self.assertEqual(item2['depth'], 760) self.assertEqual(item2['fabric']['id'], 1) self.assertEqual(Decimal(item2['unit_price']), Decimal('100000')) self.assertEqual(Decimal(item2['total']), Decimal('100000')) #Test custom item with width item3 = ack['items'][2] self.assertEqual(item3['width'], 1) self.assertEqual(item3['description'], 'test custom item') self.assertEqual(Decimal(item3['quantity']), Decimal('1.00')) #Tests links to document #self.assertIsNotNone(ack['pdf']) #self.assertIsNotNone(ack['pdf']['acknowledgement']) #self.assertIsNotNone(ack['pdf']['production']) #self.assertIsNotNone(ack['pdf']['confirmation']) def test_put(self): """ Test making a PUT call """ logger.debug("\n\n Testing updating via put \n") ack_data = base_ack.copy() ack_data['items'][0]['id'] = 1 del ack_data['items'][0]['pillows'][-1] ack_data['items'][1]['id'] = 2 ack_data['items'][1]['description'] = 'F-04 Sofa' ack_data['items'][1]['is_custom_item'] = True del ack_data['items'][2] ack_data['delivery_date'] = datetime.now() self.assertEqual(Estimate.objects.count(), 1) resp = self.client.put('/api/v1/estimate/1/', format='json', data=ack_data, authentication=self.get_credentials()) self.assertEqual(resp.status_code, 200) self.assertEqual(Estimate.objects.count(), 1) #Validate the change ack = resp.data #self.assertEqual(dateutil.parser.parse(ack['delivery_date']), ack_data['delivery_date']) logger.debug(ack['items'][0]['pillows']) #Tests ack in database ack = Estimate.objects.get(pk=1) items = ack.items.all() item1 = items[0] self.assertEqual(item1.description, 'Test Sofa Max') #self.assertEqual(item1.pillows.count(), 3) item2 = items[1] self.assertEqual(item2.description, 'F-04 Sofa') def test_changing_lead_time(self): """ Test making a PUT call """ logger.debug("\n\n Testing updating lead time via put \n") ack_data = copy.deepcopy(base_ack) ack_data['lead_time'] = '6 Weeks' self.assertEqual(Estimate.objects.count(), 1) resp = self.client.put('/api/v1/estimate/1/', format='json', data=ack_data, authentication=self.get_credentials()) self.assertEqual(resp.status_code, 200, msg=resp) ack = resp.data #logger.debug(ack['delivery_date']) #d1 = datetime.strptime(ack['delivery_date']) self.assertEqual(ack['lead_time'], '6 Weeks') ack = Estimate.objects.all()[0] self.assertEqual(ack.lead_time, '6 Weeks') #@unittest.skip('ok') def test_delete(self): """ Test making a DELETE call 'Delete' in this model has been overridden so that no acknowledgement is truly deleted. Instead the 'delete' column in the database is marked as true """ self.assertEqual(Estimate.objects.count(), 1) resp = self.client.delete('/api/v1/estimate/1/', authentication=self.get_credentials()) self.assertEqual(Estimate.objects.count(), 1)
class TestItemResource(APITestCase): def setUp(self): """ Sets up environment for tests """ super(TestItemResource, self).setUp() self.create_user() self.client.login(username='******', password='******') #Create supplier, customer and addrss self.customer = Customer(**base_customer) self.customer.save() self.supplier = Supplier(**base_supplier) self.supplier.save() self.address = Address(address1="Jiggle", contact=self.customer) self.address.save() #Create a product to add self.product = Product.create(self.user, **base_product) self.product.save() self.fabric = Fabric.create(**base_fabric) f_data = base_fabric.copy() f_data["pattern"] = "Stripe" self.fabric2 = Fabric.create(**f_data) #Create acknowledgement ack_data = base_ack.copy() del ack_data['customer'] del ack_data['items'] del ack_data['employee'] del ack_data['project'] self.ack = Estimate(**ack_data) self.ack.customer = self.customer self.ack.employee = self.user self.ack.save() #Create an item self.item_data = { 'id': 1, 'quantity': 1, 'is_custom_size': True, 'width': 1500, "fabric": { "id": 1 } } self.item = Item.create(acknowledgement=self.ack, **self.item_data) def create_user(self): self.user = User.objects.create_user('test', '*****@*****.**', 'test') self.ct = ContentType(app_label='acknowledgements') self.ct.save() perm = Permission(content_type=self.ct, codename='change_item') perm.save() self.user.user_permissions.add(perm) perm = Permission(content_type=self.ct, codename='change_fabric') perm.save() self.user.user_permissions.add(perm) self.assertTrue(self.user.has_perm('acknowledgements.change_item')) return self.user def test_get_list(self): """ Tests getting a list of items via GET """ #Tests the get resp = self.client.get('/api/v1/acknowledgement-item') #self.assertHttpOK(resp) #Tests the resp resp_obj = self.deserialize(resp) self.assertIn("objects", resp_obj) self.assertEqual(len(resp_obj['objects']), 1) def test_get(self): """ Tests getting an item via GET """ #Tests the resp resp = self.client.get('/api/v1/acknowledgement-item/1') #self.assertHttpOK(resp) def test_failed_create(self): """ Tests that when attempting to create via POST it is returned as unauthorized """ resp = self.client.post('/api/v1/acknowledgement-item/', format='json', data=self.item_data) #self.assertHttpMethodNotAllowed(resp) def test_update(self): """ Tests updating the item via PUT """ modified_item_data = self.item_data.copy() modified_item_data['fabric'] = {'id': 2} modified_item_data['width'] = 888 #Sets current fabric self.assertEqual(Item.objects.all()[0].fabric.id, 1) #Tests the response resp = self.client.put('/api/v1/acknowledgement-item/1', format='json', data=modified_item_data) #self.assertHttpOK(resp) self.assertEqual(Item.objects.all()[0].fabric.id, 2) #Tests the data returned obj = self.deserialize(resp) self.assertEqual(obj['id'], 1) self.assertEqual(obj['fabric']['id'], 2) self.assertEqual(obj['width'], 888) self.assertEqual(obj['quantity'], 1)
class CustomerResourceTest(APITestCase): def setUp(self): super(CustomerResourceTest, self).setUp() #Create the user self.username = '******' self.password = '******' self.user = User.objects.create_user(self.username, '*****@*****.**', self.password) self.client.force_authenticate(self.user) self.customer = Customer(**base_contact) self.customer.is_customer = True self.customer.save() self.address = Address(**base_address) self.address.contact = self.customer self.address.save() def get_credentials(self): return self.user #self.create_basic(username=self.username, password=self.password) def test_get_json_list(self): """ Test GET of list """ #Retrieve and validate GET response resp = self.client.get('/api/v1/customer/', format='json') self.assertEqual(resp.status_code, 200) #test deserialized response resp_obj = resp.data self.assertEqual(len(resp_obj), 1) customer = resp_obj[0] self.assertEqual(customer["name"], 'Charlie Brown') self.assertEqual(customer["first_name"], 'Charlie') self.assertEqual(customer["last_name"], 'Brown') self.assertEqual(customer["currency"], 'USD') self.assertTrue(customer["is_customer"]) self.assertEqual(customer["email"], "*****@*****.**") self.assertEqual(customer["telephone"], "08348229383") self.assertEqual(customer["fax"], "0224223423") self.assertEqual(customer['discount'], 20) def test_post(self): """ Test creating customer via POST """ #Validate resource creation m_customer_data = customer_data.copy() addr_data = base_address.copy() m_customer_data['addresses'] = [addr_data] self.assertEqual(Customer.objects.count(), 1) resp = self.client.post('/api/v1/customer/', format='json', data=m_customer_data, authentication=self.get_credentials()) self.assertEqual(resp.status_code, 201, msg=resp) self.assertEqual(Customer.objects.count(), 2) #Validated response to resource creation customer = resp.data self.assertEqual(customer['id'], 2) self.assertEqual(customer["name"], 'Charlie Brown') self.assertEqual(customer["first_name"], 'Charlie') self.assertEqual(customer["last_name"], 'Brown') self.assertEqual(customer["currency"], 'USD') self.assertTrue(customer["is_customer"]) self.assertEqual(customer["email"], "*****@*****.**") self.assertEqual(customer["telephone"], "08348229383") self.assertEqual(customer["fax"], "0224223423") self.assertEqual(customer['discount'], 20) self.assertEqual(len(customer['addresses']), 1) def test_post_with_incomplete_address(self): """ Test creating customer via POST with incomplete address data """ def test_sub_post(c_data, missing_key): #Validate resource creation resp = self.client.post('/api/v1/customer/', format='json', data=c_data, authentication=self.get_credentials()) self.assertEqual(resp.status_code, 201, msg=resp) #Validated response to resource creation customer = resp.data self.assertIsNotNone(customer['id']) self.assertEqual(customer["name"], 'Charlie Brown') self.assertEqual(customer["first_name"], 'Charlie') self.assertEqual(customer["last_name"], 'Brown') self.assertEqual(customer["currency"], 'USD') self.assertTrue(customer["is_customer"]) self.assertEqual(customer["email"], "*****@*****.**") self.assertEqual(customer["telephone"], "08348229383") self.assertEqual(customer["fax"], "0224223423") self.assertEqual(customer['discount'], 20) self.assertIsNotNone(customer['addresses']) addrs = customer['addresses'] self.assertEqual(len(addrs), 1) addr = addrs[0] self.assertIsNotNone(addr['id']) for k in [h for h in addr.keys() if h not in ['id', missing_key]]: #self.assertIsNotNone(addr[k], "{0}: {1}".format(k, addr[k])) msg = 'Error with property {0}'.format(k) self.assertEqual(addr[k], str(c_data['addresses'][0][k]), msg) # Test missing key is None self.assertEqual(addr[missing_key], None) m_customer_data = customer_data.copy() addr_data = base_address.copy() for key in addr_data.keys(): m_addr_data = addr_data.copy() del m_addr_data[key] m_customer_data['addresses'] = [m_addr_data] test_sub_post(m_customer_data, key) def test_put(self): """ Test updating a customer resource via PUT """ logger.debug('\n\n Test PUT for customer \n\n') #Validate resource update instead of creation modified_customer = customer_data.copy() modified_customer['first_name'] = 'Charles' modified_customer['discount'] = 50 self.assertEqual(Customer.objects.count(), 1) resp = self.client.put('/api/v1/customer/1/', format='json', data=modified_customer, authentication=self.get_credentials()) self.assertEqual(resp.status_code, 200) self.assertEqual(Customer.objects.count(), 1) obj = Customer.objects.all()[0] self.assertEqual(obj.id, 1) self.assertEqual(obj.first_name, 'Charles') self.assertEqual(obj.discount, 50) def test_get(self): """ TEst getting a customer resource via GET """ resp = self.client.get('/api/v1/customer/1/', format='json', authentication=self.get_credentials()) self.assertEqual(resp.status_code, 200) customer = resp.data self.assertEqual(customer['id'], 1) self.assertEqual(customer["name"], 'Charlie Brown') self.assertEqual(customer["first_name"], 'Charlie') self.assertEqual(customer["last_name"], 'Brown') self.assertEqual(customer["currency"], 'USD') self.assertTrue(customer["is_customer"]) self.assertEqual(customer["email"], "*****@*****.**") self.assertEqual(customer["telephone"], "08348229383") self.assertEqual(customer["fax"], "0224223423") self.assertEqual(customer['discount'], 20) def test_delete(self): """ Test delete a customer resource via get """ self.assertEqual(Customer.objects.count(), 1) resp = self.client.delete('/api/v1/customer/1/', authentication=self.get_credentials()) self.assertEqual(resp.status_code, 204) self.assertEqual(Customer.objects.count(), 0)
def setUp(self): """ Set up for the Estimate Test Objects created: -User -Customer -Supplier -Address -product -2 fabrics After Creating all the needed objects for the Estimate, test that all the objects have been made. """ super(EstimateResourceTest, self).setUp() self.ct = ContentType(app_label="estimates") self.ct.save() #Create the user self.username = '******' self.password = '******' self.user = User.objects.create_user(self.username, '*****@*****.**', self.password) p = Permission(content_type=self.ct, codename="change_estimate") p.save() p2 = Permission(content_type=self.ct, codename="add_estimate") p2.save() self.user.user_permissions.add(p) self.user.user_permissions.add(p2) self.user.save() #Create supplier, customer and addrss customer = copy.deepcopy(base_customer) del customer['id'] self.customer = Customer(**customer) self.customer.save() self.supplier = Supplier(**base_supplier) self.supplier.save() self.address = Address(address1="Jiggle", contact=self.customer) self.address.save() #Create a product to add self.product = Product.create(self.user, **base_product) self.product.save() #Create custom product self.custom_product = Product() self.custom_product.id = 10436 self.custom_product.save() self.fabric = Fabric.create(**base_fabric) self.fabric.quantity = 26 self.fabric.save() f_data = base_fabric.copy() f_data["pattern"] = "Stripe" self.fabric2 = Fabric.create(**f_data) #Create custom product self.custom_product = Product.create(self.user, description="Custom Custom", id=10436, width=0, depth=0, height=0, price=0, wholesale_price=0, retail_price=0) self.custom_product.id = 10436 self.custom_product.save() self.image = S3Object(key='test', bucket='test') self.image.save() #Create acknowledgement ack_data = base_ack.copy() del ack_data['customer'] del ack_data['items'] del ack_data['employee'] del ack_data['project'] self.ack = Estimate(**ack_data) self.ack.customer = self.customer self.ack.employee = self.user self.ack.save() #Create an item item_data = {'id': 1, 'quantity': 1, 'is_custom_size': True, 'width': 1500, "fabric": {"id":1}} self.item = Item.create(estimate=self.ack, **item_data) item_data = {'is_custom': True, 'description': 'F-04 Sofa', 'quantity': 3} self.item2 = Item.create(estimate=self.ack, **item_data) self.client.login(username="******", password="******") #Create fake S3Objects to test files attached to acknowledgements self.file1 = S3Object(key='test1', bucket='test') self.file2 = S3Object(key='test2', bucket='test') self.file1.save() self.file2.save()
def setUp(self): """ Set up for the Acknowledgement Test Objects created: -User -Customer -Supplier -Address -product -2 fabrics After Creating all the needed objects for the Acknowledgement, test that all the objects have been made. """ super(ShippingResourceTest, self).setUp() self.ct = ContentType(app_label="shipping") self.ct.save() #Create the user self.username = '******' self.password = '******' self.user = User.objects.create_user(self.username, '*****@*****.**', self.password) self.user.save() p = Permission(content_type=self.ct, codename="change_shipping") p.save() p2 = Permission(content_type=self.ct, codename="add_shipping") p2.save() self.user.user_permissions.add(p) self.user.user_permissions.add(p2) self.user.save() self.setup_client() #Create supplier, customer and addrss self.customer = Customer(**base_customer) self.customer.save() self.supplier = Supplier(**base_supplier) self.supplier.save() self.address = Address(address1="Jiggle", contact=self.customer) self.address.save() #Create project self.project = Project.objects.create(codename="Ladawan") #Create phase self.phase = Phase.objects.create(description="Phase 1/6", project=self.project) #Create a product to add self.product = Product.create(self.user, **base_product) self.product.save() self.fabric = Fabric.create(**base_fabric) f_data = base_fabric.copy() f_data["pattern"] = "Stripe" self.fabric2 = Fabric.create(**f_data) #Create acknowledgement ack_data = base_ack.copy() del ack_data['customer'] del ack_data['items'] del ack_data['employee'] self.ack = Acknowledgement(**ack_data) self.ack.customer = self.customer self.ack.employee = self.user self.ack.save() #Create an item item_data = {'id': 1, 'quantity': 1, 'is_custom_size': True, 'width': 1500, "fabric": {"id":1}} self.item = AckItem.create(acknowledgement=self.ack, **item_data) #Create an item item_data = {'id': 1, 'quantity': 2, 'is_custom_size': True, 'width': 1500, "fabric": {"id":1}} self.item2 = AckItem.create(acknowledgement=self.ack, **item_data)
class ShippingResourceTest(APITestCase): def setUp(self): """ Set up for the Acknowledgement Test Objects created: -User -Customer -Supplier -Address -product -2 fabrics After Creating all the needed objects for the Acknowledgement, test that all the objects have been made. """ super(ShippingResourceTest, self).setUp() self.ct = ContentType(app_label="shipping") self.ct.save() #Create the user self.username = '******' self.password = '******' self.user = User.objects.create_user(self.username, '*****@*****.**', self.password) self.user.save() p = Permission(content_type=self.ct, codename="change_shipping") p.save() p2 = Permission(content_type=self.ct, codename="add_shipping") p2.save() self.user.user_permissions.add(p) self.user.user_permissions.add(p2) self.user.save() self.setup_client() #Create supplier, customer and addrss self.customer = Customer(**base_customer) self.customer.save() self.supplier = Supplier(**base_supplier) self.supplier.save() self.address = Address(address1="Jiggle", contact=self.customer) self.address.save() #Create project self.project = Project.objects.create(codename="Ladawan") #Create phase self.phase = Phase.objects.create(description="Phase 1/6", project=self.project) #Create a product to add self.product = Product.create(self.user, **base_product) self.product.save() self.fabric = Fabric.create(**base_fabric) f_data = base_fabric.copy() f_data["pattern"] = "Stripe" self.fabric2 = Fabric.create(**f_data) #Create acknowledgement ack_data = base_ack.copy() del ack_data['customer'] del ack_data['items'] del ack_data['employee'] self.ack = Acknowledgement(**ack_data) self.ack.customer = self.customer self.ack.employee = self.user self.ack.save() #Create an item item_data = {'id': 1, 'quantity': 1, 'is_custom_size': True, 'width': 1500, "fabric": {"id":1}} self.item = AckItem.create(acknowledgement=self.ack, **item_data) #Create an item item_data = {'id': 1, 'quantity': 2, 'is_custom_size': True, 'width': 1500, "fabric": {"id":1}} self.item2 = AckItem.create(acknowledgement=self.ack, **item_data) def create_shipping(self): #create a shipping item self.shipping = Shipping.create(acknowledgement={'id': 1}, customer={'id': 1}, user=self.user, delivery_date=base_delivery_date, items=[{'id': 1}, {'id': 2}]) self.shipping.save() def get_credentials(self): return self.user#self.create_basic(username=self.username, password=self.password) def setup_client(self): # Login the Client # APIClient #self.client = APIClient(enforce_csrf_checks=False) #self.client.login(username=self.username, password=self.password) self.client.force_authenticate(self.user) def test_get_list(self): """ Tests getting a list of objects via GET """ self.skipTest('') #Create a shipping to retrieve self.create_shipping() resp = self.client.get('/api/v1/shipping/', format='json', authentication=self.get_credentials()) self.assertEqual(resp.status_code, 200) #Validate the resources returned resp_obj = resp.data self.assertEqual(len(resp_obj['objects']), 1) def test_get(self): """ Tests getting an object via GET """ self.skipTest('') self.create_shipping() #Test the resp resp = self.client.get('/api/v1/shipping/1/', format='json', authentication=self.get_credentials()) self.assertEqual(resp.status_code, 200) #Validate the object obj = resp.data self.assertEqual(obj['id'], 1) self.assertIn("customer", obj) self.assertEqual(obj['customer']['id'], 1) def test_post_project_shipping(self): """ Test creating a project packing list via POST """ data = {'project': {'id': 1}, 'customer': {'id': 1}, 'phase': {'id': 1}, 'items': [ {'description': 'TK 1/2'} ]} resp = self.client.post('/api/v1/shipping/', format='json', data=data) # Test client response self.assertEqual(resp.status_code, 201, msg=resp) def test_post_with_two_item(self): """ Tests creating a resource via POST """ #Validate the resp and obj creation self.assertEqual(Shipping.objects.count(), 0) shipping_data={'acknowledgement': {'id': 1}, 'customer': {'id': 1}, 'delivery_date': base_delivery_date, 'items': [{'item': {'id': 1}, 'description':'test1', 'quantity': 1}, {'item':{'id': 2}}]} resp = self.client.post('/api/v1/shipping/', data=shipping_data, format='json') self.assertEqual(resp.status_code, 201, msg=resp) self.assertEqual(Shipping.objects.count(), 1) #validate the object returned obj = resp.data self.assertEqual(obj['id'], 1) self.assertIn('customer', obj) self.assertEqual(obj['customer']['id'], 1) self.assertIn('last_modified', obj) self.assertIn('time_created', obj) self.assertEqual(len(obj['items']), 2) item1 = obj['items'][0] #Validate resource in the database shipping = Shipping.objects.get(pk=1) self.assertEqual(shipping.id, 1) self.assertEqual(shipping.customer.id, 1) self.assertEqual(shipping.items.count(), 2) def test_post_with_one_item(self): """ Tests creating a resource via POST """ #Validate the resp and obj creation self.assertEqual(Shipping.objects.count(), 0) shipping_data={'acknowledgement': {'id': 1}, 'customer': {'id': 1}, 'delivery_date': base_delivery_date, 'items': [{'item': {'id': 1}, 'description':'test1', 'quantity': 1}]} resp = self.client.post('/api/v1/shipping/', data=shipping_data, format='json') self.assertEqual(resp.status_code, 201, msg=resp) self.assertEqual(Shipping.objects.count(), 1) #validate the object returned obj = resp.data self.assertEqual(obj['id'], 1) self.assertIn('customer', obj) self.assertEqual(obj['customer']['id'], 1) self.assertIn('last_modified', obj) self.assertIn('time_created', obj) self.assertEqual(len(obj['items']), 1) item1 = obj['items'][0] #Validate resource in the database shipping = Shipping.objects.get(pk=1) self.assertEqual(shipping.id, 1) self.assertEqual(shipping.customer.id, 1) self.assertEqual(shipping.items.count(), 1) def test_put(self): """ Tests updating a resource via PUT """ self.skipTest('') self.create_shipping() self.assertEqual(Shipping.objects.count(), 1) resp = self.client.put('/api/v1/shipping/1/', format='json', authentication=self.get_credentials(), data={'delivery_date':base_delivery_date, 'acknowledgement': {'id': 1}}) self.assertEqual(resp.status_code, 200, msg=resp) self.assertEqual(Shipping.objects.count(), 1) #Validate the obj obj = resp.data self.assertEqual(obj['id'], 1) self.assertEqual(obj['customer']['id'], 1) self.assertEqual(obj['comments'], 'test') def test_delete(self): """ Tests deleting a resource via DELETE """ self.skipTest('') self.create_shipping() self.assertEqual(Shipping.objects.count(), 1) resp = self.client.delete('/api/v1/shipping/1/', format='json', authentication=self.get_credentials()) self.assertEqual(resp.status_code, 204) self.assertEqual(Shipping.objects.count(), 0)
class CustomerResourceTest(APITestCase): def setUp(self): super(CustomerResourceTest, self).setUp() #Create the user self.username = '******' self.password = '******' self.user = User.objects.create_user(self.username, '*****@*****.**', self.password) self.customer = Customer(**base_contact) self.customer.is_customer = True self.customer.save() self.address = Address(**base_address) self.address.contact = self.customer self.address.save() def get_credentials(self): return None #self.create_basic(username=self.username, password=self.password) def test_get_json_list(self): """ Test GET of list """ #Retrieve and validate GET response resp = self.client.get('/api/v1/customer/', format='json') self.assertEqual(resp.status_code, 200) #test deserialized response resp_obj = resp.data self.assertEqual(len(resp_obj['results']), 1) customer = resp_obj['results'][0] self.assertEqual(customer["name"], 'Charlie Brown') self.assertEqual(customer["first_name"], 'Charlie') self.assertEqual(customer["last_name"], 'Brown') self.assertEqual(customer["currency"], 'USD') self.assertTrue(customer["is_customer"]) self.assertEqual(customer["email"], "*****@*****.**") self.assertEqual(customer["telephone"], "08348229383") self.assertEqual(customer["fax"], "0224223423") self.assertEqual(customer['discount'], 20) def test_post(self): """ Test creating customer via POST """ #Validate resource creation self.assertEqual(Customer.objects.count(), 1) resp = self.client.post('/api/v1/customer/', format='json', data=customer_data, authentication=self.get_credentials()) self.assertEqual(resp.status_code, 201, msg=resp) self.assertEqual(Customer.objects.count(), 2) #Validated response to resource creation customer = resp.data self.assertEqual(customer['id'], 2) self.assertEqual(customer["name"], 'Charlie Brown') self.assertEqual(customer["first_name"], 'Charlie') self.assertEqual(customer["last_name"], 'Brown') self.assertEqual(customer["currency"], 'USD') self.assertTrue(customer["is_customer"]) self.assertEqual(customer["email"], "*****@*****.**") self.assertEqual(customer["telephone"], "08348229383") self.assertEqual(customer["fax"], "0224223423") self.assertEqual(customer['discount'], 20) def test_put(self): """ Test updating a customer resource via PUT """ logger.debug('\n\n Test PUT for customer \n\n') #Validate resource update instead of creation modified_customer = customer_data modified_customer['first_name'] = 'Charles' modified_customer['type'] = 'Dealer' modified_customer['discount'] = 50 self.assertEqual(Customer.objects.count(), 1) resp = self.client.put('/api/v1/customer/1/', format='json', data=modified_customer, authentication=self.get_credentials()) self.assertEqual(resp.status_code, 200) self.assertEqual(Customer.objects.count(), 1) obj = Customer.objects.all()[0] self.assertEqual(obj.id, 1) self.assertEqual(obj.first_name, 'Charles') self.assertEqual(obj.type, 'Dealer') self.assertEqual(obj.discount, 50) def test_get(self): """ TEst getting a customer resource via GET """ resp = self.client.get('/api/v1/customer/1/', format='json', authentication=self.get_credentials()) self.assertEqual(resp.status_code, 200) customer = resp.data self.assertEqual(customer['id'], 1) self.assertEqual(customer["name"], 'Charlie Brown') self.assertEqual(customer["first_name"], 'Charlie') self.assertEqual(customer["last_name"], 'Brown') self.assertEqual(customer["currency"], 'USD') self.assertTrue(customer["is_customer"]) self.assertEqual(customer["email"], "*****@*****.**") self.assertEqual(customer["telephone"], "08348229383") self.assertEqual(customer["fax"], "0224223423") self.assertEqual(customer['discount'], 20) def test_delete(self): """ Test delete a customer resource via get """ self.assertEqual(Customer.objects.count(), 1) resp = self.client.delete('/api/v1/customer/1/', authentication=self.get_credentials()) self.assertEqual(resp.status_code, 204) self.assertEqual(Customer.objects.count(), 0)
from django.core.wsgi import get_wsgi_application from django.conf import settings import gdata.contacts.client import gdata.contacts.data os.environ['DJANGO_SETTINGS_MODULE'] = 'EmployeeCenter.settings' application = get_wsgi_application() from contacts.models import Customer, Contact from django.contrib.auth.models import User if __name__ == "__main__": u = User.objects.get(pk=1) s = Customer.get_google_contacts_service(u) service = Customer.get_google_contacts_service(u) query = gdata.contacts.client.ContactsQuery() query.max_results = 10000 feed = service.GetContacts(q = query) for contact in feed.entry: try: print contact.id.text, contact.email[0].address print '\n' except: pass
class TestItemResource(APITestCase): def setUp(self): """ Sets up environment for tests """ super(TestItemResource, self).setUp() self.create_user() self.client.login(username='******', password='******') #Create supplier, customer and addrss self.customer = Customer(**base_customer) self.customer.save() self.supplier = Supplier(**base_supplier) self.supplier.save() self.address = Address(address1="Jiggle", contact=self.customer) self.address.save() #Create a product to add self.product = Product.create(self.user, **base_product) self.product.save() self.fabric = Fabric.create(**base_fabric) f_data = base_fabric.copy() f_data["pattern"] = "Stripe" self.fabric2 = Fabric.create(**f_data) #Create acknowledgement ack_data = base_ack.copy() del ack_data['customer'] del ack_data['items'] del ack_data['employee'] del ack_data['project'] self.ack = Estimate(**ack_data) self.ack.customer = self.customer self.ack.employee = self.user self.ack.save() #Create an item self.item_data = {'id': 1, 'quantity': 1, 'is_custom_size': True, 'width': 1500, "fabric": {"id":1}} self.item = Item.create(acknowledgement=self.ack, **self.item_data) def create_user(self): self.user = User.objects.create_user('test', '*****@*****.**', 'test') self.ct = ContentType(app_label='acknowledgements') self.ct.save() perm = Permission(content_type=self.ct, codename='change_item') perm.save() self.user.user_permissions.add(perm) perm = Permission(content_type=self.ct, codename='change_fabric') perm.save() self.user.user_permissions.add(perm) self.assertTrue(self.user.has_perm('acknowledgements.change_item')) return self.user def test_get_list(self): """ Tests getting a list of items via GET """ #Tests the get resp = self.client.get('/api/v1/acknowledgement-item') #self.assertHttpOK(resp) #Tests the resp resp_obj = self.deserialize(resp) self.assertIn("objects", resp_obj) self.assertEqual(len(resp_obj['objects']), 1) def test_get(self): """ Tests getting an item via GET """ #Tests the resp resp = self.client.get('/api/v1/acknowledgement-item/1') #self.assertHttpOK(resp) def test_failed_create(self): """ Tests that when attempting to create via POST it is returned as unauthorized """ resp = self.client.post('/api/v1/acknowledgement-item/', format='json', data=self.item_data) #self.assertHttpMethodNotAllowed(resp) def test_update(self): """ Tests updating the item via PUT """ modified_item_data = self.item_data.copy() modified_item_data['fabric'] = {'id': 2} modified_item_data['width'] = 888 #Sets current fabric self.assertEqual(Item.objects.all()[0].fabric.id, 1) #Tests the response resp = self.client.put('/api/v1/acknowledgement-item/1', format='json', data=modified_item_data) #self.assertHttpOK(resp) self.assertEqual(Item.objects.all()[0].fabric.id, 2) #Tests the data returned obj = self.deserialize(resp) self.assertEqual(obj['id'], 1) self.assertEqual(obj['fabric']['id'], 2) self.assertEqual(obj['width'], 888) self.assertEqual(obj['quantity'], 1)
class ShippingResourceTest(APITestCase): def setUp(self): """ Set up for the Acknowledgement Test Objects created: -User -Customer -Supplier -Address -product -2 fabrics After Creating all the needed objects for the Acknowledgement, test that all the objects have been made. """ super(ShippingResourceTest, self).setUp() self.ct = ContentType(app_label="shipping") self.ct.save() #Create the user self.username = '******' self.password = '******' self.user = User.objects.create_user(self.username, '*****@*****.**', self.password) self.user.save() p = Permission(content_type=self.ct, codename="change_shipping") p.save() p2 = Permission(content_type=self.ct, codename="add_shipping") p2.save() self.user.user_permissions.add(p) self.user.user_permissions.add(p2) self.user.save() self.setup_client() #Create supplier, customer and addrss self.customer = Customer(**base_customer) self.customer.save() self.supplier = Supplier(**base_supplier) self.supplier.save() self.address = Address(address1="Jiggle", contact=self.customer) self.address.save() #Create project self.project = Project.objects.create(codename="Ladawan") #Create phase self.phase = Phase.objects.create(description="Phase 1/6", project=self.project) #Create a product to add self.product = Product.create(self.user, **base_product) self.product.save() self.fabric = Fabric.create(**base_fabric) f_data = base_fabric.copy() f_data["pattern"] = "Stripe" self.fabric2 = Fabric.create(**f_data) #Create acknowledgement ack_data = base_ack.copy() del ack_data['customer'] del ack_data['items'] del ack_data['employee'] self.ack = Acknowledgement(**ack_data) self.ack.customer = self.customer self.ack.employee = self.user self.ack.save() #Create an item item_data = { 'id': 1, 'quantity': 1, 'is_custom_size': True, 'width': 1500, "fabric": { "id": 1 } } self.item = AckItem.create(acknowledgement=self.ack, **item_data) #Create an item item_data = { 'id': 1, 'quantity': 2, 'is_custom_size': True, 'width': 1500, "fabric": { "id": 1 } } self.item2 = AckItem.create(acknowledgement=self.ack, **item_data) def create_shipping(self): #create a shipping item self.shipping = Shipping.create(acknowledgement={'id': 1}, customer={'id': 1}, user=self.user, delivery_date=base_delivery_date, items=[{ 'id': 1 }, { 'id': 2 }]) self.shipping.save() def get_credentials(self): return self.user #self.create_basic(username=self.username, password=self.password) def setup_client(self): # Login the Client # APIClient #self.client = APIClient(enforce_csrf_checks=False) #self.client.login(username=self.username, password=self.password) self.client.force_authenticate(self.user) def test_get_list(self): """ Tests getting a list of objects via GET """ self.skipTest('') #Create a shipping to retrieve self.create_shipping() resp = self.client.get('/api/v1/shipping/', format='json', authentication=self.get_credentials()) self.assertEqual(resp.status_code, 200) #Validate the resources returned resp_obj = resp.data self.assertEqual(len(resp_obj['objects']), 1) def test_get(self): """ Tests getting an object via GET """ self.skipTest('') self.create_shipping() #Test the resp resp = self.client.get('/api/v1/shipping/1/', format='json', authentication=self.get_credentials()) self.assertEqual(resp.status_code, 200) #Validate the object obj = resp.data self.assertEqual(obj['id'], 1) self.assertIn("customer", obj) self.assertEqual(obj['customer']['id'], 1) def test_post_project_shipping(self): """ Test creating a project packing list via POST """ data = { 'project': { 'id': 1 }, 'customer': { 'id': 1 }, 'phase': { 'id': 1 }, 'items': [{ 'description': 'TK 1/2' }] } resp = self.client.post('/api/v1/shipping/', format='json', data=data) # Test client response self.assertEqual(resp.status_code, 201, msg=resp) def test_post_with_two_item(self): """ Tests creating a resource via POST """ #Validate the resp and obj creation self.assertEqual(Shipping.objects.count(), 0) shipping_data = { 'acknowledgement': { 'id': 1 }, 'customer': { 'id': 1 }, 'delivery_date': base_delivery_date, 'items': [{ 'item': { 'id': 1 }, 'description': 'test1', 'quantity': 1 }, { 'item': { 'id': 2 } }] } resp = self.client.post('/api/v1/shipping/', data=shipping_data, format='json') self.assertEqual(resp.status_code, 201, msg=resp) self.assertEqual(Shipping.objects.count(), 1) #validate the object returned obj = resp.data self.assertEqual(obj['id'], 1) self.assertIn('customer', obj) self.assertEqual(obj['customer']['id'], 1) self.assertIn('last_modified', obj) self.assertIn('time_created', obj) self.assertEqual(len(obj['items']), 2) item1 = obj['items'][0] #Validate resource in the database shipping = Shipping.objects.get(pk=1) self.assertEqual(shipping.id, 1) self.assertEqual(shipping.customer.id, 1) self.assertEqual(shipping.items.count(), 2) def test_post_with_one_item(self): """ Tests creating a resource via POST """ #Validate the resp and obj creation self.assertEqual(Shipping.objects.count(), 0) shipping_data = { 'acknowledgement': { 'id': 1 }, 'customer': { 'id': 1 }, 'delivery_date': base_delivery_date, 'items': [{ 'item': { 'id': 1 }, 'description': 'test1', 'quantity': 1 }] } resp = self.client.post('/api/v1/shipping/', data=shipping_data, format='json') self.assertEqual(resp.status_code, 201, msg=resp) self.assertEqual(Shipping.objects.count(), 1) #validate the object returned obj = resp.data self.assertEqual(obj['id'], 1) self.assertIn('customer', obj) self.assertEqual(obj['customer']['id'], 1) self.assertIn('last_modified', obj) self.assertIn('time_created', obj) self.assertEqual(len(obj['items']), 1) item1 = obj['items'][0] #Validate resource in the database shipping = Shipping.objects.get(pk=1) self.assertEqual(shipping.id, 1) self.assertEqual(shipping.customer.id, 1) self.assertEqual(shipping.items.count(), 1) def test_put(self): """ Tests updating a resource via PUT """ self.skipTest('') self.create_shipping() self.assertEqual(Shipping.objects.count(), 1) resp = self.client.put('/api/v1/shipping/1/', format='json', authentication=self.get_credentials(), data={ 'delivery_date': base_delivery_date, 'acknowledgement': { 'id': 1 } }) self.assertEqual(resp.status_code, 200, msg=resp) self.assertEqual(Shipping.objects.count(), 1) #Validate the obj obj = resp.data self.assertEqual(obj['id'], 1) self.assertEqual(obj['customer']['id'], 1) self.assertEqual(obj['comments'], 'test') def test_delete(self): """ Tests deleting a resource via DELETE """ self.skipTest('') self.create_shipping() self.assertEqual(Shipping.objects.count(), 1) resp = self.client.delete('/api/v1/shipping/1/', format='json', authentication=self.get_credentials()) self.assertEqual(resp.status_code, 204) self.assertEqual(Shipping.objects.count(), 0)
def setUp(self): """ Set up for the Acknowledgement Test Objects created: -User -Customer -Supplier -Address -product -2 fabrics After Creating all the needed objects for the Acknowledgement, test that all the objects have been made. """ super(ShippingResourceTest, self).setUp() self.ct = ContentType(app_label="shipping") self.ct.save() #Create the user self.username = '******' self.password = '******' self.user = User.objects.create_user(self.username, '*****@*****.**', self.password) self.user.save() p = Permission(content_type=self.ct, codename="change_shipping") p.save() p2 = Permission(content_type=self.ct, codename="add_shipping") p2.save() self.user.user_permissions.add(p) self.user.user_permissions.add(p2) self.user.save() self.setup_client() #Create supplier, customer and addrss self.customer = Customer(**base_customer) self.customer.save() self.supplier = Supplier(**base_supplier) self.supplier.save() self.address = Address(address1="Jiggle", contact=self.customer) self.address.save() #Create project self.project = Project.objects.create(codename="Ladawan") #Create phase self.phase = Phase.objects.create(description="Phase 1/6", project=self.project) #Create a product to add self.product = Product.create(self.user, **base_product) self.product.save() self.fabric = Fabric.create(**base_fabric) f_data = base_fabric.copy() f_data["pattern"] = "Stripe" self.fabric2 = Fabric.create(**f_data) #Create acknowledgement ack_data = base_ack.copy() del ack_data['customer'] del ack_data['items'] del ack_data['employee'] self.ack = Acknowledgement(**ack_data) self.ack.customer = self.customer self.ack.employee = self.user self.ack.save() #Create an item item_data = { 'id': 1, 'quantity': 1, 'is_custom_size': True, 'width': 1500, "fabric": { "id": 1 } } self.item = AckItem.create(acknowledgement=self.ack, **item_data) #Create an item item_data = { 'id': 1, 'quantity': 2, 'is_custom_size': True, 'width': 1500, "fabric": { "id": 1 } } self.item2 = AckItem.create(acknowledgement=self.ack, **item_data)
class EstimateResourceTest(APITestCase): """" This tests the api acknowledgements: GET list: -get a list of objects -objects have items and items have pillows GET: -the acknowledgement has delivery date, order date customer, status, total, vat, employee, discount -the acknowledgement has a list of items. -The items have pillows and fabrics -pillows have fabrics -the items has dimensions, pillows, fabric, comments price per item POST: -create an acknowledgement that has delivery date, order date customer, status, total, vat, employee, discount, items -the items should have fabrics and pillows where appropriate """ def setUp(self): """ Set up for the Estimate Test Objects created: -User -Customer -Supplier -Address -product -2 fabrics After Creating all the needed objects for the Estimate, test that all the objects have been made. """ super(EstimateResourceTest, self).setUp() self.ct = ContentType(app_label="estimates") self.ct.save() #Create the user self.username = '******' self.password = '******' self.user = User.objects.create_user(self.username, '*****@*****.**', self.password) p = Permission(content_type=self.ct, codename="change_estimate") p.save() p2 = Permission(content_type=self.ct, codename="add_estimate") p2.save() self.user.user_permissions.add(p) self.user.user_permissions.add(p2) self.user.save() #Create supplier, customer and addrss customer = copy.deepcopy(base_customer) del customer['id'] self.customer = Customer(**customer) self.customer.save() self.supplier = Supplier(**base_supplier) self.supplier.save() self.address = Address(address1="Jiggle", contact=self.customer) self.address.save() #Create a product to add self.product = Product.create(self.user, **base_product) self.product.save() #Create custom product self.custom_product = Product() self.custom_product.id = 10436 self.custom_product.save() self.fabric = Fabric.create(**base_fabric) self.fabric.quantity = 26 self.fabric.save() f_data = base_fabric.copy() f_data["pattern"] = "Stripe" self.fabric2 = Fabric.create(**f_data) #Create custom product self.custom_product = Product.create(self.user, description="Custom Custom", id=10436, width=0, depth=0, height=0, price=0, wholesale_price=0, retail_price=0) self.custom_product.id = 10436 self.custom_product.save() self.image = S3Object(key='test', bucket='test') self.image.save() #Create acknowledgement ack_data = base_ack.copy() del ack_data['customer'] del ack_data['items'] del ack_data['employee'] del ack_data['project'] self.ack = Estimate(**ack_data) self.ack.customer = self.customer self.ack.employee = self.user self.ack.save() #Create an item item_data = {'id': 1, 'quantity': 1, 'is_custom_size': True, 'width': 1500, "fabric": {"id":1}} self.item = Item.create(estimate=self.ack, **item_data) item_data = {'is_custom': True, 'description': 'F-04 Sofa', 'quantity': 3} self.item2 = Item.create(estimate=self.ack, **item_data) self.client.login(username="******", password="******") #Create fake S3Objects to test files attached to acknowledgements self.file1 = S3Object(key='test1', bucket='test') self.file2 = S3Object(key='test2', bucket='test') self.file1.save() self.file2.save() def get_credentials(self): return None#self.create_basic(username=self.username, password=self.password) def test_get_list(self): """ Tests getting the list of acknowledgements """ #Get and verify the resp resp = self.client.get('/api/v1/estimate/') self.assertEqual(resp.status_code, 200, msg=resp) #Verify the data sent resp_obj = resp.data self.assertIsNotNone(resp_obj['results']) self.assertEqual(len(resp_obj['results']), 1) self.assertEqual(len(resp_obj['results'][0]['items']), 2) def test_get(self): """ Tests getting the acknowledgement """ #Get and verify the resp resp = self.client.get('/api/v1/estimate/1/') self.assertEqual(resp.status_code, 200, msg=resp) #Verify the data sent ack = resp.data self.assertIsNotNone(ack) self.assertEqual(ack['id'], 1) self.assertEqual(ack['customer']['id'], 1) self.assertEqual(ack['po_id'], '123-213-231') self.assertEqual(dateutil.parser.parse(ack['delivery_date']), base_delivery_date) self.assertEqual(ack['vat'], 0) self.assertEqual(Decimal(ack['total']), Decimal(0)) def xtest_post_dr_vs_pci(self): """ Test POSTING ack with company as 'Dellarobbia Thailand' vs 'Pacific Carpet' """ logger.debug("\n\n Testing creating acknowledgement with diferring companies\n") ack1_data = copy.deepcopy(base_ack) ack1_data['company'] = 'Dellarobbia Thailand' ack def test_post_with_discount(self): """ Testing POSTing data to the api """ logger.debug("\n\n Testing creating acknowledgement with a discount \n") #Apply a discount to the customer self.customer.discount = 50 self.customer.save() modified_data = copy.deepcopy(base_ack) modified_data['items'][-1]['fabric'] = {'id': 1, 'image': {'id': 1}} modified_data['items'][-1]['fabric_quantity'] = 8 modified_data['files'] = [{'id': 1}, {'id': 2}] #POST and verify the response self.assertEqual(Estimate.objects.count(), 1) resp = self.client.post('/api/v1/estimate/', data=modified_data, format='json') #Verify that http response is appropriate self.assertEqual(resp.status_code, 201, msg=resp) #Verify that an acknowledgement is created in the system self.assertEqual(Estimate.objects.count(), 2) #Verify the resulting acknowledgement #that is returned from the post data ack = resp.data self.assertIsNotNone(ack) self.assertEqual(ack['id'], 2) self.assertEqual(ack['customer']['id'], 1) self.assertEqual(ack['employee']['id'], 1) self.assertEqual(ack['vat'], 0) self.assertEqual(Decimal(ack['total']), Decimal(158500)) self.assertEqual(len(ack['items']), 3) self.assertIn('project', ack) self.assertEqual(ack['project']['id'], 1) self.assertEqual(ack['project']['codename'], 'Ladawan1') #self.assertIsInstance(ack['files'], list) #self.assertEqual(len(ack['files']), 2) #Test standard sized item item1 = ack['items'][0] self.assertEqual(item1['id'], 3) self.assertEqual(item1['description'], 'Test Sofa Max') self.assertEqual(item1['quantity'], 2) self.assertFalse(item1['is_custom_size']) self.assertFalse(item1['is_custom_item']) self.assertEqual(item1['width'], 1000) self.assertEqual(item1['height'], 320) self.assertEqual(item1['depth'], 760) self.assertEqual(item1['fabric']['id'], 1) self.assertEqual(len(item1['pillows']), 4) self.assertEqual(Decimal(item1['unit_price']), Decimal(100000)) self.assertEqual(Decimal(item1['total']), Decimal(200000)) #Test custom sized item item2 = ack['items'][1] self.assertEqual(item2['id'], 4) self.assertEqual(item2['description'], 'High Gloss Table') self.assertEqual(item2['quantity'], 1) self.assertTrue(item2['is_custom_size']) self.assertFalse(item2['is_custom_item']) self.assertEqual(item2['width'], 1500) self.assertEqual(item2['height'], 320) self.assertEqual(item2['depth'], 760) self.assertEqual(item2['fabric']['id'], 1) self.assertEqual(Decimal(item2['unit_price']), Decimal(117000)) self.assertEqual(Decimal(item2['total']), Decimal(117000)) #Test custom item with width item3 = ack['items'][2] self.assertEqual(item3['width'], 1) self.assertEqual(item3['description'], 'test custom item') self.assertTrue(item3['is_custom_item']) self.assertEqual(item3['quantity'], 1) self.assertEqual(Decimal(item3['unit_price']), 0) self.assertEqual(item3['fabric']['id'], 1) #Tests links to document """ self.assertIsNotNone(ack['pdf']) self.assertIsNotNone(ack['pdf']['acknowledgement']) self.assertIsNotNone(ack['pdf']['production']) self.assertIsNotNone(ack['pdf']['confirmation']) """ #Tests the acknowledgement in the database root_ack = Estimate.objects.get(pk=2) logger.debug(root_ack.project) self.assertEqual(root_ack.id, 2) self.assertEqual(root_ack.items.count(), 3) self.assertIsInstance(root_ack.project, Project) self.assertEqual(root_ack.project.id, 1) self.assertEqual(root_ack.project.codename, "Ladawan1") root_ack_items = root_ack.items.all() item1 = root_ack_items[0] item2 = root_ack_items[1] item3 = root_ack_items[2] self.assertEqual(item1.estimate.id, 2) self.assertEqual(item1.description, 'Test Sofa Max') self.assertEqual(item1.quantity, 2) self.assertEqual(item1.width, 1000) self.assertEqual(item1.height, 320) self.assertEqual(item1.depth, 760) self.assertFalse(item1.is_custom_item) self.assertFalse(item1.is_custom_size) self.assertEqual(item2.estimate.id, 2) self.assertEqual(item2.description, 'High Gloss Table') self.assertEqual(item2.width, 1500) self.assertEqual(item2.height, 320) self.assertEqual(item2.depth, 760) self.assertTrue(item2.is_custom_size) self.assertFalse(item2.is_custom_item) self.assertEqual(item3.estimate.id, 2) self.assertEqual(item3.description, 'test custom item') self.assertEqual(item3.width, 1) self.assertTrue(item3.is_custom_item) self.assertEqual(item3.quantity, 1) #Tests files attached to acknowledgements """ self.assertEqual(root_ack.files.all().count(), 2) file1 = root_ack.files.all()[0] self.assertEqual(file1.id, 1) file2 = root_ack.files.all()[1] self.assertEqual(file2.id, 2) #Test Fabric Log self.assertEqual(Log.objects.filter(acknowledgement_id=root_ack.id).count(), 1) log = Log.objects.get(acknowledgement_id=root_ack.id) self.assertEqual(log.quantity, Decimal('33')) self.assertEqual(log.action, 'RESERVE') self.assertEqual(log.acknowledgement_id, '2') self.assertEqual(log.message, 'Reserve 33m of Pattern: Max, Col: charcoal for Ack#2') self.assertEqual(Fabric.objects.get(id=1).quantity, Decimal('-7')) """ def test_post_with_custom_image(self): """ Testing POSTing data to the api with custom item with custom image """ logger.debug("\n\n Testing creating acknowledgement with a custom image \n") #Apply a discount to the customer ack = copy.deepcopy(base_ack) ack['items'][2]['image'] = {'id': 1} #POST and verify the response self.assertEqual(Estimate.objects.count(), 1) resp = self.client.post('/api/v1/estimate/', data=ack, format='json') #Verify that http response is appropriate self.assertEqual(resp.status_code, 201, msg=resp) #Verify that an acknowledgement is created in the system self.assertEqual(Estimate.objects.count(), 2) #Verify the resulting acknowledgement #that is returned from the post data ack = resp.data self.assertIsNotNone(ack) self.assertEqual(ack['id'], 2) self.assertEqual(ack['customer']['id'], 1) self.assertEqual(ack['employee']['id'], 1) self.assertEqual(ack['vat'], 0) self.assertEqual(Decimal(ack['total']), Decimal(317000)) self.assertEqual(len(ack['items']), 3) self.assertIn('project', ack) self.assertEqual(ack['project']['id'], 1) self.assertEqual(ack['project']['codename'], 'Ladawan1') #Test standard sized item item1 = ack['items'][0] self.assertEqual(item1['id'], 3) self.assertEqual(item1['description'], 'Test Sofa Max') self.assertEqual(item1['quantity'], 2) self.assertFalse(item1['is_custom_size']) self.assertFalse(item1['is_custom_item']) self.assertEqual(item1['width'], 1000) self.assertEqual(item1['height'], 320) self.assertEqual(item1['depth'], 760) self.assertEqual(item1['fabric']['id'], 1) self.assertEqual(len(item1['pillows']), 4) self.assertEqual(Decimal(item1['unit_price']), Decimal(100000)) self.assertEqual(Decimal(item1['total']), Decimal(200000)) #Test custom sized item item2 = ack['items'][1] self.assertEqual(item2['id'], 4) self.assertEqual(item2['description'], 'High Gloss Table') self.assertEqual(item2['quantity'], 1) self.assertTrue(item2['is_custom_size']) self.assertFalse(item2['is_custom_item']) self.assertEqual(item2['width'], 1500) self.assertEqual(item2['height'], 320) self.assertEqual(item2['depth'], 760) self.assertEqual(item2['fabric']['id'], 1) self.assertEqual(Decimal(item2['unit_price']), Decimal(117000)) self.assertEqual(Decimal(item2['total']), Decimal(117000)) #Test custom item with width item3 = ack['items'][2] self.assertEqual(item3['width'], 1) self.assertEqual(item3['description'], 'test custom item') self.assertTrue(item3['is_custom_item']) self.assertEqual(item3['quantity'], 1) self.assertEqual(Decimal(item3['unit_price']), 0) self.assertIsNotNone(item3['image']) self.assertIn('url', item3['image']) #Tests links to document self.assertIsNotNone(ack['pdf']) #self.assertIsNotNone(ack['pdf']['acknowledgement']) #self.assertIsNotNone(ack['pdf']['production']) #self.assertIsNotNone(ack['pdf']['confirmation']) #logger.debug(ack['pdf']['confirmation']) #print "\n\n\n" #Tests the acknowledgement in the database root_ack = Estimate.objects.get(pk=2) logger.debug(root_ack.project) self.assertEqual(root_ack.id, 2) self.assertEqual(root_ack.items.count(), 3) self.assertIsInstance(root_ack.project, Project) self.assertEqual(root_ack.project.id, 1) self.assertEqual(root_ack.project.codename, "Ladawan1") root_ack_items = root_ack.items.all() item1 = root_ack_items[0] item2 = root_ack_items[1] item3 = root_ack_items[2] self.assertEqual(item1.estimate.id, 2) self.assertEqual(item1.description, 'Test Sofa Max') self.assertEqual(item1.quantity, 2) self.assertEqual(item1.width, 1000) self.assertEqual(item1.height, 320) self.assertEqual(item1.depth, 760) self.assertFalse(item1.is_custom_item) self.assertFalse(item1.is_custom_size) self.assertEqual(item2.estimate.id, 2) self.assertEqual(item2.description, 'High Gloss Table') self.assertEqual(item2.width, 1500) self.assertEqual(item2.height, 320) self.assertEqual(item2.depth, 760) self.assertTrue(item2.is_custom_size) self.assertFalse(item2.is_custom_item) self.assertEqual(item3.estimate.id, 2) self.assertEqual(item3.description, 'test custom item') self.assertEqual(item3.width, 1) self.assertTrue(item3.is_custom_item) self.assertEqual(item3.quantity, 1) def test_post_without_vat(self): """ Testing POSTing data to the api """ logger.debug("\n\n Testing creating acknowledgement without vat \n") #POST and verify the response self.assertEqual(Estimate.objects.count(), 1) resp = self.client.post('/api/v1/estimate/', format='json', data=base_ack, authentication=self.get_credentials()) self.assertEqual(resp.status_code, 201, msg=resp) self.assertEqual(Estimate.objects.count(), 2) #Verify the resulting acknowledgement #that is returned from the post data ack = resp.data self.assertIsNotNone(ack) self.assertEqual(ack['id'], 2) self.assertEqual(ack['customer']['id'], 1) self.assertEqual(ack['employee']['id'], 1) self.assertEqual(ack['vat'], 0) self.assertEqual(Decimal(ack['total']), Decimal('317000')) self.assertEqual(len(ack['items']), 3) self.assertIn('project', ack) self.assertEqual(ack['project']['id'], 1) self.assertEqual(ack['project']['codename'], 'Ladawan1') #Test standard sized item item1 = ack['items'][0] self.assertEqual(item1['id'], 3) self.assertEqual(item1['description'], 'Test Sofa Max') self.assertEqual(item1['quantity'], 2) self.assertFalse(item1['is_custom_size']) self.assertFalse(item1['is_custom_item']) self.assertEqual(item1['width'], 1000) self.assertEqual(item1['height'], 320) self.assertEqual(item1['depth'], 760) self.assertEqual(item1['fabric']['id'], 1) self.assertEqual(len(item1['pillows']), 4) self.assertEqual(Decimal(item1['unit_price']), Decimal(100000)) self.assertEqual(Decimal(item1['total']), Decimal(200000)) #Test custom sized item item2 = ack['items'][1] self.assertEqual(item2['id'], 4) self.assertEqual(item2['description'], 'High Gloss Table') self.assertEqual(item2['quantity'], 1) self.assertTrue(item2['is_custom_size']) self.assertFalse(item2['is_custom_item']) self.assertEqual(item2['width'], 1500) self.assertEqual(item2['height'], 320) self.assertEqual(item2['depth'], 760) self.assertEqual(item2['fabric']['id'], 1) self.assertEqual(Decimal(item2['unit_price']), Decimal(117000)) self.assertEqual(Decimal(item2['total']), Decimal(117000)) #Test custom item with width item3 = ack['items'][2] self.assertEqual(item3['width'], 1) self.assertEqual(item3['description'], 'test custom item') self.assertTrue(item3['is_custom_item']) self.assertEqual(item3['quantity'], 1) self.assertEqual(Decimal(item3['unit_price']), 0) #Tests links to document self.assertIsNotNone(ack['pdf']) #?self.assertIsNotNone(ack['pdf']['acknowledgement']) #self.assertIsNotNone(ack['pdf']['production']) #self.assertIsNotNone(ack['pdf']['confirmation']) def test_post_with_vat(self): """ Testing POSTing data to the api if there is vat """ logger.debug("\n\n Testing creating acknowledgement with vat \n") #Altering replication of base ack data ack_data = base_ack.copy() ack_data['vat'] = 7 #Verifying current number of acknowledgements in database self.assertEqual(Estimate.objects.count(), 1) resp = self.client.post('/api/v1/estimate/', format='json', data=ack_data, authentication=self.get_credentials()) self.assertEqual(resp.status_code, 201, msg=resp) self.assertEqual(Estimate.objects.count(), 2) #Verify the resulting acknowledgement #that is returned from the post data ack = resp.data self.assertIsNotNone(ack) self.assertEqual(ack['id'], 2) self.assertEqual(ack['customer']['id'], 1) self.assertEqual(ack['employee']['id'], 1) self.assertEqual(ack['vat'], 7) self.assertEqual(Decimal(ack['total']), Decimal(339190.00)) self.assertEqual(len(ack['items']), 3) self.assertIn('project', ack) self.assertEqual(ack['project']['id'], 1) self.assertEqual(ack['project']['codename'], 'Ladawan1') #Test standard sized item item1 = ack['items'][0] self.assertEqual(item1['id'], 3) self.assertEqual(item1['description'], 'Test Sofa Max') self.assertEqual(item1['quantity'], 2) self.assertFalse(item1['is_custom_size']) self.assertFalse(item1['is_custom_item']) self.assertEqual(item1['width'], 1000) self.assertEqual(item1['height'], 320) self.assertEqual(item1['depth'], 760) self.assertEqual(item1['fabric']['id'], 1) self.assertEqual(len(item1['pillows']), 4) self.assertEqual(Decimal(item1['unit_price']), Decimal(100000)) self.assertEqual(Decimal(item1['total']), Decimal(200000)) #Test custom sized item item2 = ack['items'][1] self.assertEqual(item2['id'], 4) self.assertEqual(item2['description'], 'High Gloss Table') self.assertEqual(item2['quantity'], 1) self.assertTrue(item2['is_custom_size']) self.assertFalse(item2['is_custom_item']) self.assertEqual(item2['width'], 1500) self.assertEqual(item2['height'], 320) self.assertEqual(item2['depth'], 760) self.assertEqual(item2['fabric']['id'], 1) self.assertEqual(Decimal(item2['unit_price']), Decimal(117000)) self.assertEqual(Decimal(item2['total']), Decimal(117000)) #Test custom item with width item3 = ack['items'][2] self.assertEqual(item3['width'], 1) self.assertEqual(item3['description'], 'test custom item') self.assertTrue(item3['is_custom_item']) self.assertEqual(item3['quantity'], 1) self.assertEqual(Decimal(item3['unit_price']), 0) #Tests links to document self.assertIsNotNone(ack['pdf']) #self.assertIsNotNone(ack['pdf']['acknowledgement']) #self.assertIsNotNone(ack['pdf']['production']) #self.assertIsNotNone(ack['pdf']['confirmation']) def test_post_with_vat_and_discount(self): """ Testing POSTing data to the api if there is vat """ logger.debug("\n\n Testing creating acknowledgement with a discount and vat \n") #Set customer discount self.customer.discount = 50 self.customer.save() #POST and verify the response ack_data = base_ack.copy() ack_data['vat'] = 7 self.assertEqual(Estimate.objects.count(), 1) resp = self.client.post('/api/v1/estimate/', format='json', data=ack_data, authentication=self.get_credentials()) self.assertEqual(resp.status_code, 201, msg=resp) self.assertEqual(Estimate.objects.count(), 2) #Verify the resulting acknowledgement #that is returned from the post data ack = resp.data self.assertIsNotNone(ack) self.assertEqual(ack['id'], 2) self.assertEqual(ack['customer']['id'], 1) self.assertEqual(ack['employee']['id'], 1) self.assertEqual(ack['vat'], 7) self.assertEqual(Decimal(ack['total']), Decimal('169595.000')) self.assertEqual(len(ack['items']), 3) #Test standard sized item item1 = ack['items'][0] self.assertEqual(item1['id'], 3) self.assertEqual(item1['description'], 'Test Sofa Max') self.assertEqual(item1['quantity'], 2) self.assertFalse(item1['is_custom_size']) self.assertFalse(item1['is_custom_item']) self.assertEqual(item1['width'], 1000) self.assertEqual(item1['height'], 320) self.assertEqual(item1['depth'], 760) self.assertEqual(item1['fabric']['id'], 1) self.assertEqual(len(item1['pillows']), 4) self.assertEqual(Decimal(item1['unit_price']), Decimal(100000)) self.assertEqual(Decimal(item1['total']), Decimal(200000)) #Test custom sized item item2 = ack['items'][1] self.assertEqual(item2['id'], 4) self.assertEqual(item2['description'], 'High Gloss Table') self.assertEqual(item2['quantity'], 1) self.assertTrue(item2['is_custom_size']) self.assertFalse(item2['is_custom_item']) self.assertEqual(item2['width'], 1500) self.assertEqual(item2['height'], 320) self.assertEqual(item2['depth'], 760) self.assertEqual(item2['fabric']['id'], 1) self.assertEqual(Decimal(item2['unit_price']), Decimal(117000)) self.assertEqual(Decimal(item2['total']), Decimal(117000)) #Test custom item with width item3 = ack['items'][2] self.assertEqual(item3['width'], 1) self.assertEqual(item3['description'], 'test custom item') self.assertTrue(item3['is_custom_item']) self.assertEqual(item3['quantity'], 1) self.assertEqual(Decimal(item3['unit_price']), 0) #Tests links to document self.assertIsNotNone(ack['pdf']) #self.assertIsNotNone(ack['pdf']['acknowledgement']) #self.assertIsNotNone(ack['pdf']['production']) #self.assertIsNotNone(ack['pdf']['confirmation']) def test_post_with_custom_price(self): """ Test creating a custom price for all three item types """ logger.debug("\n\n Testing creating acknowledgement with custom prices for all items \n") #POST and verify the response ack_data = copy.deepcopy(base_ack) ack_data['items'][0]['custom_price'] = 100 ack_data['items'][1]['custom_price'] = 200 ack_data['items'][2]['custom_price'] = 300 self.assertEqual(Estimate.objects.count(), 1) resp = self.client.post('/api/v1/estimate/', format='json', data=ack_data, authentication=self.get_credentials()) self.assertEqual(resp.status_code, 201, msg=resp) self.assertEqual(Estimate.objects.count(), 2) #Verify the resulting acknowledgement #that is returned from the post data ack = resp.data self.assertIsNotNone(ack) self.assertEqual(ack['id'], 2) self.assertEqual(ack['customer']['id'], 1) self.assertEqual(ack['employee']['id'], 1) self.assertEqual(ack['vat'], 0) self.assertEqual(Decimal(ack['total']), Decimal('700.00')) self.assertEqual(len(ack['items']), 3) #Test standard sized item item1 = ack['items'][0] self.assertEqual(item1['id'], 3) self.assertEqual(item1['description'], 'Test Sofa Max') self.assertEqual(item1['quantity'], 2) self.assertFalse(item1['is_custom_size']) self.assertFalse(item1['is_custom_item']) self.assertEqual(item1['width'], 1000) self.assertEqual(item1['height'], 320) self.assertEqual(item1['depth'], 760) self.assertEqual(item1['fabric']['id'], 1) self.assertEqual(len(item1['pillows']), 4) self.assertEqual(Decimal(item1['unit_price']), Decimal('100')) self.assertEqual(Decimal(item1['total']), Decimal('200')) #Test custom sized item item2 = ack['items'][1] self.assertEqual(item2['id'], 4) self.assertEqual(item2['description'], 'High Gloss Table') self.assertEqual(item2['quantity'], 1) self.assertTrue(item2['is_custom_size']) self.assertFalse(item2['is_custom_item']) self.assertEqual(item2['width'], 1500) self.assertEqual(item2['height'], 320) self.assertEqual(item2['depth'], 760) self.assertEqual(item2['fabric']['id'], 1) self.assertEqual(Decimal(item2['unit_price']), Decimal('200')) self.assertEqual(Decimal(item2['total']), Decimal('200')) #Test custom item with width item3 = ack['items'][2] self.assertEqual(item3['width'], 1) self.assertEqual(item3['description'], 'test custom item') self.assertTrue(item3['is_custom_item']) self.assertEqual(item3['quantity'], 1) self.assertEqual(Decimal(item3['unit_price']), Decimal('300')) #Tests links to document self.assertIsNotNone(ack['pdf']) #self.assertIsNotNone(ack['pdf']['acknowledgement']) #self.assertIsNotNone(ack['pdf']['production']) #self.assertIsNotNone(ack['pdf']['confirmation']) def test_post_where_first_item_has_no_fabric(self): """ Test creating a custom price for all three item types """ logger.debug("\n\n Testing creating acknowledgement with custom prices for all items \n") #POST and verify the response ack_data = copy.deepcopy(base_ack) del ack_data['items'][0]['fabric'] del ack_data['items'][0]['pillows'][0]['fabric'] self.assertEqual(Estimate.objects.count(), 1) resp = self.client.post('/api/v1/estimate/', format='json', data=ack_data, authentication=self.get_credentials()) self.assertEqual(resp.status_code, 201, msg=resp) self.assertEqual(Estimate.objects.count(), 2) #Verify the resulting acknowledgement #that is returned from the post data ack = resp.data self.assertIsNotNone(ack) self.assertEqual(ack['id'], 2) self.assertEqual(ack['customer']['id'], 1) self.assertEqual(ack['employee']['id'], 1) self.assertEqual(ack['vat'], 0) self.assertEqual(Decimal(ack['total']), Decimal('317000')) self.assertEqual(len(ack['items']), 3) #Test standard sized item item1 = ack['items'][0] self.assertEqual(item1['id'], 3) self.assertEqual(item1['description'], 'Test Sofa Max') self.assertEqual(item1['quantity'], 2) self.assertFalse(item1['is_custom_size']) self.assertFalse(item1['is_custom_item']) self.assertEqual(item1['width'], 1000) self.assertEqual(item1['height'], 320) self.assertEqual(item1['depth'], 760) self.assertIsNone(item1['fabric']) self.assertEqual(len(item1['pillows']), 5) self.assertEqual(Decimal(item1['unit_price']), Decimal('100000')) self.assertEqual(Decimal(item1['total']), Decimal('200000')) #Test custom sized item item2 = ack['items'][1] self.assertEqual(item2['id'], 4) self.assertEqual(item2['description'], 'High Gloss Table') self.assertEqual(item2['quantity'], 1) self.assertTrue(item2['is_custom_size']) self.assertFalse(item2['is_custom_item']) self.assertEqual(item2['width'], 1500) self.assertEqual(item2['height'], 320) self.assertEqual(item2['depth'], 760) self.assertEqual(item2['fabric']['id'], 1) self.assertEqual(Decimal(item2['unit_price']), Decimal('117000')) self.assertEqual(Decimal(item2['total']), Decimal('117000')) #Test custom item with width item3 = ack['items'][2] self.assertEqual(item3['width'], 1) self.assertEqual(item3['description'], 'test custom item') self.assertTrue(item3['is_custom_item']) self.assertEqual(item3['quantity'], 1) #Tests links to document self.assertIsNotNone(ack['pdf']) #self.assertIsNotNone(ack['pdf']['acknowledgement']) #self.assertIsNotNone(ack['pdf']['production']) #self.assertIsNotNone(ack['pdf']['confirmation']) @unittest.skip('currently not allowed') def test_put(self): """ Test making a PUT call """ logger.debug("\n\n Testing updating via put \n") ack_data = base_ack.copy() ack_data['items'][0]['id'] = 1 del ack_data['items'][0]['pillows'][-1] ack_data['items'][1]['id'] = 2 ack_data['items'][1]['description'] = 'F-04 Sofa' ack_data['items'][1]['is_custom_item'] = True del ack_data['items'][2] ack_data['delivery_date'] = datetime.now() self.assertEqual(Estimate.objects.count(), 1) resp = self.client.put('/api/v1/estimate/1/', format='json', data=ack_data, authentication=self.get_credentials()) logger.debug(resp) self.assertEqual(resp.status_code, 200) self.assertEqual(Estimate.objects.count(), 1) #Validate the change ack = resp.data #self.assertEqual(dateutil.parser.parse(ack['delivery_date']), ack_data['delivery_date']) logger.debug(ack['items'][0]['pillows']) #Tests ack in database ack = Estimate.objects.get(pk=1) items = ack.items.all() item1 = items[0] self.assertEqual(item1.description, 'Test Sofa Max') self.assertEqual(item1.pillows.count(), 3) item2 = items[1] self.assertEqual(item2.description, 'F-04 Sofa') self.assertTrue(item2.is_custom_item) def test_changing_delivery_date(self): """ Test making a PUT call """ logger.debug("\n\n Testing updating via put \n") d = datetime.now() ack_data = base_ack.copy() ack_data['delivery_date'] = d self.assertEqual(Estimate.objects.count(), 1) resp = self.client.put('/api/v1/estimate/1/', format='json', data=ack_data, authentication=self.get_credentials()) self.assertEqual(resp.status_code, 200, msg=resp) ack = resp.data #logger.debug(ack['delivery_date']) #d1 = datetime.strptime(ack['delivery_date']) #self.assertEqual(d1.date(), d.date()) ack = Estimate.objects.all()[0] self.assertEqual(ack.delivery_date.date(), d.date()) #@unittest.skip('ok') def test_delete(self): """ Test making a DELETE call 'Delete' in this model has been overridden so that no acknowledgement is truly deleted. Instead the 'delete' column in the database is marked as true """ self.assertEqual(Estimate.objects.count(), 1) resp = self.client.delete('/api/v1/estimate/1/', authentication=self.get_credentials()) self.assertEqual(Estimate.objects.count(), 1)