def setUpTestData(cls):
        cls.factory = APIRequestFactory()
        cls.test_user = User.objects.create_user(username='******',
                                                 password='******')

        cls.test_admin = User.objects.create_superuser(
            username='******',
            email='*****@*****.**',
            password='******')

        cls.report_name = "Q2 2019 Report"
        cls.test_supply1 = Supply(name="3D printer",
                                  state="Brand new",
                                  description="User manual is lost")
        cls.test_supply1.save()
        cls.test_supply2 = Supply(name="Screwdriver",
                                  state="Worn out",
                                  description="Handle is broken")
        cls.test_supply2.save()

        # Report has to be generated through view to make sure it's populated with supplies
        cls.test_report_name = 'Test report'
        request = cls.factory.post('/api/inventories/',
                                   {'name': cls.test_report_name})
        force_authenticate(request, user=cls.test_admin)
        InventoryReportListCreateView.as_view()(request)
        cls.test_report = InventoryReport.objects.get(
            name=cls.test_report_name)
Esempio n. 2
0
    def setUp(self):
        """
        Set up the view 
        
        -login the user
        """
        super(SupplyAPITestCase, self).setUp()

        self.create_user()
        self.client.login(username="******", password="******")

        self.supplier = Supplier(**base_supplier)
        self.supplier.save()
        self.supplier2 = Supplier.objects.create(**base_supplier)
        self.supply = Supply.create(**base_supply)
        self.assertIsNotNone(self.supply.pk)
        self.supply2 = Supply.create(**base_supply)
        self.assertIsNotNone(self.supply2.pk)
        self.supply3 = Supply.create(**base_supply)
        self.assertIsNotNone(self.supply3.pk)
        self.supply4 = Supply.create(**base_supply)
        self.assertIsNotNone(self.supply4.pk)

        self.product = Product(supplier=self.supplier, supply=self.supply)
        self.product.save()

        self.employee = Employee(first_name="John", last_name="Smith")
        self.employee.save()
Esempio n. 3
0
    def setUp(self):
        """
        Set up the view 
        
        -login the user
        """
        super(SupplyAPITest, self).setUp()

        self.create_user()
        self.client.login(username='******', password='******')

        self.supplier = Supplier(**base_supplier)
        self.supplier.save()
        self.supplier2 = Supplier.objects.create(**base_supplier)
        self.supply = Supply.create(**base_supply)
        self.assertIsNotNone(self.supply.pk)
        self.supply2 = Supply.create(**base_supply)
        self.assertIsNotNone(self.supply2.pk)
        self.supply3 = Supply.create(**base_supply)
        self.assertIsNotNone(self.supply3.pk)
        self.supply4 = Supply.create(**base_supply)
        self.assertIsNotNone(self.supply4.pk)

        self.product = Product(supplier=self.supplier, supply=self.supply)
        self.product.save()

        self.employee = Employee(first_name="John", last_name="Smith")
        self.employee.save()
    def setUpTestData(cls):
        cls.factory = APIRequestFactory()
        cls.test_user = User.objects.create_user(username='******',
                                                 password='******')

        cls.test_admin = User.objects.create_superuser(
            username='******',
            email='*****@*****.**',
            password='******')

        cls.report_name = "Q2 2019 Report"
        cls.test_supply_name = "3D printer"
        cls.test_supply_state = "Brand new"
        cls.test_supply_description = "User manual is lost"
        cls.test_supply = Supply(name=cls.test_supply_name,
                                 state=cls.test_supply_state,
                                 description=cls.test_supply_description)
        cls.test_supply.save()

        cls.test_report = InventoryReport(name=cls.report_name)
        cls.test_report.save()

        cls.test_inventory_supply = InventorySupply(
            inventory_supply=cls.test_supply, inventory_report=cls.test_report)
        cls.test_inventory_supply.save()
    def setUpTestData(cls):
        cls.factory = APIRequestFactory()
        cls.test_user = User.objects.create_user(username='******',
                                                 password='******')

        cls.test_admin = User.objects.create_superuser(
            username='******',
            email='*****@*****.**',
            password='******')

        cls.report_name = "Q2 2019 Report"
        cls.test_supply1 = Supply(name="3D printer",
                                  state="Brand new",
                                  description="User manual is lost")
        cls.test_supply1.save()
        cls.test_supply2 = Supply(name="Screwdriver",
                                  state="Worn out",
                                  description="Handle is broken")
        cls.test_supply2.save()
Esempio n. 6
0
 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()
Esempio n. 7
0
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())
Esempio n. 8
0
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)
Esempio n. 9
0
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)