django-es / django-elasticsearch-dsl

This is a package that allows indexing of django models in elasticsearch with elasticsearch-dsl-py.
Other
1.02k stars 264 forks source link

Ability to have Test instance database #385

Open dev-msln opened 2 years ago

dev-msln commented 2 years ago

As you know, in the Django Settings you can specify a test database for Postgresql as follows:

DATABASES = {
    'test_db': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': "some_name",
        'HOST': "127.0.0.1",
        ...
        'TEST': {
            ...
        }

    }
}

Is there a way to specify a test database in elastic-search settings so that all tests data will be inserted into that and after tests ran, all data will be removed from it?

adriantomas commented 2 years ago

@mojtabasalehiyan I use the following fixture in my test class:

@pytest.fixture(autouse=True, scope="function")
def rebuild_indices(self) -> Generator:
      for index in registry.get_indices():
          index.delete(ignore=404)
          index.create()