Skip to content

Commit 66160ff

Browse files
authored
Merge pull request #73 from grillazz/69-switch-to-pymongo-for-async
69 switch to pymongo for async
2 parents c5d223e + 0a59628 commit 66160ff

File tree

8 files changed

+26
-85
lines changed

8 files changed

+26
-85
lines changed

Dockerfile.old

Lines changed: 0 additions & 42 deletions
This file was deleted.

Makefile

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,31 @@ help: ## Show this help
44

55
.PHONY: build
66
build: ## Build project with compose
7-
docker compose build
7+
docker compose -f compose.yml up
88

99
.PHONY: up
1010
up: ## Run project with compose
11-
docker compose up
11+
docker compose -f compose.yml up
1212

1313
.PHONY: down
1414
down: ## Reset project containers with compose
15-
docker compose down -v --remove-orphans
15+
docker compose -f compose.yml down -v --remove-orphans
1616

1717
.PHONY: test
1818
test: ## Run project tests
1919
docker compose -f compose.yml run --rm web pytest -vv tests
2020

2121
.PHONY: test-snapshot
2222
test-snapshot: ## Run project tests
23-
docker compose -f compose.yml run web pytest --inline-snapshot=create tests
23+
docker compose -f compose.yml run --rm web pytest --inline-snapshot=fix tests
2424

2525
.PHONY: mypy
2626
mypy: ## mypy check.
2727
mypy --ignore-missing-imports .
2828

2929
.PHONY: lint
3030
lint: ## Lint project code.
31-
poetry run ruff check --fix .
31+
uv run ruff check --fix .
3232

3333
.PHONY: safety
3434
safety: ## apply safety check in project.
@@ -41,5 +41,5 @@ format: ## format project code.
4141

4242
.PHONY: clean
4343
clean: ## Clean Reset project containers and volumes with compose
44-
docker compose down -v --remove-orphans | true
45-
docker compose rm -f
44+
docker compose -f compose.yml down -v --remove-orphans | true
45+
docker compose -f compose.yml rm -f

greens/main.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@
1515
async def lifespan(app: FastAPI):
1616
app.state.logger = get_logger(__name__)
1717
app.state.logger.info("Starting greens on your farmland...mmm")
18-
app.state.mongo_client, app.state.mongo_db, app.state.mongo_collection = (
19-
await init_mongo(
20-
global_settings.mongodb_database,
21-
global_settings.mongodb_url.unicode_string(),
22-
global_settings.mongodb_collection,
23-
)
18+
app.state.mongo_client, app.state.mongo_db, app.state.mongo_collection = await init_mongo(
19+
global_settings.mongodb_database,
20+
global_settings.mongodb_url.unicode_string(),
21+
global_settings.mongodb_collection,
2422
)
2523
try:
2624
yield
@@ -42,4 +40,3 @@ async def health_check():
4240
# except Exception:
4341
# app.state.logger.exception("My way or highway...")
4442
return await get_mongo_meta()
45-

greens/services/repository.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ async def retrieve_document(document_id: str, collection: str) -> dict:
1919
:return:
2020
"""
2121
document_filter = {"_id": ObjectId(document_id)}
22-
if document := await greens.app.state.mongo_collection[collection].find_one(
23-
document_filter
24-
):
22+
if document := await greens.app.state.mongo_collection[collection].find_one(document_filter):
2523
return await document_id_helper(document)
2624
else:
2725
raise ValueError(f"No document found for {document_id=} in {collection=}")
@@ -35,9 +33,9 @@ async def create_document(document, collection: str) -> InsertOneResult:
3533
:return:
3634
"""
3735
try:
38-
document: InsertOneResult = await greens.app.state.mongo_collection[
39-
collection
40-
].insert_one(document.model_dump())
36+
document: InsertOneResult = await greens.app.state.mongo_collection[collection].insert_one(
37+
document.model_dump()
38+
)
4139
return document
4240
except WriteError as e:
4341
# TODO: this not make sense as id from mongo will be always unique if we not pass it
@@ -48,9 +46,7 @@ async def get_mongo_meta() -> dict:
4846
list_databases = await greens.app.state.mongo_client.list_database_names()
4947
list_of_collections = {}
5048
for db in list_databases:
51-
list_of_collections[db] = await greens.app.state.mongo_client[
52-
db
53-
].list_collection_names()
49+
list_of_collections[db] = await greens.app.state.mongo_client[db].list_collection_names()
5450
mongo_meta = await greens.app.state.mongo_client.server_info()
5551
return {
5652
"version": mongo_meta["version"],

greens/utils.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,8 @@ def get_logger(module_name):
1919
2020
"""
2121
logger = logging.getLogger(module_name)
22-
handler = RichHandler(
23-
rich_tracebacks=True, console=console, tracebacks_show_locals=True
24-
)
25-
handler.setFormatter(
26-
logging.Formatter(
27-
"%(name)s - [ %(threadName)s:%(funcName)s:%(lineno)d ] - %(message)s"
28-
)
29-
)
22+
handler = RichHandler(rich_tracebacks=True, console=console, tracebacks_show_locals=True)
23+
handler.setFormatter(logging.Formatter("%(name)s - [ %(threadName)s:%(funcName)s:%(lineno)d ] - %(message)s"))
3024
logger.addHandler(handler)
3125
logger.setLevel(logging.DEBUG)
3226
return logger

tests/conftest.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,16 @@ def anyio_backend(request):
2020
@pytest.fixture
2121
async def client() -> AsyncGenerator[AsyncClient]:
2222
transport = ASGITransport(
23-
app=app,
24-
)
23+
app=app,
24+
)
2525
async with AsyncClient(
2626
base_url="http://testserver",
2727
transport=transport,
2828
) as client:
2929
app.state.logger = get_logger(__name__)
30-
app.state.mongo_client, app.state.mongo_db, app.state.mongo_collection = (
31-
await init_mongo(
32-
global_settings.mongodb_test,
33-
global_settings.mongodb_url.unicode_string(),
34-
global_settings.mongodb_collection,
35-
)
30+
app.state.mongo_client, app.state.mongo_db, app.state.mongo_collection = await init_mongo(
31+
global_settings.mongodb_test,
32+
global_settings.mongodb_url.unicode_string(),
33+
global_settings.mongodb_collection,
3634
)
3735
yield client

tests/test_routers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ async def test_health_check(client: AsyncClient):
1313
"version": "7.0.8",
1414
"databases": ["admin", "config", "local"],
1515
"collections": {
16-
"admin": ["system.users", "system.version"],
16+
"admin": ["system.version", "system.users"],
1717
"config": ["system.sessions"],
1818
"local": ["startup_log"],
1919
},

tests/test_schemas.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,4 @@ def test_document_response_with_invalid_id(test_id, object_id, expected_exceptio
5858
with pytest.raises(expected_exception) as exc_info:
5959
DocumentResponse(id=object_id)
6060

61-
assert str(
62-
exc_info.value
63-
), f"Test case {test_id} failed: Expected exception {expected_exception} was not raised."
61+
assert str(exc_info.value), f"Test case {test_id} failed: Expected exception {expected_exception} was not raised."

0 commit comments

Comments
 (0)