Skip to content

Commit 53d4839

Browse files
author
Michael Kryukov
committed
refactor: applied ruff to sources; updated Makefile
1 parent a1738be commit 53d4839

12 files changed

+64
-36
lines changed

Makefile

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
.PHONY: all
22

3-
all: lint test
3+
all: check test
44

5-
lint:
6-
flake8 mongomock_motor/ tests/ --count --select=E9,F63,F7,F82 --show-source --statistics
7-
flake8 mongomock_motor/ tests/ --count --max-complexity=10 --max-line-length=127 --statistics
5+
check:
6+
python3 -m ruff check mongomock_motor/ tests/ && \
7+
python3 -m ruff format --check mongomock_motor/ tests/
8+
9+
format:
10+
python3 -m ruff check --fix mongomock_motor/ tests/ && \
11+
python3 -m ruff format mongomock_motor/ tests/
812

913
test:
1014
ENVIRONMENT=test python3 -m pytest tests/

ruff.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
target-version = "py38"
22

3+
[lint]
4+
select = ["E4", "E7", "E9", "F", "I"]
5+
36
[format]
47
quote-style = "single"

tests/test_async_cursor.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pymongo
22
import pytest
3-
from mongomock_motor import AsyncMongoMockClient
43

4+
from mongomock_motor import AsyncMongoMockClient
55

66
EXPECTED_DOCUMENTS_COUNT = 10
77

@@ -31,8 +31,7 @@ async def test_skip_and_limit():
3131

3232
# Query with limit, skip and sort
3333
docs = await (
34-
collection
35-
.find(projection={'_id': 0})
34+
collection.find(projection={'_id': 0})
3635
.skip(2)
3736
.limit(2)
3837
.sort('i', pymongo.DESCENDING)

tests/test_attrs.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
from pymongo import ReadPreference
21
import pytest
2+
from pymongo import ReadPreference
3+
34
from mongomock_motor import AsyncMongoMockClient
45

56

@@ -10,7 +11,9 @@ async def test_attrs():
1011
collection = database.get_collection('test')
1112
await collection.insert_one({'a': 1})
1213

13-
assert await client['tests']['test'].find_one(projection={'_id': 0, 'a': 1}) == {'a': 1}
14+
assert await client['tests']['test'].find_one(projection={'_id': 0, 'a': 1}) == {
15+
'a': 1
16+
}
1417
assert await client.tests.test.find_one(projection={'_id': 0, 'a': 1}) == {'a': 1}
1518
assert await collection.find_one(projection={'_id': 0, 'a': 1}) == {'a': 1}
1619

tests/test_beanie.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from typing import Optional
2-
from beanie import Document, Link, WriteRules, Indexed, init_beanie
3-
from pydantic import BaseModel
2+
43
import pytest
4+
from beanie import Document, Indexed, Link, WriteRules, init_beanie
5+
from pydantic import BaseModel
6+
57
from mongomock_motor import AsyncMongoMockClient
68

79

@@ -19,11 +21,15 @@ class Product(Document):
1921

2022
@pytest.mark.anyio
2123
async def test_beanie():
22-
client = AsyncMongoMockClient('mongodb://user:pass@host:27017', connectTimeoutMS=250)
24+
client = AsyncMongoMockClient(
25+
'mongodb://user:pass@host:27017', connectTimeoutMS=250
26+
)
2327

2428
await init_beanie(database=client.beanie_test, document_models=[Product])
2529

26-
chocolate = Category(name='Chocolate', description='A preparation of roasted and ground cacao seeds.')
30+
chocolate = Category(
31+
name='Chocolate', description='A preparation of roasted and ground cacao seeds.'
32+
)
2733

2834
tonybar = Product(name="Tony's", price=5.95, category=chocolate)
2935
await tonybar.insert()
@@ -55,7 +61,9 @@ class House(Document):
5561

5662
@pytest.mark.anyio
5763
async def test_beanie_links():
58-
client = AsyncMongoMockClient('mongodb://user:pass@host:27017', connectTimeoutMS=250)
64+
client = AsyncMongoMockClient(
65+
'mongodb://user:pass@host:27017', connectTimeoutMS=250
66+
)
5967

6068
await init_beanie(database=client.beanie_test, document_models=[Door, House])
6169

tests/test_hashable.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ async def test_hashing_is_possible():
1212

1313
@pytest.mark.anyio
1414
async def test_hashing_is_stable():
15-
assert hash(AsyncMongoMockClient()) == hash(AsyncMongoMockClient())
16-
assert hash(AsyncMongoMockClient('localhost')) == hash(
17-
AsyncMongoMockClient('not.localhost')
18-
)
19-
assert hash(AsyncMongoMockClient()['database1']) == hash(
20-
AsyncMongoMockClient()['database1']
21-
)
22-
assert hash(AsyncMongoMockClient()['database1']) != hash(
23-
AsyncMongoMockClient()['database2']
24-
)
15+
client1 = AsyncMongoMockClient()
16+
client2 = AsyncMongoMockClient()
17+
client3 = AsyncMongoMockClient('not.localhost')
18+
19+
assert hash(client1) == hash(client2)
20+
assert hash(client1) != hash(client3)
21+
assert hash(client2) != hash(client3)
22+
23+
assert hash(client1['database1']) == hash(client2['database1'])
24+
assert hash(client1['database1']) != hash(client2['database2'])

tests/test_list_names.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
23
from mongomock_motor import AsyncMongoMockClient
34

45

tests/test_mocking.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from unittest.mock import patch
2+
3+
import pytest
24
from bson import ObjectId
35
from pymongo.results import UpdateResult
4-
import pytest
6+
57
from mongomock_motor import AsyncMongoMockClient
68

79

@@ -20,14 +22,18 @@ async def sample_function(collection):
2022
def async_wrapper(value):
2123
async def wrapper(*args, **kwargs):
2224
return value
25+
2326
return wrapper
2427

2528

2629
@pytest.mark.anyio
2730
async def test_patch():
2831
collection = AsyncMongoMockClient()['test']['test']
2932

30-
with patch('mongomock_motor.AsyncMongoMockCollection.update_one', new=async_wrapper(UpdateResult({}, False))):
33+
with patch(
34+
'mongomock_motor.AsyncMongoMockCollection.update_one',
35+
new=async_wrapper(UpdateResult({}, False)),
36+
):
3137
with pytest.raises(RuntimeError):
3238
await sample_function(collection)
3339

@@ -36,6 +42,8 @@ async def test_patch():
3642
async def test_patch_object():
3743
collection = AsyncMongoMockClient()['test']['test']
3844

39-
with patch.object(collection, 'update_one', new=async_wrapper(UpdateResult({}, False))):
45+
with patch.object(
46+
collection, 'update_one', new=async_wrapper(UpdateResult({}, False))
47+
):
4048
with pytest.raises(RuntimeError):
4149
await sample_function(collection)

tests/test_showcase.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
23
from mongomock_motor import AsyncMongoMockClient
34

45

tests/test_umongo.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ class Meta:
2626
something1 = Something(field1='A', field2=':)', related=[])
2727
await something1.commit()
2828

29-
with pytest.raises(ValidationError, match="^{'field1': 'Field value must be unique.'}$"):
29+
with pytest.raises(
30+
ValidationError, match="^{'field1': 'Field value must be unique.'}$"
31+
):
3032
duplicate = Something(field1='A', field2=':)', related=[])
3133
await duplicate.commit()
3234

tests/test_unique.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22
from pymongo.errors import DuplicateKeyError
3+
34
from mongomock_motor import AsyncMongoMockClient
45

56

tests/test_workflow.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
from datetime import datetime, timezone
2+
13
import bson
24
import pytest
3-
from datetime import datetime, timezone
45
from pymongo import ReplaceOne
6+
57
from mongomock_motor import AsyncMongoMockClient
68

79

@@ -56,11 +58,7 @@ async def test_tz_awareness():
5658
@pytest.mark.anyio
5759
async def test_bulk_write():
5860
collection = AsyncMongoMockClient()['tests']['test']
59-
result = await collection.bulk_write([
60-
ReplaceOne(
61-
filter={'_id': 1},
62-
replacement={'_id': 1},
63-
upsert=True
64-
)
65-
])
61+
result = await collection.bulk_write(
62+
[ReplaceOne(filter={'_id': 1}, replacement={'_id': 1}, upsert=True)]
63+
)
6664
assert result.bulk_api_result['nUpserted'] == 1

0 commit comments

Comments
 (0)