mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-11-03 22:55:43 +00:00 
			
		
		
		
	Unit test speed improvements (#4463)
* Unit test speed improvements - Move from insantiating data in setUp to setUpTestData * Update UserMixin class for API testing * Bunch of test updates * Further test updates * Test fixes * Add allowances for exchange rate server not responding * Fixes for group role test
This commit is contained in:
		@@ -2357,12 +2357,12 @@ class PartPricing(common.models.MetaMixin):
 | 
			
		||||
 | 
			
		||||
        if self.scheduled_for_update:
 | 
			
		||||
            # Ignore if the pricing is already scheduled to be updated
 | 
			
		||||
            logger.info(f"Pricing for {p} already scheduled for update - skipping")
 | 
			
		||||
            logger.debug(f"Pricing for {p} already scheduled for update - skipping")
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        if counter > 25:
 | 
			
		||||
            # Prevent infinite recursion / stack depth issues
 | 
			
		||||
            logger.info(counter, f"Skipping pricing update for {p} - maximum depth exceeded")
 | 
			
		||||
            logger.debug(counter, f"Skipping pricing update for {p} - maximum depth exceeded")
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        try:
 | 
			
		||||
 
 | 
			
		||||
@@ -1893,15 +1893,16 @@ class PartAPIAggregationTest(InvenTreeAPITestCase):
 | 
			
		||||
        'part.change',
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    def setUp(self):
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def setUpTestData(cls):
 | 
			
		||||
        """Create test data as part of setup routine"""
 | 
			
		||||
        super().setUp()
 | 
			
		||||
        super().setUpTestData()
 | 
			
		||||
 | 
			
		||||
        # Ensure the part "variant" tree is correctly structured
 | 
			
		||||
        Part.objects.rebuild()
 | 
			
		||||
 | 
			
		||||
        # Add a new part
 | 
			
		||||
        self.part = Part.objects.create(
 | 
			
		||||
        cls.part = Part.objects.create(
 | 
			
		||||
            name='Banana',
 | 
			
		||||
            description='This is a banana',
 | 
			
		||||
            category=PartCategory.objects.get(pk=1),
 | 
			
		||||
@@ -1910,12 +1911,12 @@ class PartAPIAggregationTest(InvenTreeAPITestCase):
 | 
			
		||||
        # Create some stock items associated with the part
 | 
			
		||||
 | 
			
		||||
        # First create 600 units which are OK
 | 
			
		||||
        StockItem.objects.create(part=self.part, quantity=100)
 | 
			
		||||
        StockItem.objects.create(part=self.part, quantity=200)
 | 
			
		||||
        StockItem.objects.create(part=self.part, quantity=300)
 | 
			
		||||
        StockItem.objects.create(part=cls.part, quantity=100)
 | 
			
		||||
        StockItem.objects.create(part=cls.part, quantity=200)
 | 
			
		||||
        StockItem.objects.create(part=cls.part, quantity=300)
 | 
			
		||||
 | 
			
		||||
        # Now create another 400 units which are LOST
 | 
			
		||||
        StockItem.objects.create(part=self.part, quantity=400, status=StockStatus.LOST)
 | 
			
		||||
        StockItem.objects.create(part=cls.part, quantity=400, status=StockStatus.LOST)
 | 
			
		||||
 | 
			
		||||
    def get_part_data(self):
 | 
			
		||||
        """Helper function for retrieving part data"""
 | 
			
		||||
 
 | 
			
		||||
@@ -17,26 +17,35 @@ class BomUploadTest(InvenTreeAPITestCase):
 | 
			
		||||
        'part.change',
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    def setUp(self):
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def setUpTestData(cls):
 | 
			
		||||
        """Create BOM data as part of setup routine"""
 | 
			
		||||
        super().setUp()
 | 
			
		||||
        super().setUpTestData()
 | 
			
		||||
 | 
			
		||||
        self.part = Part.objects.create(
 | 
			
		||||
        cls.part = Part.objects.create(
 | 
			
		||||
            name='Assembly',
 | 
			
		||||
            description='An assembled part',
 | 
			
		||||
            assembly=True,
 | 
			
		||||
            component=False,
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
        parts = []
 | 
			
		||||
 | 
			
		||||
        for i in range(10):
 | 
			
		||||
            Part.objects.create(
 | 
			
		||||
                name=f"Component {i}",
 | 
			
		||||
                IPN=f"CMP_{i}",
 | 
			
		||||
                description="A subcomponent that can be used in a BOM",
 | 
			
		||||
                component=True,
 | 
			
		||||
                assembly=False,
 | 
			
		||||
            parts.append(
 | 
			
		||||
                Part(
 | 
			
		||||
                    name=f"Component {i}",
 | 
			
		||||
                    IPN=f"CMP_{i}",
 | 
			
		||||
                    description="A subcomponent that can be used in a BOM",
 | 
			
		||||
                    component=True,
 | 
			
		||||
                    assembly=False,
 | 
			
		||||
                    lft=0, rght=0,
 | 
			
		||||
                    level=0, tree_id=0,
 | 
			
		||||
                )
 | 
			
		||||
            )
 | 
			
		||||
 | 
			
		||||
        Part.objects.bulk_create(parts)
 | 
			
		||||
 | 
			
		||||
    def post_bom(self, filename, file_data, clear_existing=None, expected_code=None, content_type='text/plain'):
 | 
			
		||||
        """Helper function for submitting a BOM file"""
 | 
			
		||||
        bom_file = SimpleUploadedFile(
 | 
			
		||||
 
 | 
			
		||||
@@ -19,15 +19,19 @@ class CategoryTest(TestCase):
 | 
			
		||||
        'params',
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    def setUp(self):
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def setUpTestData(cls):
 | 
			
		||||
        """Extract some interesting categories for time-saving"""
 | 
			
		||||
        self.electronics = PartCategory.objects.get(name='Electronics')
 | 
			
		||||
        self.mechanical = PartCategory.objects.get(name='Mechanical')
 | 
			
		||||
        self.resistors = PartCategory.objects.get(name='Resistors')
 | 
			
		||||
        self.capacitors = PartCategory.objects.get(name='Capacitors')
 | 
			
		||||
        self.fasteners = PartCategory.objects.get(name='Fasteners')
 | 
			
		||||
        self.ic = PartCategory.objects.get(name='IC')
 | 
			
		||||
        self.transceivers = PartCategory.objects.get(name='Transceivers')
 | 
			
		||||
 | 
			
		||||
        super().setUpTestData()
 | 
			
		||||
 | 
			
		||||
        cls.electronics = PartCategory.objects.get(name='Electronics')
 | 
			
		||||
        cls.mechanical = PartCategory.objects.get(name='Mechanical')
 | 
			
		||||
        cls.resistors = PartCategory.objects.get(name='Resistors')
 | 
			
		||||
        cls.capacitors = PartCategory.objects.get(name='Capacitors')
 | 
			
		||||
        cls.fasteners = PartCategory.objects.get(name='Fasteners')
 | 
			
		||||
        cls.ic = PartCategory.objects.get(name='IC')
 | 
			
		||||
        cls.transceivers = PartCategory.objects.get(name='Transceivers')
 | 
			
		||||
 | 
			
		||||
    def test_parents(self):
 | 
			
		||||
        """Test that the parent fields are properly set, based on the test fixtures."""
 | 
			
		||||
 
 | 
			
		||||
@@ -134,14 +134,16 @@ class PartTest(TestCase):
 | 
			
		||||
        'part_pricebreaks'
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    def setUp(self):
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def setUpTestData(cls):
 | 
			
		||||
        """Create some Part instances as part of init routine"""
 | 
			
		||||
        super().setUp()
 | 
			
		||||
 | 
			
		||||
        self.r1 = Part.objects.get(name='R_2K2_0805')
 | 
			
		||||
        self.r2 = Part.objects.get(name='R_4K7_0603')
 | 
			
		||||
        super().setUpTestData()
 | 
			
		||||
 | 
			
		||||
        self.c1 = Part.objects.get(name='C_22N_0805')
 | 
			
		||||
        cls.r1 = Part.objects.get(name='R_2K2_0805')
 | 
			
		||||
        cls.r2 = Part.objects.get(name='R_4K7_0603')
 | 
			
		||||
 | 
			
		||||
        cls.c1 = Part.objects.get(name='C_22N_0805')
 | 
			
		||||
 | 
			
		||||
        Part.objects.rebuild()
 | 
			
		||||
 | 
			
		||||
@@ -529,15 +531,16 @@ class PartSubscriptionTests(InvenTreeTestCase):
 | 
			
		||||
        'part',
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    def setUp(self):
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def setUpTestData(cls):
 | 
			
		||||
        """Create category and part data as part of setup routine"""
 | 
			
		||||
        super().setUp()
 | 
			
		||||
        super().setUpTestData()
 | 
			
		||||
 | 
			
		||||
        # electronics / IC / MCU
 | 
			
		||||
        self.category = PartCategory.objects.get(pk=4)
 | 
			
		||||
        # Electronics / IC / MCU
 | 
			
		||||
        cls.category = PartCategory.objects.get(pk=4)
 | 
			
		||||
 | 
			
		||||
        self.part = Part.objects.create(
 | 
			
		||||
            category=self.category,
 | 
			
		||||
        cls.part = Part.objects.create(
 | 
			
		||||
            category=cls.category,
 | 
			
		||||
            name='STM32F103',
 | 
			
		||||
            description='Currently worth a lot of money',
 | 
			
		||||
            is_template=True,
 | 
			
		||||
@@ -629,14 +632,16 @@ class BaseNotificationIntegrationTest(InvenTreeTestCase):
 | 
			
		||||
        'stock'
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    def setUp(self):
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def setUpTestData(cls):
 | 
			
		||||
        """Add an email address as part of initialization"""
 | 
			
		||||
        super().setUp()
 | 
			
		||||
        # Add Mailadress
 | 
			
		||||
        EmailAddress.objects.create(user=self.user, email='test@testing.com')
 | 
			
		||||
        super().setUpTestData()
 | 
			
		||||
 | 
			
		||||
        # Add email address
 | 
			
		||||
        EmailAddress.objects.create(user=cls.user, email='test@testing.com')
 | 
			
		||||
 | 
			
		||||
        # Define part that will be tested
 | 
			
		||||
        self.part = Part.objects.get(name='R_2K2_0805')
 | 
			
		||||
        cls.part = Part.objects.get(name='R_2K2_0805')
 | 
			
		||||
 | 
			
		||||
    def _notification_run(self, run_class=None):
 | 
			
		||||
        """Run a notification test suit through.
 | 
			
		||||
 
 | 
			
		||||
@@ -20,6 +20,8 @@ class PartPricingTests(InvenTreeTestCase):
 | 
			
		||||
    def setUp(self):
 | 
			
		||||
        """Setup routines"""
 | 
			
		||||
 | 
			
		||||
        super().setUp()
 | 
			
		||||
 | 
			
		||||
        self.generate_exchange_rates()
 | 
			
		||||
 | 
			
		||||
        # Create a new part for performing pricing calculations
 | 
			
		||||
@@ -29,8 +31,6 @@ class PartPricingTests(InvenTreeTestCase):
 | 
			
		||||
            assembly=True
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
        return super().setUp()
 | 
			
		||||
 | 
			
		||||
    def create_price_breaks(self):
 | 
			
		||||
        """Create some price breaks for the part, in various currencies"""
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user