mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-17 12:35:46 +00:00
Allow searching of BOM List API endpoint (#3296)
* Allow searching of BOM List API endpoint * Bump API version * Adds ordering field options to BOM List API endpoint * Add some unit testing for new API features * Fixes for unit tests
This commit is contained in:
@ -1647,6 +1647,102 @@ class BomItemTest(InvenTreeAPITestCase):
|
||||
for key in ['available_stock', 'available_substitute_stock']:
|
||||
self.assertTrue(key in el)
|
||||
|
||||
def test_bom_list_search(self):
|
||||
"""Test that we can search the BOM list API endpoint"""
|
||||
|
||||
url = reverse('api-bom-list')
|
||||
|
||||
response = self.get(url, expected_code=200)
|
||||
|
||||
self.assertEqual(len(response.data), 6)
|
||||
|
||||
# Limit the results with a search term
|
||||
response = self.get(
|
||||
url,
|
||||
{
|
||||
'search': '0805',
|
||||
},
|
||||
expected_code=200,
|
||||
)
|
||||
|
||||
self.assertEqual(len(response.data), 3)
|
||||
|
||||
# Search by 'reference' field
|
||||
for q in ['ABCDE', 'LMNOP', 'VWXYZ']:
|
||||
response = self.get(
|
||||
url,
|
||||
{
|
||||
'search': q,
|
||||
},
|
||||
expected_code=200
|
||||
)
|
||||
|
||||
self.assertEqual(len(response.data), 1)
|
||||
self.assertEqual(response.data[0]['reference'], q)
|
||||
|
||||
# Search by nonsense data
|
||||
response = self.get(
|
||||
url,
|
||||
{
|
||||
'search': 'xxxxxxxxxxxxxxxxx',
|
||||
},
|
||||
expected_code=200
|
||||
)
|
||||
|
||||
self.assertEqual(len(response.data), 0)
|
||||
|
||||
def test_bom_list_ordering(self):
|
||||
"""Test that the BOM list results can be ordered"""
|
||||
|
||||
url = reverse('api-bom-list')
|
||||
|
||||
# Order by increasing quantity
|
||||
response = self.get(
|
||||
f"{url}?ordering=+quantity",
|
||||
expected_code=200
|
||||
)
|
||||
|
||||
self.assertEqual(len(response.data), 6)
|
||||
|
||||
q1 = response.data[0]['quantity']
|
||||
q2 = response.data[-1]['quantity']
|
||||
|
||||
self.assertTrue(q1 < q2)
|
||||
|
||||
# Order by decreasing quantity
|
||||
response = self.get(
|
||||
f"{url}?ordering=-quantity",
|
||||
expected_code=200,
|
||||
)
|
||||
|
||||
self.assertEqual(q1, response.data[-1]['quantity'])
|
||||
self.assertEqual(q2, response.data[0]['quantity'])
|
||||
|
||||
# Now test ordering by 'sub_part' (which is actually 'sub_part__name')
|
||||
response = self.get(
|
||||
url,
|
||||
{
|
||||
'ordering': 'sub_part',
|
||||
'sub_part_detail': True,
|
||||
},
|
||||
expected_code=200,
|
||||
)
|
||||
|
||||
n1 = response.data[0]['sub_part_detail']['name']
|
||||
n2 = response.data[-1]['sub_part_detail']['name']
|
||||
|
||||
response = self.get(
|
||||
url,
|
||||
{
|
||||
'ordering': '-sub_part',
|
||||
'sub_part_detail': True,
|
||||
},
|
||||
expected_code=200,
|
||||
)
|
||||
|
||||
self.assertEqual(n1, response.data[-1]['sub_part_detail']['name'])
|
||||
self.assertEqual(n2, response.data[0]['sub_part_detail']['name'])
|
||||
|
||||
def test_get_bom_detail(self):
|
||||
"""Get the detail view for a single BomItem object."""
|
||||
url = reverse('api-bom-item-detail', kwargs={'pk': 3})
|
||||
|
Reference in New Issue
Block a user