Skip to content

Commit

Permalink
pass tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aaxelb committed Sep 9, 2022
1 parent 5d77519 commit a262459
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion elasticsearch_metrics/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 17 additions & 10 deletions tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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}
Expand All @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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"}
Expand Down

0 comments on commit a262459

Please sign in to comment.