Skip to content

Commit 99b7800

Browse files
committed
Make ruff happy.
1 parent 16fefaf commit 99b7800

15 files changed

+117
-40
lines changed

trolldb/api/routes/databases.py

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
response_model=list[str],
2525
summary="Gets the list of all database names")
2626
async def database_names(exclude_defaults: bool = exclude_defaults_query) -> list[str]:
27+
"""TODO."""
2728
db_names = await MongoDB.list_database_names()
2829

2930
if not exclude_defaults:
@@ -37,6 +38,7 @@ async def database_names(exclude_defaults: bool = exclude_defaults_query) -> lis
3738
responses=Databases.union().fastapi_descriptor,
3839
summary="Gets the list of all collection names for the given database name")
3940
async def collection_names(db: CheckDataBaseDependency) -> list[str]:
41+
"""TODO."""
4042
return await db.list_collection_names()
4143

4244

@@ -45,6 +47,7 @@ async def collection_names(db: CheckDataBaseDependency) -> list[str]:
4547
responses=database_collection_error_descriptor,
4648
summary="Gets the object ids of all documents for the given database and collection name")
4749
async def documents(collection: CheckCollectionDependency) -> list[str]:
50+
"""TODO."""
4851
return await get_ids(collection.find({}))
4952

5053

@@ -53,6 +56,7 @@ async def documents(collection: CheckCollectionDependency) -> list[str]:
5356
responses=database_collection_document_error_descriptor,
5457
summary="Gets the document content in json format given its object id, database, and collection name")
5558
async def document_by_id(collection: CheckCollectionDependency, _id: MongoObjectId) -> _DocumentType:
59+
"""TODO."""
5660
if document := await collection.find_one({"_id": _id}):
5761
return dict(document) | {"_id": str(_id)}
5862

trolldb/api/routes/datetime_.py

+5
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,19 @@
1616

1717

1818
class TimeModel(TypedDict):
19+
"""TODO."""
1920
_id: str
2021
_time: datetime
2122

2223

2324
class TimeEntry(TypedDict):
25+
"""TODO."""
2426
_min: TimeModel
2527
_max: TimeModel
2628

2729

2830
class ResponseModel(BaseModel):
31+
"""TODO."""
2932
start_time: TimeEntry
3033
end_time: TimeEntry
3134

@@ -38,6 +41,7 @@ class ResponseModel(BaseModel):
3841
responses=database_collection_error_descriptor,
3942
summary="Gets the the minimum and maximum values for the start and end times")
4043
async def datetime(collection: CheckCollectionDependency) -> ResponseModel:
44+
"""TODO."""
4145
agg_result = await collection.aggregate([{
4246
"$group": {
4347
"_id": None,
@@ -48,6 +52,7 @@ async def datetime(collection: CheckCollectionDependency) -> ResponseModel:
4852
}}]).next()
4953

5054
def _aux(query):
55+
"""TODO."""
5156
return get_id(collection.find_one(query))
5257

5358
return ResponseModel(

trolldb/api/routes/platforms.py

+1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@
1717
responses=database_collection_error_descriptor,
1818
summary="Gets the list of all platform names")
1919
async def platform_names(collection: CheckCollectionDependency) -> list[str]:
20+
"""TODO."""
2021
return await get_distinct_items_in_collection(collection, "platform_name")

trolldb/api/routes/queries.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
2222
summary="Gets the database UUIDs of the documents that match specifications determined by the query string")
2323
async def queries(
2424
collection: CheckCollectionDependency,
25-
platform: list[str] = Query(None),
26-
sensor: list[str] = Query(None),
27-
time_min: datetime.datetime = Query(None),
28-
time_max: datetime.datetime = Query(None)) -> list[str]:
25+
platform: list[str] = Query(default=None), # noqa: B008
26+
sensor: list[str] = Query(default=None), # noqa: B008
27+
time_min: datetime.datetime = Query(default=None), # noqa: B008
28+
time_max: datetime.datetime = Query(default=None)) -> list[str]: # noqa: B008
29+
"""TODO."""
2930
pipelines = Pipelines()
3031

3132
if platform:

trolldb/api/routes/root.py

+1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111

1212
@router.get("/", summary="The root route which is mainly used to check the status of connection")
1313
async def root() -> Response:
14+
"""TODO."""
1415
return Response(status_code=status.HTTP_200_OK)

trolldb/api/routes/sensors.py

+1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@
1717
responses=database_collection_error_descriptor,
1818
summary="Gets the list of all sensor names")
1919
async def sensor_names(collection: CheckCollectionDependency) -> list[str]:
20+
"""TODO."""
2021
return await get_distinct_items_in_collection(collection, "sensor")

trolldb/api/tests/conftest.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""TODO."""
2+
13
import pytest
24

35
from trolldb.api.api import server_process_context
@@ -7,13 +9,15 @@
79

810

911
@pytest.fixture(scope="session")
10-
def run_mongodb_server_instance():
12+
def _run_mongodb_server_instance():
13+
"""TODO."""
1114
with mongodb_instance_server_process_context():
1215
yield
1316

1417

15-
@pytest.fixture(scope="session", autouse=True)
16-
def test_server_fixture(run_mongodb_server_instance):
18+
@pytest.fixture(scope="session")
19+
def _test_server_fixture(_run_mongodb_server_instance):
20+
"""TODO."""
1721
TestDatabase.prepare()
1822
with server_process_context(test_app_config, startup_time=2000):
1923
yield

trolldb/api/tests/test_api.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,44 @@
1+
"""TODO."""
2+
import pytest
13
from fastapi import status
24

35
from trolldb.test_utils.common import assert_equal, http_get
46
from trolldb.test_utils.mongodb_database import TestDatabase, test_mongodb_context
57

68

9+
@pytest.mark.usefixtures("_test_server_fixture")
710
def test_root():
811
"""Checks that the server is up and running, i.e. the root routes responds with 200."""
912
assert_equal(http_get().status, status.HTTP_200_OK)
1013

1114

15+
@pytest.mark.usefixtures("_test_server_fixture")
1216
def test_platforms():
1317
"""Checks that the retrieved platform names match the expected names."""
1418
assert_equal(http_get("platforms").json(), TestDatabase.platform_names)
1519

1620

21+
@pytest.mark.usefixtures("_test_server_fixture")
1722
def test_sensors():
18-
"""Checks that the retrieved sensor names match the expected names.
19-
"""
23+
"""Checks that the retrieved sensor names match the expected names."""
2024
assert_equal(http_get("sensors").json(), TestDatabase.sensors)
2125

2226

27+
@pytest.mark.usefixtures("_test_server_fixture")
2328
def test_database_names():
2429
"""Checks that the retrieved database names match the expected names."""
2530
assert_equal(http_get("databases").json(), TestDatabase.database_names)
2631
assert_equal(http_get("databases?exclude_defaults=True").json(), TestDatabase.database_names)
2732
assert_equal(http_get("databases?exclude_defaults=False").json(), TestDatabase.all_database_names)
2833

2934

35+
@pytest.mark.usefixtures("_test_server_fixture")
3036
def test_database_names_negative():
3137
"""Checks that the non-existing databases cannot be found."""
3238
assert_equal(http_get("databases/non_existing_database").status, status.HTTP_404_NOT_FOUND)
3339

3440

41+
@pytest.mark.usefixtures("_test_server_fixture")
3542
def test_collections():
3643
"""Check the presence of existing collections and that the ids of documents therein can be correctly retrieved."""
3744
with test_mongodb_context() as client:
@@ -50,9 +57,10 @@ def test_collections():
5057
)
5158

5259

60+
@pytest.mark.usefixtures("_test_server_fixture")
5361
def test_collections_negative():
5462
"""Checks that the non-existing collections cannot be found."""
55-
for database_name, collection_name in zip(TestDatabase.database_names, TestDatabase.collection_names, strict=False):
63+
for database_name in TestDatabase.database_names:
5664
assert_equal(
5765
http_get(f"databases/{database_name}/non_existing_collection").status,
5866
status.HTTP_404_NOT_FOUND

trolldb/database/errors.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
"""The modules which defines the error responses that might occur while working with the
2-
MongoDB database.
1+
"""The modules which defines the error responses that might occur while working with the MongoDB database.
32
43
Note:
54
The error responses are grouped into classes, with each class representing the major
@@ -79,14 +78,13 @@ class Documents(ResponsesErrorGroup):
7978
database_collection_error_descriptor = (
8079
Databases.union() | Collections.union()
8180
).fastapi_descriptor
82-
"""
83-
A response descriptor for the Fast API routes. This combines all the error messages that might
84-
occur as result of working with databases and collections. See the fast api documentation for TODO.
81+
"""A response descriptor for the Fast API routes.
82+
83+
This combines all the error messages that might occur as result of working with databases and collections. See the
84+
fast api documentation for TODO.
8585
"""
8686

8787
database_collection_document_error_descriptor = (
8888
Databases.union() | Collections.union() | Documents.union()
8989
).fastapi_descriptor
90-
"""
91-
Same as :obj:`database_collection_error_descriptor` but including documents as well.
92-
"""
90+
"""Same as :obj:`database_collection_error_descriptor` but including documents as well."""

trolldb/database/piplines.py

+15
Original file line numberDiff line numberDiff line change
@@ -21,45 +21,60 @@ class PipelineDict(dict):
2121
"""
2222

2323
def __or__(self, other: Self):
24+
"""TODO."""
2425
return PipelineDict({"$or": [self, other]})
2526

2627
def __and__(self, other: Self):
28+
"""TODO."""
2729
return PipelineDict({"$and": [self, other]})
2830

2931

3032
class PipelineAttribute:
33+
"""TODO."""
34+
3135
def __init__(self, key: str):
36+
"""TODO."""
3237
self.__key = key
3338

3439
def __eq__(self, other: Any) -> PipelineDict:
40+
"""TODO."""
3541
if isinstance(other, list):
3642
return PipelineDict(**{"$or": [{self.__key: v} for v in other]})
3743
return PipelineDict(**{self.__key: other})
3844

3945
def __aux_operators(self, other: Any, operator: str) -> PipelineDict:
46+
"""TODO."""
4047
return PipelineDict(**{self.__key: {operator: other}} if other else {})
4148

4249
def __ge__(self, other: Any) -> PipelineDict:
50+
"""TODO."""
4351
return self.__aux_operators(other, "$gte")
4452

4553
def __gt__(self, other: Any) -> PipelineDict:
54+
"""TODO."""
4655
return self.__aux_operators(other, "$gt")
4756

4857
def __le__(self, other: Any) -> PipelineDict:
58+
"""TODO."""
4959
return self.__aux_operators(other, "$lte")
5060

5161
def __lt__(self, other: Any) -> PipelineDict:
62+
"""TODO."""
5263
return self.__aux_operators(other, "$le")
5364

5465

5566
class Pipelines(list):
67+
"""TODO."""
5668
def __init__(self, *args, **kwargs):
69+
"""TODO."""
5770
super().__init__(*args, **kwargs)
5871

5972
def __iadd__(self, other):
73+
"""TODO."""
6074
self.extend([{"$match": other}])
6175
return self
6276

6377
def __add__(self, other):
78+
"""TODO."""
6479
self.append({"$match": other})
6580
return self

trolldb/errors/errors.py

+18-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""The module which defines the base functionality for error responses that will be returned by the API.
2+
23
This module only includes the generic utilities using which each module should define its own error responses
34
specifically. See :obj:`trolldb.database.errors` as an example on how this module is used.
45
"""
@@ -18,19 +19,18 @@ class ResponseError(Exception):
1819
"""The base class for all error responses. This is derivative of the ``Exception`` class."""
1920

2021
descriptor_delimiter: str = " |OR| "
21-
"""
22-
A delimiter to combine the message part of several error responses into a single one. This will be shown in textual
23-
format for the response descriptors of the Fast API routes. For example:
22+
"""A delimiter to combine the message part of several error responses into a single one.
23+
24+
This will be shown in textual format for the response descriptors of the Fast API routes. For example:
2425
2526
``ErrorA |OR| ErrorB``
2627
"""
2728

28-
defaultResponseClass: Response = PlainTextResponse
29-
"""
30-
The default type of the response which will be returned when an error occurs.
31-
"""
29+
DefaultResponseClass: Response = PlainTextResponse
30+
"""The default type of the response which will be returned when an error occurs."""
3231

3332
def __init__(self, args_dict: OrderedDict[StatusCode, str | list[str]] | dict) -> None:
33+
"""TODO."""
3434
self.__dict: OrderedDict = OrderedDict(args_dict)
3535
self.extra_information: dict | None = None
3636

@@ -64,6 +64,7 @@ def __or__(self, other: Self):
6464
def __assert_existence_multiple_response_codes(
6565
self,
6666
status_code: StatusCode | None = None) -> (StatusCode, str):
67+
"""TODO."""
6768
match status_code, len(self.__dict):
6869
case None, n if n > 1:
6970
raise ValueError("In case of multiple response status codes, the status code must be specified.")
@@ -80,6 +81,7 @@ def get_error_details(
8081
self,
8182
extra_information: dict | None = None,
8283
status_code: int | None = None) -> (StatusCode, str):
84+
"""TODO."""
8385
status_code, msg = self.__assert_existence_multiple_response_codes(status_code)
8486
return (
8587
status_code,
@@ -91,6 +93,7 @@ def sys_exit_log(
9193
exit_code: int = -1,
9294
extra_information: dict | None = None,
9395
status_code: int | None = None) -> None:
96+
"""TODO."""
9497
msg, _ = self.get_error_details(extra_information, status_code)
9598
logger.error(msg)
9699
exit(exit_code)
@@ -99,32 +102,39 @@ def log_as_warning(
99102
self,
100103
extra_information: dict | None = None,
101104
status_code: int | None = None):
105+
"""TODO."""
102106
msg, _ = self.get_error_details(extra_information, status_code)
103107
logger.warning(msg)
104108

105109
@property
106110
def fastapi_descriptor(self) -> dict[StatusCode, dict[Literal["description"], str]]:
111+
"""TODO."""
107112
return {status: {Literal["description"]: ResponseError.__stringify(msg)} for status, msg in self.__dict.items()}
108113

109114
@staticmethod
110115
def __listify(item: str | list[str]) -> list[str]:
116+
"""TODO."""
111117
return item if isinstance(item, list) else [item]
112118

113119
@staticmethod
114120
def __stringify(item: str | list[str]) -> str:
121+
"""TODO."""
115122
return ResponseError.descriptor_delimiter.join(ResponseError.__listify(item))
116123

117124

118125
class ResponsesErrorGroup:
126+
"""TODO."""
119127

120128
@classmethod
121129
def fields(cls):
130+
"""TODO."""
122131
return {k: v for k, v in cls.__dict__.items() if isinstance(v, ResponseError)}
123132

124133
@classmethod
125134
def union(cls):
135+
"""TODO."""
126136
buff = None
127-
for k, v in cls.fields().items():
137+
for v in cls.fields().values():
128138
if buff is None:
129139
buff = v
130140
else:

trolldb/run_api.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""The main entry point to run the API server according to the configurations given in `config.yaml`
1+
"""The main entry point to run the API server according to the configurations given in `config.yaml`.
22
33
Note:
44
For more information on the API server, see the automatically generated documentation by FastAPI.

0 commit comments

Comments
 (0)