@@ -594,6 +594,60 @@ async def test_disable_monitors_without_code_modules_no_monitors_to_disable(mock
594594 disable_monitor_spy .assert_not_called ()
595595
596596
597+ async def test_get_monitors_to_load (clear_database ):
598+ """'_get_monitors_to_load' should return only the updated monitors"""
599+ await databases .execute_application (
600+ 'insert into "Monitors"(id, name, enabled) values'
601+ "(9999123, 'monitor_1', true),"
602+ "(9999456, 'internal.monitor_2', true),"
603+ "(9999457, 'disabled_monitor', false);"
604+ )
605+ await databases .execute_application (
606+ 'insert into "CodeModules"(monitor_id, code, registered_at) values'
607+ "(9999123, 'def get_value(): return 10', '2025-01-10 00:00'),"
608+ "(9999456, 'def get_value(): return 11', '2025-01-20 00:00'),"
609+ "(9999457, 'def get_value(): return 12', '2025-01-30 00:00');"
610+ )
611+
612+ monitors , code_modules = await monitors_loader ._get_monitors_to_load (
613+ datetime (2025 , 1 , 15 , tzinfo = timezone .utc )
614+ )
615+
616+ assert len (monitors ) == 2
617+ assert monitors [9999123 ].name == "monitor_1"
618+ assert monitors [9999456 ].name == "internal.monitor_2"
619+ assert len (code_modules ) == 1
620+ assert code_modules [0 ].monitor_id == 9999456
621+
622+
623+ async def test_get_monitors_to_load_enabled_not_in_registry (clear_database ):
624+ """'_get_monitors_to_load' should return enabled monitors that are not in the registry"""
625+ await databases .execute_application (
626+ 'insert into "Monitors"(id, name, enabled) values'
627+ "(9999123, 'monitor_1', true),"
628+ "(9999456, 'internal.monitor_2', true),"
629+ "(9999457, 'disabled_monitor', false);"
630+ )
631+ await databases .execute_application (
632+ 'insert into "CodeModules"(monitor_id, code) values'
633+ "(9999123, 'def get_value(): return 10', '2025-01-10 00:00'),"
634+ "(9999456, 'def get_value(): return 11', '2025-01-20 00:00'),"
635+ "(9999457, 'def get_value(): return 12', '2025-01-30 00:00');"
636+ )
637+
638+ registry ._monitors = {9999456 : {"module" : ModuleType }}
639+
640+ monitors , code_modules = await monitors_loader ._get_monitors_to_load (
641+ datetime (2025 , 1 , 15 , tzinfo = timezone .utc )
642+ )
643+
644+ assert len (monitors ) == 2
645+ assert monitors [9999123 ].name == "monitor_1"
646+ assert monitors [9999456 ].name == "internal.monitor_2"
647+ assert len (code_modules ) == 2
648+ assert {code_module .monitor_id for code_module in code_modules } == {9999123 , 9999456 }
649+
650+
597651async def test_load_monitors (clear_database ):
598652 """'_load_monitors' should load all enabled monitors from the database and add them to the
599653 registry"""
0 commit comments