From 25a7664948d62a966552fc4b6ea3d7916f85b2d6 Mon Sep 17 00:00:00 2001 From: Eyo Chen Date: Sun, 1 Jun 2025 09:01:03 +0800 Subject: [PATCH 1/2] feat: support running one testing file --- Makefile | 2 +- docker-compose.yml | 6 +++--- tools/tests/run_tests.sh | 12 +++++++++++- tools/tests/wait_for_mongo.py | 6 ++---- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 74c0e4a..80e0e7c 100644 --- a/Makefile +++ b/Makefile @@ -2,4 +2,4 @@ generate: docker-compose run --rm -T protoc test: - ./tools/tests/run_tests.sh \ No newline at end of file + ./tools/tests/run_tests.sh $(TEST_FILE) \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index fd50606..0aa611f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,7 +5,7 @@ services: image: mongo:latest container_name: test_mongodb ports: - - "27017:27017" + - "27015:27017" environment: MONGO_INITDB_DATABASE: test_stock_db protoc: @@ -15,8 +15,8 @@ services: - ./src/proto:/app/src/proto - ./tools/protoc:/app/tools/protoc working_dir: /app - command: ["/app/tools/protoc/gen-protoc.sh"] + command: [ "/app/tools/protoc/gen-protoc.sh" ] networks: default: - name: python-proto-net \ No newline at end of file + name: python-proto-net diff --git a/tools/tests/run_tests.sh b/tools/tests/run_tests.sh index f4a8c3d..8938261 100755 --- a/tools/tests/run_tests.sh +++ b/tools/tests/run_tests.sh @@ -50,8 +50,18 @@ else cov_report="--cov-report=term-missing" fi +# Check if a test file path is provided as an argument +TEST_FILE=$1 +if [ -n "$TEST_FILE" ]; then + echo -e "Running tests for: $TEST_FILE" + TEST_PATH="$TEST_FILE" +else + echo -e "Running all tests under src/tests" + TEST_PATH="src/tests/" +fi + # Run pytest with coverage -PYTHONPATH=./src uv run pytest src/tests/ --cov=src $cov_report -v +PYTHONPATH=./src uv run pytest "$TEST_PATH" --cov=src $cov_report -v pytest_exit_code=$? # Extract total coverage percentage total_coverage=$(uv run coverage report | grep TOTAL | awk '{print $NF}' | sed 's/%//') diff --git a/tools/tests/wait_for_mongo.py b/tools/tests/wait_for_mongo.py index d0ab757..4a08930 100644 --- a/tools/tests/wait_for_mongo.py +++ b/tools/tests/wait_for_mongo.py @@ -3,14 +3,12 @@ from pymongo.errors import ConnectionFailure, ServerSelectionTimeoutError -def wait_for_mongo(host="localhost", port=27017, timeout=30, db_name="test_stock_db"): +def wait_for_mongo(host="localhost", port=27015, timeout=30, db_name="test_stock_db"): """Wait for MongoDB to be ready by attempting to connect and ping.""" start_time = time.time() while time.time() - start_time < timeout: try: - client = MongoClient( - f"mongodb://{host}:{port}", serverSelectionTimeoutMS=1000 - ) + client = MongoClient(f"mongodb://{host}:{port}", serverSelectionTimeoutMS=1000) client.admin.command("ping") # Ping the server to check if it's ready client.drop_database(db_name) # Ensure clean state client.close() From 1ef51ce50658c3474b816d8455c335719e612938 Mon Sep 17 00:00:00 2001 From: Eyo Chen Date: Sun, 1 Jun 2025 09:03:58 +0800 Subject: [PATCH 2/2] fix: correct testing mongo port --- src/tests/test_stock_adapters.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tests/test_stock_adapters.py b/src/tests/test_stock_adapters.py index 2a79dad..f458143 100644 --- a/src/tests/test_stock_adapters.py +++ b/src/tests/test_stock_adapters.py @@ -10,7 +10,7 @@ @pytest.fixture(scope="module") def mongo_client(): - client = MongoClient("mongodb://localhost:27017") + client = MongoClient("mongodb://localhost:27015") yield client client.drop_database("test_stock_db") client.close()