Add fixtures to the part test

- Make development of further tests much easier
This commit is contained in:
Oliver Walters 2019-05-04 22:58:55 +10:00
parent d6285643b6
commit 6d803d48b5
3 changed files with 161 additions and 43 deletions

View File

@ -0,0 +1,56 @@
# Create some PartCategory objects
- model: part.partcategory
pk: 1
fields:
name: Electronics
description: Electronic components
parent: null
default_location: null
- model: part.partcategory
pk: 2
fields:
name: Resistors
description: Resistors
parent: 1
default_location: null
- model: part.partcategory
pk: 3
fields:
name: Capacitors
description: Capacitors
parent: 1
default_location: null
- model: part.partcategory
pk: 4
fields:
name: IC
description: Integrated Circuits
parent: 1
default_location: null
- model: part.partcategory
pk: 5
fields:
name: MCU
description: Microcontrollers
parent: 4
default_location: null
- model: part.partcategory
pk: 6
fields:
name: Transceivers
description: Communication interfaces
parent: 4
default_location: null
- model: part.partcategory
pk: 7
fields:
name: Mechanical
description: Mechanical componenets
default_location: null
- model: part.partcategory
pk: 8
fields:
name: Fasteners
description: Screws, bolts, etc
parent: 7

View File

@ -0,0 +1,32 @@
# Create some fasteners
- model: part.part
fields:
name: 'M2x4 LPHS'
description: 'M2x4 low profile head screw'
category: 8
- model: part.part
fields:
name: 'M3x12 SHCS'
description: 'M3x12 socket head cap screw'
category: 8
# Create some resistors
- model: part.part
fields:
name: 'R_2K2_0805'
description: '2.2kOhm resistor in 0805 package'
category: 2
- model: part.part
fields:
name: 'R_4K7_0603'
description: '4.7kOhm resistor in 0603 package'
category: 2
# Create some capacitors
- model: part.part
fields:
name: 'C_22N_0805'
description: '22nF capacitor in 0805 package'
category: 3

View File

@ -6,71 +6,101 @@ from .models import Part, PartCategory
class CategoryTest(TestCase): class CategoryTest(TestCase):
""" """
Tests to ensure that the relational category tree functions correctly. Tests to ensure that the relational category tree functions correctly.
Loads the following test fixtures:
- category.yaml
""" """
fixtures = [
'category',
'part',
]
def setUp(self): def setUp(self):
self.p1 = PartCategory.objects.create(name='A', # Extract some interesting categories for time-saving
description='Most highest level', self.electronics = PartCategory.objects.get(name='Electronics')
parent=None) self.mechanical = PartCategory.objects.get(name='Mechanical')
self.resistors = PartCategory.objects.get(name='Resistors')
self.p2 = PartCategory.objects.create(name='B', self.capacitors = PartCategory.objects.get(name='Capacitors')
description='Sits under second', self.fasteners = PartCategory.objects.get(name='Fasteners')
parent=self.p1) self.ic = PartCategory.objects.get(name='IC')
self.transceivers = PartCategory.objects.get(name='Transceivers')
self.p3 = PartCategory.objects.create(name='C',
description='Third tier category',
parent=self.p2)
# Add two parts in p2
Part.objects.create(name='Flange', category=self.p2)
Part.objects.create(name='Flob', category=self.p2)
# Add one part in p3
Part.objects.create(name='Blob', category=self.p3)
def test_parents(self): def test_parents(self):
self.assertEqual(self.p1.parent, None) """ Test that the parent fields are properly set,
self.assertEqual(self.p2.parent, self.p1) based on the test fixtures """
self.assertEqual(self.p3.parent, self.p2)
self.assertEqual(self.resistors.parent, self.electronics)
self.assertEqual(self.capacitors.parent, self.electronics)
self.assertEqual(self.electronics.parent, None)
self.assertEqual(self.fasteners.parent, self.mechanical)
def test_children_count(self): def test_children_count(self):
self.assertEqual(self.p1.has_children, True) """ Test that categories have the correct number of children """
self.assertEqual(self.p2.has_children, True)
self.assertEqual(self.p3.has_children, False) self.assertTrue(self.electronics.has_children)
self.assertTrue(self.mechanical.has_children)
self.assertEqual(len(self.electronics.children.all()), 3)
self.assertEqual(len(self.mechanical.children.all()), 1)
def test_unique_childs(self): def test_unique_childs(self):
childs = self.p1.getUniqueChildren() """ Test the 'unique_children' functionality """
self.assertIn(self.p2.id, childs) childs = self.electronics.getUniqueChildren()
self.assertIn(self.p3.id, childs)
self.assertIn(self.transceivers.id, childs)
self.assertIn(self.ic.id, childs)
self.assertNotIn(self.fasteners.id, childs)
def test_unique_parents(self): def test_unique_parents(self):
parents = self.p2.getUniqueParents() """ Test the 'unique_parents' functionality """
self.assertIn(self.p1.id, parents) parents = self.transceivers.getUniqueParents()
self.assertIn(self.electronics.id, parents)
self.assertIn(self.ic.id, parents)
self.assertNotIn(self.fasteners.id, parents)
def test_path_string(self): def test_path_string(self):
self.assertEqual(str(self.p3), 'A/B/C') """ Test that the category path string works correctly """
self.assertEqual(str(self.resistors), 'Electronics/Resistors')
self.assertEqual(str(self.transceivers), 'Electronics/IC/Transceivers')
def test_url(self): def test_url(self):
self.assertEqual(self.p1.get_absolute_url(), '/part/category/1/') """ Test that the PartCategory URL works """
self.assertEqual(self.capacitors.get_absolute_url(), '/part/category/3/')
def test_part_count(self): def test_part_count(self):
# No direct parts in the top-level category """ Test that the Category part count works """
self.assertEqual(self.p1.has_parts, False)
self.assertEqual(self.p2.has_parts, True)
self.assertEqual(self.p3.has_parts, True)
self.assertEqual(self.p1.partcount, 3) self.assertTrue(self.resistors.has_parts)
self.assertEqual(self.p2.partcount, 3) self.assertTrue(self.fasteners.has_parts)
self.assertEqual(self.p3.partcount, 1) self.assertFalse(self.transceivers.has_parts)
self.assertEqual(self.fasteners.partcount, 2)
self.assertEqual(self.capacitors.partcount, 1)
def test_delete(self): def test_delete(self):
self.assertEqual(Part.objects.filter(category=self.p1).count(), 0) """ Test that category deletion moves the children properly """
# Delete p2 (it has 2 direct parts and one child category) # Delete the 'IC' category and 'Transceiver' should move to be under 'Electronics'
self.p2.delete() self.assertEqual(self.transceivers.parent, self.ic)
self.assertEqual(self.ic.parent, self.electronics)
self.assertEqual(Part.objects.filter(category=self.p1).count(), 2) self.ic.delete()
self.assertEqual(PartCategory.objects.get(pk=self.p3.id).parent, self.p1) # Get the data again
transceivers = PartCategory.objects.get(name='Transceivers')
self.assertEqual(transceivers.parent, self.electronics)
# Now delete the 'fasteners' category - the parts should move to 'mechanical'
self.fasteners.delete()
fasteners = Part.objects.filter(description__contains='screw')
for f in fasteners:
self.assertEqual(f.category, self.mechanical)