forked from thombashi/pytablewriter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_elasticsearch_writer.py
194 lines (172 loc) · 5.96 KB
/
test_elasticsearch_writer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# encoding: utf-8
"""
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
"""
from __future__ import absolute_import, print_function, unicode_literals
import collections
import datetime
import platform # noqa: W0611
from decimal import Decimal
import pytablewriter as ptw
import pytest
import simplejson as json
import six # noqa: W0611
from ._common import print_test_result
from .data import headers, value_matrix
inf = Decimal("Infinity")
nan = None
Data = collections.namedtuple("Data", "table header value expected")
exception_test_data_list = [
Data(table="", header=headers, value=value_matrix, expected=ptw.EmptyTableNameError),
Data(table="dummy", header=[], value=[], expected=ptw.EmptyTableDataError),
Data(table="dummy", header=headers, value=[], expected=ptw.EmptyValueError),
]
table_writer_class = ptw.ElasticsearchWriter
class Test_ElasticsearchWriter__get_mappings(object):
@pytest.mark.skipif("platform.system() == 'Windows' and six.PY2")
def test_normal(self):
writer = table_writer_class()
writer.table_name = "es mappings"
writer.headers = [
"text",
"byte",
"short",
"int",
"long",
"float",
"date",
"bool",
"ip",
"none",
"inf",
"nan",
]
writer.value_matrix = [
[
"This is XXX",
100,
10000,
2000000000,
200000000000,
0.1,
datetime.datetime(2017, 1, 3, 4, 5, 6),
True,
"127.0.0.1",
None,
float("inf"),
float("nan"),
],
[
"What is it",
-10,
-1000,
-200000000,
-20000000000,
100.1,
datetime.datetime(2017, 1, 3, 4, 5, 6),
False,
"::1",
None,
float("inf"),
float("nan"),
],
]
# mappings w/o type hint ---
writer._preprocess()
mappings = writer._get_mappings()
expected_mappings = {
"mappings": {
"table": {
"properties": {
"text": {"type": "text"},
"byte": {"type": "byte"},
"short": {"type": "short"},
"int": {"type": "integer"},
"long": {"type": "long"},
"float": {"type": "double"},
"date": {"type": "date", "format": "date_optional_time"},
"bool": {"type": "boolean"},
"ip": {"type": "text"},
"none": {"type": "keyword"},
"inf": {"type": "keyword"},
"nan": {"type": "keyword"},
}
}
}
}
print_test_result(expected=expected_mappings, actual=json.dumps(mappings, indent=4))
assert mappings == expected_mappings
# mappings w/ type hint ---
writer.type_hints = [None, None, None, None, None, None, None, None, ptw.IpAddress]
writer._preprocess()
mappings = writer._get_mappings()
expected_mappings = {
"mappings": {
"table": {
"properties": {
"text": {"type": "text"},
"byte": {"type": "byte"},
"short": {"type": "short"},
"int": {"type": "integer"},
"long": {"type": "long"},
"float": {"type": "double"},
"date": {"type": "date", "format": "date_optional_time"},
"bool": {"type": "boolean"},
"ip": {"type": "ip"},
"none": {"type": "keyword"},
"inf": {"type": "keyword"},
"nan": {"type": "keyword"},
}
}
}
}
print_test_result(expected=expected_mappings, actual=json.dumps(mappings, indent=4))
assert mappings == expected_mappings
# body ---
body = list(writer._get_body())
expected_body = [
{
"text": "This is XXX",
"byte": 100,
"short": 10000,
"int": 2000000000,
"long": 200000000000,
"float": Decimal("0.1"),
"date": "2017-01-03T04:05:06",
"bool": True,
"ip": "127.0.0.1",
"none": None,
"inf": "Infinity",
"nan": "NaN",
},
{
"text": "What is it",
"byte": -10,
"short": -1000,
"int": -200000000,
"long": -20000000000,
"float": Decimal("100.1"),
"date": "2017-01-03T04:05:06",
"bool": False,
"ip": "::1",
"none": None,
"inf": "Infinity",
"nan": "NaN",
},
]
print_test_result(expected=expected_body, actual=body)
assert body == expected_body
class Test_ElasticsearchWriter_write_table(object):
@pytest.mark.parametrize(
["table", "header", "value", "expected"],
[[data.table, data.header, data.value, data.expected] for data in exception_test_data_list],
)
def test_exception(self, table, header, value, expected):
import elasticsearch
writer = table_writer_class()
writer.stream = elasticsearch.Elasticsearch()
writer.table_name = table
writer.headers = header
writer.value_matrix = value
with pytest.raises(expected):
writer.write_table()