Skip to content

Commit 733d5e2

Browse files
committed
Make pyarrow dependency optional for tests
1 parent cb389d9 commit 733d5e2

File tree

3 files changed

+48
-55
lines changed

3 files changed

+48
-55
lines changed

test_elasticsearch/test_client/test_deprecated_options.py

Lines changed: 20 additions & 19 deletions
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+
EXPECTED_SERIALIZERS.add("application/vnd.apache.arrow.stream")
37+
except ImportError:
38+
pa = None
39+
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

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,22 @@
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+
EXPECTED_SERIALIZERS.add("application/vnd.apache.arrow.stream")
36+
except ImportError:
37+
pa = None
38+
2339

2440
class TestSerializers(DummyTransportTestCase):
2541
def test_compat_mode_on_by_default(self):
@@ -90,16 +106,8 @@ class CustomSerializer:
90106
"https://localhost:9200", serializers={f"application/{mime_subtype}": ser}
91107
)
92108
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-
}
102109

110+
assert set(serializers.keys()) == EXPECTED_SERIALIZERS
103111
assert serializers[f"application/{mime_subtype}"] is ser
104112
assert serializers[f"application/vnd.elasticsearch+{mime_subtype}"] is ser
105113

@@ -118,16 +126,7 @@ class CustomSerializer:
118126
},
119127
)
120128
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-
129+
assert set(serializers.keys()) == EXPECTED_SERIALIZERS
131130
assert serializers[f"application/{mime_subtype}"] is ser1
132131
assert serializers[f"application/vnd.elasticsearch+{mime_subtype}"] is ser2
133132

@@ -138,15 +137,6 @@ class CustomSerializer:
138137
ser = CustomSerializer()
139138
client = Elasticsearch("https://localhost:9200", serializer=ser)
140139
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-
140+
assert set(serializers.keys()) == EXPECTED_SERIALIZERS
151141
assert serializers["application/json"] is ser
152142
assert serializers["application/vnd.elasticsearch+json"] is ser

test_elasticsearch/test_serializer.py

Lines changed: 9 additions & 7 deletions
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)