-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrun_tests.sh
More file actions
executable file
Β·221 lines (192 loc) Β· 5.98 KB
/
Copy pathrun_tests.sh
File metadata and controls
executable file
Β·221 lines (192 loc) Β· 5.98 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
#!/bin/bash
# Test runner script for Meutch application
# This script runs the complete test suite with different options
set -e # Exit on any error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_color() {
printf "${1}${2}${NC}\n"
}
# Function to print usage
usage() {
echo "Usage: $0 [options]"
echo "Options:"
echo " -u, --unit Run only unit tests"
echo " -i, --integration Run only integration tests"
echo " -f, --functional Run only functional tests"
echo " -c, --coverage Run tests with coverage report"
echo " -q, --quiet Run tests in quiet mode"
echo " -v, --verbose Run tests in verbose mode"
echo " -s, --slow Include slow tests"
echo " -h, --help Show this help message"
echo ""
echo "Examples:"
echo " $0 # Run all tests"
echo " $0 -u -c # Run unit tests with coverage"
echo " $0 -f -v # Run functional tests verbosely"
}
# Default options
RUN_UNIT=false
RUN_INTEGRATION=false
RUN_FUNCTIONAL=false
RUN_ALL=true
COVERAGE=false
QUIET=false
VERBOSE=false
INCLUDE_SLOW=false
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
-u|--unit)
RUN_UNIT=true
RUN_ALL=false
shift
;;
-i|--integration)
RUN_INTEGRATION=true
RUN_ALL=false
shift
;;
-f|--functional)
RUN_FUNCTIONAL=true
RUN_ALL=false
shift
;;
-c|--coverage)
COVERAGE=true
shift
;;
-q|--quiet)
QUIET=true
shift
;;
-v|--verbose)
VERBOSE=true
shift
;;
-s|--slow)
INCLUDE_SLOW=true
shift
;;
-h|--help)
usage
exit 0
;;
*)
echo "Unknown option $1"
usage
exit 1
;;
esac
done
# Check if pytest is installed
if ! command -v pytest &> /dev/null; then
print_color $RED "pytest is not installed. Please install it with: pip install pytest"
exit 1
fi
# Set up environment
export FLASK_ENV=testing
export PYTHONPATH="${PYTHONPATH}:$(pwd)"
export TEST_DATABASE_URL="postgresql://test_user:test_password@localhost:5433/meutch_test"
print_color $BLUE "π§ͺ Running Meutch Test Suite"
print_color $BLUE "================================"
# Check if test database is running and healthy
check_test_db() {
# Check if container exists and is running
if ! docker ps --filter "name=meutch-test-db" --format "{{.Names}}" | grep -q "meutch-test-db"; then
return 1
fi
# Check if PostgreSQL is ready to accept connections
if ! docker compose -f docker-compose.test.yml exec -T test-postgres pg_isready -U test_user -d postgres > /dev/null 2>&1; then
return 1
fi
return 0
}
ensure_test_db_exists() {
DB_EXISTS=$(docker exec meutch-test-db psql -U test_user -d postgres -tAc "SELECT 1 FROM pg_database WHERE datname='meutch_test'" 2>/dev/null | tr -d ' ')
if [ "$DB_EXISTS" != "1" ]; then
print_color $BLUE "π Creating test database (meutch_test)..."
docker exec meutch-test-db psql -U test_user -d postgres -c "CREATE DATABASE meutch_test" > /dev/null
fi
}
if ! check_test_db; then
print_color $YELLOW "β οΈ Test database is not running or not ready. Starting it now..."
# Stop any potentially problematic containers first
docker compose -f docker-compose.test.yml down > /dev/null 2>&1 || true
# Start the test database
./test-db.sh start
if [ $? -ne 0 ]; then
print_color $RED "β Failed to start test database. Please run './test-db.sh start' manually."
exit 1
fi
# Double-check that it's ready
if ! check_test_db; then
print_color $RED "β Test database started but is not ready. Please check './test-db.sh status'."
exit 1
fi
print_color $GREEN "β
Test database is now ready!"
fi
ensure_test_db_exists
# Build pytest command
PYTEST_CMD="pytest"
# Add verbosity options
if [ "$VERBOSE" = true ]; then
PYTEST_CMD="$PYTEST_CMD -v"
elif [ "$QUIET" = true ]; then
PYTEST_CMD="$PYTEST_CMD -q"
fi
# Add coverage options
if [ "$COVERAGE" = true ]; then
PYTEST_CMD="$PYTEST_CMD --cov=app --cov-report=html:htmlcov --cov-report=term-missing --cov-report=xml"
fi
# Add slow test marker
if [ "$INCLUDE_SLOW" = false ]; then
PYTEST_CMD="$PYTEST_CMD -m \"not slow\""
fi
# Determine which tests to run
if [ "$RUN_ALL" = true ]; then
print_color $YELLOW "Running all tests..."
TEST_PATH="tests/"
elif [ "$RUN_UNIT" = true ]; then
print_color $YELLOW "Running unit tests..."
TEST_PATH="tests/unit/"
elif [ "$RUN_INTEGRATION" = true ]; then
print_color $YELLOW "Running integration tests..."
TEST_PATH="tests/integration/"
elif [ "$RUN_FUNCTIONAL" = true ]; then
print_color $YELLOW "Running functional tests..."
TEST_PATH="tests/functional/"
else
# Multiple test types selected
TEST_PATH=""
if [ "$RUN_UNIT" = true ]; then
TEST_PATH="$TEST_PATH tests/unit/"
fi
if [ "$RUN_INTEGRATION" = true ]; then
TEST_PATH="$TEST_PATH tests/integration/"
fi
if [ "$RUN_FUNCTIONAL" = true ]; then
TEST_PATH="$TEST_PATH tests/functional/"
fi
fi
# Run the tests
print_color $BLUE "Executing: $PYTEST_CMD $TEST_PATH"
echo ""
if eval "$PYTEST_CMD $TEST_PATH"; then
print_color $GREEN "β
All tests passed!"
if [ "$COVERAGE" = true ]; then
print_color $BLUE "π Coverage report generated:"
print_color $BLUE " - HTML: htmlcov/index.html"
print_color $BLUE " - XML: coverage.xml"
fi
else
print_color $RED "β Some tests failed!"
exit 1
fi
print_color $BLUE ""
print_color $BLUE "Test run completed successfully! π"