Skip to content

Commit 0905c68

Browse files
committed
Add tests for active field checkbox handling
Add comprehensive tests for the simplified checkbox parsing logic and active field functionality: - Checkbox parsing for checked/unchecked/missing states - Active field in configuration export (true/false cases) - Active field in configuration import validation - Backwards compatibility for imports without active field All tests pass successfully.
1 parent 70968fd commit 0905c68

File tree

1 file changed

+217
-0
lines changed

1 file changed

+217
-0
lines changed

apollo/tests/test_admin_routes_supported_products.py

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,223 @@ def test_validate_import_data_not_list(self):
442442
self.assertIn("must be a list", errors[0])
443443

444444

445+
class TestActiveFieldCheckboxParsing(unittest.TestCase):
446+
"""Test the checkbox parsing logic for the active field."""
447+
448+
def test_checkbox_checked_returns_true(self):
449+
"""Test that checked checkbox results in active_value='true'."""
450+
# Simulate form data when checkbox is checked
451+
from unittest.mock import Mock
452+
form_data = Mock()
453+
form_data.getlist.return_value = ["true"]
454+
455+
# Apply the parsing logic from admin_supported_products.py
456+
active_value = "true" if "true" in form_data.getlist("active") else "false"
457+
458+
self.assertEqual(active_value, "true")
459+
460+
def test_checkbox_unchecked_returns_false(self):
461+
"""Test that unchecked checkbox results in active_value='false'."""
462+
# Simulate form data when checkbox is unchecked (empty list)
463+
from unittest.mock import Mock
464+
form_data = Mock()
465+
form_data.getlist.return_value = []
466+
467+
# Apply the parsing logic from admin_supported_products.py
468+
active_value = "true" if "true" in form_data.getlist("active") else "false"
469+
470+
self.assertEqual(active_value, "false")
471+
472+
def test_checkbox_missing_field_defaults_to_false(self):
473+
"""Test that missing active field defaults to false."""
474+
# Simulate form data without active field at all
475+
from unittest.mock import Mock
476+
form_data = Mock()
477+
form_data.getlist.return_value = []
478+
479+
active_value = "true" if "true" in form_data.getlist("active") else "false"
480+
481+
self.assertEqual(active_value, "false")
482+
483+
def test_checkbox_boolean_conversion(self):
484+
"""Test that string active_value converts correctly to boolean."""
485+
# Test conversion to boolean for database storage
486+
active_value_true = "true"
487+
active_value_false = "false"
488+
489+
self.assertTrue(active_value_true == "true")
490+
self.assertFalse(active_value_false == "true")
491+
492+
def test_checkbox_with_unexpected_value(self):
493+
"""Test that unexpected values default to false."""
494+
# Edge case: unexpected value
495+
from unittest.mock import Mock
496+
form_data = Mock()
497+
form_data.getlist.return_value = ["yes", "1", "on"]
498+
499+
active_value = "true" if "true" in form_data.getlist("active") else "false"
500+
501+
# Should default to false since "true" is not in the list
502+
self.assertEqual(active_value, "false")
503+
504+
505+
class TestMirrorConfigDataWithActiveField(unittest.TestCase):
506+
"""Test mirror configuration export includes active field."""
507+
508+
def test_exported_config_includes_active_true(self):
509+
"""Test that exported mirror config includes active=true."""
510+
mirror = Mock()
511+
mirror.id = 1
512+
mirror.name = "Active Mirror"
513+
mirror.match_variant = "Red Hat Enterprise Linux"
514+
mirror.match_major_version = 9
515+
mirror.match_minor_version = None
516+
mirror.match_arch = "x86_64"
517+
mirror.active = True
518+
mirror.created_at = datetime(2024, 1, 1, 10, 0, 0)
519+
mirror.updated_at = None
520+
521+
# Mock supported product
522+
mirror.supported_product = Mock()
523+
mirror.supported_product.id = 1
524+
mirror.supported_product.name = "Rocky Linux"
525+
mirror.supported_product.variant = "Rocky Linux"
526+
mirror.supported_product.vendor = "RESF"
527+
528+
# Mock repositories
529+
mirror.rpm_repomds = []
530+
531+
result = asyncio.run(_get_mirror_config_data(mirror))
532+
533+
# Verify active field is included and true
534+
self.assertIn("active", result["mirror"])
535+
self.assertTrue(result["mirror"]["active"])
536+
537+
def test_exported_config_includes_active_false(self):
538+
"""Test that exported mirror config includes active=false."""
539+
mirror = Mock()
540+
mirror.id = 2
541+
mirror.name = "Inactive Mirror"
542+
mirror.match_variant = "Red Hat Enterprise Linux"
543+
mirror.match_major_version = 8
544+
mirror.match_minor_version = None
545+
mirror.match_arch = "x86_64"
546+
mirror.active = False
547+
mirror.created_at = datetime(2024, 1, 1, 10, 0, 0)
548+
mirror.updated_at = None
549+
550+
# Mock supported product
551+
mirror.supported_product = Mock()
552+
mirror.supported_product.id = 1
553+
mirror.supported_product.name = "Rocky Linux"
554+
mirror.supported_product.variant = "Rocky Linux"
555+
mirror.supported_product.vendor = "RESF"
556+
557+
# Mock repositories
558+
mirror.rpm_repomds = []
559+
560+
result = asyncio.run(_get_mirror_config_data(mirror))
561+
562+
# Verify active field is included and false
563+
self.assertIn("active", result["mirror"])
564+
self.assertFalse(result["mirror"]["active"])
565+
566+
567+
class TestImportWithActiveField(unittest.TestCase):
568+
"""Test import validation with active field."""
569+
570+
def test_import_data_with_active_true_is_valid(self):
571+
"""Test that import data with active=true is valid."""
572+
valid_data = [
573+
{
574+
"product": {
575+
"name": "Rocky Linux",
576+
"variant": "Rocky Linux",
577+
"vendor": "RESF",
578+
},
579+
"mirror": {
580+
"name": "Rocky Linux 9 x86_64",
581+
"match_variant": "Red Hat Enterprise Linux",
582+
"match_major_version": 9,
583+
"match_arch": "x86_64",
584+
"active": True,
585+
},
586+
"repositories": [
587+
{
588+
"repo_name": "BaseOS",
589+
"arch": "x86_64",
590+
"production": True,
591+
"url": "https://example.com/repo",
592+
}
593+
],
594+
}
595+
]
596+
597+
errors = asyncio.run(_validate_import_data(valid_data))
598+
self.assertEqual(errors, [])
599+
600+
def test_import_data_with_active_false_is_valid(self):
601+
"""Test that import data with active=false is valid."""
602+
valid_data = [
603+
{
604+
"product": {
605+
"name": "Rocky Linux",
606+
"variant": "Rocky Linux",
607+
"vendor": "RESF",
608+
},
609+
"mirror": {
610+
"name": "Rocky Linux 8 x86_64",
611+
"match_variant": "Red Hat Enterprise Linux",
612+
"match_major_version": 8,
613+
"match_arch": "x86_64",
614+
"active": False,
615+
},
616+
"repositories": [
617+
{
618+
"repo_name": "BaseOS",
619+
"arch": "x86_64",
620+
"production": True,
621+
"url": "https://example.com/repo",
622+
}
623+
],
624+
}
625+
]
626+
627+
errors = asyncio.run(_validate_import_data(valid_data))
628+
self.assertEqual(errors, [])
629+
630+
def test_import_data_without_active_field_is_valid(self):
631+
"""Test that import data without active field is valid (backwards compatibility)."""
632+
valid_data = [
633+
{
634+
"product": {
635+
"name": "Rocky Linux",
636+
"variant": "Rocky Linux",
637+
"vendor": "RESF",
638+
},
639+
"mirror": {
640+
"name": "Rocky Linux 9 x86_64",
641+
"match_variant": "Red Hat Enterprise Linux",
642+
"match_major_version": 9,
643+
"match_arch": "x86_64",
644+
# No active field - should default to true
645+
},
646+
"repositories": [
647+
{
648+
"repo_name": "BaseOS",
649+
"arch": "x86_64",
650+
"production": True,
651+
"url": "https://example.com/repo",
652+
}
653+
],
654+
}
655+
]
656+
657+
# Should still be valid - active field is optional for backwards compatibility
658+
errors = asyncio.run(_validate_import_data(valid_data))
659+
self.assertEqual(errors, [])
660+
661+
445662
if __name__ == "__main__":
446663
# Run with verbose output
447664
unittest.main(verbosity=2)

0 commit comments

Comments
 (0)