diff --git a/.travis.yml b/.travis.yml index 6451edf162..67d8c0502a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -31,7 +31,8 @@ script: - cd InvenTree && python3 manage.py makemigrations && cd .. - python3 ci/check_migration_files.py - invoke coverage - - invoke test --database=mysql + - cd InvenTree && python3 manage.py test --settings=InvenTree.ci_mysql && cd .. + - cd InvenTree && python3 manage.py test --settings=InvenTree.ci_postgresql && cd .. - invoke translate - invoke style diff --git a/InvenTree/build/fixtures/build.yaml b/InvenTree/build/fixtures/build.yaml index 47e77dff07..532d502098 100644 --- a/InvenTree/build/fixtures/build.yaml +++ b/InvenTree/build/fixtures/build.yaml @@ -1,6 +1,7 @@ # Construct build objects - model: build.build + pk: 1 fields: part: 25 batch: 'B1' @@ -16,6 +17,7 @@ tree_id: 0 - model: build.build + pk: 2 fields: part: 50 title: 'Making things' diff --git a/InvenTree/build/test_build.py b/InvenTree/build/test_build.py index 32ad33dab3..e69853c269 100644 --- a/InvenTree/build/test_build.py +++ b/InvenTree/build/test_build.py @@ -203,22 +203,25 @@ class BuildTest(TestCase): # - Three for the split items assigned to the build self.assertEqual(StockItem.objects.count(), 16) + A = StockItem.objects.get(pk=self.stock_1_1.pk) + B = StockItem.objects.get(pk=self.stock_1_2.pk) + C = StockItem.objects.get(pk=self.stock_2_1.pk) + # Stock should have been subtracted from the original items - self.assertEqual(StockItem.objects.get(pk=1).quantity, 950) - self.assertEqual(StockItem.objects.get(pk=2).quantity, 50) - self.assertEqual(StockItem.objects.get(pk=3).quantity, 4750) + self.assertEqual(A.quantity, 950) + self.assertEqual(B.quantity, 50) + self.assertEqual(C.quantity, 4750) - # New stock items created and assigned to the build - self.assertEqual(StockItem.objects.get(pk=4).quantity, 50) - self.assertEqual(StockItem.objects.get(pk=4).build_order, self.build) + # New stock items should have also been allocated to the build + allocated = StockItem.objects.filter(build_order=self.build) - self.assertEqual(StockItem.objects.get(pk=5).quantity, 50) - self.assertEqual(StockItem.objects.get(pk=5).build_order, self.build) + self.assertEqual(allocated.count(), 3) - self.assertEqual(StockItem.objects.get(pk=6).quantity, 250) - self.assertEqual(StockItem.objects.get(pk=6).build_order, self.build) + q = sum([item.quantity for item in allocated.all()]) + + self.assertEqual(q, 350) - # And a new stock item created for the build output - self.assertEqual(StockItem.objects.get(pk=7).quantity, 1) - self.assertEqual(StockItem.objects.get(pk=7).serial, 1) - self.assertEqual(StockItem.objects.get(pk=7).build, self.build) + # And 10 new stock items created for the build output + outputs = StockItem.objects.filter(build=self.build) + + self.assertEqual(outputs.count(), 10) diff --git a/InvenTree/company/fixtures/price_breaks.yaml b/InvenTree/company/fixtures/price_breaks.yaml index 6616ab36b1..6ae8cce94c 100644 --- a/InvenTree/company/fixtures/price_breaks.yaml +++ b/InvenTree/company/fixtures/price_breaks.yaml @@ -3,18 +3,21 @@ # Price breaks for ACME0001 - model: company.supplierpricebreak + pk: 1 fields: part: 1 quantity: 1 cost: 10 - model: company.supplierpricebreak + pk: 2 fields: part: 1 quantity: 5 cost: 7.50 - model: company.supplierpricebreak + pk: 3 fields: part: 1 quantity: 25 @@ -22,12 +25,14 @@ # Price breaks for ACME0002 - model: company.supplierpricebreak + pk: 4 fields: part: 2 quantity: 5 cost: 7.00 - model: company.supplierpricebreak + pk: 5 fields: part: 2 quantity: 50 @@ -35,12 +40,14 @@ # Price breaks for ZERGLPHS - model: company.supplierpricebreak + pk: 6 fields: part: 4 quantity: 25 cost: 8 - model: company.supplierpricebreak + pk: 7 fields: part: 4 quantity: 100 diff --git a/InvenTree/company/tests.py b/InvenTree/company/tests.py index ecc942f652..db515d3e59 100644 --- a/InvenTree/company/tests.py +++ b/InvenTree/company/tests.py @@ -113,17 +113,17 @@ class ContactSimpleTest(TestCase): def setUp(self): # Create a simple company - c = Company.objects.create(name='Test Corp.', description='We make stuff good') + self.c = Company.objects.create(name='Test Corp.', description='We make stuff good') # Add some contacts - Contact.objects.create(name='Joe Smith', company=c) - Contact.objects.create(name='Fred Smith', company=c) - Contact.objects.create(name='Sally Smith', company=c) + Contact.objects.create(name='Joe Smith', company=self.c) + Contact.objects.create(name='Fred Smith', company=self.c) + Contact.objects.create(name='Sally Smith', company=self.c) def test_exists(self): self.assertEqual(Contact.objects.count(), 3) def test_delete(self): # Remove the parent company - Company.objects.get(pk=1).delete() + Company.objects.get(pk=self.c.pk).delete() self.assertEqual(Contact.objects.count(), 0) diff --git a/InvenTree/order/fixtures/order.yaml b/InvenTree/order/fixtures/order.yaml index ed4eae84eb..4ceaf3d482 100644 --- a/InvenTree/order/fixtures/order.yaml +++ b/InvenTree/order/fixtures/order.yaml @@ -29,6 +29,7 @@ # 250 x ACME0002 (M2x4 LPHS) # Partially received (50) - model: order.purchaseorderlineitem + pk: 2 fields: order: 1 part: 2 @@ -37,6 +38,7 @@ # 1000 x ACME0003 - model: order.purchaseorderlineitem + pk: 3 fields: order: 1 part: 3 diff --git a/InvenTree/order/test_sales_order.py b/InvenTree/order/test_sales_order.py index 6cc48c3b6f..dc4530e808 100644 --- a/InvenTree/order/test_sales_order.py +++ b/InvenTree/order/test_sales_order.py @@ -27,8 +27,8 @@ class SalesOrderTest(TestCase): self.part = Part.objects.create(name='Spanner', salable=True, description='A spanner that I sell') # Create some stock! - StockItem.objects.create(part=self.part, quantity=100) - StockItem.objects.create(part=self.part, quantity=200) + self.Sa = StockItem.objects.create(part=self.part, quantity=100) + self.Sb = StockItem.objects.create(part=self.part, quantity=200) # Create a SalesOrder to ship against self.order = SalesOrder.objects.create( @@ -57,15 +57,16 @@ class SalesOrderTest(TestCase): SalesOrderLineItem.objects.create(order=self.order, part=self.part) def allocate_stock(self, full=True): + # Allocate stock to the order SalesOrderAllocation.objects.create( line=self.line, - item=StockItem.objects.get(pk=1), + item=StockItem.objects.get(pk=self.Sa.pk), quantity=25) SalesOrderAllocation.objects.create( line=self.line, - item=StockItem.objects.get(pk=2), + item=StockItem.objects.get(pk=self.Sb.pk), quantity=25 if full else 20 ) @@ -119,15 +120,23 @@ class SalesOrderTest(TestCase): # There should now be 4 stock items self.assertEqual(StockItem.objects.count(), 4) - self.assertEqual(StockItem.objects.get(pk=1).quantity, 75) - self.assertEqual(StockItem.objects.get(pk=2).quantity, 175) - self.assertEqual(StockItem.objects.get(pk=3).quantity, 25) - self.assertEqual(StockItem.objects.get(pk=3).quantity, 25) + Sa = StockItem.objects.get(pk=self.Sa.pk) + Sb = StockItem.objects.get(pk=self.Sb.pk) + + # 25 units subtracted from each of the original items + self.assertEqual(Sa.quantity, 75) + self.assertEqual(Sb.quantity, 175) + + # And 2 items created which are associated with the order + outputs = StockItem.objects.filter(sales_order=self.order) + self.assertEqual(outputs.count(), 2) + + for item in outputs.all(): + self.assertEqual(item.quantity, 25) + - self.assertEqual(StockItem.objects.get(pk=1).sales_order, None) - self.assertEqual(StockItem.objects.get(pk=2).sales_order, None) - self.assertEqual(StockItem.objects.get(pk=3).sales_order, self.order) - self.assertEqual(StockItem.objects.get(pk=4).sales_order, self.order) + self.assertEqual(Sa.sales_order, None) + self.assertEqual(Sb.sales_order, None) # And no allocations self.assertEqual(SalesOrderAllocation.objects.count(), 0) diff --git a/InvenTree/part/fixtures/bom.yaml b/InvenTree/part/fixtures/bom.yaml index 60ca136ce2..a9e1bed6f0 100644 --- a/InvenTree/part/fixtures/bom.yaml +++ b/InvenTree/part/fixtures/bom.yaml @@ -2,6 +2,7 @@ # 10 x M2x4 LPHS - model: part.bomitem + pk: 1 fields: part: 100 sub_part: 1 @@ -9,6 +10,7 @@ # 40 x R_2K2_0805 - model: part.bomitem + pk: 2 fields: part: 100 sub_part: 3 @@ -16,6 +18,7 @@ # 25 x C_22N_0805 - model: part.bomitem + pk: 3 fields: part: 100 sub_part: 5 @@ -23,6 +26,7 @@ # 3 x Orphan - model: part.bomitem + pk: 4 fields: part: 100 sub_part: 50 diff --git a/InvenTree/part/fixtures/params.yaml b/InvenTree/part/fixtures/params.yaml index 4827d0987b..121bb79074 100644 --- a/InvenTree/part/fixtures/params.yaml +++ b/InvenTree/part/fixtures/params.yaml @@ -20,12 +20,14 @@ # And some parameters (requires part.yaml) - model: part.PartParameter + pk: 1 fields: part: 1 template: 1 data: 4 - model: part.PartParameter + pk: 2 fields: part: 2 template: 1 diff --git a/InvenTree/part/fixtures/part.yaml b/InvenTree/part/fixtures/part.yaml index 76a073261b..c77fd5dc57 100644 --- a/InvenTree/part/fixtures/part.yaml +++ b/InvenTree/part/fixtures/part.yaml @@ -38,6 +38,7 @@ - model: part.part + pk: 4 fields: name: 'R_4K7_0603' description: '4.7kOhm resistor in 0603 package' @@ -50,6 +51,7 @@ # Create some capacitors - model: part.part + pk: 5 fields: name: 'C_22N_0805' description: '22nF capacitor in 0805 package' diff --git a/InvenTree/part/fixtures/test_templates.yaml b/InvenTree/part/fixtures/test_templates.yaml index f939dea0c1..2e06fe9205 100644 --- a/InvenTree/part/fixtures/test_templates.yaml +++ b/InvenTree/part/fixtures/test_templates.yaml @@ -1,25 +1,30 @@ # Tests for the top-level "chair" part - model: part.parttesttemplate + pk: 1 fields: part: 10000 test_name: Test strength of chair - model: part.parttesttemplate + pk: 2 fields: part: 10000 test_name: Apply paint - model: part.parttesttemplate + pk: 3 fields: part: 10000 test_name: Sew cushion - model: part.parttesttemplate + pk: 4 fields: part: 10000 test_name: Attach legs - model: part.parttesttemplate + pk: 5 fields: part: 10000 test_name: Record weight @@ -27,12 +32,14 @@ # Add some tests for one of the variants - model: part.parttesttemplate + pk: 6 fields: part: 10003 test_name: Check that chair is green required: true - model: part.parttesttemplate + pk: 7 fields: part: 10004 test_name: Check that chair is especially green diff --git a/InvenTree/stock/fixtures/stock.yaml b/InvenTree/stock/fixtures/stock.yaml index 156aa8db53..dd528775eb 100644 --- a/InvenTree/stock/fixtures/stock.yaml +++ b/InvenTree/stock/fixtures/stock.yaml @@ -2,6 +2,7 @@ # 4,000 screws in the dining room - model: stock.stockitem + pk: 1 fields: part: 1 location: 3 @@ -14,6 +15,7 @@ # 5,000 screws in the bathroom - model: stock.stockitem + pk: 2 fields: part: 1 location: 2 diff --git a/InvenTree/stock/fixtures/stock_tests.yaml b/InvenTree/stock/fixtures/stock_tests.yaml index 8207ccaa8c..7ee6f3b387 100644 --- a/InvenTree/stock/fixtures/stock_tests.yaml +++ b/InvenTree/stock/fixtures/stock_tests.yaml @@ -1,4 +1,5 @@ - model: stock.stockitemtestresult + pk: 1 fields: stock_item: 105 test: "Firmware Version" @@ -7,6 +8,7 @@ date: 2020-02-02 - model: stock.stockitemtestresult + pk: 2 fields: stock_item: 105 test: "Settings Checksum" @@ -15,6 +17,7 @@ date: 2020-02-02 - model: stock.stockitemtestresult + pk: 3 fields: stock_item: 105 test: "Temperature Test" @@ -23,6 +26,7 @@ notes: 'Got too hot or something' - model: stock.stockitemtestresult + pk: 4 fields: stock_item: 105 test: "Temperature Test" @@ -31,6 +35,7 @@ notes: 'Passed temperature test by making it cooler' - model: stock.stockitemtestresult + pk: 5 fields: stock_item: 522 test: 'applypaint' @@ -38,6 +43,7 @@ date: 2020-05-17 - model: stock.stockitemtestresult + pk: 6 fields: stock_item: 522 test: 'applypaint' @@ -45,6 +51,7 @@ date: 2020-05-18 - model: stock.stockitemtestresult + pk: 7 fields: stock_item: 522 test: 'Attach Legs' @@ -52,6 +59,7 @@ date: 2020-05-17 - model: stock.stockitemtestresult + pk: 8 fields: stock_item: 522 test: 'Check that chair is GreEn ' diff --git a/InvenTree/stock/test_api.py b/InvenTree/stock/test_api.py index 47f2e5fcb1..a522bc5415 100644 --- a/InvenTree/stock/test_api.py +++ b/InvenTree/stock/test_api.py @@ -21,7 +21,7 @@ class StockAPITestCase(APITestCase): def setUp(self): # Create a user for auth User = get_user_model() - User.objects.create_user('testuser', 'test@testing.com', 'password') + self.user = User.objects.create_user('testuser', 'test@testing.com', 'password') self.client.login(username='testuser', password='password') def doPost(self, url, data={}): @@ -308,4 +308,4 @@ class StockTestResultTest(StockAPITestCase): test = response.data[0] self.assertEqual(test['value'], '150kPa') - self.assertEqual(test['user'], 1) + self.assertEqual(test['user'], self.user.pk) diff --git a/tasks.py b/tasks.py index a5e7f61430..2707f377ba 100644 --- a/tasks.py +++ b/tasks.py @@ -174,37 +174,16 @@ def style(c): print("Running PEP style checks...") c.run('flake8 InvenTree') -@task(help={'database': "Database framework to use (default=sqlite)"}) +@task def test(c, database=None): """ Run unit-tests for InvenTree codebase. """ - - if database is None: - database = 'sqlite' - - database = database.lower() - - allowed = { - 'sqlite': 'InvenTree.settings', - 'postgresql': 'InvenTree.ci_postgresql', - 'mysql': 'InvenTree.ci_mysql', - } - - if database not in allowed.keys(): - print("Database framework not supported for testing:") - print("Choose from: '{a}'".format(a=allowed)) - - return False - # Run sanity check on the django install manage(c, 'check') # Run coverage tests - manage(c, 'test {apps} --settings={sett}'.format( - apps=' '.join(apps()), - sett=allowed[database] - )) + manage(c, 'test', pty=True) @task def coverage(c):