Skip to content

Commit b989dbb

Browse files
committed
Added es 7 compatibility with es 8 client, just for tests
1 parent 841fdfc commit b989dbb

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ formatting: codestyle
4040
#* Linting
4141
.PHONY: test
4242
test:
43-
PYTHONPATH=$(PYTHONPATH) poetry run pytest -c pyproject.toml --cov-report=html --cov=pydastic tests/
43+
PYTHONPATH=$(PYTHONPATH) poetry run pytest -c pyproject.toml --cov-report html --cov-report xml --cov=pydastic tests/
4444
poetry run coverage-badge -o assets/images/coverage.svg -f
4545

4646
.PHONY: check-codestyle

assets/images/coverage.svg

+3-3
Loading

tests/conftest.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1+
import os
2+
import sys
3+
14
import pytest
25
from elasticsearch import Elasticsearch
36
from user import User
47

8+
# Enable backwards compatibility of es client 8.x.x with ES db 7.x.x
9+
os.environ["ELASTIC_CLIENT_APIVERSIONING"] = "1"
10+
511

612
@pytest.fixture()
713
def es() -> Elasticsearch:
8-
return Elasticsearch(hosts="http://localhost:9200", ssl_show_warn=False, headers={"Accept": "application/json", "Content-Type": "application/json"})
14+
return Elasticsearch(hosts="http://localhost:9200", ssl_show_warn=False)
915

1016

1117
@pytest.fixture()

tests/test_model.py

+14
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ def test_model_from_es(es: Elasticsearch):
123123
assert user == user_from_es
124124

125125

126+
def test_model_from_es_empty_data():
127+
user = User.from_es({})
128+
assert user is None
129+
130+
126131
def test_model_from_es_invalid_format():
127132
res = {"does not": "include _source", "or": "_id"}
128133

@@ -139,6 +144,15 @@ def test_model_to_es(es: Elasticsearch):
139144
assert res["_source"] == es_from_user
140145

141146

147+
def test_model_to_es_with_exclude(es: Elasticsearch):
148+
user = User(name="Carla")
149+
user.save(es, wait_for=True)
150+
es_from_user = user.to_es(exclude={"last_login", "phone"})
151+
152+
# Check that id excluded and fields excluded
153+
assert es_from_user == {"name": "Carla"}
154+
155+
142156
def test_model_get(es: Elasticsearch):
143157
user = User(name="Jean", phone="128")
144158
user.save(es, wait_for=True)

0 commit comments

Comments
 (0)