@@ -79,3 +79,96 @@ def test_reformat_price(self):
7979The function assertEqual flags an error in case the two arguments are not equal. In equal case
8080no error is flagged and the test passes. The test function tests five different
8181input variations. More might be added based on the requirements.
82+
83+ ### Involve the database
84+ Now we test a function that uses InvenTree database objects. The function checks if a part
85+ should be updated with latest data from a supplier. Parts that are not purchasable or inactive
86+ should not be updated. The function in the plugin has the following form:
87+
88+ ```
89+ class MySupplier():
90+
91+ def should_be_updated(self, my_part):
92+
93+ ...
94+ return True/False
95+ ```
96+
97+ To test this function, parts are needed in the database. The test framework creates
98+ a dummy database for each run which is empty. Parts for testing need to be added.
99+ This is done in the test function which looks like:
100+
101+ ```
102+ from part.models import Part, PartCategory
103+
104+
105+ def test_should_be_updated(self):
106+ test_cat = PartCategory.objects.create(name='test_cat')
107+ active_part = Part.objects.create(
108+ name='Part1',
109+ IPN='IPN1',
110+ category=test_cat,
111+ active=True,
112+ purchaseable=True,
113+ component=True,
114+ virtual=False)
115+ inactive_part = Part.objects.create(
116+ name='Part2',
117+ IPN='IPN2',
118+ category=test_cat,
119+ active=False,
120+ purchaseable=True,
121+ component=True,
122+ virtual=False)
123+ non_purchasable_part = Part.objects.create(
124+ name='Part3',
125+ IPN='IPN3',
126+ category=test_cat,
127+ active=True,
128+ purchaseable=False,
129+ component=True,
130+ virtual=False)
131+
132+ self.assertEqual(MySupplier.should_be_updated(self, active_part, True, 'Active part')
133+ self.assertEqual(MySupplier.should_be_updated(self, inactive_part, False, 'Inactive part')
134+ self.assertEqual(MySupplier.should_be_updated(self, non_purchasable_part, False, 'Non purchasable part')
135+ ```
136+
137+ A category and three parts are created. One part is active, one is inactive and one is not
138+ purchasable. The function should_be_updated is tested with all
139+ three parts. The first test should return True, the others False. A message was added to the assert
140+ function for better clarity of test results.
141+
142+ The dummy database is completely separate from the one that you might use for development
143+ and it is deleted after the test. There is no danger for your development database.
144+
145+ In case everything is OK, the result looks like:
146+
147+ ```
148+ ----------------------------------------------------------------------
149+ Ran 1 tests in 0.809s
150+
151+ OK
152+ Destroying test database for alias 'default'...
153+ ```
154+
155+ In case of a problem you will see something like:
156+
157+ ```
158+ ======================================================================
159+ FAIL: test_should_be_updated (inventree_supplier_sync.test_supplier_sync.TestSyncPlugin)
160+ ----------------------------------------------------------------------
161+ Traceback (most recent call last):
162+ File "/home/michael/.local/lib/python3.10/site-packages/inventree_supplier_sync/test_supplier_sync.py", line 73, in test_should_be_updated
163+ self.assertEqual(SupplierSyncPlugin.should_be_updated(self, non_purchasable_part,), False, 'Non purchasable part')
164+ AssertionError: True != False : Non purchasable part
165+
166+ ----------------------------------------------------------------------
167+ Ran 3 tests in 0.679s
168+
169+ FAILED (failures=1)
170+ Destroying test database for alias 'default'...
171+
172+ ```
173+
174+ In the AssertionError the message appears that was added to the assertEqual function.
0 commit comments