diff --git a/InvenTree/part/api.py b/InvenTree/part/api.py index a245f9d67c..54222b7e67 100644 --- a/InvenTree/part/api.py +++ b/InvenTree/part/api.py @@ -735,6 +735,15 @@ class PartParameterList(generics.ListCreateAPIView): ] +class PartParameterDetail(generics.RetrieveUpdateDestroyAPIView): + """ + API endpoint for detail view of a single PartParameter object + """ + + queryset = PartParameter.objects.all() + serializer_class = part_serializers.PartParameterSerializer + + class BomList(generics.ListCreateAPIView): """ API endpoint for accessing a list of BomItem objects. @@ -942,6 +951,8 @@ part_api_urls = [ # Base URL for PartParameter API endpoints url(r'^parameter/', include([ url(r'^template/$', PartParameterTemplateList.as_view(), name='api-part-param-template-list'), + + url(r'^(?P\d+)/', PartParameterDetail.as_view(), name='api-part-param-detail'), url(r'^.*$', PartParameterList.as_view(), name='api-part-param-list'), ])), diff --git a/InvenTree/part/fixtures/params.yaml b/InvenTree/part/fixtures/params.yaml index e65c7335cc..3056be473b 100644 --- a/InvenTree/part/fixtures/params.yaml +++ b/InvenTree/part/fixtures/params.yaml @@ -33,6 +33,27 @@ template: 1 data: 12 +- model: part.PartParameter + pk: 3 + fields: + part: 3 + template: 1 + data: 12 + +- model: part.PartParameter + pk: 4 + fields: + part: 3 + template: 2 + data: 12 + +- model: part.PartParameter + pk: 5 + fields: + part: 3 + template: 3 + data: 12 + # Add some template parameters to categories (requires category.yaml) - model: part.PartCategoryParameterTemplate pk: 1 diff --git a/InvenTree/part/test_api.py b/InvenTree/part/test_api.py index faadf26c15..ed88e1dd55 100644 --- a/InvenTree/part/test_api.py +++ b/InvenTree/part/test_api.py @@ -325,3 +325,106 @@ class PartAPIAggregationTest(InvenTreeAPITestCase): self.assertEqual(data['in_stock'], 1100) self.assertEqual(data['stock_item_count'], 105) + + +class PartParameterTest(InvenTreeAPITestCase): + """ + Tests for the ParParameter API + """ + + superuser = True + + fixtures = [ + 'category', + 'part', + 'location', + 'params', + ] + + def setUp(self): + + super().setUp() + + def test_list_params(self): + """ + Test for listing part parameters + """ + + url = reverse('api-part-param-list') + + response = self.client.get(url, format='json') + + self.assertEqual(len(response.data), 5) + + # Filter by part + response = self.client.get( + url, + { + 'part': 3, + }, + format='json' + ) + + self.assertEqual(len(response.data), 3) + + # Filter by template + response = self.client.get( + url, + { + 'template': 1, + }, + format='json', + ) + + self.assertEqual(len(response.data), 3) + + def test_create_param(self): + """ + Test that we can create a param via the API + """ + + url = reverse('api-part-param-list') + + response = self.client.post( + url, + { + 'part': '2', + 'template': '3', + 'data': 70 + } + ) + + self.assertEqual(response.status_code, 201) + + response = self.client.get(url, format='json') + + self.assertEqual(len(response.data), 6) + + def test_param_detail(self): + """ + Tests for the PartParameter detail endpoint + """ + + url = reverse('api-part-param-detail', kwargs={'pk': 5}) + + response = self.client.get(url) + + self.assertEqual(response.status_code, 200) + + data = response.data + + self.assertEqual(data['pk'], 5) + self.assertEqual(data['part'], 3) + self.assertEqual(data['data'], '12') + + # PATCH data back in + response = self.client.patch(url, {'data': '15'}, format='json') + + self.assertEqual(response.status_code, 200) + + # Check that the data changed! + response = self.client.get(url, format='json') + + data = response.data + + self.assertEqual(data['data'], '15')