mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Part API test suite now uses test data fixtures
This commit is contained in:
parent
e2ced18753
commit
fcc83ca7c7
@ -7,6 +7,7 @@
|
|||||||
description: Electronic components
|
description: Electronic components
|
||||||
parent: null
|
parent: null
|
||||||
default_location: 1 # Home
|
default_location: 1 # Home
|
||||||
|
|
||||||
- model: part.partcategory
|
- model: part.partcategory
|
||||||
pk: 2
|
pk: 2
|
||||||
fields:
|
fields:
|
||||||
@ -14,6 +15,7 @@
|
|||||||
description: Resistors
|
description: Resistors
|
||||||
parent: 1
|
parent: 1
|
||||||
default_location: null
|
default_location: null
|
||||||
|
|
||||||
- model: part.partcategory
|
- model: part.partcategory
|
||||||
pk: 3
|
pk: 3
|
||||||
fields:
|
fields:
|
||||||
@ -21,6 +23,7 @@
|
|||||||
description: Capacitors
|
description: Capacitors
|
||||||
parent: 1
|
parent: 1
|
||||||
default_location: null
|
default_location: null
|
||||||
|
|
||||||
- model: part.partcategory
|
- model: part.partcategory
|
||||||
pk: 4
|
pk: 4
|
||||||
fields:
|
fields:
|
||||||
@ -28,6 +31,7 @@
|
|||||||
description: Integrated Circuits
|
description: Integrated Circuits
|
||||||
parent: 1
|
parent: 1
|
||||||
default_location: null
|
default_location: null
|
||||||
|
|
||||||
- model: part.partcategory
|
- model: part.partcategory
|
||||||
pk: 5
|
pk: 5
|
||||||
fields:
|
fields:
|
||||||
@ -35,6 +39,7 @@
|
|||||||
description: Microcontrollers
|
description: Microcontrollers
|
||||||
parent: 4
|
parent: 4
|
||||||
default_location: null
|
default_location: null
|
||||||
|
|
||||||
- model: part.partcategory
|
- model: part.partcategory
|
||||||
pk: 6
|
pk: 6
|
||||||
fields:
|
fields:
|
||||||
@ -42,12 +47,14 @@
|
|||||||
description: Communication interfaces
|
description: Communication interfaces
|
||||||
parent: 4
|
parent: 4
|
||||||
default_location: null
|
default_location: null
|
||||||
|
|
||||||
- model: part.partcategory
|
- model: part.partcategory
|
||||||
pk: 7
|
pk: 7
|
||||||
fields:
|
fields:
|
||||||
name: Mechanical
|
name: Mechanical
|
||||||
description: Mechanical componenets
|
description: Mechanical componenets
|
||||||
default_location: null
|
default_location: null
|
||||||
|
|
||||||
- model: part.partcategory
|
- model: part.partcategory
|
||||||
pk: 8
|
pk: 8
|
||||||
fields:
|
fields:
|
||||||
|
@ -3,9 +3,6 @@ from rest_framework import status
|
|||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
|
|
||||||
from .models import Part, PartCategory
|
|
||||||
from .models import BomItem
|
|
||||||
|
|
||||||
|
|
||||||
class PartAPITest(APITestCase):
|
class PartAPITest(APITestCase):
|
||||||
"""
|
"""
|
||||||
@ -14,36 +11,29 @@ class PartAPITest(APITestCase):
|
|||||||
- Tests for PartCategory API
|
- Tests for PartCategory API
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
fixtures = [
|
||||||
|
'category',
|
||||||
|
'part',
|
||||||
|
'location',
|
||||||
|
'bom',
|
||||||
|
]
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
# Create a user for auth
|
# Create a user for auth
|
||||||
User = get_user_model()
|
User = get_user_model()
|
||||||
User.objects.create_user('testuser', 'test@testing.com', 'password')
|
User.objects.create_user('testuser', 'test@testing.com', 'password')
|
||||||
|
|
||||||
self.client.login(username='testuser', password='password')
|
self.client.login(username='testuser', password='password')
|
||||||
|
|
||||||
# Create some test data
|
|
||||||
TOP = PartCategory.objects.create(name='Top', description='Top level category')
|
|
||||||
|
|
||||||
A = PartCategory.objects.create(name='A', description='Cat A', parent=TOP)
|
|
||||||
B = PartCategory.objects.create(name='B', description='Cat B', parent=TOP)
|
|
||||||
C = PartCategory.objects.create(name='C', description='Cat C', parent=TOP)
|
|
||||||
|
|
||||||
Part.objects.create(name='Top.t', description='t in TOP', category=TOP)
|
|
||||||
|
|
||||||
Part.objects.create(name='A.a', description='a in A', category=A)
|
|
||||||
Part.objects.create(name='B.b', description='b in B', category=B)
|
|
||||||
Part.objects.create(name='C.c1', description='c1 in C', category=C)
|
|
||||||
Part.objects.create(name='C.c2', description='c2 in C', category=C)
|
|
||||||
|
|
||||||
def test_get_categories(self):
|
def test_get_categories(self):
|
||||||
# Test that we can retrieve list of part categories
|
""" Test that we can retrieve list of part categories """
|
||||||
url = reverse('api-part-category-list')
|
url = reverse('api-part-category-list')
|
||||||
response = self.client.get(url, format='json')
|
response = self.client.get(url, format='json')
|
||||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
self.assertEqual(len(response.data), 4)
|
self.assertEqual(len(response.data), 8)
|
||||||
|
|
||||||
def test_add_categories(self):
|
def test_add_categories(self):
|
||||||
# Check that we can add categories
|
""" Check that we can add categories """
|
||||||
data = {
|
data = {
|
||||||
'name': 'Animals',
|
'name': 'Animals',
|
||||||
'description': 'All animals go here'
|
'description': 'All animals go here'
|
||||||
@ -52,31 +42,32 @@ class PartAPITest(APITestCase):
|
|||||||
url = reverse('api-part-category-list')
|
url = reverse('api-part-category-list')
|
||||||
response = self.client.post(url, data, format='json')
|
response = self.client.post(url, data, format='json')
|
||||||
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||||
self.assertEqual(response.data['pk'], 5)
|
|
||||||
|
parent = response.data['pk']
|
||||||
|
|
||||||
# Add some sub-categories to the top-level 'Animals' category
|
# Add some sub-categories to the top-level 'Animals' category
|
||||||
for animal in ['cat', 'dog', 'zebra']:
|
for animal in ['cat', 'dog', 'zebra']:
|
||||||
data = {
|
data = {
|
||||||
'name': animal,
|
'name': animal,
|
||||||
'description': 'A sort of animal',
|
'description': 'A sort of animal',
|
||||||
'parent': 5,
|
'parent': parent,
|
||||||
}
|
}
|
||||||
response = self.client.post(url, data, format='json')
|
response = self.client.post(url, data, format='json')
|
||||||
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||||
self.assertEqual(response.data['parent'], 5)
|
self.assertEqual(response.data['parent'], parent)
|
||||||
self.assertEqual(response.data['name'], animal)
|
self.assertEqual(response.data['name'], animal)
|
||||||
self.assertEqual(response.data['pathstring'], 'Animals/' + animal)
|
self.assertEqual(response.data['pathstring'], 'Animals/' + animal)
|
||||||
|
|
||||||
# There should be now 8 categories
|
# There should be now 8 categories
|
||||||
response = self.client.get(url, format='json')
|
response = self.client.get(url, format='json')
|
||||||
self.assertEqual(len(response.data), 8)
|
self.assertEqual(len(response.data), 12)
|
||||||
|
|
||||||
def test_cat_detail(self):
|
def test_cat_detail(self):
|
||||||
url = reverse('api-part-category-detail', kwargs={'pk': 4})
|
url = reverse('api-part-category-detail', kwargs={'pk': 4})
|
||||||
response = self.client.get(url, format='json')
|
response = self.client.get(url, format='json')
|
||||||
|
|
||||||
# Test that we have retrieved the category
|
# Test that we have retrieved the category
|
||||||
self.assertEqual(response.data['description'], 'Cat C')
|
self.assertEqual(response.data['description'], 'Integrated Circuits')
|
||||||
self.assertEqual(response.data['parent'], 1)
|
self.assertEqual(response.data['parent'], 1)
|
||||||
|
|
||||||
# Change some data and post it back
|
# Change some data and post it back
|
||||||
@ -87,16 +78,17 @@ class PartAPITest(APITestCase):
|
|||||||
response = self.client.patch(url, data, format='json')
|
response = self.client.patch(url, data, format='json')
|
||||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
self.assertEqual(response.data['description'], 'Changing the description')
|
self.assertEqual(response.data['description'], 'Changing the description')
|
||||||
|
self.assertIsNone(response.data['parent'])
|
||||||
|
|
||||||
def test_get_all_parts(self):
|
def test_get_all_parts(self):
|
||||||
url = reverse('api-part-list')
|
url = reverse('api-part-list')
|
||||||
response = self.client.get(url, format='json')
|
response = self.client.get(url, format='json')
|
||||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
self.assertEqual(len(response.data), 5)
|
self.assertEqual(len(response.data), 8)
|
||||||
|
|
||||||
def test_get_parts_by_cat(self):
|
def test_get_parts_by_cat(self):
|
||||||
url = reverse('api-part-list')
|
url = reverse('api-part-list')
|
||||||
data = {'category': 4}
|
data = {'category': 2}
|
||||||
response = self.client.get(url, data, format='json')
|
response = self.client.get(url, data, format='json')
|
||||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
|
|
||||||
@ -104,7 +96,7 @@ class PartAPITest(APITestCase):
|
|||||||
self.assertEqual(len(response.data), 2)
|
self.assertEqual(len(response.data), 2)
|
||||||
|
|
||||||
for part in response.data:
|
for part in response.data:
|
||||||
self.assertEqual(part['category'], 4)
|
self.assertEqual(part['category'], 2)
|
||||||
|
|
||||||
def test_include_children(self):
|
def test_include_children(self):
|
||||||
""" Test the special 'include_child_categories' flag
|
""" Test the special 'include_child_categories' flag
|
||||||
@ -116,7 +108,7 @@ class PartAPITest(APITestCase):
|
|||||||
response = self.client.get(url, data, format='json')
|
response = self.client.get(url, data, format='json')
|
||||||
|
|
||||||
# There should be 1 part in this category
|
# There should be 1 part in this category
|
||||||
self.assertEqual(len(response.data), 1)
|
self.assertEqual(len(response.data), 0)
|
||||||
|
|
||||||
data['include_child_categories'] = 1
|
data['include_child_categories'] = 1
|
||||||
|
|
||||||
@ -125,35 +117,10 @@ class PartAPITest(APITestCase):
|
|||||||
|
|
||||||
# Now there should be 5 total parts
|
# Now there should be 5 total parts
|
||||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
self.assertEqual(len(response.data), 5)
|
self.assertEqual(len(response.data), 3)
|
||||||
|
|
||||||
|
|
||||||
class BomAPITest(APITestCase):
|
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
# Create a user for auth
|
|
||||||
User = get_user_model()
|
|
||||||
User.objects.create_user('testuser', 'test@testing.com', 'password')
|
|
||||||
|
|
||||||
self.client.login(username='testuser', password='password')
|
|
||||||
|
|
||||||
# Create some parts
|
|
||||||
m1 = Part.objects.create(name='A thing', description='Made from other parts', buildable=True)
|
|
||||||
m2 = Part.objects.create(name='Another thing', description='Made from other parts', buildable=True)
|
|
||||||
|
|
||||||
s1 = Part.objects.create(name='Sub 1', description='Required to make a thing')
|
|
||||||
s2 = Part.objects.create(name='Sub 2', description='Required to make a thing')
|
|
||||||
s3 = Part.objects.create(name='Sub 3', description='Required to make a thing')
|
|
||||||
|
|
||||||
# Link BOM items together
|
|
||||||
BomItem.objects.create(part=m1, sub_part=s1, quantity=10)
|
|
||||||
BomItem.objects.create(part=m1, sub_part=s2, quantity=100)
|
|
||||||
BomItem.objects.create(part=m1, sub_part=s3, quantity=40)
|
|
||||||
|
|
||||||
BomItem.objects.create(part=m2, sub_part=s3, quantity=7)
|
|
||||||
|
|
||||||
def test_get_bom_list(self):
|
def test_get_bom_list(self):
|
||||||
# There should be 4 BomItem objects in the database
|
""" There should be 4 BomItem objects in the database """
|
||||||
url = reverse('api-bom-list')
|
url = reverse('api-bom-list')
|
||||||
response = self.client.get(url, format='json')
|
response = self.client.get(url, format='json')
|
||||||
self.assertEqual(len(response.data), 4)
|
self.assertEqual(len(response.data), 4)
|
||||||
@ -163,7 +130,7 @@ class BomAPITest(APITestCase):
|
|||||||
url = reverse('api-bom-detail', kwargs={'pk': 3})
|
url = reverse('api-bom-detail', kwargs={'pk': 3})
|
||||||
response = self.client.get(url, format='json')
|
response = self.client.get(url, format='json')
|
||||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
self.assertEqual(response.data['quantity'], 40)
|
self.assertEqual(response.data['quantity'], 25)
|
||||||
|
|
||||||
# Increase the quantity
|
# Increase the quantity
|
||||||
data = response.data
|
data = response.data
|
||||||
@ -181,7 +148,7 @@ class BomAPITest(APITestCase):
|
|||||||
url = reverse('api-bom-list')
|
url = reverse('api-bom-list')
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
'part': 2,
|
'part': 100,
|
||||||
'sub_part': 4,
|
'sub_part': 4,
|
||||||
'quantity': 777,
|
'quantity': 777,
|
||||||
}
|
}
|
||||||
@ -194,9 +161,7 @@ class BomAPITest(APITestCase):
|
|||||||
response = self.client.post(url, data, format='json')
|
response = self.client.post(url, data, format='json')
|
||||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
# Now try to create a BomItem which references itself
|
# TODO - Now try to create a BomItem which references itself
|
||||||
data['part'] = 2
|
data['part'] = 2
|
||||||
data['sub_part'] = 2
|
data['sub_part'] = 2
|
||||||
response = self.client.post(url, data, format='json')
|
response = self.client.post(url, data, format='json')
|
||||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
|
||||||
self.assertIn('cannot be added to its own', str(response.data['sub_part'][0]))
|
|
||||||
|
Loading…
Reference in New Issue
Block a user