def work(): data = [] review = [] supplier = Supplier.objects.get(name__icontains="blue international") with open("/Users/Charlie/Sites/employee/backend/blue-inter.csv", 'r') as f: reader = csv.reader(f) for row in reader: data.append({ 'reference': row[0], 'description': row[1], 'cost': row[2], 'units': row[3] }) for index, d in enumerate(data): for key in d.keys(): try: d[key] = d[key].encode('utf-8') except UnicodeDecodeError as e: pass if key == "cost": try: d[key] = Decimal(d[key].replace(',', '')) except InvalidOperation as e: review.append(data.pop(index)) if key == "units": if not d[key]: review.append(data.pop(index)) for index, d in enumerate(data): try: try: supply = Supply.objects.get(description=d['description']) except Supply.DoesNotExist: supply = Supply() supply.description = d['description'] supply.units = d['units'] supply.full_clean() supply.save() try: product = Product.objects.get(supply=supply) except Product.DoesNotExist: product = Product(supply=supply, supplier=supplier) product.supplier = supplier product.supply = supply product.reference = d['reference'] product.cost = d['cost'] product.purchasing_units = d['units'] product.full_clean() product.save() except ValidationError as e: print e review.append(data.pop(index)) assert Supply.objects.filter(description=d['description']).count() == 1 with open('blue-inter-review.csv', 'w') as f: fieldnames = ['reference', 'description', 'cost', 'units'] writer = csv.DictWriter(f) writer.write_header() for d in review: writer.writerow(d) assert supplier.supplies.all().count() == len(data)
class ProjectResourceTestCase(APITestCase): def setUp(self): """ Sets up for tests """ super(ProjectResourceTestCase, self).setUp() self.create_user() #self.api_client.client.login(username='******', password='******') #self.customer = Customer.create(**base_customer) #self.project = Project.create(**base_project) self.project = Project(codename="Ladawan") self.project.save() self.supply = Supply(description='Grommet') self.supply.save() self.supply2 = Supply(description='Hinge') self.supply2.save() self.project_supply = ProjectSupply(supply=self.supply, project=self.project, quantity=2) self.project_supply.save() def create_user(self): self.user = User.objects.create_user('test', '*****@*****.**', 'test') self.ct = ContentType(app_label='projects') self.ct.save() #self._create_and_add_permission('view_cost', self.user) self._create_and_add_permission('change_project', self.user) #self._create_and_add_permission('add_supply', self.user) #self._create_and_add_permission('add_quantity', self.user) #self._create_and_add_permission('subtract_quantity', self.user) def _create_and_add_permission(self, codename, user): p = Permission(content_type=self.ct, codename=codename) p.save() user.user_permissions.add(p) def test_get(self): """ Test retrieving resources via GET """ resp = self.api_client.get('/api/v1/project') self.assertHttpOK(resp) obj = self.deserialize(resp) self.assertEqual(len(obj['objects']), 1) project = obj['objects'][0] self.assertEqual(project['id'], 1) self.assertEqual(project['codename'], 'Ladawan') def test_get_obj(self): """ Tests retrieving a single object via GET """ resp = self.api_client.get('/api/v1/project/1') self.assertHttpOK(resp) obj = self.deserialize(resp) self.assertEqual(obj['id'], 1) self.assertEqual(obj['codename'], 'Ladawan') self.assertIn('supplies', obj) self.assertEqual(len(obj['supplies']), 1) supply = obj['supplies'][0] self.assertEqual(supply['id'], 1) self.assertEqual(supply['description'], 'Grommet') self.assertEqual(int(supply['quantity']), 2) def test_get_all_projects(self): """ Test retrieving full list of projects over 100""" for i in xrange(0, 100): project = Project(codename=i) project.save() self.assertEqual(Project.objects.all().count(), 101) resp = self.client.get('/api/v1/project/?page_size=99999', format='json') self.assertEqual(resp.status_code, 200) data = resp.data['results'] self.assertEqual(len(data), 101) @unittest.skip('') def test_create_project(self): """ Tests creating a project """ self.assertIsNotNone(self.project) self.assertIsInstance(self.project, Project) self.assertIsInstance(self.project.customer, Customer) self.assertEqual(self.project.customer, self.customer) self.assertEqual(self.project.reference, "S5") self.assertEqual(self.project.type, "Condominium") self.assertEqual(self.project.due_date, base_due_date.date()) self.assertEqual(self.project.codename, "Haze") def test_update_project(self): """ Tests updating a project """ data = {'codename': "Ladawan 329", 'supplies': [{'id': 1, 'description': 'Grommet', 'quantity': 5}, {'id': 2, 'quantity': 10}]} resp = self.api_client.put('/api/v1/project/1', format='json', data=data) self.assertHttpOK(resp) obj = self.deserialize(resp) self.assertEqual(obj['id'], 1) self.assertEqual(obj['codename'], "Ladawan 329") self.assertEqual(len(obj['supplies']), 2) supply1 = obj['supplies'][0] self.assertEqual(supply1['id'], 1) self.assertEqual(supply1['description'], 'Grommet') self.assertEqual(int(supply1['quantity']), 5) supply2 = obj['supplies'][1] self.assertEqual(supply2['id'], 2) self.assertEqual(supply2['description'], 'Hinge') self.assertEqual(int(supply2['quantity']), 10) def test_update_project_deleting_supply(self): """ Tests deleting a project supply via PUT """ data = {'codename': "Ladawan 329", 'supplies': [{'id': 2, 'description': 'Hinge', 'quantity': 10}]} resp = self.api_client.put('/api/v1/project/1', format='json', data=data) self.assertHttpOK(resp) obj = self.deserialize(resp) self.assertEqual(obj['id'], 1) self.assertEqual(obj['codename'], "Ladawan 329") self.assertEqual(len(obj['supplies']), 1) supply1 = obj['supplies'][0] self.assertEqual(supply1['id'], 2) self.assertEqual(supply1['description'], 'Hinge') self.assertEqual(int(supply1['quantity']), 10) @unittest.skip('') def _update_due_date(self): """ Tests the updating of the due date """ due_date = dateutil.parser.parse("2013-04-26T13:59:01.143Z") self.project.update(due_date=due_date.isoformat()) self.assertEqual(self.project.due_date, due_date.date()) delta = datetime.timedelta(days=5) due_date2 = due_date + delta self.project.update(due_date=due_date2) self.assertEqual(self.project.due_date, due_date2.date())
def work(): data = [] review = [] supplier = Supplier.objects.get(name__icontains="blue international") with open("/Users/Charlie/Sites/employee/backend/blue-inter.csv", 'r') as f: reader = csv.reader(f) for row in reader: data.append({'reference': row[0], 'description': row[1], 'cost': row[2], 'units': row[3]}) for index, d in enumerate(data): for key in d.keys(): try: d[key] = d[key].encode('utf-8') except UnicodeDecodeError as e: pass if key == "cost": try: d[key] = Decimal(d[key].replace(',', '')) except InvalidOperation as e: review.append(data.pop(index)) if key == "units": if not d[key]: review.append(data.pop(index)) for index, d in enumerate(data): try: try: supply = Supply.objects.get(description=d['description']) except Supply.DoesNotExist: supply = Supply() supply.description = d['description'] supply.units = d['units'] supply.full_clean() supply.save() try: product = Product.objects.get(supply=supply) except Product.DoesNotExist: product = Product(supply=supply, supplier=supplier) product.supplier = supplier product.supply = supply product.reference = d['reference'] product.cost = d['cost'] product.purchasing_units = d['units'] product.full_clean() product.save() except ValidationError as e: print e review.append(data.pop(index)) assert Supply.objects.filter(description=d['description']).count() == 1 with open('blue-inter-review.csv', 'w') as f: fieldnames = ['reference', 'description', 'cost', 'units'] writer = csv.DictWriter(f) writer.write_header() for d in review: writer.writerow(d) assert supplier.supplies.all().count() == len(data)