Add form to add multiple things to a box
All checks were successful
Build containers when image tags change / build-if-image-changed (., web, containers, main container, git.baumann.gr/adebaumann/labhelper) (push) Successful in 32s
Build containers when image tags change / build-if-image-changed (data-loader, loader, initContainers, init-container, git.baumann.gr/adebaumann/labhelper-data-loader) (push) Successful in 8s

This commit is contained in:
2025-12-28 22:50:37 +01:00
parent f30627196e
commit f6a953158d
7 changed files with 400 additions and 3 deletions

View File

@@ -663,3 +663,126 @@ class SearchApiTests(TestCase):
results = response.json()['results']
self.assertEqual(results[0]['type'], 'Electronics')
self.assertEqual(results[0]['box'], 'BOX001')
class AddThingsViewTests(TestCase):
"""Tests for add things view."""
def setUp(self):
"""Set up test fixtures."""
self.client = Client()
self.box_type = BoxType.objects.create(
name='Standard Box',
width=200,
height=100,
length=300
)
self.box = Box.objects.create(
id='BOX001',
box_type=self.box_type
)
self.thing_type = ThingType.objects.create(name='Electronics')
def test_add_things_get_request(self):
"""Add things page should return 200 for GET request."""
response = self.client.get(f'/box/{self.box.id}/add/')
self.assertEqual(response.status_code, 200)
def test_add_things_shows_box_id(self):
"""Add things page should show box ID."""
response = self.client.get(f'/box/{self.box.id}/add/')
self.assertContains(response, 'BOX001')
self.assertContains(response, 'Standard Box')
def test_add_things_shows_form(self):
"""Add things page should show form."""
response = self.client.get(f'/box/{self.box.id}/add/')
self.assertContains(response, 'Save Things')
self.assertContains(response, 'Name')
self.assertContains(response, 'Type')
self.assertContains(response, 'Description')
self.assertContains(response, 'Picture')
def test_add_things_post_valid(self):
"""Adding valid things should redirect to box detail."""
response = self.client.post(f'/box/{self.box.id}/add/', {
'form-TOTAL_FORMS': '3',
'form-0-name': 'Arduino Uno',
'form-0-thing_type': self.thing_type.id,
'form-0-description': 'A microcontroller',
'form-1-name': 'LED Strip',
'form-1-thing_type': self.thing_type.id,
'form-1-description': 'Lighting component',
})
self.assertRedirects(response, f'/box/{self.box.id}/')
self.assertEqual(Thing.objects.count(), 2)
def test_add_things_post_required_name(self):
"""Adding things without name should show error."""
response = self.client.post(f'/box/{self.box.id}/add/', {
'form-TOTAL_FORMS': '2',
'form-0-thing_type': self.thing_type.id,
'form-1-thing_type': self.thing_type.id,
})
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'This field is required')
def test_add_things_post_partial_valid_invalid(self):
"""Partial submission: one valid, one missing name."""
response = self.client.post(f'/box/{self.box.id}/add/', {
'form-TOTAL_FORMS': '2',
'form-0-name': 'Arduino Uno',
'form-0-thing_type': self.thing_type.id,
'form-1-thing_type': self.thing_type.id,
})
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'This field is required')
self.assertEqual(Thing.objects.count(), 1)
def test_add_things_creates_thing_types(self):
"""Can create new thing types while adding things."""
new_type = ThingType.objects.create(name='Components')
response = self.client.post(f'/box/{self.box.id}/add/', {
'form-TOTAL_FORMS': '2',
'form-0-name': 'Resistor',
'form-0-thing_type': new_type.id,
'form-1-name': 'Capacitor',
'form-1-thing_type': new_type.id,
})
self.assertRedirects(response, f'/box/{self.box.id}/')
self.assertEqual(Thing.objects.count(), 2)
self.assertEqual(Thing.objects.filter(thing_type=new_type).count(), 2)
def test_add_things_empty_all(self):
"""Submitting empty forms should not create anything."""
response = self.client.post(f'/box/{self.box.id}/add/', {
'form-TOTAL_FORMS': '2',
'form-0-name': '',
'form-0-thing_type': self.thing_type.id,
'form-1-name': '',
'form-1-thing_type': self.thing_type.id,
})
self.assertRedirects(response, f'/box/{self.box.id}/')
self.assertEqual(Thing.objects.count(), 0)
def test_add_things_box_not_exists(self):
"""Adding things to non-existent box should return 404."""
response = self.client.get('/box/INVALID/add/')
self.assertEqual(response.status_code, 404)
def test_add_things_populates_box(self):
"""Created things should be assigned to the correct box."""
self.thing_type_2 = ThingType.objects.create(name='Mechanical')
response = self.client.post(f'/box/{self.box.id}/add/', {
'form-TOTAL_FORMS': '3',
'form-0-name': 'Bolt',
'form-0-thing_type': self.thing_type_2.id,
'form-1-name': 'Nut',
'form-1-thing_type': self.thing_type_2.id,
'form-2-name': 'Washer',
'form-2-thing_type': self.thing_type_2.id,
})
self.assertRedirects(response, f'/box/{self.box.id}/')
things = Thing.objects.all()
for thing in things:
self.assertEqual(thing.box, self.box)