Skip to content

Commit c2a16a8

Browse files
author
Eyo Chen
committed
feat: set up integration tests
1 parent dfc111e commit c2a16a8

File tree

7 files changed

+295
-1
lines changed

7 files changed

+295
-1
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
generate:
2-
docker-compose run --rm -T protoc
2+
docker-compose run --rm -T protoc
3+
4+
test:
5+
./tools/tests/run_tests.sh

docker-compose.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
version: '3.8'
22

33
services:
4+
mongodb-test:
5+
image: mongo:latest
6+
container_name: test_mongodb
7+
ports:
8+
- "27017:27017"
9+
environment:
10+
MONGO_INITDB_DATABASE: test_stock_db
411
protoc:
512
build:
613
context: ./tools/protoc

pyproject.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ dependencies = [
1515
"markupsafe==3.0.2",
1616
"polygon-api-client==1.14.5",
1717
"protobuf==5.29.4",
18+
"pymongo>=4.13.0",
1819
"python-dateutil==2.9.0.post0",
1920
"python-dotenv==1.1.0",
2021
"pyyaml==6.0.2",
@@ -25,3 +26,8 @@ dependencies = [
2526
"validate-email==1.3",
2627
"websockets==14.2",
2728
]
29+
30+
[dependency-groups]
31+
dev = [
32+
"pytest>=8.3.5",
33+
]

pytest.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[pytest]
2+
python_files = test_*.py
3+
python_functions = test_*
4+
addopts = -v --tb=short
5+
testpaths = src/tests

src/tests/tools/wait_for_mongo.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import time
2+
from pymongo import MongoClient
3+
from pymongo.errors import ConnectionFailure, ServerSelectionTimeoutError
4+
5+
6+
def wait_for_mongo(host="localhost", port=27017, timeout=30, db_name="test_stock_db"):
7+
"""Wait for MongoDB to be ready by attempting to connect and ping."""
8+
start_time = time.time()
9+
while time.time() - start_time < timeout:
10+
try:
11+
client = MongoClient(
12+
f"mongodb://{host}:{port}", serverSelectionTimeoutMS=1000
13+
)
14+
client.admin.command("ping") # Ping the server to check if it's ready
15+
client.drop_database(db_name) # Ensure clean state
16+
client.close()
17+
print("MongoDB is ready and test database cleared!")
18+
return True
19+
except (ConnectionFailure, ServerSelectionTimeoutError):
20+
print("MongoDB not ready, retrying...")
21+
time.sleep(1)
22+
raise TimeoutError("MongoDB did not become ready within the timeout period")
23+
24+
25+
if __name__ == "__main__":
26+
wait_for_mongo()

tools/tests/run_tests.sh

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/bin/bash
2+
3+
# Define color codes
4+
RED='\033[0;31m'
5+
GREEN='\033[0;32m'
6+
BLUE='\033[0;34m'
7+
YELLOW='\033[0;33m'
8+
NC='\033[0m'
9+
10+
# Function to print a line of repeated emojis
11+
print_emoji_line() {
12+
local emoji=$1
13+
local color=$2
14+
local cols=$(tput cols) # Get terminal width
15+
local line=""
16+
17+
# Calculate how many emojis fit in the terminal width (approximate, as emojis vary in width)
18+
for ((i=0; i<cols/2; i++)); do
19+
line="${line}${emoji}"
20+
done
21+
echo -e "${color}${line}"
22+
}
23+
24+
# Function to clean up resources
25+
cleanup() {
26+
echo -e "${BLUE}"
27+
print_emoji_line "<=" "${BLUE}"
28+
echo -e "<= Cleaning up resources..."
29+
print_emoji_line "<=" "${BLUE}"
30+
docker-compose down -v mongodb-test
31+
rm -rf __pycache__ tests/__pycache__
32+
}
33+
34+
# Set trap to call cleanup on exit (success or failure)
35+
trap cleanup EXIT
36+
37+
echo -e "${YELLOW}"
38+
print_emoji_line "=>" "${YELLOW}"
39+
echo -e "=> Starting test environment..."
40+
print_emoji_line "=>" "${YELLOW}"
41+
docker-compose up -d mongodb-test
42+
uv run src/tests/tools/wait_for_mongo.py
43+
PYTHONPATH=./src python -m pytest src/tests/
44+
if [ $? -eq 0 ]; then
45+
echo -e "${GREEN}🎉 Tests passed successfully!"
46+
else
47+
echo -e "${RED}❌ Tests failed!"
48+
fi

uv.lock

Lines changed: 199 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)