Skip to content

Commit 2ffd4e6

Browse files
author
Andrew Brookins
committed
Run tests across multiple cores/CPUs
1 parent bd24050 commit 2ffd4e6

File tree

7 files changed

+368
-292
lines changed

7 files changed

+368
-292
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ format: $(INSTALL_STAMP)
4444

4545
.PHONY: test
4646
test: $(INSTALL_STAMP)
47-
$(POETRY) run pytest -s -vv ./tests/ --cov-report term-missing --cov $(NAME)
47+
$(POETRY) run pytest -n auto -s -vv ./tests/ --cov-report term-missing --cov $(NAME)
4848

4949
.PHONY: shell
5050
shell: $(INSTALL_STAMP)

poetry.lock

+54-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ flake8 = "^4.0.1"
2929
bandit = "^1.7.0"
3030
coverage = "^6.0.2"
3131
pytest-cov = "^3.0.0"
32+
pytest-xdist = "^2.4.0"
3233

3334

3435
[tool.poetry.scripts]

redis_developer/model/migrations/migrator.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ def create_index(index_name, schema, current_hash):
4747
try:
4848
redis.execute_command(f"ft.info {index_name}")
4949
except ResponseError:
50+
redis.execute_command(f"ft.create {index_name} {schema}")
51+
redis.set(schema_hash_key(index_name), current_hash)
52+
else:
5053
log.info("Index already exists, skipping. Index hash: %s", index_name)
51-
return
52-
redis.execute_command(f"ft.create {index_name} {schema}")
53-
redis.set(schema_hash_key(index_name), current_hash)
5454

5555

5656
class MigrationAction(Enum):
@@ -74,10 +74,16 @@ def run(self):
7474
self.drop()
7575

7676
def create(self):
77-
return create_index(self.index_name, self.schema, self.hash)
77+
try:
78+
return create_index(self.index_name, self.schema, self.hash)
79+
except ResponseError:
80+
log.info("Index already exists: %s", self.index_name)
7881

7982
def drop(self):
80-
redis.execute_command(f"FT.DROPINDEX {self.index_name}")
83+
try:
84+
redis.execute_command(f"FT.DROPINDEX {self.index_name}")
85+
except ResponseError:
86+
log.info("Index does not exist: %s", self.index_name)
8187

8288

8389
class Migrator:

tests/conftest.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1+
import random
2+
13
import pytest
24
from redis import Redis
35

46
from redis_developer.connections import get_redis_connection
5-
from redis_developer.model.migrations.migrator import Migrator
6-
7-
8-
@pytest.fixture(scope="module", autouse=True)
9-
def migrations():
10-
Migrator().run()
117

128

139
@pytest.fixture
1410
def redis():
1511
yield get_redis_connection()
1612

1713

18-
@pytest.fixture
19-
def key_prefix():
20-
yield "redis-developer"
21-
22-
2314
def _delete_test_keys(prefix: str, conn: Redis):
15+
keys = []
2416
for key in conn.scan_iter(f"{prefix}:*"):
25-
conn.delete(key)
17+
keys.append(key)
18+
if keys:
19+
conn.delete(*keys)
20+
2621

22+
@pytest.fixture
23+
def key_prefix(redis):
24+
key_prefix = f"redis-developer:{random.random()}"
25+
yield key_prefix
26+
_delete_test_keys(key_prefix, redis)
2727

28-
@pytest.fixture(scope="function", autouse=True)
28+
@pytest.fixture(autouse=True)
2929
def delete_test_keys(redis, request, key_prefix):
3030
_delete_test_keys(key_prefix, redis)

0 commit comments

Comments
 (0)