Skip to content

Commit 1cd765e

Browse files
committed
refactor: don't use testcontainers for the unit tests, also remove unnecessary asserts in the 'reporters.py'.
1 parent ffe730e commit 1cd765e

File tree

3 files changed

+35
-26
lines changed

3 files changed

+35
-26
lines changed

docker/docker.mk

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
MONGO_DOCKER_TAG := mongo:8.0.9
2+
MONGO_CONTAINER_NAME := mongo-$(UTIL_NAME)-test
3+
4+
15
docker-help:
26
@echo "Docker:"
37
@echo " docker-run Runs docker container with installed util;"
48
@echo " docker-image Builds a docker image;"
59
@echo " ALL=[1|0] REBUILD=[1|0]"
610
@echo " docker-base-image Builds a basic docker image from which to build other images;"
711
@echo " docker-test-image Builds a docker image for running tests and building package;"
12+
@echo " docker-test-mongo-run Runs the MongoDB docker container (${MONGO_CONTAINER_NAME}) for the unit test purpose;"
13+
@echo " docker-test-mongo-stop Stops and removes the MongoDB docker container (${MONGO_CONTAINER_NAME});"
814
@echo " docker-test Runs unit tests with pytest framework in the docker container;"
915
@echo " docker-autotest Runs autotests in docker container;"
1016
@echo " docker-build-package Builds the debian package in special docker image;"
@@ -36,11 +42,24 @@ docker-test-image: docker-base-image
3642
--file docker/test_ubuntu2204.dockerfile \
3743
.
3844

39-
docker-test: docker-test-image
40-
docker run --rm \
45+
docker-test-mongo-run:
46+
@docker run \
47+
--env MONGO_INITDB_ROOT_USERNAME=root \
48+
--env MONGO_INITDB_ROOT_PASSWORD=root \
49+
--detach \
50+
--name $(MONGO_CONTAINER_NAME) \
51+
$(MONGO_DOCKER_TAG)
52+
53+
docker-test-mongo-stop:
54+
@docker container stop $(MONGO_CONTAINER_NAME)
55+
@docker container rm $(MONGO_CONTAINER_NAME)
56+
57+
docker-test: docker-test-image docker-test-mongo-run
58+
@docker run --rm \
59+
--env MONGO_HOST=$(shell docker inspect --format '{{ .NetworkSettings.IPAddress }}' $(MONGO_CONTAINER_NAME)) \
4160
--volume $(PWD)/test:/usr/src/$(UTIL_NAME)/test \
42-
--volume /var/run/docker.sock:/var/run/docker.sock \
43-
"$(TEST_DOCKER_TAG)"
61+
"$(TEST_DOCKER_TAG)" || (make docker-test-mongo-stop && exit 200)
62+
@make docker-test-mongo-stop
4463

4564
docker-autotest: docker-test-image docker-build-package
4665
@if [ $(shell find . -maxdepth 1 -type f -name .env | wc --lines) != 1 ]; then \

src/codeplag/reporters.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,8 @@ def serialize_compare_result_to_dict(compare_info: FullCompareInfo) -> dict:
189189

190190

191191
def deserialize_compare_result_from_dict(compare_result: dict) -> FullCompareInfo:
192-
assert compare_result is not None
193192
structure_d = dict(compare_result["structure"])
194-
assert structure_d is not None
195193
fast_d = dict(compare_result["fast"])
196-
assert fast_d is not None
197194

198195
compare_info = FullCompareInfo(
199196
fast=FastCompareInfo(

test/unit/codeplag/db/test_mongo.py

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
"""MIT License.
22
3-
Written 2025 by Konstantin Rogozhin, Nikolai Myshkin
3+
Written 2025 by Konstantin Rogozhin, Nikolai Myshkin, Semidolin Artyom.
44
"""
55

66
import dataclasses
7-
import time
7+
import os
88

99
import pytest
10-
from testcontainers.mongodb import MongoDbContainer
1110
from typing_extensions import Self
1211

13-
from codeplag.consts import DEFAULT_MONGO_USER
12+
from codeplag.consts import DEFAULT_MONGO_PORT, DEFAULT_MONGO_USER
1413
from codeplag.db.mongo import (
1514
FeaturesRepository,
1615
MongoDBConnection,
@@ -23,25 +22,19 @@
2322

2423

2524
@pytest.fixture(scope="module")
26-
def mongo_container() -> MongoDbContainer:
27-
with MongoDbContainer("mongo:8.0", username=DEFAULT_MONGO_USER) as mongo:
28-
mongo.start()
29-
time.sleep(7)
30-
yield mongo
25+
def mongo_host() -> str:
26+
host = os.environ.get("MONGO_HOST")
27+
assert host, f"Invalid MONGO_HOST environment '{host}'."
28+
return host
3129

3230

3331
@pytest.fixture(scope="module")
34-
def mongo_connection(mongo_container: MongoDbContainer) -> MongoDBConnection:
35-
host = mongo_container.get_container_host_ip()
36-
port = int(mongo_container.get_exposed_port(27017))
37-
user = mongo_container.username
38-
password = mongo_container.password
39-
32+
def mongo_connection(mongo_host: str) -> MongoDBConnection:
4033
conn = MongoDBConnection(
41-
host=host,
42-
port=port,
43-
user=user,
44-
password=password,
34+
host=mongo_host,
35+
port=DEFAULT_MONGO_PORT,
36+
user=DEFAULT_MONGO_USER,
37+
password=DEFAULT_MONGO_USER,
4538
)
4639
yield conn
4740

0 commit comments

Comments
 (0)