-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrun_tests.py
More file actions
executable file
·203 lines (169 loc) · 6.48 KB
/
run_tests.py
File metadata and controls
executable file
·203 lines (169 loc) · 6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env python3
"""
Simplified test runner for Couchbase Python SDK samples.
This runner handles the dependency and import issues gracefully.
"""
import sys
import os
# Add the parent directory to Python path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
def setup_exception_mocks():
"""Set up exception classes that work with assertRaises."""
# First, try to import real couchbase.exceptions to preserve exception classes
try:
import couchbase.exceptions
# If successful, return - real exceptions are already available
return
except ImportError:
# If couchbase isn't installed, we need to create proper exception classes
from types import ModuleType
exceptions_module = ModuleType('couchbase.exceptions')
# Create real exception classes that can be used with assertRaises
class CouchbaseException(Exception):
pass
class DocumentExistsException(CouchbaseException):
pass
class DocumentNotFoundException(CouchbaseException):
pass
class TimeoutException(CouchbaseException):
pass
class ServiceUnavailableException(CouchbaseException):
pass
class ParsingFailedException(CouchbaseException):
pass
class CASMismatchException(CouchbaseException):
pass
# Add exception classes to the mock module
exceptions_module.CouchbaseException = CouchbaseException
exceptions_module.DocumentExistsException = DocumentExistsException
exceptions_module.DocumentNotFoundException = DocumentNotFoundException
exceptions_module.TimeoutException = TimeoutException
exceptions_module.ServiceUnavailableException = ServiceUnavailableException
exceptions_module.ParsingFailedException = ParsingFailedException
exceptions_module.CASMismatchException = CASMismatchException
sys.modules['couchbase.exceptions'] = exceptions_module
# Set up exception mocks BEFORE any other imports
setup_exception_mocks()
# Now import unittest and other dependencies
from unittest.mock import patch, MagicMock
import unittest
def mock_missing_dependencies():
"""Mock missing dependencies to prevent import errors."""
# Try to import couchbase - if it works, skip mocking it
try:
import couchbase
couchbase_available = True
except ImportError:
couchbase_available = False
mock_modules = [
'couchbase',
'couchbase.cluster',
'couchbase.auth',
'couchbase.options',
'couchbase.subdocument',
'couchbase.transactions',
'couchbase.search',
'couchbase.diagnostics',
'couchbase.n1ql',
'couchbase.management',
'couchbase.management.search',
'acouchbase',
'acouchbase.cluster',
'pandas',
'openpyxl',
'opentelemetry',
'opentelemetry.trace',
'opentelemetry.sdk',
'opentelemetry.sdk.trace',
'opentelemetry.sdk.trace.export'
]
for module_name in mock_modules:
# Skip mocking couchbase modules if couchbase is available
if couchbase_available and module_name.startswith('couchbase'):
continue
# Only mock if not already imported
if module_name not in sys.modules:
sys.modules[module_name] = MagicMock()
def get_working_tests():
"""Return list of test modules that should work properly."""
working_tests = [
'tests.test_advanced_prepared_statement_wrapper',
'tests.test_01_cb_set_get',
'tests.test_11_cb_async_operations',
'tests.test_12_cb_async_queries',
]
# Try to add tests that don't have "invalid format" issues
safe_tests = [
'tests.test_excel_to_json_to_cb',
'tests.test_01a_cb_get_update_w_cas',
'tests.test_02_cb_upsert_delete',
'tests.test_03a_cb_query',
'tests.test_03b_cb_query_profile',
'tests.test_04_cb_sub_doc_ops',
'tests.test_ai_vector_search',
'tests.test_05_cb_exception_handling',
'tests.test_06_cb_get_retry_replica_read',
'tests.test_09_cb_fts_search',
'tests.test_10_cb_debug_tracing',
]
# Only add tests that can be imported safely
for test_module in safe_tests:
try:
# Test if we can import the test module
__import__(test_module)
working_tests.append(test_module)
except (ValueError, ImportError) as e:
if "invalid format" in str(e):
print(f"⚠️ Skipping {test_module}: {e}")
else:
print(f"⚠️ Skipping {test_module}: Import error")
# Skip tests with invalid format errors
return working_tests
def main():
"""Run tests with proper error handling."""
print("Setting up mock dependencies...")
mock_missing_dependencies()
print("Running Couchbase SDK sample tests...")
print("=" * 50)
# Get working tests
working_tests = get_working_tests()
# Create test suite
loader = unittest.TestLoader()
suite = unittest.TestSuite()
total_tests = 0
successful_modules = 0
for test_module in working_tests:
try:
tests = loader.loadTestsFromName(test_module)
suite.addTest(tests)
total_tests += tests.countTestCases()
successful_modules += 1
print(f"✅ Loaded {tests.countTestCases()} tests from {test_module}")
except Exception as e:
print(f"❌ Failed to load {test_module}: {e}")
print("=" * 50)
print(f"Loaded {total_tests} tests from {successful_modules} modules")
print("=" * 50)
# Run tests
runner = unittest.TextTestRunner(verbosity=2)
result = runner.run(suite)
print("=" * 50)
print("Test Summary:")
print(f"Tests run: {result.testsRun}")
print(f"Failures: {len(result.failures)}")
print(f"Errors: {len(result.errors)}")
if result.failures:
print("\nFailures:")
for test, traceback in result.failures:
print(f"- {test}")
if result.errors:
print("\nErrors:")
for test, traceback in result.errors:
print(f"- {test}")
# Return appropriate exit code
if result.failures or result.errors:
return 1
return 0
if __name__ == '__main__':
exit_code = main()
sys.exit(exit_code)