|
1 | 1 | from django import forms
|
2 | 2 | from django.test import TestCase
|
3 | 3 |
|
| 4 | +from dcim.models import Site |
4 | 5 | from netbox.choices import ImportFormatChoices
|
5 | 6 | from utilities.forms.bulk_import import BulkImportForm
|
6 | 7 | from utilities.forms.forms import BulkRenameForm
|
7 |
| -from utilities.forms.utils import expand_alphanumeric_pattern, expand_ipaddress_pattern |
| 8 | +from utilities.forms.utils import get_field_value, expand_alphanumeric_pattern, expand_ipaddress_pattern |
8 | 9 |
|
9 | 10 |
|
10 | 11 | class ExpandIPAddress(TestCase):
|
@@ -387,3 +388,63 @@ def test_no_strip_whitespace(self):
|
387 | 388 | self.assertTrue(form.is_valid())
|
388 | 389 | self.assertEqual(form.cleaned_data["find"], " hello ")
|
389 | 390 | self.assertEqual(form.cleaned_data["replace"], " world ")
|
| 391 | + |
| 392 | + |
| 393 | +class GetFieldValueTest(TestCase): |
| 394 | + |
| 395 | + @classmethod |
| 396 | + def setUpTestData(cls): |
| 397 | + class TestForm(forms.Form): |
| 398 | + site = forms.ModelChoiceField( |
| 399 | + queryset=Site.objects.all(), |
| 400 | + required=False |
| 401 | + ) |
| 402 | + cls.form_class = TestForm |
| 403 | + |
| 404 | + cls.sites = ( |
| 405 | + Site(name='Test Site 1', slug='test-site-1'), |
| 406 | + Site(name='Test Site 2', slug='test-site-2'), |
| 407 | + ) |
| 408 | + Site.objects.bulk_create(cls.sites) |
| 409 | + |
| 410 | + def test_unbound_without_initial(self): |
| 411 | + form = self.form_class() |
| 412 | + self.assertEqual( |
| 413 | + get_field_value(form, 'site'), |
| 414 | + None |
| 415 | + ) |
| 416 | + |
| 417 | + def test_unbound_with_initial(self): |
| 418 | + form = self.form_class(initial={'site': self.sites[0].pk}) |
| 419 | + self.assertEqual( |
| 420 | + get_field_value(form, 'site'), |
| 421 | + self.sites[0].pk |
| 422 | + ) |
| 423 | + |
| 424 | + def test_bound_value_without_initial(self): |
| 425 | + form = self.form_class({'site': self.sites[0].pk}) |
| 426 | + self.assertEqual( |
| 427 | + get_field_value(form, 'site'), |
| 428 | + self.sites[0].pk |
| 429 | + ) |
| 430 | + |
| 431 | + def test_bound_value_with_initial(self): |
| 432 | + form = self.form_class({'site': self.sites[0].pk}, initial={'site': self.sites[1].pk}) |
| 433 | + self.assertEqual( |
| 434 | + get_field_value(form, 'site'), |
| 435 | + self.sites[0].pk |
| 436 | + ) |
| 437 | + |
| 438 | + def test_bound_null_without_initial(self): |
| 439 | + form = self.form_class({'site': None}) |
| 440 | + self.assertEqual( |
| 441 | + get_field_value(form, 'site'), |
| 442 | + None |
| 443 | + ) |
| 444 | + |
| 445 | + def test_bound_null_with_initial(self): |
| 446 | + form = self.form_class({'site': None}, initial={'site': self.sites[1].pk}) |
| 447 | + self.assertEqual( |
| 448 | + get_field_value(form, 'site'), |
| 449 | + None |
| 450 | + ) |
0 commit comments