Skip to content

Commit d692c70

Browse files
authored
Make pyarrow dependency optional for tests (#2733)
1 parent cb389d9 commit d692c70

File tree

3 files changed

+49
-55
lines changed

3 files changed

+49
-55
lines changed

test_elasticsearch/test_client/test_deprecated_options.py

+20-19
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,23 @@
2121

2222
from elasticsearch import Elasticsearch, JsonSerializer
2323

24+
EXPECTED_SERIALIZERS = {
25+
"application/vnd.mapbox-vector-tile",
26+
"application/x-ndjson",
27+
"application/json",
28+
"text/*",
29+
"application/vnd.elasticsearch+json",
30+
"application/vnd.elasticsearch+x-ndjson",
31+
}
32+
33+
34+
try:
35+
import pyarrow as pa
36+
37+
EXPECTED_SERIALIZERS.add("application/vnd.apache.arrow.stream")
38+
except ImportError:
39+
pa = None
40+
2441

2542
def test_sniff_on_connection_fail():
2643
with warnings.catch_warnings(record=True) as w:
@@ -129,15 +146,7 @@ class CustomSerializer(JsonSerializer):
129146
client.transport.serializers.get_serializer("application/json"),
130147
CustomSerializer,
131148
)
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-
}
149+
assert set(client.transport.serializers.serializers.keys()) == EXPECTED_SERIALIZERS
141150

142151
client = Elasticsearch(
143152
"http://localhost:9200",
@@ -150,13 +159,5 @@ class CustomSerializer(JsonSerializer):
150159
client.transport.serializers.get_serializer("application/json"),
151160
CustomSerializer,
152161
)
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-
}
162+
expected = EXPECTED_SERIALIZERS | {"application/cbor"}
163+
assert set(client.transport.serializers.serializers.keys()) == expected

test_elasticsearch/test_client/test_serializers.py

+20-29
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,23 @@
2020
from elasticsearch import Elasticsearch
2121
from test_elasticsearch.test_cases import DummyTransportTestCase
2222

23+
EXPECTED_SERIALIZERS = {
24+
"application/json",
25+
"text/*",
26+
"application/x-ndjson",
27+
"application/vnd.mapbox-vector-tile",
28+
"application/vnd.elasticsearch+json",
29+
"application/vnd.elasticsearch+x-ndjson",
30+
}
31+
32+
33+
try:
34+
import pyarrow as pa
35+
36+
EXPECTED_SERIALIZERS.add("application/vnd.apache.arrow.stream")
37+
except ImportError:
38+
pa = None
39+
2340

2441
class TestSerializers(DummyTransportTestCase):
2542
def test_compat_mode_on_by_default(self):
@@ -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

+9-7
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,15 @@
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+
27+
from elasticsearch.serializer import PyArrowSerializer
28+
except ImportError:
29+
pa = None
30+
2531
try:
2632
import numpy as np
2733
import pandas as pd
@@ -32,12 +38,7 @@
3238

3339
from elasticsearch import Elasticsearch
3440
from elasticsearch.exceptions import SerializationError
35-
from elasticsearch.serializer import (
36-
JSONSerializer,
37-
OrjsonSerializer,
38-
PyArrowSerializer,
39-
TextSerializer,
40-
)
41+
from elasticsearch.serializer import JSONSerializer, OrjsonSerializer, TextSerializer
4142

4243
requires_numpy_and_pandas = pytest.mark.skipif(
4344
np is None or pd is None, reason="Test requires numpy and pandas to be available"
@@ -163,6 +164,7 @@ def test_serializes_pandas_category(json_serializer):
163164
assert b'{"d":[1,2,3]}' == json_serializer.dumps({"d": cat})
164165

165166

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

0 commit comments

Comments
 (0)