Unit testing elastic search inside Django app

Viewed 4289

I have a django app that uses elastic search. I want to have 100% code test coverage so I need to test API calls to elasticsearch (which is "installed" locally).

So my question is this: is it better to mock whole elasticsearch or should I run elasticserver and check the results?

IMO It is better to mock elasticsearch and just check the python code (test if everything was called with the correct params).

2 Answers

My personal opinion is that tests that call the real Elasticsearch will generate more value and confidence than tests that mock it out. And it's very doable to setup test infrastructure in Django so that these tests are isolated:

from example.elasticsearch import search_media


def index_test_fixtures(es, index_name, data):
    created = es.index(index=index_name, body=data)
    assert created["result"] == "created"
    es.indices.refresh(index_name)


class TestElasticsearch:
    def test_elasticsearch(self, elasticsearch):
        index_test_fixtures(
            elasticsearch,
            "movie",
            {
                "slug": "episode-5",
                "title": "Star Wars: Episode V - The Empire Strikes Back",
                "description": "After the Rebels are brutally overpowered by the Empire on the ice planet Hoth, Luke Skywalker begins Jedi training with Yoda, while his friends are pursued by Darth Vader and a bounty hunter named Boba Fett all over the galaxy.",
            },
        )

        results = search_media("Star Wars")
        assert results[0]["slug"] == "episode-5"

I wrote a bit about how to setup all the configuration (docker-compose, pytest, elasticsearch, etc) in my personal blog: https://yanglinzhao.com/posts/test-elasticsearch-in-django. There's also a working example demo https://github.com/yanglinz/django-pytest-elasticsearch-example if that helps.

Related