mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-29 12:06:44 +00:00
* Create new model for storing Part pricing data Currently this model does not "do" anything but will be used for caching pre-calculated pricing information * Define function for accessing pricing information for a specific part * Adds admin site support for new PartPricing model * Specify role for PartPricing model * Allow blank values for PartPricing model fields * Add some TODO entries * Update migration files to sync with latest master * Expose API endpoint for viewing part pricing information * Update migration file * Improvements: - Updated model with new fields - Code for calculating BOM price - Code for calculating internal price - Code for calculating supplier price - Updated unit testing * Fix (and test) for API serializer * Including min/max pricing data in part serializer * Bump API version * Add pricing overview information in part table - Adds helper function for formatting currency data - No longer pre-render "price strings" on the server * Overhaul of BOM API - Pricing data no longer calculated "on the fly" - Remove expensive annotation operations - Display cached price range information in BOM table * Filter BOM items by "has pricing" * Part API endpoint can be filtered by price range * Updpated API version notes * Improvements for price caching calculations - Handle null price values - Handle case where conversion rates are missing - Allow manual update via API * Button to manually refresh pricing * Improve rendering of price-break table * Update supplier part pricing table * Updated js functions * Adds background task to update assembly pricing whenever a part price cache is changed * Updates for task offloading * HTML tweaks * Implement calculation of historical purchase cost - take supplier part pack size into account - improve unit tests * Improvements for pricing tab rendering * Refactor of pricing page - Move javascript functions out into separate files - Change price-break tables to use bar graphs - Display part pricing history table and chart - Remove server-side rendering for price history data - Fix rendering of supplier pricing table - Adds extra filtering options to the SupplierPriceBreak API endpoint * Refactor BOM pricing chart / table - Display as bar chart with min/max pricing - Display simplified BOM table * Update page anchors * Improvements for BOM pricing table display * Refactoring sales data tables - Add extra data and filter options to sales order API endpoints - Display sales order history table and chart * Add extra fields to PartPricing model: - sale_price_min - sale_price_max - sale_history_min - sale_history_max * Calculate and cache sale price data * Update part pricing when PurchaseOrder is completed * Update part pricing when sales order is completed * Signals for updating part pricing cache - Whenever an internal price break is created / edited / deleted - Whenever a sale price break is created / edited / deleted * Also trigger part pricing update when BomItem is created / edited / deleted * Update part pricing whenever a supplier price break is updated * Remove has_complete_bom_pricing method * Export min/max pricing data in BOM file * Fix pricing data in BOM export - Calculate total line cost - Use more than two digits * Add pricing information to part export Also some improvements to part exporting * Allow download of part category table * Allow export of stock location data to file * Improved exporting of StockItem data * Add cached variant pricing data - New fields in part pricing model - Display variant pricing overview in "pricing" tab * Remove outdated "PART_SHOW_PRICE_HISTORY" setting * Adds scheduled background task to periodically update part pricing * Internal prices can optionally override other pricing * Update js file checks * Update price breaks to use 6 decimal places * Fix for InvenTreeMoneySerializer class - Allow 6 decimal places through the API * Update for supplier price break table * javascript linting fix * Further js fixes * Unit test updates * Improve rendering of currency in templates - Do not artificially limit to 2 decimal places * Unit test fixes * Add pricing information to part "details" tab * Tweak for money formatting * Enable sort-by-price in BOM table * More unit test tweaks * Update BOM exporting * Fixes for background worker process - To determine if worker is running, look for *any* successful task, not just heartbeat - Heartbeat rate increased to 5 minute intervals - Small adjustments to django_q settings Ref: https://github.com/inventree/InvenTree/issues/3921 (cherry picked from commit cb26003b92536f67fba640d84aa2a6596d18b4e0) * Force background processing of heartbeat task when server is started - Removes the ~5 minute window in which the server "thinks" that the worker is not actually running * Adjust strategy for preventing recursion - Rather than looking for duplicate parts, simply increment a counter - Add a "scheduled_for_update" flag to prevent multiple updates being scheduled - Consolidate migration files * Adds helper function for rendering a range of prices * Include variant cost in calculations * Fixes for "has_pricing" API filters * Ensure part pricing status flags are reset when the server restarts * Bug fix for BOM API filter * Include BOM quantity in BOM pricing chart * Small tweaks to pricing tab * Prevent caching when looking up settings in background worker - Caching across mnultiple processes causes issues - Need to move to something like redis to solve this - Ref: https://github.com/inventree/InvenTree/issues/3921 * Fixes for /part/pricing/ detail API endpoint * Update pricing tab - Consistent naming * Unit test fixes * Prevent pricing updates when loading test fixtures * Fix for Part.pricing * Updates for "check_missing_pricing" * Change to pie chart for BOM pricing * Unit test fix * Updates - Sort BOM pie chart correctly - Simplify PartPricing.is_valid - Pass "limit" through to check_missing_pricing - Improved logic for update scheduling * Add option for changing how many decimals to use when displaying pricing data * remove old unused setting * Consolidate settings tabs for pricing and currencies * Fix CI after changing settings page * Fix rendering for "Supplier Pricing" - Take unit pricing / pack size into account * Extra filtering / ordering options for the SupplierPriceBreak API endpoint * Fix for purchase price history graph - Use unit pricing (take pack size into account) * JS fixes
329 lines
12 KiB
Python
329 lines
12 KiB
Python
"""Unit tests for the PartCategory model"""
|
|
|
|
from django.core.exceptions import ValidationError
|
|
from django.test import TestCase
|
|
|
|
from .models import Part, PartCategory, PartParameter, PartParameterTemplate
|
|
|
|
|
|
class CategoryTest(TestCase):
|
|
"""Tests to ensure that the relational category tree functions correctly.
|
|
|
|
Loads the following test fixtures:
|
|
- category.yaml
|
|
"""
|
|
fixtures = [
|
|
'category',
|
|
'part',
|
|
'location',
|
|
'params',
|
|
]
|
|
|
|
def setUp(self):
|
|
"""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')
|
|
|
|
def test_parents(self):
|
|
"""Test that the parent fields are properly set, based on the test fixtures."""
|
|
self.assertEqual(self.resistors.parent, self.electronics)
|
|
self.assertEqual(self.capacitors.parent, self.electronics)
|
|
self.assertEqual(self.electronics.parent, None)
|
|
|
|
self.assertEqual(self.fasteners.parent, self.mechanical)
|
|
|
|
def test_children_count(self):
|
|
"""Test that categories have the correct number of children."""
|
|
self.assertTrue(self.electronics.has_children)
|
|
self.assertTrue(self.mechanical.has_children)
|
|
|
|
self.assertEqual(len(self.electronics.children.all()), 3)
|
|
self.assertEqual(len(self.mechanical.children.all()), 1)
|
|
|
|
def test_unique_childs(self):
|
|
"""Test the 'unique_children' functionality."""
|
|
childs = [item.pk for item in self.electronics.getUniqueChildren()]
|
|
|
|
self.assertIn(self.transceivers.id, childs)
|
|
self.assertIn(self.ic.id, childs)
|
|
|
|
self.assertNotIn(self.fasteners.id, childs)
|
|
|
|
def test_unique_parents(self):
|
|
"""Test the 'unique_parents' functionality."""
|
|
parents = [item.pk for item in self.transceivers.getUniqueParents()]
|
|
|
|
self.assertIn(self.electronics.id, parents)
|
|
self.assertIn(self.ic.id, parents)
|
|
self.assertNotIn(self.fasteners.id, parents)
|
|
|
|
def test_path_string(self):
|
|
"""Test that the category path string works correctly."""
|
|
|
|
# Note that due to data migrations, these fields need to be saved first
|
|
self.resistors.save()
|
|
self.transceivers.save()
|
|
|
|
self.assertEqual(str(self.resistors), 'Electronics/Resistors - Resistors')
|
|
self.assertEqual(str(self.transceivers.pathstring), 'Electronics/IC/Transceivers')
|
|
|
|
# Create a new subcategory
|
|
subcat = PartCategory.objects.create(
|
|
name='Subcategory',
|
|
description='My little sub category',
|
|
parent=self.transceivers
|
|
)
|
|
|
|
# Pathstring should have been updated correctly
|
|
self.assertEqual(subcat.pathstring, 'Electronics/IC/Transceivers/Subcategory')
|
|
self.assertEqual(len(subcat.path), 4)
|
|
|
|
# Move to a new parent location
|
|
subcat.parent = self.resistors
|
|
subcat.save()
|
|
self.assertEqual(subcat.pathstring, 'Electronics/Resistors/Subcategory')
|
|
self.assertEqual(len(subcat.path), 3)
|
|
|
|
# Move to top-level
|
|
subcat.parent = None
|
|
subcat.save()
|
|
self.assertEqual(subcat.pathstring, 'Subcategory')
|
|
self.assertEqual(len(subcat.path), 1)
|
|
|
|
# Construct a very long pathstring and ensure it gets updated correctly
|
|
cat = PartCategory.objects.create(
|
|
name='Cat',
|
|
description='A long running category',
|
|
parent=None
|
|
)
|
|
|
|
parent = cat
|
|
|
|
for idx in range(26):
|
|
letter = chr(ord('A') + idx)
|
|
|
|
child = PartCategory.objects.create(
|
|
name=letter * 10,
|
|
description=f"Subcategory {letter}",
|
|
parent=parent
|
|
)
|
|
|
|
parent = child
|
|
|
|
self.assertTrue(len(child.path), 26)
|
|
self.assertEqual(
|
|
child.pathstring,
|
|
"Cat/AAAAAAAAAA/BBBBBBBBBB/CCCCCCCCCC/DDDDDDDDDD/EEEEEEEEEE/FFFFFFFFFF/GGGGGGGGGG/HHHHHHHHHH/IIIIIIIIII/JJJJJJJJJJ/KKKKKKKKK...OO/PPPPPPPPPP/QQQQQQQQQQ/RRRRRRRRRR/SSSSSSSSSS/TTTTTTTTTT/UUUUUUUUUU/VVVVVVVVVV/WWWWWWWWWW/XXXXXXXXXX/YYYYYYYYYY/ZZZZZZZZZZ"
|
|
)
|
|
self.assertTrue(len(child.pathstring) <= 250)
|
|
|
|
# Attempt an invalid move
|
|
with self.assertRaises(ValidationError):
|
|
cat.parent = child
|
|
cat.save()
|
|
|
|
def test_url(self):
|
|
"""Test that the PartCategory URL works."""
|
|
self.assertEqual(self.capacitors.get_absolute_url(), '/part/category/3/')
|
|
|
|
def test_part_count(self):
|
|
"""Test that the Category part count works."""
|
|
|
|
self.assertEqual(self.fasteners.partcount(), 2)
|
|
self.assertEqual(self.capacitors.partcount(), 1)
|
|
|
|
self.assertEqual(self.electronics.partcount(), 3)
|
|
|
|
self.assertEqual(self.mechanical.partcount(), 9)
|
|
self.assertEqual(self.mechanical.partcount(active=True), 8)
|
|
self.assertEqual(self.mechanical.partcount(False), 7)
|
|
|
|
self.assertEqual(self.electronics.item_count, self.electronics.partcount())
|
|
|
|
def test_parameters(self):
|
|
"""Test that the Category parameters are correctly fetched."""
|
|
# Check number of SQL queries to iterate other parameters
|
|
with self.assertNumQueries(8):
|
|
# Prefetch: 3 queries (parts, parameters and parameters_template)
|
|
fasteners = self.fasteners.prefetch_parts_parameters()
|
|
# Iterate through all parts and parameters
|
|
for fastener in fasteners:
|
|
self.assertIsInstance(fastener, Part)
|
|
for parameter in fastener.parameters.all():
|
|
self.assertIsInstance(parameter, PartParameter)
|
|
self.assertIsInstance(parameter.template, PartParameterTemplate)
|
|
|
|
# Test number of unique parameters
|
|
self.assertEqual(len(self.fasteners.get_unique_parameters(prefetch=fasteners)), 1)
|
|
# Test number of parameters found for each part
|
|
parts_parameters = self.fasteners.get_parts_parameters(prefetch=fasteners)
|
|
part_infos = ['pk', 'name', 'description']
|
|
for part_parameter in parts_parameters:
|
|
# Remove part informations
|
|
for item in part_infos:
|
|
part_parameter.pop(item)
|
|
self.assertEqual(len(part_parameter), 1)
|
|
|
|
def test_delete(self):
|
|
"""Test that category deletion moves the children properly."""
|
|
# Delete the 'IC' category and 'Transceiver' should move to be under 'Electronics'
|
|
self.assertEqual(self.transceivers.parent, self.ic)
|
|
self.assertEqual(self.ic.parent, self.electronics)
|
|
|
|
self.ic.delete()
|
|
|
|
# Get the data again
|
|
transceivers = PartCategory.objects.get(name='Transceivers')
|
|
self.assertEqual(transceivers.parent, self.electronics)
|
|
|
|
# Now delete the 'fasteners' category - the parts should move to 'mechanical'
|
|
self.fasteners.delete()
|
|
|
|
fasteners = Part.objects.filter(description__contains='screw')
|
|
|
|
for f in fasteners:
|
|
self.assertEqual(f.category, self.mechanical)
|
|
|
|
def test_default_locations(self):
|
|
"""Test traversal for default locations."""
|
|
|
|
self.assertIsNotNone(self.fasteners.default_location)
|
|
self.fasteners.default_location.save()
|
|
self.assertEqual(str(self.fasteners.default_location), 'Office/Drawer_1 - In my desk')
|
|
|
|
# Any part under electronics should default to 'Home'
|
|
r1 = Part.objects.get(name='R_2K2_0805')
|
|
self.assertIsNone(r1.default_location)
|
|
self.assertEqual(r1.get_default_location().name, 'Home')
|
|
|
|
# But one part has a default_location set
|
|
r2 = Part.objects.get(name='R_4K7_0603')
|
|
self.assertEqual(r2.get_default_location().name, 'Bathroom')
|
|
|
|
# And one part should have no default location at all
|
|
w = Part.objects.get(name='Widget')
|
|
self.assertIsNone(w.get_default_location())
|
|
|
|
def test_category_tree(self):
|
|
"""Unit tests for the part category tree structure (MPTT)
|
|
|
|
Ensure that the MPTT structure is rebuilt correctly,
|
|
and the correct ancestor tree is observed.
|
|
"""
|
|
# Clear out any existing parts
|
|
Part.objects.all().delete()
|
|
|
|
# First, create a structured tree of part categories
|
|
A = PartCategory.objects.create(
|
|
name='A',
|
|
description='Top level category',
|
|
)
|
|
|
|
B1 = PartCategory.objects.create(name='B1', parent=A)
|
|
B2 = PartCategory.objects.create(name='B2', parent=A)
|
|
B3 = PartCategory.objects.create(name='B3', parent=A)
|
|
|
|
C11 = PartCategory.objects.create(name='C11', parent=B1)
|
|
C12 = PartCategory.objects.create(name='C12', parent=B1)
|
|
C13 = PartCategory.objects.create(name='C13', parent=B1)
|
|
|
|
C21 = PartCategory.objects.create(name='C21', parent=B2)
|
|
C22 = PartCategory.objects.create(name='C22', parent=B2)
|
|
C23 = PartCategory.objects.create(name='C23', parent=B2)
|
|
|
|
C31 = PartCategory.objects.create(name='C31', parent=B3)
|
|
C32 = PartCategory.objects.create(name='C32', parent=B3)
|
|
C33 = PartCategory.objects.create(name='C33', parent=B3)
|
|
|
|
# Check that the tree_id value is correct
|
|
for cat in [B1, B2, B3, C11, C22, C33]:
|
|
self.assertEqual(cat.tree_id, A.tree_id)
|
|
self.assertEqual(cat.level, cat.parent.level + 1)
|
|
self.assertEqual(cat.get_ancestors().count(), cat.level)
|
|
|
|
# Spot check for C31
|
|
ancestors = C31.get_ancestors(include_self=True)
|
|
|
|
self.assertEqual(ancestors.count(), 3)
|
|
self.assertEqual(ancestors[0], A)
|
|
self.assertEqual(ancestors[1], B3)
|
|
self.assertEqual(ancestors[2], C31)
|
|
|
|
# At this point, we are confident that the tree is correctly structured
|
|
|
|
# Add some parts to category B3
|
|
|
|
for i in range(10):
|
|
Part.objects.create(
|
|
name=f'Part {i}',
|
|
description='A test part',
|
|
category=B3,
|
|
)
|
|
|
|
self.assertEqual(Part.objects.filter(category=B3).count(), 10)
|
|
self.assertEqual(Part.objects.filter(category=A).count(), 0)
|
|
|
|
# Delete category B3
|
|
B3.delete()
|
|
|
|
# Child parts have been moved to category A
|
|
self.assertEqual(Part.objects.filter(category=A).count(), 10)
|
|
|
|
for cat in [C31, C32, C33]:
|
|
# These categories should now be directly under A
|
|
cat.refresh_from_db()
|
|
|
|
self.assertEqual(cat.parent, A)
|
|
self.assertEqual(cat.level, 1)
|
|
self.assertEqual(cat.get_ancestors().count(), 1)
|
|
self.assertEqual(cat.get_ancestors()[0], A)
|
|
|
|
# Now, delete category A
|
|
A.delete()
|
|
|
|
# Parts have now been moved to the top-level category
|
|
self.assertEqual(Part.objects.filter(category=None).count(), 10)
|
|
|
|
for loc in [B1, B2, C31, C32, C33]:
|
|
# These should now all be "top level" categories
|
|
loc.refresh_from_db()
|
|
|
|
self.assertEqual(loc.level, 0)
|
|
self.assertEqual(loc.parent, None)
|
|
|
|
# Check descendants for B1
|
|
descendants = B1.get_descendants()
|
|
self.assertEqual(descendants.count(), 3)
|
|
|
|
for loc in [C11, C12, C13]:
|
|
self.assertTrue(loc in descendants)
|
|
|
|
# Check category C1x, should be B1 -> C1x
|
|
for loc in [C11, C12, C13]:
|
|
loc.refresh_from_db()
|
|
|
|
self.assertEqual(loc.level, 1)
|
|
self.assertEqual(loc.parent, B1)
|
|
ancestors = loc.get_ancestors(include_self=True)
|
|
|
|
self.assertEqual(ancestors.count(), 2)
|
|
self.assertEqual(ancestors[0], B1)
|
|
self.assertEqual(ancestors[1], loc)
|
|
|
|
# Check category C2x, should be B2 -> C2x
|
|
for loc in [C21, C22, C23]:
|
|
loc.refresh_from_db()
|
|
|
|
self.assertEqual(loc.level, 1)
|
|
self.assertEqual(loc.parent, B2)
|
|
ancestors = loc.get_ancestors(include_self=True)
|
|
|
|
self.assertEqual(ancestors.count(), 2)
|
|
self.assertEqual(ancestors[0], B2)
|
|
self.assertEqual(ancestors[1], loc)
|