From a262459ef0a1133081d915fb5207bfc8027f0890 Mon Sep 17 00:00:00 2001 From: Abram Booth Date: Fri, 9 Sep 2022 17:04:44 -0400 Subject: [PATCH] pass tests --- elasticsearch_metrics/metrics.py | 2 +- tests/conftest.py | 2 +- tests/test_metrics.py | 27 +++++++++++++++++---------- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/elasticsearch_metrics/metrics.py b/elasticsearch_metrics/metrics.py index ed1221a..cbefe56 100644 --- a/elasticsearch_metrics/metrics.py +++ b/elasticsearch_metrics/metrics.py @@ -164,7 +164,7 @@ def check_index_template(cls, using=None): """ client = connections.get_connection(using or "default") try: - template = client.indices.get_template(cls._template_name) + template = client.indices.get_template(name=cls._template_name) except NotFoundError as client_error: template_name = cls._template_name metric_name = cls.__name__ diff --git a/tests/conftest.py b/tests/conftest.py index ea95af8..f042419 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -19,7 +19,7 @@ def _es_marker(request, client): def teardown_es(): client.indices.delete(index="*") - client.indices.delete_template("*") + client.indices.delete_template(name="*") teardown_es() yield diff --git a/tests/test_metrics.py b/tests/test_metrics.py index 8fe1ed4..75d0eab 100644 --- a/tests/test_metrics.py +++ b/tests/test_metrics.py @@ -27,6 +27,11 @@ ) +# support elasticsearch-dsl 6 and 7 +def mappings_fortytwo(mapping): + return mapping.get("doc", mapping) + + class PreprintView(metrics.Metric): provider_id = metrics.Keyword(index=True) user_id = metrics.Keyword(index=True) @@ -95,9 +100,9 @@ def test_get_index_template_respects_index_settings(self): def test_get_index_template_creates_template_with_mapping(self): template = PreprintView.get_index_template() - mappings = template.to_dict()["mappings"] - assert mappings["doc"]["_source"]["enabled"] is False - properties = mappings["doc"]["properties"] + mappings = mappings_fortytwo(template.to_dict()["mappings"]) + assert mappings["_source"]["enabled"] is False + properties = mappings["properties"] assert "timestamp" in properties assert properties["timestamp"] == {"doc_values": True, "type": "date"} assert properties["provider_id"] == {"type": "keyword", "index": True} @@ -108,10 +113,12 @@ def test_get_index_template_creates_template_with_mapping(self): def test_mappings_are_not_shared(self): template1 = DummyMetric.get_index_template() template2 = DummyMetricWithExplicitTemplateName.get_index_template() - assert "my_int" in template1.to_dict()["mappings"]["doc"]["properties"] - assert "my_keyword" not in template1.to_dict()["mappings"]["doc"]["properties"] - assert "my_int" not in template2.to_dict()["mappings"]["doc"]["properties"] - assert "my_keyword" in template2.to_dict()["mappings"]["doc"]["properties"] + mappings1 = mappings_fortytwo(template1.to_dict()["mappings"]) + mappings2 = mappings_fortytwo(template2.to_dict()["mappings"]) + assert "my_int" in mappings1["properties"] + assert "my_keyword" not in mappings1["properties"] + assert "my_int" not in mappings2["properties"] + assert "my_keyword" in mappings2["properties"] def test_declaring_metric_with_no_app_label_or_template_name_errors(self): with pytest.raises(RuntimeError): @@ -189,8 +196,8 @@ class Meta: template = MyMetric.get_index_template() template_dict = template.to_dict() - doc = template_dict["mappings"]["doc"] - assert doc["_source"]["enabled"] is True + mappings = mappings_fortytwo(template_dict["mappings"]) + assert mappings["_source"]["enabled"] is True class TestRecord: @@ -264,7 +271,7 @@ def test_init(self, client): PreprintView.init() name = PreprintView.get_index_name() mapping = client.indices.get_mapping(index=name) - properties = mapping[name]["mappings"]["doc"]["properties"] + properties = mappings_fortytwo(mapping[name]["mappings"])["properties"] assert properties["timestamp"] == {"type": "date"} assert properties["provider_id"] == {"type": "keyword"} assert properties["user_id"] == {"type": "keyword"}