Skip to content

Instantly share code, notes, and snippets.

@sirkuttin
Last active June 5, 2020 19:47
Show Gist options
  • Save sirkuttin/a4681f4e0fead682ec9411d0ad6cfbd3 to your computer and use it in GitHub Desktop.
Save sirkuttin/a4681f4e0fead682ec9411d0ad6cfbd3 to your computer and use it in GitHub Desktop.
dependency injection makes testing easier
# have a function to create the client so you can dependcy in ject it into the exceite function
# pass in host, port and region so your tests can override these
def create_es_client(host, port, region):
return Elasticsearch(
hosts=[{"host": host, "port": port}],
http_auth=BotoAWSRequestsAuth(
aws_host=host, aws_region=region, aws_service="es"
),
use_ssl=True,
verify_certs=True,
connection_class=RequestsHttpConnection,
)
# have a small function that exceutes your query
# pass in the es_client and the query so the values can be overidden in tests
def execute_es_query(es_client, query):
return query.using(es_client).execute(ignore_cache=True).to_dict()
# integration test.
def test_execute_query(self):
test_extent = {
"ymin": -105.2,
"ymax": -105.1,
"xmin": 35.7,
"xmax": 35.8,
}
test_tenant = "DEV"
test_timestamp = 12345
# use your normal create query function. this will test you real query
query = create_es_query(test_extent, test_tenant, test_timestamp)
# create a client that points to localstack just for testing
# if running tests inside a docker container, set the docker envar to ES_ENDPOINT=localstack
# if running the tests on your host machine, set envar to localhost when running tests (ES_ENDPOINT=localhost PORT=4571 pytest -rA tests/integration)
client = create_es_client(
os.environ["ES_ENPOINT"],
os.environ["PORT"],
os.environ["AWS_REGION"])
result = execute_es_query(client, query)
print(str(result))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment