Skip to content

Commit 5ab53c9

Browse files
author
Andrew Brookins
committed
Support both sync and asyncio uses
1 parent ca6ae7d commit 5ab53c9

File tree

10 files changed

+47
-70
lines changed

10 files changed

+47
-70
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,5 @@ data
133133
.install.stamp
134134

135135
# Sync version of the library, via Unasync
136-
redis_om/
136+
redis_om/
137+
tests_sync/

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ format: $(INSTALL_STAMP) sync
6262

6363
.PHONY: test
6464
test: $(INSTALL_STAMP) sync
65-
$(POETRY) run pytest -n auto -s -vv ./tests/ --cov-report term-missing --cov $(NAME) $(SYNC_NAME)
65+
$(POETRY) run pytest -n auto -vv ./tests/ ./tests_sync/ --cov-report term-missing --cov $(NAME) $(SYNC_NAME)
6666

6767
.PHONY: test_oss
6868
test_oss: $(INSTALL_STAMP) sync
6969
# Specifically tests against a local OSS Redis instance via
7070
# docker-compose.yml. Do not use this for CI testing, where we should
7171
# instead have a matrix of Docker images.
72-
REDIS_OM_URL="redis://localhost:6381" $(POETRY) run pytest -n auto -s -vv ./tests/ --cov-report term-missing --cov $(NAME)
72+
REDIS_OM_URL="redis://localhost:6381" $(POETRY) run pytest -n auto -vv ./tests/ ./tests_sync/ --cov-report term-missing --cov $(NAME)
7373

7474

7575
.PHONY: shell

aredis_om/model/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
from .migrations.migrator import MigrationError, Migrator
2-
from .model import EmbeddedJsonModel, Field, HashModel, JsonModel, RedisModel
2+
from .model import EmbeddedJsonModel, Field, HashModel, JsonModel, RedisModel, NotFoundError

aredis_om/model/migrations/migrator.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
from aioredis import Redis, ResponseError
88

9-
from aredis_om.model.model import model_registry
10-
119

1210
log = logging.getLogger(__name__)
1311

@@ -96,6 +94,10 @@ async def run(self):
9694
if self.module:
9795
import_submodules(self.module)
9896

97+
# Import this at run-time to avoid triggering import-time side effects,
98+
# e.g. checks for RedisJSON, etc.
99+
from aredis_om.model.model import model_registry
100+
99101
for name, cls in model_registry.items():
100102
hash_key = schema_hash_key(cls.Meta.index_name)
101103
try:

aredis_om/model/model.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from typing import (
1111
AbstractSet,
1212
Any,
13-
AsyncGenerator,
1413
Callable,
1514
Dict,
1615
List,
@@ -1295,7 +1294,7 @@ async def save(self, pipeline: Optional[Pipeline] = None) -> "HashModel":
12951294
return self
12961295

12971296
@classmethod
1298-
async def all_pks(cls) -> AsyncGenerator[str, None]: # type: ignore
1297+
async def all_pks(cls): # type: ignore
12991298
key_prefix = cls.make_key(cls._meta.primary_key_pattern.format(pk=""))
13001299
# TODO: We assume the key ends with the default separator, ":" -- when
13011300
# we make the separator configurable, we need to update this as well.
@@ -1437,13 +1436,16 @@ def schema_for_type(cls, name, typ: Any, field_info: PydanticFieldInfo):
14371436

14381437
class JsonModel(RedisModel, abc.ABC):
14391438
def __init_subclass__(cls, **kwargs):
1440-
if not has_redis_json(cls.db()):
1439+
# Generate the RediSearch schema once to validate fields.
1440+
cls.redisearch_schema()
1441+
1442+
def __init__(self, *args, **kwargs):
1443+
if not has_redis_json(self.db()):
14411444
log.error(
14421445
"Your Redis instance does not have the RedisJson module "
14431446
"loaded. JsonModel depends on RedisJson."
14441447
)
1445-
# Generate the RediSearch schema once to validate fields.
1446-
cls.redisearch_schema()
1448+
super().__init__(*args, **kwargs)
14471449

14481450
async def save(self, pipeline: Optional[Pipeline] = None) -> "JsonModel":
14491451
self.check()

make_sync.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,29 @@
33

44
import unasync
55

6+
ADDITIONAL_REPLACEMENTS = {
7+
"aredis_om": "redis_om",
8+
"aioredis": "redis",
9+
":tests.": ":tests_sync.",
10+
}
11+
612

713
def main():
8-
additional_replacements = {
9-
"aredis_om": "redis_om",
10-
"aioredis": "redis"
11-
}
1214
rules = [
1315
unasync.Rule(
1416
fromdir="/aredis_om/",
1517
todir="/redis_om/",
16-
additional_replacements=additional_replacements,
18+
additional_replacements=ADDITIONAL_REPLACEMENTS,
19+
),
20+
unasync.Rule(
21+
fromdir="/tests/",
22+
todir="/tests_sync/",
23+
additional_replacements=ADDITIONAL_REPLACEMENTS,
1724
),
1825
]
19-
2026
filepaths = []
2127
for root, _, filenames in os.walk(
22-
Path(__file__).absolute().parent / "aredis_om"
28+
Path(__file__).absolute().parent
2329
):
2430
for filename in filenames:
2531
if filename.rpartition(".")[-1] in ("py", "pyi",):

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ classifiers = [
1919
include=[
2020
"docs/*",
2121
"images/*",
22+
"redis_om/**/*",
2223
]
2324

2425
[tool.poetry.dependencies]

setup.py

-47
This file was deleted.

tests/test_hash_model.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@
1414
Migrator,
1515
QueryNotSupportedError,
1616
RedisModelError,
17-
has_redisearch,
1817
)
1918

19+
# We need to run this check as sync code (during tests) even in async mode
20+
# because we call it in the top-level module scope.
21+
from redis_om import has_redisearch
22+
2023

2124
if not has_redisearch():
2225
pytestmark = pytest.mark.skip
@@ -438,7 +441,11 @@ class Address(m.BaseHashModel):
438441
another_integer: int
439442
another_float: float
440443

444+
# We need to build the key prefix because it will differ based on whether
445+
# these tests were copied into the tests_sync folder and unasynce'd.
446+
key_prefix = Address.make_key(Address._meta.primary_key_pattern.format(pk=""))
447+
441448
assert (
442449
Address.redisearch_schema()
443-
== f"ON HASH PREFIX 1 {key_prefix}:tests.test_hash_model.Address: SCHEMA pk TAG SEPARATOR | a_string TAG SEPARATOR | a_full_text_string TAG SEPARATOR | a_full_text_string_fts TEXT an_integer NUMERIC SORTABLE a_float NUMERIC"
450+
== f"ON HASH PREFIX 1 {key_prefix} SCHEMA pk TAG SEPARATOR | a_string TAG SEPARATOR | a_full_text_string TAG SEPARATOR | a_full_text_string_fts TEXT an_integer NUMERIC SORTABLE a_float NUMERIC"
444451
)

0 commit comments

Comments
 (0)