matthewdeanmartin / openmock

Python Opensearch Mock for test purposes (fork of elasticmock)
https://pypi.python.org/pypi/openmock
MIT License
12 stars 11 forks source link

Don't work with pytest? #2

Closed muriloazevedotw closed 1 year ago

muriloazevedotw commented 1 year ago

I tried to use openmock with pytest, it's bit little different that the example on Readme, but I think should work.

Here's my code:

    @openmock
    def test_insert_report_data_on_opensearch(self, report_data):
        report_repository = ReportRepository()
        report_saved = report_repository.save(json.dumps(report_data))
        inserted_document = report_repository.client.get(report_saved[0]['_index'], report_saved[0]['_id'])
        assert inserted_document['_id'] == report_saved[0]['_id']

This test only pass if run opensearch server. Any suggestion?

matthewdeanmartin commented 1 year ago

Sorry just saw this. I can only guess because I can't see the rest of the code under test. Often if you import like this:

from elasticsearch import foo

The the mock can't intercept calls to the class/function because it has already been imported.

This is a common problem with all mock libraries in python.

mhkhodashahi commented 1 year ago

Is there a way to resolve it?

matthewdeanmartin commented 1 year ago

@mhkhodashahi I don't know what your specific problem is, maybe if you post some sample code on a new thread.

As for the OP's question,

Instead of

from foo import ReportRepository
# probably connect to real OpenSearch
ReportRepository()

Do this.

import foo
# probably connect to real OpenSearch
foo.ReportRepository()