|
| 1 | +""" |
| 2 | +Integration tests that make real API calls to Cronitor. |
| 3 | +
|
| 4 | +These tests are skipped by default unless CRONITOR_TEST_API_KEY environment |
| 5 | +variable is set. They test against the real Cronitor API to verify behavior. |
| 6 | +
|
| 7 | +Usage: |
| 8 | + export CRONITOR_TEST_API_KEY=your_api_key_here |
| 9 | + python -m pytest cronitor/tests/test_integration.py -v |
| 10 | + # or |
| 11 | + python -m unittest cronitor.tests.test_integration -v |
| 12 | +""" |
| 13 | + |
| 14 | +import os |
| 15 | +import unittest |
| 16 | +import cronitor |
| 17 | + |
| 18 | + |
| 19 | +# Check if integration tests should run |
| 20 | +INTEGRATION_API_KEY = os.getenv('CRONITOR_TEST_API_KEY') |
| 21 | +SKIP_INTEGRATION = not INTEGRATION_API_KEY |
| 22 | +SKIP_REASON = "Set CRONITOR_TEST_API_KEY environment variable to run integration tests" |
| 23 | + |
| 24 | + |
| 25 | +@unittest.skipIf(SKIP_INTEGRATION, SKIP_REASON) |
| 26 | +class MonitorListIntegrationTests(unittest.TestCase): |
| 27 | + """Integration tests for Monitor.list() against real API""" |
| 28 | + |
| 29 | + @classmethod |
| 30 | + def setUpClass(cls): |
| 31 | + """Set up API key for all tests""" |
| 32 | + cronitor.api_key = INTEGRATION_API_KEY |
| 33 | + |
| 34 | + def test_list_all_monitors(self): |
| 35 | + """Test listing all monitors (first page)""" |
| 36 | + monitors = cronitor.Monitor.list() |
| 37 | + |
| 38 | + # Should return a list (may be empty for new accounts) |
| 39 | + self.assertIsInstance(monitors, list) |
| 40 | + |
| 41 | + # If there are monitors, verify structure |
| 42 | + if len(monitors) > 0: |
| 43 | + monitor = monitors[0] |
| 44 | + self.assertIsInstance(monitor, cronitor.Monitor) |
| 45 | + self.assertIsNotNone(monitor.data.key) |
| 46 | + self.assertIsNotNone(monitor.data.name) |
| 47 | + print(f"\n✓ Found {len(monitors)} monitors on first page (default pageSize=100)") |
| 48 | + print(f" First monitor: {monitor.data.name} ({monitor.data.key})") |
| 49 | + |
| 50 | + def test_list_all_monitors_auto_paginate(self): |
| 51 | + """Test listing all monitors with auto_paginate""" |
| 52 | + monitors_all = cronitor.Monitor.list(auto_paginate=True) |
| 53 | + |
| 54 | + self.assertIsInstance(monitors_all, list) |
| 55 | + |
| 56 | + # Check if there were multiple pages |
| 57 | + monitors_page1 = cronitor.Monitor.list(pageSize=100) |
| 58 | + if len(monitors_all) > 100: |
| 59 | + print(f"\n✓ Auto-paginate fetched all {len(monitors_all)} monitors across multiple pages") |
| 60 | + print(f" First page had {len(monitors_page1)}, total is {len(monitors_all)}") |
| 61 | + else: |
| 62 | + print(f"\n✓ Auto-paginate fetched {len(monitors_all)} monitors (all fit in one page)") |
| 63 | + |
| 64 | + def test_list_with_pagination(self): |
| 65 | + """Test listing monitors with specific page size""" |
| 66 | + monitors = cronitor.Monitor.list(pageSize=5) |
| 67 | + |
| 68 | + self.assertIsInstance(monitors, list) |
| 69 | + # Should return at most 5 monitors |
| 70 | + self.assertLessEqual(len(monitors), 5) |
| 71 | + print(f"\n✓ Pagination works, got {len(monitors)} monitors (max 5)") |
| 72 | + |
| 73 | + def test_list_with_filter(self): |
| 74 | + """Test listing monitors with type filter""" |
| 75 | + monitors = cronitor.Monitor.list(type='job') |
| 76 | + |
| 77 | + self.assertIsInstance(monitors, list) |
| 78 | + |
| 79 | + # Verify all returned monitors are jobs |
| 80 | + for monitor in monitors: |
| 81 | + self.assertEqual(monitor.data.type, 'job') |
| 82 | + |
| 83 | + print(f"\n✓ Filter works, got {len(monitors)} job monitors") |
| 84 | + |
| 85 | + def test_list_with_search(self): |
| 86 | + """Test listing monitors with search parameter""" |
| 87 | + monitors = cronitor.Monitor.list(search='test job') |
| 88 | + |
| 89 | + self.assertIsInstance(monitors, list) |
| 90 | + |
| 91 | + # Should return monitors matching search term |
| 92 | + if len(monitors) > 0: |
| 93 | + print(f"\n✓ Search works, found {len(monitors)} monitors matching 'test job'") |
| 94 | + for monitor in monitors[:3]: # Show first 3 |
| 95 | + print(f" - {monitor.data.name} ({monitor.data.key})") |
| 96 | + else: |
| 97 | + print(f"\n✓ Search works, found 0 monitors matching 'test job'") |
| 98 | + |
| 99 | + def test_list_specific_keys(self): |
| 100 | + """Test listing specific monitors by key""" |
| 101 | + # First get some monitors to test with |
| 102 | + all_monitors = cronitor.Monitor.list(pageSize=2) |
| 103 | + |
| 104 | + if len(all_monitors) == 0: |
| 105 | + self.skipTest("No monitors found in account") |
| 106 | + |
| 107 | + # Get keys to fetch |
| 108 | + keys_to_fetch = [m.data.key for m in all_monitors[:min(2, len(all_monitors))]] |
| 109 | + |
| 110 | + # Fetch them specifically |
| 111 | + monitors = cronitor.Monitor.list(keys_to_fetch) |
| 112 | + |
| 113 | + self.assertEqual(len(monitors), len(keys_to_fetch)) |
| 114 | + returned_keys = [m.data.key for m in monitors] |
| 115 | + self.assertEqual(set(returned_keys), set(keys_to_fetch)) |
| 116 | + |
| 117 | + print(f"\n✓ Fetched specific monitors: {', '.join(keys_to_fetch)}") |
| 118 | + |
| 119 | + def test_monitor_data_structure(self): |
| 120 | + """Test that monitor data structure is correct""" |
| 121 | + monitors = cronitor.Monitor.list(pageSize=1) |
| 122 | + |
| 123 | + if len(monitors) == 0: |
| 124 | + self.skipTest("No monitors found in account") |
| 125 | + |
| 126 | + monitor = monitors[0] |
| 127 | + |
| 128 | + # Test basic fields exist |
| 129 | + self.assertIsNotNone(monitor.data.key) |
| 130 | + self.assertIsNotNone(monitor.data.name) |
| 131 | + self.assertIsNotNone(monitor.data.type) |
| 132 | + |
| 133 | + # Test nested attribute access works |
| 134 | + self.assertIsNotNone(monitor.data.attributes) |
| 135 | + self.assertIsNotNone(monitor.data.attributes.code) |
| 136 | + |
| 137 | + # Test pretty printing works |
| 138 | + json_str = str(monitor.data) |
| 139 | + self.assertIn(monitor.data.key, json_str) |
| 140 | + self.assertIn('\n', json_str) # Pretty formatted |
| 141 | + |
| 142 | + print(f"\n✓ Monitor data structure correct") |
| 143 | + print(f" Key: {monitor.data.key}") |
| 144 | + print(f" Name: {monitor.data.name}") |
| 145 | + print(f" Type: {monitor.data.type}") |
| 146 | + |
| 147 | + |
| 148 | +if __name__ == '__main__': |
| 149 | + if SKIP_INTEGRATION: |
| 150 | + print(f"\n⚠️ {SKIP_REASON}\n") |
| 151 | + print("Example:") |
| 152 | + print(" export CRONITOR_TEST_API_KEY=your_api_key_here") |
| 153 | + print(" python -m unittest cronitor.tests.test_integration -v\n") |
| 154 | + else: |
| 155 | + print(f"\n🚀 Running integration tests against Cronitor API...\n") |
| 156 | + unittest.main() |
0 commit comments