def new_contact(request): data = {} template = 'contacts/new_contact.html' data['message'] = None if request.method == "POST": data['form'] = ContactForm(request.POST) if data['form'].is_valid(): # each Address has a User ID # TODO: if email exists - solve this passwd = User.objects.make_random_password() user = User(username=data['form'].cleaned_data['searchname'], email=data['form'].cleaned_data['email'], password=passwd) user.save() adr = Address(adr_searchname=data['form'].cleaned_data['searchname'], adr_email=data['form'].cleaned_data['email'], adr_user_id=user, ) adr.save() contacttypes = ContactType.objects.all() for ct_type in contacttypes: ctdata = ContactData(cd_contacttype_id=ct_type, cd_textfield=data['form'].cleaned_data['{index}'.format(index=ct_type.id)], cd_address_id=adr) ctdata.save() return redirect('proj_contacts') else: data['form'] = ContactForm() print data return render(request, template, data)
def edit_contact(request, address_id): message = None contacttypes = ContactType.objects.all() categories = Category.objects.all() if request.method == "POST": form = ContactForm(request.POST) if form.is_valid(): adr = Address(id=address_id, adr_searchname=form.cleaned_data['searchname'], adr_email=form.cleaned_data['email']) adr.save() # TODO: ct_type proof need to be implemented for ct_type in contacttypes: cd_id = ContactData.objects.filter(cd_contacttype_id__id=ct_type.id, cd_address_id__id=address_id) if len(cd_id) > 0: ctdata = ContactData(id=cd_id, cd_contacttype_id=ct_type, cd_textfield=form.cleaned_data['{index}'.format(index=ct_type.id)], cd_address_id=adr) else: ctdata = ContactData(cd_contacttype_id=ct_type, cd_textfield=form.cleaned_data['{index}'.format(index=ct_type.id)], cd_address_id=adr) print (ctdata, cd_id) ctdata.save() return redirect('all_contacts') else: adr = get_object_or_404(Address, pk=address_id) datadict = {'searchname': adr.adr_searchname, 'email': adr.adr_email} adr_data = ContactData.objects.filter(cd_address_id=address_id).order_by('cd_contacttype_id__ct_sort_id') for adr_element in adr_data: datadict['{index}'.format(index=adr_element.cd_contacttype_id.id)] = adr_element.cd_textfield form = ContactForm(initial=datadict) # TODO: datadict index out of contacttype_id and catagory_id for Tab's # or send filtered by category and ordered and give a list with the stop-point return render(request, 'contacts/edit_contact.html', {'address': adr, 'message': message, 'form': form, 'categories': categories})
def new_contact(request): data = {} template = 'contacts/new_contact.html' data['message'] = None if request.method == "POST": data['form'] = ContactForm(request.POST) if data['form'].is_valid(): # each Address has a User ID # TODO: if email exists - solve this passwd = User.objects.make_random_password() user = User(username=data['form'].cleaned_data['searchname'], email=data['form'].cleaned_data['email'], password=passwd) user.save() adr = Address( adr_searchname=data['form'].cleaned_data['searchname'], adr_email=data['form'].cleaned_data['email'], adr_user_id=user, ) adr.save() contacttypes = ContactType.objects.all() for ct_type in contacttypes: ctdata = ContactData( cd_contacttype_id=ct_type, cd_textfield=data['form'].cleaned_data['{index}'.format( index=ct_type.id)], cd_address_id=adr) ctdata.save() return redirect('proj_contacts') else: data['form'] = ContactForm() print data return render(request, template, data)
def edit_contact(request, address_id): message = None contacttypes = ContactType.objects.all() categories = Category.objects.all() if request.method == "POST": form = ContactForm(request.POST) if form.is_valid(): adr = Address(id=address_id, adr_searchname=form.cleaned_data['searchname'], adr_email=form.cleaned_data['email']) adr.save() # TODO: ct_type proof need to be implemented for ct_type in contacttypes: cd_id = ContactData.objects.filter( cd_contacttype_id__id=ct_type.id, cd_address_id__id=address_id) if len(cd_id) > 0: ctdata = ContactData( id=cd_id, cd_contacttype_id=ct_type, cd_textfield=form.cleaned_data['{index}'.format( index=ct_type.id)], cd_address_id=adr) else: ctdata = ContactData( cd_contacttype_id=ct_type, cd_textfield=form.cleaned_data['{index}'.format( index=ct_type.id)], cd_address_id=adr) print(ctdata, cd_id) ctdata.save() return redirect('all_contacts') else: adr = get_object_or_404(Address, pk=address_id) datadict = {'searchname': adr.adr_searchname, 'email': adr.adr_email} adr_data = ContactData.objects.filter( cd_address_id=address_id).order_by('cd_contacttype_id__ct_sort_id') for adr_element in adr_data: datadict['{index}'.format(index=adr_element.cd_contacttype_id.id )] = adr_element.cd_textfield form = ContactForm( initial=datadict ) # TODO: datadict index out of contacttype_id and catagory_id for Tab's # or send filtered by category and ordered and give a list with the stop-point return render(request, 'contacts/edit_contact.html', { 'address': adr, 'message': message, 'form': form, 'categories': categories })
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)
class SupplierResourceTest(APITestCase): def setUp(self): super(SupplierResourceTest, 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.supplier_data = supplier_data self.supplier_data['addresses'] = [base_address] self.mod_supplier_data = self.supplier_data.copy() del self.mod_supplier_data['addresses'] try: del self.mod_supplier_data['contacts'] except KeyError: pass self.supplier = Supplier(**self.mod_supplier_data) self.supplier.is_supplier = True self.supplier.save() self.address = Address(**base_address) self.address.contact = self.supplier self.address.save() self.contact = SupplierContact(**base_supplier_contact[0]) self.contact.supplier = self.supplier self.contact.save() def get_credentials(self): return None #self.create_basic(username=self.username, password=self.password) def test_get_list(self): """ Test GET of list """ logger.debug("\n\n Test GET request for a list of suppliers \n") #Retrieve and validate GET response resp = self.client.get('/api/v1/supplier/', format='json') self.assertEqual(resp.status_code, 200) #test deserialized response resp_obj = resp.data self.assertEqual(len(resp_obj), 1) supplier = resp_obj[0] self.assertEqual(supplier["name"], 'Zipper World Co., Ltd.') self.assertEqual(supplier["currency"], 'USD') self.assertTrue(supplier["is_supplier"]) self.assertEqual(supplier["email"], "*****@*****.**") self.assertEqual(supplier["telephone"], "08348229383") self.assertEqual(supplier["fax"], "0224223423") self.assertEqual(supplier['discount'], 20) #Tests the contacts self.assertNotIn('contacts', supplier) """ self.assertEqual(len(supplier['contacts']), 1) contact = supplier['contacts'][0] self.assertIn('id', contact) self.assertEqual(contact['id'], 1) self.assertEqual(contact['name'], 'Charlie P') self.assertEqual(contact['email'], '*****@*****.**') self.assertEqual(contact['telephone'], '123456789') """ def test_post(self): """ Test creating supplier via POST """ logger.debug("\n\nTesting POST for Supplier \n\n") #Validate resource creation self.assertEqual(Supplier.objects.count(), 1) resp = self.client.post('/api/v1/supplier/', format='json', data=self.supplier_data) self.assertEqual(resp.status_code, 201, msg=resp) self.assertEqual(Supplier.objects.count(), 2) #Validated response to resource creation supplier = resp.data self.assertEqual(supplier['id'], 2) self.assertEqual(supplier["name"], 'Zipper World Co., Ltd.') self.assertEqual(supplier["currency"], 'USD') self.assertTrue(supplier["is_supplier"]) self.assertEqual(supplier["email"], "*****@*****.**") self.assertEqual(supplier["telephone"], "08348229383") self.assertEqual(supplier["fax"], "0224223423") self.assertEqual(supplier['notes'], "woohoo") self.assertEqual(supplier['discount'], 20) #Validate the the supplier contact was created """ self.assertIn("contacts", supplier) self.assertEqual(len(supplier['contacts']), 1) contact = supplier['contacts'][0] self.assertEqual(contact['id'], 2) self.assertEqual(contact['name'], 'Charlie P') self.assertEqual(contact['email'], '*****@*****.**') self.assertEqual(contact['telephone'], '123456789') self.assertTrue(contact['primary']) """ #Verify address self.assertIn('addresses', supplier) self.assertEqual(len(supplier['addresses']), 1) #Validate the created supplier instance supp = Supplier.objects.order_by('-id').all()[0] self.assertEqual(supp.notes, 'woohoo') self.assertEqual(supp.telephone, "08348229383") self.assertEqual(supp.fax, "0224223423") self.assertEqual(supp.name, "Zipper World Co., Ltd.") self.assertEqual(supp.discount, 20) def test_post_with_single_address(self): """ Test creating supplier via POST """ logger.debug("\n\nTesting POST for Supplier with Single Address \n\n") #Validate resource creation self.assertEqual(Supplier.objects.count(), 1) mod_data = copy.deepcopy(self.supplier_data) del mod_data['addresses'] mod_data['address'] = base_address resp = self.client.post('/api/v1/supplier/', format='json', data=self.supplier_data) self.assertEqual(resp.status_code, 201, msg=resp) self.assertEqual(Supplier.objects.count(), 2) #Validated response to resource creation supplier = resp.data self.assertEqual(supplier['id'], 2) self.assertEqual(supplier["name"], 'Zipper World Co., Ltd.') self.assertEqual(supplier["currency"], 'USD') self.assertTrue(supplier["is_supplier"]) self.assertEqual(supplier["email"], "*****@*****.**") self.assertEqual(supplier["telephone"], "08348229383") self.assertEqual(supplier["fax"], "0224223423") self.assertEqual(supplier['notes'], "woohoo") self.assertEqual(supplier['discount'], 20) #Validate the the supplier contact was created """ self.assertIn("contacts", supplier) self.assertEqual(len(supplier['contacts']), 1) contact = supplier['contacts'][0] self.assertEqual(contact['id'], 2) self.assertEqual(contact['name'], 'Charlie P') self.assertEqual(contact['email'], '*****@*****.**') self.assertEqual(contact['telephone'], '123456789') self.assertTrue(contact['primary']) """ #Verify address self.assertIn('addresses', supplier) self.assertEqual(len(supplier['addresses']), 1) #Validate the created supplier instance supp = Supplier.objects.order_by('-id').all()[0] self.assertEqual(supp.notes, 'woohoo') self.assertEqual(supp.telephone, "08348229383") self.assertEqual(supp.fax, "0224223423") self.assertEqual(supp.name, "Zipper World Co., Ltd.") self.assertEqual(supp.discount, 20) 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 supplier resource via PUT """ logger.debug("\n\nTesting PUT for Supplier \n\n") #Validate resource update instead of creation modified_supplier = copy.deepcopy(self.supplier_data) modified_supplier['name'] = 'Zipper Land Ltd.' modified_supplier['terms'] = 120 modified_supplier['discount'] = 75 """ modified_supplier['contacts'][0]['email'] = '*****@*****.**' modified_supplier['contacts'][0]['id'] = 1 del modified_supplier['contacts'][0]['primary'] modified_supplier['contacts'].append({'name': 'test', 'email': '*****@*****.**', 'telephone': 'ok', 'primary': True}) """ self.assertEqual(Supplier.objects.count(), 1) """ self.assertEqual(Supplier.objects.all()[0].contacts.count(), 1) self.assertEqual(len(modified_supplier['contacts']), 2) """ resp = self.client.put('/api/v1/supplier/1/', format='json', data=modified_supplier) self.assertEqual(resp.status_code, 200) #Tests database state self.assertEqual(Supplier.objects.count(), 1) obj = Supplier.objects.all()[0] self.assertEqual(obj.id, 1) self.assertEqual(obj.name, 'Zipper Land Ltd.') self.assertEqual(obj.terms, 120) self.assertEqual(obj.discount, 75) """ self.assertEqual(obj.contacts.count(), 2) contacts = obj.contacts.order_by('id').all() self.assertEqual(contacts[0].id, 1) self.assertEqual(contacts[0].email, '*****@*****.**') self.assertEqual(contacts[0].name, 'Charlie P') self.assertEqual(contacts[0].telephone, '123456789') self.assertEqual(contacts[1].id, 2) self.assertEqual(contacts[1].name, 'test') self.assertEqual(contacts[1].email, '*****@*****.**') self.assertEqual(contacts[1].telephone, 'ok') """ #Tests the response supplier = resp.data self.assertEqual(supplier['id'], 1) self.assertEqual(supplier["name"], 'Zipper Land Ltd.') self.assertEqual(supplier["currency"], 'USD') self.assertTrue(supplier["is_supplier"]) self.assertEqual(supplier["email"], "*****@*****.**") self.assertEqual(supplier["telephone"], "08348229383") self.assertEqual(supplier["fax"], "0224223423") self.assertEqual(supplier['discount'], 75) self.assertEqual(supplier['terms'], 120) """ #Tests contacts in response self.assertIn('contacts', supplier) self.assertEqual(len(supplier['contacts']), 2) contacts = supplier['contacts'] self.assertEqual(contacts[0]['id'], 1) self.assertEqual(contacts[0]['name'], 'Charlie P') self.assertEqual(contacts[0]['email'], '*****@*****.**') self.assertEqual(contacts[0]['telephone'], '123456789') self.assertEqual(contacts[1]['id'], 2) self.assertEqual(contacts[1]['name'], 'test') self.assertEqual(contacts[1]['email'], '*****@*****.**') self.assertEqual(contacts[1]['telephone'], 'ok') self.assertTrue(contacts[1]['primary']) """ def test_get(self): """ TEst getting a supplier resource via GET """ resp = self.client.get('/api/v1/supplier/1/', format='json', authentication=self.get_credentials()) self.assertEqual(resp.status_code, 200) supplier = resp.data self.assertEqual(supplier['id'], 1) self.assertEqual(supplier["name"], 'Zipper World Co., Ltd.') self.assertEqual(supplier["currency"], 'USD') self.assertTrue(supplier["is_supplier"]) self.assertEqual(supplier["email"], "*****@*****.**") self.assertEqual(supplier["telephone"], "08348229383") self.assertEqual(supplier["fax"], "0224223423") self.assertEqual(supplier['discount'], 20) def test_delete(self): """ Test delete a supplier resource via get """ self.assertEqual(Supplier.objects.count(), 1) resp = self.client.delete('/api/v1/supplier/1/', authentication=self.get_credentials()) self.assertEqual(resp.status_code, 204) self.assertEqual(Supplier.objects.count(), 0)
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 ItemTest(TestCase): """ Tests the PO Item """ def setUp(self): """ Set up dependent objects """ super(ItemTest, self).setUp() self.ct = ContentType(app_label="po") self.ct.save() self.p = Permission(codename="add_purchaseorder", content_type=self.ct) self.p.save() self.p2 = Permission(codename="change_purchaseorder", content_type=self.ct) self.p2.save() #Create the user self.username = '******' self.password = '******' self.user = User.objects.create_user( self.username, '*****@*****.**', self.password) self.user.save() self.user.user_permissions.add(self.p) self.user.user_permissions.add(self.p2) self.client.login(username=self.username, password=self.password) self.supplier = Supplier(**base_supplier) self.supplier.save() self.address = Address(**base_address) self.address.contact = self.supplier self.address.save() self.contact = SupplierContact(name='test', email='*****@*****.**', telephone=1234, primary=True) self.contact.supplier = self.supplier self.contact.save() self.supply = Fabric.create(**base_fabric) #self.supply.units = "m^2" self.supply.save() self.po = PurchaseOrder() self.po.employee = self.user self.po.supplier = self.supplier self.po.terms = self.supplier.terms self.po.vat = 7 self.po.order_date = datetime.datetime(2014, 3, 2) self.po.save() self.item = Item(unit_cost=Decimal('13.55'), quantity=Decimal('10'), supply=self.supply) self.item.description = self.supply.description self.item.purchase_order = self.po self.item.save() def test_creating_item_with_no_product_with_unit_cost(self): """Test creating a item via the serializer where there is no product """ context = {'po': self.po, 'supplier': self.supplier} data = { 'supply': { 'id': self.supply.id }, 'unit_cost': 10, 'quantity': 5, 'units': 'yd' } item_serializer = ItemSerializer(context=context, data=data) if item_serializer.is_valid(raise_exception=True): item_serializer.save() # Verify product is created self.assertEqual( Product.objects.filter(supply=self.supply, supplier=self.supplier).count(), 1) # Verify item resp_data = item_serializer.data self.assertEqual(resp_data['description'], 'Pattern: Maxx, Col: Blue') self.assertEqual(resp_data['units'], 'yd') self.assertEqual(Decimal(resp_data['quantity']), Decimal('5')) self.assertEqual(Decimal(resp_data['total']), Decimal('50')) def test_creating_item_with_product_with_no_unit_cost(self): """Test creating a item via the serializer where there is no product """ Product.objects.create(supply=self.supply, supplier=self.supplier, cost=Decimal('12.11'), purchasing_units="yd") context = {'po': self.po, 'supplier': self.supplier} data = {'supply': {'id': self.supply.id}, 'quantity': 5, 'units': 'yd'} item_serializer = ItemSerializer(context=context, data=data) if item_serializer.is_valid(raise_exception=True): item_serializer.save() # Verify product is created self.assertEqual( Product.objects.filter(supply=self.supply, supplier=self.supplier).count(), 1) # Verify item resp_data = item_serializer.data self.assertEqual(resp_data['description'], 'Pattern: Maxx, Col: Blue') self.assertEqual(resp_data['units'], 'yd') self.assertEqual(Decimal(resp_data['quantity']), Decimal('5')) self.assertEqual(Decimal(resp_data['total']), Decimal('60.55')) def test_updating_item_without_product(self): context = {'po': self.po, 'supplier': self.supplier} data = { 'supply': { 'id': self.supply.id }, 'unit_cost': Decimal('11.22'), 'quantity': 4, 'units': 'yd' } # Verify there is no product self.assertEqual( Product.objects.filter(supply=self.supply, supplier=self.supplier).count(), 0) # Update item item_serializer = ItemSerializer(self.item, context=context, data=data) if item_serializer.is_valid(raise_exception=True): item_serializer.save() # Verify product is created self.assertEqual( Product.objects.filter(supply=self.supply, supplier=self.supplier).count(), 1) # Verify item resp_data = item_serializer.data self.assertEqual(resp_data['description'], 'Pattern: Maxx, Col: Blue') self.assertEqual(resp_data['units'], 'yd') self.assertEqual(Decimal(resp_data['quantity']), Decimal('4')) self.assertEqual(Decimal(resp_data['total']), Decimal('44.88')) def test_updating_item_with_product(self): Product.objects.create(supply=self.supply, supplier=self.supplier, cost=Decimal('12.11'), purchasing_units="yd") context = {'po': self.po, 'supplier': self.supplier} data = { 'supply': { 'id': self.supply.id }, 'unit_cost': Decimal('11.22'), 'quantity': 4, 'units': 'm' } # Verify there is a product products = Product.objects.filter(supply=self.supply, supplier=self.supplier) self.assertEqual(products.count(), 1) self.assertEqual(products[0].cost, Decimal('12.11')) self.assertEqual(products[0].purchasing_units, 'yd') # Update item item_serializer = ItemSerializer(self.item, context=context, data=data) if item_serializer.is_valid(raise_exception=True): item_serializer.save() # Verify product is created products2 = Product.objects.filter(supply=self.supply, supplier=self.supplier) self.assertEqual(products2.count(), 1) self.assertEqual(products2[0].cost, Decimal('11.22')) self.assertEqual(products2[0].purchasing_units, 'm') # Verify item resp_data = item_serializer.data self.assertEqual(resp_data['description'], 'Pattern: Maxx, Col: Blue') self.assertEqual(resp_data['units'], 'm') self.assertEqual(Decimal(resp_data['quantity']), Decimal('4')) self.assertEqual(Decimal(resp_data['total']), Decimal('44.88'))
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 ItemTest(TestCase): """ Tests the PO Item """ def setUp(self): """ Set up dependent objects """ super(ItemTest, self).setUp() self.ct = ContentType(app_label="po") self.ct.save() self.p = Permission(codename="add_purchaseorder", content_type=self.ct) self.p.save() self.p2 = Permission(codename="change_purchaseorder", content_type=self.ct) self.p2.save() #Create the user self.username = '******' self.password = '******' self.user = User.objects.create_user(self.username, '*****@*****.**', self.password) self.user.save() self.user.user_permissions.add(self.p) self.user.user_permissions.add(self.p2) self.client.login(username=self.username, password=self.password) self.supplier = Supplier(**base_supplier) self.supplier.save() self.address = Address(**base_address) self.address.contact = self.supplier self.address.save() self.contact = SupplierContact(name='test', email='*****@*****.**', telephone=1234, primary=True) self.contact.supplier = self.supplier self.contact.save() self.supply = Fabric.create(**base_fabric) #self.supply.units = "m^2" self.supply.save() self.po = PurchaseOrder() self.po.employee = self.user self.po.supplier = self.supplier self.po.terms = self.supplier.terms self.po.vat = 7 self.po.order_date = datetime.datetime(2014, 3, 2) self.po.save() self.item = Item(unit_cost=Decimal('13.55'), quantity=Decimal('10'), supply=self.supply) self.item.description = self.supply.description self.item.purchase_order = self.po self.item.save() def test_creating_item_with_no_product_with_unit_cost(self): """Test creating a item via the serializer where there is no product """ context = {'po': self.po, 'supplier': self.supplier} data = {'supply': self.supply.id, 'unit_cost': 10, 'quantity': 5, 'units': 'yd'} item_serializer = ItemSerializer(context=context, data=data) if item_serializer.is_valid(raise_exception=True): item_serializer.save() # Verify product is created self.assertEqual(Product.objects.filter(supply=self.supply, supplier=self.supplier).count(), 1) # Verify item resp_data = item_serializer.data self.assertEqual(resp_data['description'], 'Pattern: Maxx, Col: Blue') self.assertEqual(resp_data['units'], 'yd') self.assertEqual(Decimal(resp_data['quantity']), Decimal('5')) self.assertEqual(Decimal(resp_data['total']), Decimal('50')) def test_creating_item_with_product_with_no_unit_cost(self): """Test creating a item via the serializer where there is no product """ Product.objects.create(supply=self.supply, supplier=self.supplier, cost=Decimal('12.11'), purchasing_units="yd") context = {'po': self.po, 'supplier': self.supplier} data = {'supply': self.supply.id, 'quantity': 5, 'units': 'yd'} item_serializer = ItemSerializer(context=context, data=data) if item_serializer.is_valid(raise_exception=True): item_serializer.save() # Verify product is created self.assertEqual(Product.objects.filter(supply=self.supply, supplier=self.supplier).count(), 1) # Verify item resp_data = item_serializer.data self.assertEqual(resp_data['description'], 'Pattern: Maxx, Col: Blue') self.assertEqual(resp_data['units'], 'yd') self.assertEqual(Decimal(resp_data['quantity']), Decimal('5')) self.assertEqual(Decimal(resp_data['total']), Decimal('60.55')) def test_updating_item_without_product(self): context = {'po': self.po, 'supplier': self.supplier} data = {'supply': self.supply.id, 'unit_cost': Decimal('11.22'), 'quantity': 4, 'units': 'yd'} # Verify there is no product self.assertEqual(Product.objects.filter(supply=self.supply, supplier=self.supplier).count(), 0) # Update item item_serializer = ItemSerializer(self.item, context=context, data=data) if item_serializer.is_valid(raise_exception=True): item_serializer.save() # Verify product is created self.assertEqual(Product.objects.filter(supply=self.supply, supplier=self.supplier).count(), 1) # Verify item resp_data = item_serializer.data self.assertEqual(resp_data['description'], 'Pattern: Maxx, Col: Blue') self.assertEqual(resp_data['units'], 'yd') self.assertEqual(Decimal(resp_data['quantity']), Decimal('4')) self.assertEqual(Decimal(resp_data['total']), Decimal('44.88')) def test_updating_item_with_product(self): Product.objects.create(supply=self.supply, supplier=self.supplier, cost=Decimal('12.11'), purchasing_units="yd") context = {'po': self.po, 'supplier': self.supplier} data = {'supply': self.supply.id, 'unit_cost': Decimal('11.22'), 'quantity': 4, 'units': 'm'} # Verify there is a product products = Product.objects.filter(supply=self.supply, supplier=self.supplier) self.assertEqual(products.count(), 1) self.assertEqual(products[0].cost, Decimal('12.11')) self.assertEqual(products[0].purchasing_units, 'yd') # Update item item_serializer = ItemSerializer(self.item, context=context, data=data) if item_serializer.is_valid(raise_exception=True): item_serializer.save() # Verify product is created products2 = Product.objects.filter(supply=self.supply, supplier=self.supplier) self.assertEqual(products2.count(), 1) self.assertEqual(products2[0].cost, Decimal('11.22')) self.assertEqual(products2[0].purchasing_units, 'm') # Verify item resp_data = item_serializer.data self.assertEqual(resp_data['description'], 'Pattern: Maxx, Col: Blue') self.assertEqual(resp_data['units'], 'm') self.assertEqual(Decimal(resp_data['quantity']), Decimal('4')) self.assertEqual(Decimal(resp_data['total']), Decimal('44.88'))
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)
def index(request, form_class=ContactForm, template_name="form.html"): if request.method == "POST": form = form_class(request.POST) if form.is_valid(): email = form.cleaned_data.get('email', None) first_name = form.cleaned_data.get('first_name', None) last_name = form.cleaned_data.get('last_name', None) if listed_in_email_block(email): # listed in the email blocks - it's a spam email we want to block # log the spam log_defaults = { 'event_id' : 130999, 'event_data': 'SPAM detected in email from %s %s, %s.' \ % (first_name, last_name, email), 'description': 'email spam detected', 'user': request.user, 'request': request, } EventLog.objects.log(**log_defaults) # redirect normally so they don't suspect return HttpResponseRedirect(reverse('form.confirmation')) address = form.cleaned_data.get('address', None) city = form.cleaned_data.get('city', None) state = form.cleaned_data.get('state', None) zipcode = form.cleaned_data.get('zipcode', None) country = form.cleaned_data.get('country', None) phone = form.cleaned_data.get('phone', None) url = form.cleaned_data.get('url', None) message = form.cleaned_data.get('message', None) contact_kwargs = { 'first_name': first_name, 'last_name': last_name, 'message': message, } contact = Contact(**contact_kwargs) contact.creator_id = 1 # TODO: decide if we should use tendenci base model contact.owner_id = 1 # TODO: decide if we should use tendenci base model contact.save() if address or city or state or zipcode or country: address_kwargs = { 'address': address, 'city': city, 'state': state, 'zipcode': zipcode, 'country': country, } obj_address = Address(**address_kwargs) obj_address.save() # saves object contact.addresses.add(obj_address) # saves relationship if phone: obj_phone = Phone(number=phone) obj_phone.save() # saves object contact.phones.add(obj_phone) # saves relationship if email: obj_email = Email(email=email) obj_email.save() # saves object contact.emails.add(obj_email) # saves relationship if url: obj_url = URL(url=url) obj_url.save() # saves object contact.urls.add(obj_url) # saves relationship site_name = get_setting('site', 'global', 'sitedisplayname') message_link = get_setting('site', 'global', 'siteurl') # send notification to administrators # get admin notice recipients recipients = get_notice_recipients('module', 'contacts', 'contactrecipients') if recipients: if notification: extra_context = { 'reply_to': email, 'contact':contact, 'first_name':first_name, 'last_name':last_name, 'address':address, 'city':city, 'state':state, 'zipcode':zipcode, 'country':country, 'phone':phone, 'email':email, 'url':url, 'message':message, 'message_link':message_link, 'site_name':site_name, } notification.send_emails(recipients,'contact_submitted', extra_context) try: user = User.objects.filter(email=email)[0] except: user = None if user: event_user = user event_id = 125115 else: event_user = AnonymousUser() event_id = 125114 log_defaults = { 'event_id' : event_id, 'event_data': 'Contact Form (id:%d) submitted by %s' % (contact.pk, email), 'description': '%s added' % contact._meta.object_name, 'user': event_user, 'request': request, 'instance': contact, } EventLog.objects.log(**log_defaults) return HttpResponseRedirect(reverse('form.confirmation')) else: return render_to_response(template_name, {'form': form}, context_instance=RequestContext(request)) form = form_class() return render_to_response(template_name, {'form': form}, context_instance=RequestContext(request))
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)
def edit(request, contact_id=None): ''' Show the edit page for the current contact, or create a new one ''' exists = False if contact_id == None else True if request.method == 'POST': # save data contact = None street = request.POST.get('street') apartment = request.POST.get('apartment') city = request.POST.get('city') state = request.POST.get('state') zip = request.POST.get('zip') owned_by = request.user first_name = request.POST.get('first_name') last_name = request.POST.get('last_name') phone_number = request.POST.get('phone_number') email_address = request.POST.get('email_address') if exists: # if the contact already exists, update it - else add it contact = Contact.objects.get(id=contact_id) address = Address.objects.get(id=contact.address.id) address.street = street address.apartment = apartment address.city = city address.state = state address.zip = zip address.save() contact.first_name = first_name contact.last_name = last_name contact.phone_number = phone_number contact.email_address = email_address contact.address = address contact.save() else: # add the address address = Address(street=street, apartment=apartment, city=city, state=state, zip=zip) address.save() # add the contact contact = Contact(owned_by=owned_by, first_name=first_name, last_name=last_name, phone_number=phone_number, email_address=email_address, address=address) contact.save() return redirect('contacts:contacts') else: if contact_id == None: return render(request, 'contacts/edit.html', { 'contact': None, 'exists': exists }) else: contact = Contact.objects.get(id=contact_id) # check if contact belongs to logged in user if contact.owned_by == request.user: exists = True return render(request, 'contacts/edit.html', { 'contact': contact, 'exists': exists }) else: return redirect('contacts:contacts')
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 PurchaseOrderTest(APITestCase): """ Tests the Purchase Order """ def setUp(self): """ Set up dependent objects """ super(PurchaseOrderTest, self).setUp() self.ct = ContentType(app_label="po") self.ct.save() self.p = Permission(codename="add_purchaseorder", content_type=self.ct) self.p.save() self.p2 = Permission(codename="change_purchaseorder", content_type=self.ct) self.p2.save() #Create the user self.username = '******' self.password = '******' self.user = User.objects.create_user( self.username, '*****@*****.**', self.password) self.user.save() self.user.user_permissions.add(self.p) self.user.user_permissions.add(self.p2) self.client.login(username=self.username, password=self.password) self.client.force_authenticate(self.user) self.supplier = Supplier(**base_supplier) self.supplier.save() self.address = Address(**base_address) self.address.contact = self.supplier self.address.save() self.contact = SupplierContact(name='test', email='*****@*****.**', telephone=1234, primary=True) self.contact.supplier = self.supplier self.contact.save() # Create Custom Supply # not implemented # Create Fabric self.supply = Fabric.create(**base_fabric) #self.supply.units = "m^2" self.supply.save() self.supply1 = self.supply self.product = Product(supply=self.supply, supplier=self.supplier, cost=base_fabric['unit_cost'], purchasing_units='m') self.product.save() self.supply2 = Fabric.create(**base_fabric2) self.supply2.discount = 5 self.supply2.save() self.product2 = Product(supply=self.supply2, supplier=self.supplier, cost=base_fabric['unit_cost']) self.product2.save() self.supply1.supplier = self.supplier self.supply2.supplier = self.supplier #Create supply with no target item self.supply3 = Supply.objects.create(description='test supply') self.supply3.id = 203 self.supply3.save() #Create a project self.project = Project() self.project.codename = 'MC House' self.project.save() self.po = PurchaseOrder() self.po.employee = self.user self.po.supplier = self.supplier self.po.terms = self.supplier.terms self.po.vat = 7 self.order_date = datetime.datetime(2017, 1, 15, 15, 30, 0, 0, tzinfo=timezone('Asia/Bangkok')) self.po.order_date = self.order_date self.po.receive_date = datetime.datetime.now() self.po.save() #self.po.create_and_upload_pdf() self.item = Item.create(supplier=self.supplier, id=1, **base_purchase_order['items'][0]) self.item.purchase_order = self.po self.item.save() self.po.calculate_total() self.po.save() def test_get_list(self): """ Tests getting a list of po's via GET """ #Validate the response resp = self.client.get('/api/v1/purchase-order/', format='json') self.assertEqual(resp.status_code, 200) #Validate the returned data resp = resp.data self.assertIsInstance(resp, list) self.assertEqual(len(resp), 1) def test_get(self): """ Tests getting a single resource via GET """ #Validate the response resp = self.client.get('/api/v1/purchase-order/1/') self.assertEqual(resp.status_code, 200) #Validate the returned data obj = resp.data self.assertEqual(obj['id'], 1) self.assertEqual(obj['terms'], '0/net') self.assertEqual(obj['revision'], 0) #Test items self.assertIn('items', obj) self.assertEqual(len(obj['items']), 1) item1 = obj['items'][0] #self.assertIn('purchasing_units', item1) #self.assertEqual(item1['purchasing_units'], 'm') def test_get_with_pdf(self): """ Tests getting a resource with the pdf """ self.skipTest("") self.po.create_and_upload_pdf() resp = self.client.get('/api/v1/purchase-order/1/') self.assertEqual(resp.status_code, 200) obj = resp.data self.assertIn('pdf', obj) self.assertIn('url', obj['pdf']) self.assertIsNotNone(obj['pdf']['url']) def test_post(self): """ Tests creating a new resource via POST """ print '\n' logger.debug("Creating new po") print '\n' #validate the response resp = self.client.post('/api/v1/purchase-order/', data=base_purchase_order, format='json') self.assertEqual(resp.status_code, 201, msg=resp) #Validate the data returned obj = resp.data self.assertEqual(obj['id'], 2) self.assertIsNotNone(obj['items']) self.assertIsInstance(obj['items'], list) self.assertEqual(len(obj['items']), 2) self.assertEqual(obj['currency'], 'USD') self.assertIn('project', obj) self.assertIsInstance(obj['project'], dict) self.assertEqual(obj['project']['id'], 1) self.assertEqual(obj['project']['codename'], 'MC House') #validate the resource in the database po = PurchaseOrder.objects.get(pk=2) self.assertIsInstance(po.project, Project) #self.assertIsNotNone(obj['pdf']) #self.assertIsNotNone(obj['pdf']['url']) self.items = po.items.all().order_by('id') self.item1 = self.items[0] self.item2 = self.items[1] self.assertIsInstance(self.item1, Item) self.assertIsInstance(self.item1.supply, Supply) self.assertEqual(self.item1.supply.id, 1) self.assertEqual(self.item1.unit_cost, Decimal('12.11')) self.assertEqual(self.item1.quantity, 10) self.assertEqual(self.item1.total, Decimal('121.1')) self.assertIsInstance(self.item2, Item) self.assertIsInstance(self.item2.supply, Supply) self.assertEqual(self.item2.supply.id, 2) self.assertEqual(self.item2.unit_cost, Decimal('12.11')) self.assertEqual(self.item2.quantity, 3) self.assertEqual(self.item2.total, Decimal('34.51')) self.assertEqual(Project.objects.count(), 1) project = Project.objects.all()[0] self.assertIsInstance(project, Project) self.assertEqual(project.id, 1) self.assertEqual(project.codename, 'MC House') def test_post_with_new_project(self): """ Tests creating a new resource via POST """ print '\n' logger.debug("Creating new po with a project") print '\n' #validate the response po = base_purchase_order.copy() po['project'] = {'codename': 'Ladawan'} po['currency'] = 'RMB' resp = self.client.post('/api/v1/purchase-order/', data=po, format='json') self.assertEqual(resp.status_code, 201, msg=resp) #Validate the data returned obj = resp.data self.assertEqual(obj['id'], 2) self.assertIsNotNone(obj['items']) self.assertIsInstance(obj['items'], list) self.assertEqual(len(obj['items']), 2) self.assertIn('project', obj) self.assertIsInstance(obj['project'], dict) self.assertEqual(obj['project']['id'], 2) self.assertEqual(obj['project']['codename'], 'Ladawan') self.assertEqual(obj['currency'], 'RMB') #validate the resource in the database po = PurchaseOrder.objects.get(pk=2) self.assertIsInstance(po.project, Project) #self.assertIsNotNone(obj['pdf']) #self.assertIsNotNone(obj['pdf']['url']) self.items = po.items.all().order_by('id') self.item1 = self.items[0] self.item2 = self.items[1] self.assertIsInstance(self.item1, Item) self.assertIsInstance(self.item1.supply, Supply) self.assertEqual(self.item1.supply.id, 1) self.assertEqual(self.item1.unit_cost, Decimal('12.11')) self.assertEqual(self.item1.quantity, 10) self.assertEqual(self.item1.total, Decimal('121.1')) self.assertIsInstance(self.item2, Item) self.assertIsInstance(self.item2.supply, Supply) self.assertEqual(self.item2.supply.id, 2) #self.assertEqual(self.item2.unit_cost, Decimal('11.50')) self.assertEqual(self.item2.quantity, 3) self.assertEqual(self.item2.total, Decimal('34.51')) project = po.project self.assertEqual(Project.objects.all().count(), 2) self.assertIsInstance(project, Project) self.assertEqual(project.id, 2) self.assertEqual(project.codename, 'Ladawan') def test_creating_new_po_with_price_change(self): """ Tests creating a new po via post while also changing the price of a supply """ print '\n' logger.debug("Creating new po with a price change") print '\n' #validate the response po = copy.deepcopy(base_purchase_order) del po['items'][1] po['items'][0]['cost'] = '1.99' po['items'][0]['unit_cost'] = '1.99' resp = self.client.post('/api/v1/purchase-order/', data=po, format='json') self.assertEqual(resp.status_code, 201, msg=resp) resp_obj = resp.data #webbrowser.get("open -a /Applications/Google\ Chrome.app %s").open(resp_obj['pdf']['url']) #Verify the returned data self.assertEqual(resp_obj['id'], 2) self.assertEqual(resp_obj['vat'], 7) self.assertEqual(Decimal(resp_obj['grand_total']), Decimal('21.29')) item = resp_obj['items'][0] self.assertEqual(Decimal(item['unit_cost']), Decimal('1.99')) self.assertEqual(Decimal(item['total']), Decimal('19.90')) #Verify data in the database supply = Supply.objects.get(pk=1) supply.supplier = self.supplier self.assertEqual(supply.cost, Decimal('1.99')) self.assertEqual(Log.objects.all().count(), 1) log = Log.objects.all()[0] self.assertEqual( log.message, "Price change from 12.11USD to 1.99USD for Pattern: Maxx, Col: Blue [Supplier: Zipper World]" ) def test_creating_new_po_with_different_currency(self): """ Tests creating a new po via post while also changing the price of a supply """ print '\n' logger.debug("Creating new po with a price change") print '\n' #validate the response po = copy.deepcopy(base_purchase_order) del po['items'][1] po['items'][0]['cost'] = '1.99' po['currency'] = 'RMB' resp = self.client.post('/api/v1/purchase-order/', data=po, format='json') self.assertEqual(resp.status_code, 201, msg=resp) resp_obj = resp.data #webbrowser.get("open -a /Applications/Google\ Chrome.app %s").open(resp_obj['pdf']['url']) #Verify the returned data self.assertEqual(resp_obj['id'], 2) self.assertEqual(resp_obj['vat'], 7) self.assertEqual(resp_obj['currency'], 'RMB') self.assertEqual(Decimal(resp_obj['grand_total']), Decimal('21.29')) item = resp_obj['items'][0] self.assertEqual(Decimal(item['unit_cost']), Decimal('1.99')) self.assertEqual(Decimal(item['total']), Decimal('19.90')) po = PurchaseOrder.objects.get(pk=2) self.assertEqual(po.currency, 'RMB') def test_updating_the_po(self): """ Tests updating the purchase order via a PUT request """ print '\n' logger.debug('Updating PO') print '\n' #Verifying po in database self.assertEqual(self.po.id, 1) self.assertEqual(self.po.items.count(), 1) self.assertEqual(self.po.grand_total, Decimal('129.58')) self.assertEqual( timezone('Asia/Bangkok').normalize(self.po.order_date).date(), datetime.datetime.now().date()) item = self.po.items.all()[0] self.assertEqual(item.id, 1) self.assertEqual(item.quantity, 10) self.assertEqual(item.total, Decimal('121.1')) modified_po_data = copy.deepcopy(base_purchase_order) del modified_po_data['items'][1] modified_po_data['id'] = 1 modified_po_data['items'][0]['id'] = 1 modified_po_data['items'][0]['comments'] = 'test change' modified_po_data['items'][0]['quantity'] = 3 modified_po_data['items'][0]['description'] = 'test description change' resp = self.client.put('/api/v1/purchase-order/1/', format='json', data=modified_po_data) #Verify the response self.assertEqual(resp.status_code, 200, msg=resp) po = resp.data self.assertEqual(po['id'], 1) self.assertEqual(po['supplier']['id'], 1) self.assertEqual(po['vat'], 7) self.assertEqual(Decimal(po['grand_total']), Decimal('38.87')) self.assertEqual(po['discount'], 0) self.assertEqual(po['revision'], 1) self.assertEqual(len(po['items']), 1) #self.assertEqual(po['status'], 'PAID') #Check the new pdf #webbrowser.get("open -a /Applications/Google\ Chrome.app %s").open(po['pdf']['url']) item2 = po['items'][0] self.assertEqual(item2['id'], 1) self.assertEqual(item2['quantity'], Decimal('3.0000000000')) self.assertEqual(item2['comments'], 'test change') self.assertEqual(item2['description'], 'test description change') self.assertEqual(Decimal(item2['unit_cost']), Decimal('12.11')) self.assertEqual(Decimal(item2['total']), Decimal('36.33')) #Verify database record po = PurchaseOrder.objects.get(pk=1) self.assertEqual(po.supplier.id, 1) #self.assertEqual(timezone('Asia/Bangkok').normalize(po.order_date), datetime.datetime.now().date()) self.assertEqual(po.vat, 7) self.assertEqual(po.grand_total, Decimal('38.87')) self.assertEqual(po.items.count(), 1) item2 = po.items.all().order_by('id')[0] self.assertEqual(item2.id, 1) self.assertEqual(item2.description, 'test description change') self.assertEqual(item2.comments, 'test change') self.assertEqual(item2.quantity, 3) self.assertEqual(item2.unit_cost, Decimal('12.11')) self.assertEqual(item2.total, Decimal('36.33')) def test_adding_a_new_item_with_no_supply(self): """ Test adding a new item to the purchase order with no previous supply or product" """ print '\n' logger.debug('Add a new item to a current PO via PUT') print '\n' #Verifying po in database self.assertEqual(self.po.id, 1) self.assertEqual(self.po.items.count(), 1) self.assertEqual(self.po.grand_total, Decimal('129.58')) self.assertEqual( timezone('Asia/Bangkok').normalize(self.po.order_date).date(), datetime.datetime.now().date()) item = self.po.items.all()[0] self.assertEqual(item.id, 1) self.assertEqual(item.quantity, 10) self.assertEqual(item.total, Decimal('121.1')) modified_po_data = copy.deepcopy(base_purchase_order) modified_po_data['items'][1]['unit_cost'] = Decimal('11.99') modified_po_data['items'][1]['comments'] = 'test change' modified_po_data['items'][1]['description'] = "test description change" del modified_po_data['items'][1]['supply'] resp = self.client.put('/api/v1/purchase-order/1/', format='json', data=modified_po_data) #Verify the response self.assertEqual(resp.status_code, 200, msg=resp) po = resp.data self.assertEqual(po['id'], 1) self.assertEqual(po['supplier']['id'], 1) self.assertEqual(po['vat'], 7) #self.assertEqual(Decimal(po['grand_total']), Decimal('74.85')) self.assertEqual(po['discount'], 0) self.assertEqual(po['revision'], 1) self.assertEqual(len(po['items']), 2) #self.assertEqual(po['status'], 'PAID') #Check the new pdf #webbrowser.get("open -a /Applications/Google\ Chrome.app %s").open(po['pdf']['url']) item1 = po['items'][0] logger.debug(item1) self.assertEqual(item1['id'], 2) self.assertEqual(item1['quantity'], Decimal('10.0000000000')) self.assertEqual(item1['description'], u'Pattern: Maxx, Col: Blue') self.assertEqual(Decimal(item1['unit_cost']), Decimal('12.1100')) self.assertEqual(Decimal(item1['total']), Decimal('121.10')) item2 = po['items'][1] logger.debug(item2) self.assertEqual(item2['id'], 3) self.assertEqual(item2['quantity'], Decimal('3.0000000000')) self.assertEqual(item2['comments'], 'test change') self.assertEqual(item2['description'], 'test description change') self.assertEqual(Decimal(item2['unit_cost']), Decimal('11.99')) self.assertEqual(Decimal(item2['total']), Decimal('35.97')) #Verify database record po = PurchaseOrder.objects.get(pk=1) self.assertEqual(po.supplier.id, 1) #self.assertEqual(timezone('Asia/Bangkok').normalize(po.order_date), datetime.datetime.now().date()) self.assertEqual(po.vat, 7) self.assertEqual(po.grand_total, Decimal('168.06')) self.assertEqual(po.items.count(), 2) # Check new item in the database item2_d = po.items.all().order_by('id')[1] self.assertEqual(item2_d.id, 3) self.assertEqual(item2_d.description, 'test description change') self.assertEqual(item2_d.comments, 'test change') self.assertEqual(item2_d.quantity, 3) self.assertEqual(item2_d.unit_cost, Decimal('11.99')) self.assertEqual(item2_d.total, Decimal('35.97')) # Check new supply product in the database products = SupplyProduct.objects.filter(supply=item2_d.supply, supplier=self.po.supplier) self.assertEqual(products.count(), 1) product = products.all()[0] self.assertEqual(product.supply.id, item2_d.supply.id) self.assertEqual(product.supplier.id, self.po.supplier.id) self.assertEqual(product.cost, Decimal('11.99')) def xtest_adding_a_new_item_with_no_supply(self): """ Test adding a new item to the purchase order with no previous supply or product" """ print '\n' logger.debug('Add a new item to a current PO via PUT') print '\n' #Verifying po in database self.assertEqual(self.po.id, 1) self.assertEqual(self.po.items.count(), 1) self.assertEqual(self.po.grand_total, Decimal('129.58')) self.assertEqual( timezone('Asia/Bangkok').normalize(self.po.order_date).date(), datetime.datetime.now().date()) item = self.po.items.all()[0] self.assertEqual(item.id, 1) self.assertEqual(item.quantity, 10) self.assertEqual(item.total, Decimal('121.1')) modified_po_data = copy.deepcopy(base_purchase_order) modified_po_data['items'][1]['unit_cost'] = Decimal('11.99') modified_po_data['items'][1]['comments'] = 'test change' modified_po_data['items'][1]['description'] = "test description change" modified_po_data['status'] = 'PROCESSED' logger.debug(modified_po_data) resp = self.client.put('/api/v1/purchase-order/1/', format='json', data=modified_po_data) #Verify the response self.assertEqual(resp.status_code, 200, msg=resp) po = resp.data self.assertEqual(po['id'], 1) self.assertEqual(po['supplier']['id'], 1) self.assertEqual(po['vat'], 7) #self.assertEqual(Decimal(po['grand_total']), Decimal('74.85')) self.assertEqual(po['discount'], 0) self.assertEqual(po['revision'], 1) self.assertEqual(len(po['items']), 2) #self.assertEqual(po['status'], 'PAID') #Check the new pdf #webtbrowser.get("open -a /Applications/Google\ Chrome.app %s").open(po['pdf']['url']) item1 = po['items'][0] logger.debug(item1) self.assertEqual(item1['id'], 2) self.assertEqual(item1['quantity'], '10.0000000000') self.assertEqual(item1['description'], u'Pattern: Maxx, Col: Blue') self.assertEqual(Decimal(item1['unit_cost']), Decimal('12.1100')) self.assertEqual(Decimal(item1['total']), Decimal('121.10')) item2 = po['items'][1] logger.debug(item2) self.assertEqual(item2['id'], 3) self.assertEqual(item2['quantity'], '3.0000000000') self.assertEqual(item2['comments'], 'test change') self.assertEqual(item2['description'], 'test description change') self.assertEqual(Decimal(item2['unit_cost']), Decimal('11.99')) self.assertEqual(Decimal(item2['total']), Decimal('35.97')) #Verify database record po = PurchaseOrder.objects.get(pk=1) self.assertEqual(po.supplier.id, 1) self.assertEqual(po.status, 'PROCESSED') #self.assertEqual(timezone('Asia/Bangkok').normalize(po.order_date), datetime.datetime.now().date()) self.assertEqual(po.vat, 7) self.assertEqual(po.grand_total, Decimal('168.07')) self.assertEqual(po.items.count(), 2) # Check new item in the database item2_d = po.items.all().order_by('id')[1] self.assertEqual(item2_d.id, 203) self.assertEqual(item2_d.description, 'test description change') self.assertEqual(item2_d.comments, 'test change') self.assertEqual(item2_d.quantity, 3) self.assertEqual(item2_d.unit_cost, Decimal('11.99')) self.assertEqual(item2_d.total, Decimal('35.97')) # Check new supply product in the database products = SupplyProduct.objects.filter(supply=item2_d.supply, supplier=self.po.supplier) self.assertEqual(products.count(), 1) product = products.all()[0] self.assertEqual(product.supply.id, item2_d.supply.id) self.assertEqual(product.supplier.id, self.po.supplier.id) self.assertEqual(product.cost, Decimal('11.99')) def test_updating_po_items(self): """ Test updating properties of items in the purchase order """ print '\n' logger.debug('Updating items') print '\n' modified_po_data = copy.deepcopy(base_purchase_order) modified_po_data['status'] = 'PROCESSED' modified_po_data['items'][0]['purchasing_units'] = 'set' resp = self.client.put('/api/v1/purchase-order/1/', format='json', data=modified_po_data) po = resp.data item1 = po['items'][0] #self.assertIn('purchasing_units', item1) #self.assertEqual(item1['purchasing_units'], 'set') def xtest_updating_po_with_discount(self): """ """ print '\n' logger.debug( "Update purchase order with a discount for individual supply") print '\n' #Verify the original po self.assertEqual(self.po.id, 1) self.assertEqual(self.po.items.count(), 1) self.assertEqual(self.po.grand_total, Decimal('129.58')) item = self.po.items.all()[0] self.assertEqual(item.id, 1) self.assertEqual(item.quantity, 10) self.assertEqual(item.total, Decimal('121.1')) modified_po = copy.deepcopy(base_purchase_order) modified_po['items'][0]['discount'] = 50 modified_po['items'][0]['id'] = 1 modified_po['status'] = 'PROCESSED' self.assertEqual(len(modified_po['items']), 2) resp = self.client.put('/api/v1/purchase-order/1/', format='json', data=modified_po) self.assertEqual(resp.status_code, 200, msg=resp) resp_obj = resp.data self.assertEqual(resp_obj['revision'], 1) #Check the new pdf #webbrowser.get("open -a /Applications/Google\ Chrome.app %s").open(resp_obj['pdf']['url']) item1 = resp_obj['items'][0] item2 = resp_obj['items'][1] self.assertEqual(item1['id'], 1) self.assertEqual(item1['quantity'], Decimal('10.0000000000')) self.assertEqual(Decimal(item1['unit_cost']), Decimal('12.11')) self.assertEqual(Decimal(item1['total']), Decimal('60.55')) self.assertEqual(item2['id'], 2) self.assertEqual(item2['quantity'], Decimal('3.0000000000')) self.assertEqual(item2['discount'], 5) self.assertEqual(Decimal(item2['unit_cost']), Decimal('12.11')) self.assertEqual(Decimal(item2['total']), Decimal('34.51')) self.assertEqual(Decimal(resp_obj['grand_total']), Decimal('101.72')) po = PurchaseOrder.objects.get(pk=1) item1 = po.items.order_by('id').all()[0] self.assertEqual(item1.id, 1) self.assertEqual(item1.quantity, Decimal('10.00')) self.assertEqual(item1.discount, 50) self.assertEqual(item1.unit_cost, Decimal('12.11')) self.assertEqual(item1.total, Decimal('60.55')) item2 = po.items.order_by('id').all()[1] self.assertEqual(item2.id, 2) self.assertEqual(item2.quantity, Decimal('3.00')) self.assertEqual(item2.unit_cost, Decimal('12.11')) self.assertEqual(item2.discount, 5) self.assertEqual(item2.total, Decimal('34.51')) def test_updating_po_with_new_currency(self): """ Test updating the status of supplies and automatically checking in supplies """ #test original quantity modified_po = copy.deepcopy(base_purchase_order) modified_po['currency'] = 'RMB' resp = self.client.put('/api/v1/purchase-order/1/', format='json', data=modified_po) self.assertEqual(resp.status_code, 200, msg=resp) po = resp.data self.assertEqual(po['currency'], 'RMB') def test_updating_the_supply_price(self): """ Test updating a po with a new cost for an item """ self.assertEqual(self.po.id, 1) self.assertEqual(self.po.items.count(), 1) item = self.po.items.all()[0] self.assertEqual(item.id, 1) self.assertEqual(item.unit_cost, Decimal('12.11')) self.assertEqual(Log.objects.all().count(), 0) modified_po = copy.deepcopy(base_purchase_order) modified_po['items'][0]['unit_cost'] = Decimal('10.05') modified_po['items'][0]['id'] = 1 modified_po['status'] = 'PROCESSED' del modified_po['items'][1] resp = self.client.put('/api/v1/purchase-order/1/', format='json', data=modified_po) self.assertEqual(resp.status_code, 200, msg=resp) resp_obj = resp.data self.assertEqual(resp_obj['revision'], 1) #Check the new pdf #webbrowser.get("open -a /Applications/Google\ Chrome.app %s").open(resp_obj['pdf']['url']) self.assertEqual(resp_obj['id'], 1) self.assertEqual(resp_obj['supplier']['id'], 1) self.assertEqual(resp_obj['vat'], 7) self.assertEqual(resp_obj['discount'], 0) self.assertEqual(resp_obj['revision'], 1) self.assertEqual(Decimal(resp_obj['grand_total']), Decimal('107.54')) self.assertEqual(len(resp_obj['items']), 1) item1 = resp_obj['items'][0] self.assertEqual(item1['id'], 1) self.assertEqual(item1['quantity'], Decimal('10.0000000000')) self.assertEqual(Decimal(item1['unit_cost']), Decimal('10.05')) self.assertEqual(Decimal(item1['total']), Decimal('100.50')) #Confirm cost change for item and supply in the database po = PurchaseOrder.objects.get(pk=1) self.assertEqual(po.grand_total, Decimal('107.54')) item1 = po.items.order_by('id').all()[0] self.assertEqual(item1.id, 1) self.assertEqual(item1.quantity, 10) self.assertEqual(item1.unit_cost, Decimal('10.05')) supply = item1.supply supply.supplier = po.supplier self.assertEqual(supply.cost, Decimal('10.05')) self.assertEqual(Log.objects.all().count(), 1) log = Log.objects.all()[0] self.assertEqual(log.cost, Decimal('10.05')) self.assertEqual(log.supply, supply) self.assertEqual(log.supplier, po.supplier) self.assertEqual( log.message, "Price change from 12.11USD to 10.05USD for Pattern: Maxx, Col: Blue [Supplier: Zipper World]" ) # Confirm that there is still only one product for this supply and supplier # in the database products = Product.objects.filter(supply=supply, supplier=po.supplier) self.assertEqual(len(products), 1) def test_updating_item_status(self): """ Test updating the status of supplies and automatically checking in supplies """ #test original quantity self.assertEqual(self.supply1.quantity, 10) self.assertEqual(self.supply2.quantity, 10) modified_po = copy.deepcopy(base_purchase_order) modified_po['status'] = 'Received' modified_po['items'][0]['id'] = 1 modified_po['items'][0]['status'] = 'Receieved' resp = self.client.put('/api/v1/purchase-order/1/', format='json', data=modified_po) self.assertEqual(resp.status_code, 200, msg=resp) po = resp.data self.assertEqual(Supply.objects.get(pk=1).quantity, 20) def test_updating_the_project(self): """ Tests updating the project, phase, and room of a purchase order """ modified_po = copy.deepcopy(base_purchase_order) def test_updating_to_receive_items(self): """ Test updating the status of the po in order to to receive it When a purchase order is received, the items received should automatically be added to the supply inventory quantity """ modified_po = copy.deepcopy(base_purchase_order) del modified_po['items'][1] modified_po['items'][0]['id'] = 1 modified_po['items'][0]['status'] = 'RECEIVED' modified_po['status'] = 'RECEIVED' self.assertEqual(Supply.objects.get(pk=1).quantity, 10) resp = self.client.put('/api/v1/purchase-order/1/', format='json', data=modified_po) self.assertEqual(resp.status_code, 200, msg=resp) po_data = resp.data self.assertEqual(po_data['id'], 1) self.assertEqual(po_data['status'], 'RECEIVED') item1 = po_data['items'][0] self.assertEqual(item1['id'], 1) self.assertEqual(item1['status'], 'RECEIVED') #Test database values po = PurchaseOrder.objects.get(pk=1) self.assertEqual(po.id, 1) self.assertEqual(po.status, 'RECEIVED') for item in po.items.all(): self.assertEqual(item.status, "RECEIVED") supply = Supply.objects.get(pk=1) self.assertEqual(supply.quantity, 20) log = Log.objects.all().order_by('-id')[0] self.assertEqual(log.action, "ADD") self.assertEqual(log.quantity, 10) self.assertEqual(log.supplier.id, 1) self.assertEqual( log.message, "Received 10m of Pattern: Maxx, Col: Blue from Zipper World") def test_multiple_creates_do_not_increase_products(self): """ Test multiple creates When a purchase order is received, the items received should automatically be added to the supply inventory quantity """ for i in xrange(0, 10): modified_po = copy.deepcopy(base_purchase_order) self.assertEqual(Supply.objects.get(pk=1).quantity, 10) resp = self.client.post('/api/v1/purchase-order/', format='json', data=modified_po) self.assertEqual(resp.status_code, 201, msg=resp) po_data = resp.data self.assertEqual(po_data['status'], 'AWAITING APPROVAL') item1 = po_data['items'][0] #self.assertEqual(item1['supply']['id'], 1) self.assertEqual(item1['status'], u'Ordered') item2 = po_data['items'][1] #self.assertEqual(item1['supply']['id'], 2) self.assertEqual(item1['status'], u'Ordered') #Test database values po = PurchaseOrder.objects.get(pk=resp.data['id']) self.assertEqual(po.status, 'AWAITING APPROVAL') for item in po.items.all(): self.assertEqual(item.status, u"Ordered") supplier = Supplier.objects.get(pk=1) supply = Supply.objects.get(pk=1) self.assertEqual(supply.quantity, 10) self.assertEqual( supply.products.filter(supplier=supplier).count(), 1) supply = Supply.objects.get(pk=2) self.assertEqual(supply.quantity, 10) self.assertEqual( supply.products.filter(supplier=supplier).count(), 1)
class PurchaseOrderTest(APITestCase): """ Tests the Purchase Order """ def setUp(self): """ Set up dependent objects """ super(PurchaseOrderTest, self).setUp() self.ct = ContentType(app_label="po") self.ct.save() self.p = Permission(codename="add_purchaseorder", content_type=self.ct) self.p.save() self.p2 = Permission(codename="change_purchaseorder", content_type=self.ct) self.p2.save() #Create the user self.username = '******' self.password = '******' self.user = User.objects.create_user(self.username, '*****@*****.**', self.password) self.user.save() self.user.user_permissions.add(self.p) self.user.user_permissions.add(self.p2) self.client.login(username=self.username, password=self.password) self.supplier = Supplier(**base_supplier) self.supplier.save() self.address = Address(**base_address) self.address.contact = self.supplier self.address.save() self.contact = SupplierContact(name='test', email='*****@*****.**', telephone=1234, primary=True) self.contact.supplier = self.supplier self.contact.save() self.supply = Fabric.create(**base_fabric) #self.supply.units = "m^2" self.supply.save() self.supply1 = self.supply self.product = Product(supply=self.supply, supplier=self.supplier, cost=base_fabric['unit_cost'], purchasing_units='m') self.product.save() self.supply2 = Fabric.create(**base_fabric2) self.supply2.discount = 5 self.supply2.save() self.product2 = Product(supply=self.supply2, supplier=self.supplier, cost=base_fabric['unit_cost']) self.product2.save() self.supply.supplier = self.supplier self.supply2.supplier = self.supplier #Create a project self.project = Project() self.project.codename = 'MC House' self.project.save() self.po = PurchaseOrder() self.po.employee = self.user self.po.supplier = self.supplier self.po.terms = self.supplier.terms self.po.vat = 7 self.po.order_date = datetime.datetime(2014, 3, 2) self.po.save() #self.po.create_and_upload_pdf() self.item = Item.create(supplier=self.supplier, id=1, **base_purchase_order['items'][0]) self.item.purchase_order = self.po self.item.save() self.po.calculate_total() self.po.save() def test_get_list(self): """ Tests getting a list of po's via GET """ #Validate the response resp = self.client.get('/api/v1/purchase-order/', format='json') self.assertEqual(resp.status_code, 200) #Validate the returned data resp = resp.data self.assertIsInstance(resp, dict) self.assertIsInstance(resp['results'], list) self.assertEqual(len(resp['results']), 1) def test_get(self): """ Tests getting a single resource via GET """ #Validate the response resp = self.client.get('/api/v1/purchase-order/1/') self.assertEqual(resp.status_code, 200) #Validate the returned data obj = resp.data self.assertEqual(obj['id'], 1) self.assertEqual(obj['terms'], 30) self.assertEqual(obj['revision'], 0) #Test items self.assertIn('items', obj) self.assertEqual(len(obj['items']), 1) item1 = obj['items'][0] #self.assertIn('purchasing_units', item1) #self.assertEqual(item1['purchasing_units'], 'm') def test_get_with_pdf(self): """ Tests getting a resource with the pdf """ self.skipTest("") self.po.create_and_upload_pdf() resp = self.client.get('/api/v1/purchase-order/1/') self.assertEqual(resp.status_code, 200) obj = resp.data self.assertIn('pdf', obj) self.assertIn('url', obj['pdf']) self.assertIsNotNone(obj['pdf']['url']) def test_post(self): """ Tests creating a new resource via POST """ print '\n' logger.debug("Creating new po") print '\n' #validate the response resp = self.client.post('/api/v1/purchase-order/', data=base_purchase_order, format='json') self.assertEqual(resp.status_code, 201, msg=resp) #Validate the data returned obj = resp.data self.assertEqual(obj['id'], 2) self.assertIsNotNone(obj['items']) self.assertIsInstance(obj['items'], list) self.assertEqual(len(obj['items']), 2) self.assertEqual(obj['currency'], 'USD') self.assertIn('project', obj) self.assertIsInstance(obj['project'], dict) self.assertEqual(obj['project']['id'], 1) self.assertEqual(obj['project']['codename'], 'MC House') #validate the resource in the database po = PurchaseOrder.objects.get(pk=2) self.assertIsInstance(po.project, Project) #self.assertIsNotNone(obj['pdf']) #self.assertIsNotNone(obj['pdf']['url']) self.items = po.items.all().order_by('id') self.item1 = self.items[0] self.item2 = self.items[1] self.assertIsInstance(self.item1, Item) self.assertIsInstance(self.item1.supply, Supply) self.assertEqual(self.item1.supply.id, 1) self.assertEqual(self.item1.unit_cost, Decimal('12.11')) self.assertEqual(self.item1.quantity, 10) self.assertEqual(self.item1.total, Decimal('121.1')) self.assertIsInstance(self.item2, Item) self.assertIsInstance(self.item2.supply, Supply) self.assertEqual(self.item2.supply.id, 2) self.assertEqual(self.item2.unit_cost, Decimal('12.11')) self.assertEqual(self.item2.quantity, 3) self.assertEqual(self.item2.total, Decimal('34.51')) self.assertEqual(Project.objects.count(), 1) project = Project.objects.all()[0] self.assertIsInstance(project, Project) self.assertEqual(project.id, 1) self.assertEqual(project.codename, 'MC House') def test_post_with_new_project(self): """ Tests creating a new resource via POST """ print '\n' logger.debug("Creating new po with a project") print '\n' #validate the response po = base_purchase_order.copy() po['project'] = {'codename': 'Ladawan'} po['currency'] = 'RMB' resp = self.client.post('/api/v1/purchase-order/', data=po, format='json') self.assertEqual(resp.status_code, 201, msg=resp) #Validate the data returned obj = resp.data self.assertEqual(obj['id'], 2) self.assertIsNotNone(obj['items']) self.assertIsInstance(obj['items'], list) self.assertEqual(len(obj['items']), 2) self.assertIn('project', obj) self.assertIsInstance(obj['project'], dict) self.assertEqual(obj['project']['id'], 2) self.assertEqual(obj['project']['codename'], 'Ladawan') self.assertEqual(obj['currency'], 'RMB') #validate the resource in the database po = PurchaseOrder.objects.get(pk=2) self.assertIsInstance(po.project, Project) #self.assertIsNotNone(obj['pdf']) #self.assertIsNotNone(obj['pdf']['url']) self.items = po.items.all().order_by('id') self.item1 = self.items[0] self.item2 = self.items[1] self.assertIsInstance(self.item1, Item) self.assertIsInstance(self.item1.supply, Supply) self.assertEqual(self.item1.supply.id, 1) self.assertEqual(self.item1.unit_cost, Decimal('12.11')) self.assertEqual(self.item1.quantity, 10) self.assertEqual(self.item1.total, Decimal('121.1')) self.assertIsInstance(self.item2, Item) self.assertIsInstance(self.item2.supply, Supply) self.assertEqual(self.item2.supply.id, 2) #self.assertEqual(self.item2.unit_cost, Decimal('11.50')) self.assertEqual(self.item2.quantity, 3) self.assertEqual(self.item2.total, Decimal('34.51')) project = po.project self.assertEqual(Project.objects.all().count(), 2) self.assertIsInstance(project, Project) self.assertEqual(project.id, 2) self.assertEqual(project.codename, 'Ladawan') def test_creating_new_po_with_price_change(self): """ Tests creating a new po via post while also changing the price of a supply """ print '\n' logger.debug("Creating new po with a price change") print '\n' #validate the response po = copy.deepcopy(base_purchase_order) del po['items'][1] po['items'][0]['cost'] = '1.99' resp = self.client.post('/api/v1/purchase-order/', data=po, format='json') self.assertEqual(resp.status_code, 201, msg=resp) resp_obj = resp.data #webbrowser.get("open -a /Applications/Google\ Chrome.app %s").open(resp_obj['pdf']['url']) #Verify the returned data self.assertEqual(resp_obj['id'], 2) self.assertEqual(resp_obj['vat'], 7) self.assertEqual(Decimal(resp_obj['grand_total']), Decimal('21.30')) item = resp_obj['items'][0] self.assertEqual(Decimal(item['unit_cost']), Decimal('1.99')) self.assertEqual(Decimal(item['total']), Decimal('19.90')) #Verify data in the database supply = Supply.objects.get(pk=1) supply.supplier = self.supplier self.assertEqual(supply.cost, Decimal('1.99')) self.assertEqual(Log.objects.all().count(), 1) log = Log.objects.all()[0] self.assertEqual(log.message, "Price change from 12.11USD to 1.99USD for Pattern: Maxx, Col: Blue [Supplier: Zipper World]") def test_creating_new_po_with_different_currency(self): """ Tests creating a new po via post while also changing the price of a supply """ print '\n' logger.debug("Creating new po with a price change") print '\n' #validate the response po = copy.deepcopy(base_purchase_order) del po['items'][1] po['items'][0]['cost'] = '1.99' po['currency'] = 'RMB' resp = self.client.post('/api/v1/purchase-order/', data=po, format='json') self.assertEqual(resp.status_code, 201, msg=resp) resp_obj = resp.data #webbrowser.get("open -a /Applications/Google\ Chrome.app %s").open(resp_obj['pdf']['url']) #Verify the returned data self.assertEqual(resp_obj['id'], 2) self.assertEqual(resp_obj['vat'], 7) self.assertEqual(resp_obj['currency'], 'RMB') self.assertEqual(Decimal(resp_obj['grand_total']), Decimal('21.30')) item = resp_obj['items'][0] self.assertEqual(Decimal(item['unit_cost']), Decimal('1.99')) self.assertEqual(Decimal(item['total']), Decimal('19.90')) po = PurchaseOrder.objects.get(pk=2) self.assertEqual(po.currency, 'RMB') def test_updating_the_po(self): """ Tests updating the purchase order via a PUT request """ print '\n' logger.debug('Updating PO') print '\n' #Verifying po in database self.assertEqual(self.po.id, 1) self.assertEqual(self.po.items.count(), 1) self.assertEqual(self.po.grand_total, Decimal('129.58')) self.assertEqual(self.po.order_date.date(), datetime.now().date()) item = self.po.items.all()[0] self.assertEqual(item.id, 1) self.assertEqual(item.quantity, 10) self.assertEqual(item.total, Decimal('121.1')) modified_po_data = copy.deepcopy(base_purchase_order) del modified_po_data['items'][0] modified_po_data['items'][0]['id'] = 1 modified_po_data['items'][0]['comments'] = 'test change' modified_po_data['items'][0]['description'] = "test description change" modified_po_data['status'] = 'PROCESSED' resp = self.client.put('/api/v1/purchase-order/1/', format='json', data=modified_po_data) #Verify the response self.assertEqual(resp.status_code, 200, msg=resp) po = resp.data self.assertEqual(po['id'], 1) self.assertEqual(po['supplier']['id'], 1) self.assertEqual(po['vat'], 7) self.assertEqual(Decimal(po['grand_total']), Decimal('38.88')) self.assertEqual(po['discount'], 0) self.assertEqual(po['revision'], 1) self.assertEqual(len(po['items']), 1) #self.assertEqual(po['status'], 'PAID') #Check the new pdf #webbrowser.get("open -a /Applications/Google\ Chrome.app %s").open(po['pdf']['url']) item2 = po['items'][0] self.assertEqual(item2['id'], 1) self.assertEqual(item2['quantity'], '3.0000000000') self.assertEqual(item2['comments'], 'test change') self.assertEqual(item2['description'], 'test description change') self.assertEqual(Decimal(item2['unit_cost']), Decimal('12.11')) self.assertEqual(Decimal(item2['total']), Decimal('36.33')) #Verify database record po = PurchaseOrder.objects.get(pk=1) self.assertEqual(po.supplier.id, 1) self.assertEqual(po.status, 'PROCESSED') self.assertEqual(po.order_date.date(), datetime.datetime.now(timezone('Asia/Bangkok')).date()) self.assertEqual(po.vat, 7) self.assertEqual(po.grand_total, Decimal('38.88')) self.assertEqual(po.items.count(), 1) item2 = po.items.all().order_by('id')[0] self.assertEqual(item2.id, 1) self.assertEqual(item2.description, 'test description change') self.assertEqual(item2.comments, 'test change') self.assertEqual(item2.quantity, 3) self.assertEqual(item2.unit_cost, Decimal('12.11')) self.assertEqual(item2.total, Decimal('36.33')) def test_updating_po_items(self): """ Test updating properties of items in the purchase order """ print '\n' logger.debug('Updating items') print '\n' modified_po_data = copy.deepcopy(base_purchase_order) modified_po_data['status'] = 'PROCESSED' modified_po_data['items'][0]['purchasing_units'] = 'set' resp = self.client.put('/api/v1/purchase-order/1/', format='json', data=modified_po_data) po = resp.data item1 = po['items'][0] #self.assertIn('purchasing_units', item1) #self.assertEqual(item1['purchasing_units'], 'set') def test_updating_po_with_discount(self): """ """ print '\n' logger.debug("Update purchase order with a discount for individual supply") print '\n' #Verify the original po self.assertEqual(self.po.id, 1) self.assertEqual(self.po.items.count(), 1) self.assertEqual(self.po.grand_total, Decimal('129.58')) item = self.po.items.all()[0] self.assertEqual(item.id, 1) self.assertEqual(item.quantity, 10) self.assertEqual(item.total, Decimal('121.1')) modified_po = copy.deepcopy(base_purchase_order) modified_po['items'][0]['discount'] = 50 modified_po['items'][0]['id'] = 1 modified_po['status'] = 'PROCESSED' self.assertEqual(len(modified_po['items']), 2) resp = self.client.put('/api/v1/purchase-order/1/', format='json', data=modified_po) self.assertEqual(resp.status_code, 200, msg=resp) resp_obj = resp.data self.assertEqual(resp_obj['revision'], 1) #Check the new pdf #webbrowser.get("open -a /Applications/Google\ Chrome.app %s").open(resp_obj['pdf']['url']) item1 = resp_obj['items'][0] item2 = resp_obj['items'][1] self.assertEqual(item1['id'], 1) self.assertEqual(item1['quantity'], '10.0000000000') self.assertEqual(Decimal(item1['unit_cost']), Decimal('12.11')) self.assertEqual(Decimal(item1['total']), Decimal('60.55')) self.assertEqual(item2['id'], 2) self.assertEqual(item2['quantity'], '3.0000000000') self.assertEqual(item2['discount'], 5) self.assertEqual(Decimal(item2['unit_cost']), Decimal('12.11')) self.assertEqual(Decimal(item2['total']), Decimal('34.51')) self.assertEqual(Decimal(resp_obj['grand_total']), Decimal('101.72')) po = PurchaseOrder.objects.get(pk=1) item1 = po.items.order_by('id').all()[0] self.assertEqual(item1.id, 1) self.assertEqual(item1.quantity, Decimal('10.00')) self.assertEqual(item1.discount, 50) self.assertEqual(item1.unit_cost, Decimal('12.11')) self.assertEqual(item1.total, Decimal('60.55')) item2 = po.items.order_by('id').all()[1] self.assertEqual(item2.id, 2) self.assertEqual(item2.quantity, Decimal('3.00')) self.assertEqual(item2.unit_cost, Decimal('12.11')) self.assertEqual(item2.discount, 5) self.assertEqual(item2.total, Decimal('34.51')) def test_updating_po_with_new_currency(self): """ Test updating the status of supplies and automatically checking in supplies """ #test original quantity modified_po = copy.deepcopy(base_purchase_order) modified_po['currency'] = 'RMB' resp = self.client.put('/api/v1/purchase-order/1/', format='json', data=modified_po) self.assertEqual(resp.status_code, 200, msg=resp) po = resp.data self.assertEqual(po['currency'], 'RMB') def test_updating_the_supply_price(self): """ Test updating a po with a new cost for an item """ self.assertEqual(self.po.id, 1) self.assertEqual(self.po.items.count(), 1) item = self.po.items.all()[0] self.assertEqual(item.id, 1) self.assertEqual(item.unit_cost, Decimal('12.11')) self.assertEqual(Log.objects.all().count(), 0) modified_po = copy.deepcopy(base_purchase_order) modified_po['items'][0]['unit_cost'] = Decimal('10.05') modified_po['items'][0]['id'] = 1 modified_po['status'] = 'PROCESSED' del modified_po['items'][1] resp = self.client.put('/api/v1/purchase-order/1/', format='json', data=modified_po) self.assertEqual(resp.status_code, 200, msg=resp) resp_obj = resp.data self.assertEqual(resp_obj['revision'], 1) #Check the new pdf #webbrowser.get("open -a /Applications/Google\ Chrome.app %s").open(resp_obj['pdf']['url']) self.assertEqual(resp_obj['id'], 1) self.assertEqual(resp_obj['supplier']['id'], 1) self.assertEqual(resp_obj['vat'], 7) self.assertEqual(resp_obj['discount'], 0) self.assertEqual(resp_obj['revision'], 1) self.assertEqual(Decimal(resp_obj['grand_total']), Decimal('107.54')) item1 = resp_obj['items'][0] self.assertEqual(item1['id'], 2) self.assertEqual(item1['quantity'], '10.0000000000') self.assertEqual(Decimal(item1['unit_cost']), Decimal('10.05')) self.assertEqual(Decimal(item1['total']), Decimal('100.50')) #Confirm cost change for item and supply in the database po = PurchaseOrder.objects.get(pk=1) self.assertEqual(po.grand_total, Decimal('107.54')) item1 = po.items.order_by('id').all()[0] self.assertEqual(item1.id, 1) self.assertEqual(item1.quantity, 10) self.assertEqual(item1.unit_cost, Decimal('10.05')) supply = item1.supply supply.supplier = po.supplier self.assertEqual(supply.cost, Decimal('10.05')) self.assertEqual(Log.objects.all().count(), 1) log = Log.objects.all()[0] self.assertEqual(log.cost, Decimal('10.05')) self.assertEqual(log.supply, supply) self.assertEqual(log.supplier, po.supplier) self.assertEqual(log.message, "Price change from 12.11USD to 10.05USD for Pattern: Maxx, Col: Blue [Supplier: Zipper World]") def test_updating_item_status(self): """ Test updating the status of supplies and automatically checking in supplies """ #test original quantity self.assertEqual(self.supply1.quantity, 10) self.assertEqual(self.supply2.quantity, 10) modified_po = copy.deepcopy(base_purchase_order) modified_po['status'] = 'Received' modified_po['items'][0]['id'] = 1 #modified_po['items'][0]['status'] = 'Receieved' resp = self.client.put('/api/v1/purchase-order/1/', format='json', data=modified_po) self.assertEqual(resp.status_code, 200, msg=resp) po = resp.data self.assertEqual(Supply.objects.get(pk=1).quantity, 20) def test_updating_the_project(self): """ Tests updating the project, phase, and room of a purchase order """ modified_po = copy.deepcopy(base_purchase_order) def test_updating_to_receive_items(self): """ Test updating the status of the po in order to to receive it When a purchase order is received, the items received should automatically be added to the supply inventory quantity """ modified_po = copy.deepcopy(base_purchase_order) modified_po['items'][0]['id'] = 1 modified_po['items'][0]['status'] = 'RECEIVED' modified_po['status'] = 'RECEIVED' self.assertEqual(Supply.objects.get(pk=1).quantity, 10) resp = self.client.put('/api/v1/purchase-order/1/', format='json', data=modified_po) self.assertEqual(resp.status_code, 200, msg=resp) po_data = resp.data self.assertEqual(po_data['id'], 1) self.assertEqual(po_data['status'], 'RECEIVED') item1 = po_data['items'][0] self.assertEqual(item1['id'], 1) self.assertEqual(item1['status'], 'RECEIVED') #Test database values po = PurchaseOrder.objects.get(pk=1) self.assertEqual(po.id, 1) self.assertEqual(po.status, 'RECEIVED') for item in po.items.all(): self.assertEqual(item.status, "RECEIVED") supply = Supply.objects.get(pk=1) self.assertEqual(supply.quantity, 20) log = Log.objects.all().order_by('-id')[0] self.assertEqual(log.action, "ADD") self.assertEqual(log.quantity, 10) self.assertEqual(log.supplier.id, 1) self.assertEqual(log.message, "Received 10m of Pattern: Maxx, Col: Blue from Zipper World")
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)