-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrun_tests.py
More file actions
244 lines (202 loc) Β· 7.39 KB
/
run_tests.py
File metadata and controls
244 lines (202 loc) Β· 7.39 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env python3
"""
Local test runner for Trends.Earth API
This script helps developers run tests locally before pushing to GitHub
"""
import argparse
import os
from pathlib import Path
import subprocess
import sys
def run_command(cmd, description, ignore_errors=False):
"""Run a command and handle errors"""
print(f"\nπ {description}...")
print(f"Running: {cmd}")
try:
result = subprocess.run(
cmd,
shell=True,
check=True,
capture_output=True,
text=True, # noqa: S602
)
print(f"β
{description} completed successfully")
if result.stdout:
print("Output:", result.stdout[-500:]) # Show last 500 chars
return True
except subprocess.CalledProcessError as e:
print(f"β {description} failed")
if e.stdout:
print("Output:", e.stdout[-500:])
if e.stderr:
print("Error:", e.stderr[-500:])
if not ignore_errors:
return False
print("β οΈ Continuing despite errors...")
return True
def check_dependencies():
"""Check if required dependencies are installed"""
print("π Checking dependencies...")
# Check if pytest is installed
try:
import pytest
print(f"β
pytest {pytest.__version__} found")
except ImportError:
print("β pytest not found. Please run: pip install -r requirements-dev.txt")
return False
# Check if flask is installed
try:
import flask
print(f"β
Flask {flask.__version__} found")
except ImportError:
print("β Flask not found. Please run: pip install -r requirements.txt")
return False
return True
def setup_environment():
"""Set up environment variables for testing"""
print("π§ Setting up test environment...")
env_vars = {
"DATABASE_URL": "sqlite:///test.db",
"REDIS_URL": "redis://localhost:6379/1",
"JWT_SECRET_KEY": "test-secret-key",
"FLASK_ENV": "testing",
"TESTING": "true",
}
for key, value in env_vars.items():
os.environ[key] = value
print(f" {key} = {value}")
print("β
Environment setup complete")
def main():
parser = argparse.ArgumentParser(description="Run Trends.Earth API tests locally")
parser.add_argument("--unit", action="store_true", help="Run unit tests only")
parser.add_argument(
"--integration", action="store_true", help="Run integration tests only"
)
parser.add_argument(
"--validation", action="store_true", help="Run API validation tests only"
)
parser.add_argument(
"--performance", action="store_true", help="Run performance tests"
)
parser.add_argument("--lint", action="store_true", help="Run linting only")
parser.add_argument(
"--coverage", action="store_true", help="Generate coverage report"
)
parser.add_argument("--fast", action="store_true", help="Skip slow tests")
parser.add_argument("--verbose", "-v", action="store_true", help="Verbose output")
parser.add_argument(
"--install-deps", action="store_true", help="Install dependencies first"
)
args = parser.parse_args()
# Change to project directory
project_root = Path(__file__).parent
os.chdir(project_root)
print("π Trends.Earth API Test Runner")
print(f"π Working directory: {os.getcwd()}")
# Install dependencies if requested
if args.install_deps:
if not run_command(
"pip install -r requirements.txt", "Installing main dependencies"
):
return 1
if not run_command(
"pip install -r requirements-dev.txt", "Installing dev dependencies"
):
return 1
# Check dependencies
if not check_dependencies():
print("\nπ‘ Try running with --install-deps to install missing dependencies")
return 1
# Setup environment
setup_environment()
success = True
# Run linting if requested or if no specific tests requested
if args.lint or not any(
[args.unit, args.integration, args.validation, args.performance]
):
print("\n" + "=" * 50)
print("π RUNNING LINTING")
print("=" * 50)
linting_commands = [
("python -m ruff check gefapi/ tests/", "Ruff linting", True),
(
"python -m ruff format --check gefapi/ tests/",
"Ruff formatting check",
True,
),
]
for cmd, description, ignore_errors in linting_commands:
if not run_command(cmd, description, ignore_errors):
success = False
# Build pytest command
pytest_args = []
if args.verbose:
pytest_args.append("-v")
if args.coverage:
pytest_args.extend(["--cov=gefapi", "--cov-report=html", "--cov-report=term"])
# Run specific test categories
if args.unit:
print("\n" + "=" * 50)
print("π§ͺ RUNNING UNIT TESTS")
print("=" * 50)
cmd_args = ["pytest", "tests/"] + pytest_args
if args.fast:
cmd_args.extend(["-m", "not slow and not integration"])
cmd = " ".join(cmd_args)
if not run_command(cmd, "Unit tests"):
success = False
if args.integration:
print("\n" + "=" * 50)
print("π RUNNING INTEGRATION TESTS")
print("=" * 50)
cmd_args = ["pytest", "tests/test_integration.py"] + pytest_args
cmd = " ".join(cmd_args)
if not run_command(cmd, "Integration tests"):
success = False
if args.validation:
print("\n" + "=" * 50)
print("β
RUNNING API VALIDATION TESTS")
print("=" * 50)
cmd_args = ["pytest", "tests/test_api_validation.py"] + pytest_args
cmd = " ".join(cmd_args)
if not run_command(cmd, "API validation tests"):
success = False
if args.performance:
print("\n" + "=" * 50)
print("β‘ RUNNING PERFORMANCE TESTS")
print("=" * 50)
cmd_args = ["pytest", "tests/test_performance.py"] + pytest_args
if args.fast:
cmd_args.extend(["-m", "not slow"])
cmd = " ".join(cmd_args)
if not run_command(cmd, "Performance tests", ignore_errors=True):
print("β οΈ Performance tests completed with issues")
# If no specific category was requested, run all tests
if not any(
[args.unit, args.integration, args.validation, args.performance, args.lint]
):
print("\n" + "=" * 50)
print("π§ͺ RUNNING ALL TESTS")
print("=" * 50)
cmd_args = ["pytest", "tests/"] + pytest_args
if args.fast:
cmd_args.extend(["-m", "not slow"])
cmd = " ".join(cmd_args)
if not run_command(cmd, "All tests"):
success = False
# Print summary
print("\n" + "=" * 50)
print("π TEST SUMMARY")
print("=" * 50)
if success:
print("β
All tests passed! Ready to push to GitHub.")
else:
print("β Some tests failed. Please fix the issues before pushing.")
print("\nπ Test artifacts:")
if os.path.exists("htmlcov/index.html"):
print(f" Coverage report: {os.path.abspath('htmlcov/index.html')}")
if os.path.exists("test-report.html"):
print(f" Test report: {os.path.abspath('test-report.html')}")
return 0 if success else 1
if __name__ == "__main__":
sys.exit(main())