InvenTree/InvenTree/part/test_bom_item.py
Matthias Mair 0c97a50e47
Docstring checks in QC checks (#3089)
* Add pre-commit to the stack

* exclude static

* Add locales to excludes

* fix style errors

* rename pipeline steps

* also wait on precommit

* make template matching simpler

* Use the same code for python setup everywhere

* use step and cache for python setup

* move regular settings up into general envs

* just use full update

* Use invoke instead of static references

* make setup actions more similar

* use python3

* refactor names to be similar

* fix runner version

* fix references

* remove incidential change

* use matrix for os

* Github can't do this right now

* ignore docstyle errors

* Add seperate docstring test

* update flake call

* do not fail on docstring

* refactor setup into workflow

* update reference

* switch to action

* resturcture

* add bash statements

* remove os from cache

* update input checks

* make code cleaner

* fix boolean

* no relative paths

* install wheel by python

* switch to install

* revert back to simple wheel

* refactor import export tests

* move setup keys back to not disturbe tests

* remove docstyle till that is fixed

* update references

* continue on error

* add docstring test

* use relativ action references

* Change step / job docstrings

* update to merge

* reformat comments 1

* fix docstrings 2

* fix docstrings 3

* fix docstrings 4

* fix docstrings 5

* fix docstrings 6

* fix docstrings 7

* fix docstrings 8

* fix docstirns 9

* fix docstrings 10

* docstring adjustments

* update the remaining docstrings

* small docstring changes

* fix function name

* update support files for docstrings

* Add missing args to docstrings

* Remove outdated function

* Add docstrings for the 'build' app

* Make API code cleaner

* add more docstrings for plugin app

* Remove dead code for plugin settings
No idea what that was even intended for

* ignore __init__ files for docstrings

* More docstrings

* Update docstrings for the 'part' directory

* Fixes for related_part functionality

* Fix removed stuff from merge 99676ee

* make more consistent

* Show statistics for docstrings

* add more docstrings

* move specific register statements to make them clearer to understant

* More docstrings for common

* and more docstrings

* and more

* simpler call

* docstrings for notifications

* docstrings for common/tests

* Add docs for common/models

* Revert "move specific register statements to make them clearer to understant"

This reverts commit ca96654622.

* use typing here

* Revert "Make API code cleaner"

This reverts commit 24fb68bd3e.

* docstring updates for the 'users' app

* Add generic Meta info to simple Meta classes

* remove unneeded unique_together statements

* More simple metas

* Remove unnecessary format specifier

* Remove extra json format specifiers

* Add docstrings for the 'plugin' app

* Docstrings for the 'label' app

* Add missing docstrings for the 'report' app

* Fix build test regression

* Fix top-level files

* docstrings for InvenTree/InvenTree

* reduce unneeded code

* add docstrings

* and more docstrings

* more docstrings

* more docstrings for stock

* more docstrings

* docstrings for order/views

* Docstrings for various files in the 'order' app

* Docstrings for order/test_api.py

* Docstrings for order/serializers.py

* Docstrings for order/admin.py

* More docstrings for the order app

* Add docstrings for the 'company' app

* Add unit tests for rebuilding the reference fields

* Prune out some more dead code

* remove more dead code

Co-authored-by: Oliver Walters <oliver.henry.walters@gmail.com>
2022-06-02 01:37:39 +10:00

200 lines
6.1 KiB
Python

"""Unit tests for the BomItem model"""
from decimal import Decimal
import django.core.exceptions as django_exceptions
from django.db import transaction
from django.test import TestCase
from .models import BomItem, BomItemSubstitute, Part
class BomItemTest(TestCase):
"""Class for unit testing BomItem model"""
fixtures = [
'category',
'part',
'location',
'bom',
'company',
'supplier_part',
'part_pricebreaks',
'price_breaks',
]
def setUp(self):
"""Create initial data"""
self.bob = Part.objects.get(id=100)
self.orphan = Part.objects.get(name='Orphan')
self.r1 = Part.objects.get(name='R_2K2_0805')
def test_str(self):
"""Test the string representation of a BOMItem"""
b = BomItem.objects.get(id=1)
self.assertEqual(str(b), '10 x M2x4 LPHS to make BOB | Bob | A2')
def test_has_bom(self):
"""Test the has_bom attribute"""
self.assertFalse(self.orphan.has_bom)
self.assertTrue(self.bob.has_bom)
self.assertEqual(self.bob.bom_count, 4)
def test_in_bom(self):
"""Test BOM aggregation"""
parts = self.bob.getRequiredParts()
self.assertIn(self.orphan, parts)
self.assertTrue(self.bob.check_if_part_in_bom(self.orphan))
def test_used_in(self):
"""Test that the 'used_in_count' attribute is calculated correctly"""
self.assertEqual(self.bob.used_in_count, 1)
self.assertEqual(self.orphan.used_in_count, 1)
def test_self_reference(self):
"""Test that we get an appropriate error when we create a BomItem which points to itself."""
with self.assertRaises(django_exceptions.ValidationError):
# A validation error should be raised here
item = BomItem.objects.create(part=self.bob, sub_part=self.bob, quantity=7)
item.clean() # pragma: no cover
def test_integer_quantity(self):
"""Test integer validation for BomItem."""
p = Part.objects.create(name="test", description="d", component=True, trackable=True)
# Creation of a BOMItem with a non-integer quantity of a trackable Part should fail
with self.assertRaises(django_exceptions.ValidationError):
BomItem.objects.create(part=self.bob, sub_part=p, quantity=21.7)
# But with an integer quantity, should be fine
BomItem.objects.create(part=self.bob, sub_part=p, quantity=21)
def test_overage(self):
"""Test that BOM line overages are calculated correctly."""
item = BomItem.objects.get(part=100, sub_part=50)
q = 300
item.quantity = q
# Test empty overage
n = item.get_overage_quantity(q)
self.assertEqual(n, 0)
# Test improper overage
item.overage = 'asf234?'
n = item.get_overage_quantity(q)
self.assertEqual(n, 0)
# Test absolute overage
item.overage = '3'
n = item.get_overage_quantity(q)
self.assertEqual(n, 3)
# Test percentage-based overage
item.overage = '5.0 % '
n = item.get_overage_quantity(q)
self.assertEqual(n, 15)
# Calculate total required quantity
# Quantity = 300 (+ 5%)
# Get quantity required to build B = 10
# Q * B = 3000 + 5% = 3150
n = item.get_required_quantity(10)
self.assertEqual(n, 3150)
def test_item_hash(self):
"""Test BOM item hash encoding."""
item = BomItem.objects.get(part=100, sub_part=50)
h1 = item.get_item_hash()
# Change data - the hash must change
item.quantity += 1
h2 = item.get_item_hash()
item.validate_hash()
self.assertNotEqual(h1, h2)
def test_pricing(self):
"""Test BOM pricing"""
self.bob.get_price(1)
self.assertEqual(
self.bob.get_bom_price_range(1, internal=True),
(Decimal(29.5), Decimal(89.5))
)
# remove internal price for R_2K2_0805
self.r1.internal_price_breaks.delete()
self.assertEqual(
self.bob.get_bom_price_range(1, internal=True),
(Decimal(27.5), Decimal(87.5))
)
def test_substitutes(self):
"""Tests for BOM item substitutes."""
# We will make some subtitute parts for the "orphan" part
bom_item = BomItem.objects.get(
part=self.bob,
sub_part=self.orphan
)
# No substitute parts available
self.assertEqual(bom_item.substitutes.count(), 0)
subs = []
for ii in range(5):
# Create a new part
sub_part = Part.objects.create(
name=f"Orphan {ii}",
description="A substitute part for the orphan part",
component=True,
is_template=False,
assembly=False,
)
subs.append(sub_part)
# Link it as a substitute part
BomItemSubstitute.objects.create(
bom_item=bom_item,
part=sub_part
)
# Try to link it again (this should fail as it is a duplicate substitute)
with self.assertRaises(django_exceptions.ValidationError):
with transaction.atomic():
BomItemSubstitute.objects.create(
bom_item=bom_item,
part=sub_part
)
# There should be now 5 substitute parts available
self.assertEqual(bom_item.substitutes.count(), 5)
# Try to create a substitute which points to the same sub-part (should fail)
with self.assertRaises(django_exceptions.ValidationError):
BomItemSubstitute.objects.create(
bom_item=bom_item,
part=self.orphan,
)
# Remove one substitute part
bom_item.substitutes.last().delete()
self.assertEqual(bom_item.substitutes.count(), 4)
for sub in subs:
sub.delete()
# The substitution links should have been automatically removed
self.assertEqual(bom_item.substitutes.count(), 0)