Skip to content

Commit 490fcec

Browse files
committed
Fix DeprecationWarning '@pytest.fixture'
1 parent 00ed537 commit 490fcec

8 files changed

+64
-16
lines changed

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ clean:
3333
rm -rf build
3434
rm -rf dist
3535
rm -rf redis_om
36+
rm -rf tests_sync
3637
docker-compose down
3738

3839

aredis_om/connections.py

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import aioredis
44

5+
56
URL = os.environ.get("REDIS_OM_URL", None)
67

78

make_sync.py

+42-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,52 @@
11
import os
22
from pathlib import Path
3+
from typing import Iterable, Optional, Union
34

45
import unasync
56

67
ADDITIONAL_REPLACEMENTS = {
78
"aredis_om": "redis_om",
89
"aioredis": "redis",
910
":tests.": ":tests_sync.",
11+
"pytest_asyncio": "pytest",
1012
}
1113

14+
STRINGS_TO_REMOVE_FROM_SYNC_TESTS = {
15+
"@pytest.mark.asyncio",
16+
}
17+
18+
19+
def remove_strings_from_files(
20+
filepaths: Iterable[Union[bytes, str, os.PathLike]],
21+
strings_to_remove: Iterable[str],
22+
):
23+
for filepath in filepaths:
24+
tmp_filepath = f"{filepath}.tmp"
25+
with open(filepath, "r") as read_file, open(tmp_filepath, "w") as write_file:
26+
for line in read_file:
27+
if line.strip() in strings_to_remove:
28+
continue
29+
print(line, end="", file=write_file)
30+
os.replace(tmp_filepath, filepath)
31+
32+
33+
def get_source_filepaths(directory: Optional[Union[bytes, str, os.PathLike]] = None):
34+
walk_path = (
35+
Path(__file__).absolute().parent
36+
if directory is None
37+
else os.path.join(Path(__file__).absolute().parent, directory)
38+
)
39+
40+
filepaths = []
41+
for root, _, filenames in os.walk(walk_path):
42+
for filename in filenames:
43+
if filename.rpartition(".")[-1] in (
44+
"py",
45+
"pyi",
46+
):
47+
filepaths.append(os.path.join(root, filename))
48+
return filepaths
49+
1250

1351
def main():
1452
rules = [
@@ -23,15 +61,11 @@ def main():
2361
additional_replacements=ADDITIONAL_REPLACEMENTS,
2462
),
2563
]
26-
filepaths = []
27-
for root, _, filenames in os.walk(
28-
Path(__file__).absolute().parent
29-
):
30-
for filename in filenames:
31-
if filename.rpartition(".")[-1] in ("py", "pyi",):
32-
filepaths.append(os.path.join(root, filename))
3364

34-
unasync.unasync_files(filepaths, rules)
65+
unasync.unasync_files(get_source_filepaths(), rules)
66+
remove_strings_from_files(
67+
get_source_filepaths("tests_sync"), STRINGS_TO_REMOVE_FROM_SYNC_TESTS
68+
)
3569

3670

3771
if __name__ == "__main__":

pytest.ini

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[pytest]
2+
asyncio_mode = strict

tests/test_hash_model.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from unittest import mock
1010

1111
import pytest
12+
import pytest_asyncio
1213
from pydantic import ValidationError
1314

1415
from aredis_om import (
@@ -30,7 +31,7 @@
3031
today = datetime.date.today()
3132

3233

33-
@pytest.fixture
34+
@pytest_asyncio.fixture
3435
async def m(key_prefix, redis):
3536
class BaseHashModel(HashModel, abc.ABC):
3637
class Meta:
@@ -60,7 +61,7 @@ class Meta:
6061
)
6162

6263

63-
@pytest.fixture
64+
@pytest_asyncio.fixture
6465
async def members(m):
6566
member1 = m.Member(
6667
first_name="Andrew",
@@ -357,6 +358,7 @@ def test_validation_passes(m):
357358
)
358359
assert member.first_name == "Andrew"
359360

361+
360362
@pytest.mark.asyncio
361363
async def test_retrieve_first(m):
362364
member = m.Member(
@@ -395,6 +397,7 @@ async def test_retrieve_first(m):
395397
first_one = await m.Member.find().sort_by("age").first()
396398
assert first_one == member3
397399

400+
398401
@pytest.mark.asyncio
399402
async def test_saves_model_and_creates_pk(m):
400403
member = m.Member(
@@ -411,6 +414,7 @@ async def test_saves_model_and_creates_pk(m):
411414
member2 = await m.Member.get(member.pk)
412415
assert member2 == member
413416

417+
414418
@pytest.mark.asyncio
415419
async def test_all_pks(m):
416420
member = m.Member(
@@ -441,6 +445,7 @@ async def test_all_pks(m):
441445

442446
assert len(pk_list) == 2
443447

448+
444449
@pytest.mark.asyncio
445450
async def test_delete(m):
446451
member = m.Member(

tests/test_json_model.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from unittest import mock
1010

1111
import pytest
12+
import pytest_asyncio
1213
from pydantic import ValidationError
1314

1415
from aredis_om import (
@@ -32,7 +33,7 @@
3233
today = datetime.date.today()
3334

3435

35-
@pytest.fixture
36+
@pytest_asyncio.fixture
3637
async def m(key_prefix, redis):
3738
class BaseJsonModel(JsonModel, abc.ABC):
3839
class Meta:
@@ -94,7 +95,7 @@ def address(m):
9495
)
9596

9697

97-
@pytest.fixture()
98+
@pytest_asyncio.fixture()
9899
async def members(address, m):
99100
member1 = m.Member(
100101
first_name="Andrew",
@@ -186,6 +187,7 @@ async def test_saves_model_and_creates_pk(address, m, redis):
186187
assert member2 == member
187188
assert member2.address == address
188189

190+
189191
@pytest.mark.asyncio
190192
async def test_all_pks(address, m, redis):
191193
member = m.Member(
@@ -197,7 +199,7 @@ async def test_all_pks(address, m, redis):
197199
address=address,
198200
)
199201

200-
await member.save()
202+
await member.save()
201203

202204
member1 = m.Member(
203205
first_name="Simon",
@@ -216,6 +218,7 @@ async def test_all_pks(address, m, redis):
216218

217219
assert len(pk_list) == 2
218220

221+
219222
@pytest.mark.asyncio
220223
async def test_delete(address, m, redis):
221224
member = m.Member(

tests/test_oss_redis_features.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing import Optional
66

77
import pytest
8+
import pytest_asyncio
89
from pydantic import ValidationError
910

1011
from aredis_om import HashModel, Migrator, NotFoundError, RedisModelError
@@ -13,7 +14,7 @@
1314
today = datetime.date.today()
1415

1516

16-
@pytest.fixture
17+
@pytest_asyncio.fixture
1718
async def m(key_prefix, redis):
1819
class BaseHashModel(HashModel, abc.ABC):
1920
class Meta:
@@ -42,7 +43,7 @@ class Meta:
4243
)
4344

4445

45-
@pytest.fixture
46+
@pytest_asyncio.fixture
4647
async def members(m):
4748
member1 = m.Member(
4849
first_name="Andrew",

tests/test_pydantic_integrations.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from collections import namedtuple
44

55
import pytest
6+
import pytest_asyncio
67
from pydantic import EmailStr, ValidationError
78

89
from aredis_om import Field, HashModel, Migrator
@@ -11,7 +12,7 @@
1112
today = datetime.date.today()
1213

1314

14-
@pytest.fixture
15+
@pytest_asyncio.fixture
1516
async def m(key_prefix, redis):
1617
class BaseHashModel(HashModel, abc.ABC):
1718
class Meta:

0 commit comments

Comments
 (0)