How to test a JSONModel in a unittest?

86 views Asked by At

A very trivial question: How does one test a simple JsonModel without connection to a redis instance?

from pydantic import EmailStr
from redis_om import (Field, JsonModel)

class Person(JsonModel):
    id: str = Field(index=True)
    email: EmailStr = Field(index=True)

The this test results in: redis.exceptions.ConnectionError: Error 10061 connecting to localhost:6379. No connection could be made because the target machine actively refused it.

import pytest
from models import Person

def test_person_model():        
    person= Person(id='123', email='[email protected]')

In short and very very simple: how does one properly unit-test models without having a redis instance at hand in unit test (and also later in the automated test-environment further down the CI/CD pipeline)?

-------WORKAROUND--------

The only way I would found this possible on localhost is to use a @pytest.fixture. Anyhow, that might NOT be available down the CI/CD pipline. This code will very likely fail on github actions runnin the unit tests. It does not help that github actions can bring up a redis service itself.

@pytest.fixture(scope="function", autouse=True)
def setup_redis():
    os.system('docker run -d --name redis-unit-test -p 6379:6379 -p 8009:8001 redis/redis-stack:latest ')
    # give some time for the Redis server to start
    time.sleep(2)
    yield
    os.system('docker stop redis-unit-test')
    os.system('docker rm redis-unit-test')
0

There are 0 answers