Skip to content

Commit 740005d

Browse files
committed
Make pyarrow dependency optional for tests
1 parent cb389d9 commit 740005d

File tree

3 files changed

+48
-50
lines changed

3 files changed

+48
-50
lines changed

test_elasticsearch/test_client/test_deprecated_options.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,24 @@
2222
from elasticsearch import Elasticsearch, JsonSerializer
2323

2424

25+
EXPECTED_SERIALIZERS = {
26+
"application/vnd.mapbox-vector-tile",
27+
"application/x-ndjson",
28+
"application/json",
29+
"text/*",
30+
"application/vnd.elasticsearch+json",
31+
"application/vnd.elasticsearch+x-ndjson",
32+
}
33+
34+
35+
try:
36+
import pyarrow as pa
37+
EXPECTED_SERIALIZERS.add("application/vnd.apache.arrow.stream")
38+
except ImportError:
39+
pa = None
40+
41+
42+
2543
def test_sniff_on_connection_fail():
2644
with warnings.catch_warnings(record=True) as w:
2745
client = Elasticsearch("http://localhost:9200", sniff_on_connection_fail=True)
@@ -129,15 +147,7 @@ class CustomSerializer(JsonSerializer):
129147
client.transport.serializers.get_serializer("application/json"),
130148
CustomSerializer,
131149
)
132-
assert set(client.transport.serializers.serializers.keys()) == {
133-
"application/vnd.mapbox-vector-tile",
134-
"application/x-ndjson",
135-
"application/json",
136-
"text/*",
137-
"application/vnd.apache.arrow.stream",
138-
"application/vnd.elasticsearch+json",
139-
"application/vnd.elasticsearch+x-ndjson",
140-
}
150+
assert set(client.transport.serializers.serializers.keys()) == EXPECTED_SERIALIZERS
141151

142152
client = Elasticsearch(
143153
"http://localhost:9200",
@@ -150,13 +160,5 @@ class CustomSerializer(JsonSerializer):
150160
client.transport.serializers.get_serializer("application/json"),
151161
CustomSerializer,
152162
)
153-
assert set(client.transport.serializers.serializers.keys()) == {
154-
"application/vnd.mapbox-vector-tile",
155-
"application/x-ndjson",
156-
"application/json",
157-
"text/*",
158-
"application/vnd.apache.arrow.stream",
159-
"application/vnd.elasticsearch+json",
160-
"application/vnd.elasticsearch+x-ndjson",
161-
"application/cbor",
162-
}
163+
expected = EXPECTED_SERIALIZERS | {"application/cbor"}
164+
assert set(client.transport.serializers.serializers.keys()) == expected

test_elasticsearch/test_client/test_serializers.py

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,23 @@
2121
from test_elasticsearch.test_cases import DummyTransportTestCase
2222

2323

24+
EXPECTED_SERIALIZERS = {
25+
"application/json",
26+
"text/*",
27+
"application/x-ndjson",
28+
"application/vnd.mapbox-vector-tile",
29+
"application/vnd.elasticsearch+json",
30+
"application/vnd.elasticsearch+x-ndjson",
31+
}
32+
33+
34+
try:
35+
import pyarrow as pa
36+
EXPECTED_SERIALIZERS.add("application/vnd.apache.arrow.stream")
37+
except ImportError:
38+
pa = None
39+
40+
2441
class TestSerializers(DummyTransportTestCase):
2542
def test_compat_mode_on_by_default(self):
2643
calls = self.client.transport.calls
@@ -90,16 +107,8 @@ class CustomSerializer:
90107
"https://localhost:9200", serializers={f"application/{mime_subtype}": ser}
91108
)
92109
serializers = client.transport.serializers.serializers
93-
assert set(serializers.keys()) == {
94-
"application/json",
95-
"text/*",
96-
"application/x-ndjson",
97-
"application/vnd.apache.arrow.stream",
98-
"application/vnd.mapbox-vector-tile",
99-
"application/vnd.elasticsearch+json",
100-
"application/vnd.elasticsearch+x-ndjson",
101-
}
102110

111+
assert set(serializers.keys()) == EXPECTED_SERIALIZERS
103112
assert serializers[f"application/{mime_subtype}"] is ser
104113
assert serializers[f"application/vnd.elasticsearch+{mime_subtype}"] is ser
105114

@@ -118,16 +127,7 @@ class CustomSerializer:
118127
},
119128
)
120129
serializers = client.transport.serializers.serializers
121-
assert set(serializers.keys()) == {
122-
"application/json",
123-
"text/*",
124-
"application/x-ndjson",
125-
"application/vnd.apache.arrow.stream",
126-
"application/vnd.mapbox-vector-tile",
127-
"application/vnd.elasticsearch+json",
128-
"application/vnd.elasticsearch+x-ndjson",
129-
}
130-
130+
assert set(serializers.keys()) == EXPECTED_SERIALIZERS
131131
assert serializers[f"application/{mime_subtype}"] is ser1
132132
assert serializers[f"application/vnd.elasticsearch+{mime_subtype}"] is ser2
133133

@@ -138,15 +138,6 @@ class CustomSerializer:
138138
ser = CustomSerializer()
139139
client = Elasticsearch("https://localhost:9200", serializer=ser)
140140
serializers = client.transport.serializers.serializers
141-
assert set(serializers.keys()) == {
142-
"application/json",
143-
"text/*",
144-
"application/x-ndjson",
145-
"application/vnd.apache.arrow.stream",
146-
"application/vnd.mapbox-vector-tile",
147-
"application/vnd.elasticsearch+json",
148-
"application/vnd.elasticsearch+x-ndjson",
149-
}
150-
141+
assert set(serializers.keys()) == EXPECTED_SERIALIZERS
151142
assert serializers["application/json"] is ser
152143
assert serializers["application/vnd.elasticsearch+json"] is ser

test_elasticsearch/test_serializer.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@
1919
from datetime import datetime
2020
from decimal import Decimal
2121

22-
import pyarrow as pa
2322
import pytest
2423

24+
try:
25+
import pyarrow as pa
26+
from elasticsearch.serializer import PyArrowSerializer
27+
except ImportError:
28+
pa = None
29+
2530
try:
2631
import numpy as np
2732
import pandas as pd
@@ -35,7 +40,6 @@
3540
from elasticsearch.serializer import (
3641
JSONSerializer,
3742
OrjsonSerializer,
38-
PyArrowSerializer,
3943
TextSerializer,
4044
)
4145

@@ -163,6 +167,7 @@ def test_serializes_pandas_category(json_serializer):
163167
assert b'{"d":[1,2,3]}' == json_serializer.dumps({"d": cat})
164168

165169

170+
@pytest.mark.skipif(pa is None, reason="Test requires pyarrow to be available")
166171
def test_pyarrow_loads():
167172
data = [
168173
pa.array([1, 2, 3, 4]),

0 commit comments

Comments
 (0)