-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest_document.py
More file actions
295 lines (260 loc) · 14.2 KB
/
Copy pathtest_document.py
File metadata and controls
295 lines (260 loc) · 14.2 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
import json
from future import standard_library
standard_library.install_aliases()
from builtins import str
from builtins import range
from builtins import *
from random import getrandbits, randint
from ojai.types.OInterval import OInterval
from mapr.ojai.ojai.OJAIDocument import OJAIDocument
from ojai.types.ODate import ODate
from ojai.types.OTime import OTime
from ojai.types.OTimestamp import OTimestamp
from mapr.ojai.ojai_utils.ojai_document_creator import OJAIDocumentCreator
try:
import unittest2 as unittest
except ImportError:
import unittest
class DocumentTest(unittest.TestCase):
def test_empty_doc(self):
doc = OJAIDocument()
self.assertTrue(doc.empty())
self.assertEqual(doc.as_dictionary(), {})
def test_doc_insert_id(self):
doc = OJAIDocument()
doc.set_id("75")
self.assertEqual(doc.get_id(), "75")
self.assertIsInstance(doc.get_id(), str)
doc.set_id(str("75"))
self.assertEqual(doc.get_id(), "75")
self.assertIsInstance(doc.get_id(), str)
self.assertEqual(doc.as_dictionary(), {'_id': "75"})
def test_doc_set_int(self):
doc = OJAIDocument().set('test_int', 123).set_id('121212')
self.assertEqual(doc.as_dictionary(), {'_id': '121212', 'test_int': 123})
def test_doc_set_bool(self):
doc = OJAIDocument() \
.set('test_bool', True) \
.set('test_boolean_false', False)
self.assertEqual(doc.as_dictionary(), {'test_bool': True, 'test_boolean_false': False})
doc.set('test_int', 11) \
.set('test_long', int(123))
self.assertEqual(doc.as_dictionary(), {'test_bool': True,
'test_boolean_false': False,
'test_int': 11,
'test_long': 123})
def test_doc_set_float(self):
doc = OJAIDocument() \
.set('test_float', 11.1) \
.set('test_float_two', 12.34)
self.assertEqual(doc.as_dictionary(), {'test_float': 11.1,
'test_float_two': 12.34})
doc.set('test_int', 999).set('test_long', int(51233123))
self.assertEqual(doc.as_dictionary(), {'test_float': 11.1,
'test_float_two': 12.34,
'test_long': 51233123,
'test_int': 999})
doc.set('test_bool', False)
self.assertEqual(doc.as_dictionary(), {'test_float': 11.1,
'test_float_two': 12.34,
'test_long': 51233123,
'test_int': 999,
'test_bool': False})
def test_doc_set_dict(self):
test_dict = {'field_one': 12, 'field_two': 14}
doc = OJAIDocument().set('test_dict', test_dict)
self.assertEqual(doc.as_dictionary(), {'test_dict': {'field_one': 12,
'field_two': 14}})
doc.set_id('50')
self.assertEqual(doc.as_dictionary(), {'_id': '50', 'test_dict': {'field_one': 12,
'field_two': 14}})
doc.set('test_dict.insert', 90)
self.assertEqual(doc.as_dictionary(), {'_id': '50',
'test_dict': {'field_one': 12,
'field_two': 14,
'insert': 90}})
def test_doc_set_byte_array(self):
byte_array = bytearray([0x13, 0x00, 0x00, 0x00, 0x08, 0x00])
doc = OJAIDocument().set('test_byte_array', byte_array)
self.assertEqual(doc.as_dictionary(), {'test_byte_array': bytearray(b'\x13\x00\x00\x00\x08\x00')})
self.assertEqual(doc.as_json_str(),
'{"test_byte_array": {"$binary": "EwAAAAgA"}}')
recreated_doc = OJAIDocumentCreator().create_document(doc.as_json_str())
self.assertEqual(recreated_doc.as_dictionary(), {'test_byte_array': bytearray(b'\x13\x00\x00\x00\x08\x00')})
def test_doc_set_none(self):
doc = OJAIDocument().set('test_none', None)
self.assertEqual(doc.as_dictionary(), {'test_none': None})
def test_doc_delete_first_level(self):
doc = OJAIDocument().set_id('121212') \
.set('test_int', 123) \
.set('test_float', 11.1)
self.assertEqual(doc.as_dictionary(), {'_id': '121212',
'test_int': 123,
'test_float': 11.1})
doc.delete('test_timestamp')
self.assertEqual(doc.as_dictionary(), {'_id': '121212',
'test_int': 123,
'test_float': 11.1})
def test_delete_by_index(self):
doc = OJAIDocument().set('test_list',
[1, 2, 3, 4, 5, 6, 7])
self.assertEqual({'test_list': [1, 2, 3, 4, 5, 6, 7]},
doc.as_dictionary())
doc.delete('test_list[3]')
self.assertEqual({'test_list': [1, 2, 3, 5, 6, 7]},
doc.as_dictionary())
def test_doc_delete_nested(self):
doc = OJAIDocument().set_id('121212') \
.set('test_int', 123) \
.set('first.test_int', 1235) \
.set('first.test_float', 12.2) \
.set('test_float', 11.1)
self.assertEqual(doc.as_dictionary(), {'_id': '121212',
'test_int': 123,
'first': {'test_float': 12.2,
'test_int': 1235},
'test_float': 11.1})
doc.delete('first.test_int')
self.assertEqual(doc.as_dictionary(), {'_id': '121212',
'test_int': 123,
'first': {'test_float': 12.2},
'test_float': 11.1})
doc.delete('first.test_float')
self.assertEqual(doc.as_dictionary(), {'_id': '121212',
'test_int': 123,
'first': {},
'test_float': 11.1})
def set_random_bytearray(self):
arr_len = randint(10, 100)
val = bytearray(getrandbits(8) for i in range(arr_len))
return val
def test_byte_array(self):
b = self.set_random_bytearray()
doc = OJAIDocument().set('b_array', b)
def test_doc_set_list(self):
nested_doc = OJAIDocument().set('nested_int', 11).set('nested_str', 'strstr')
doc = OJAIDocument().set('test_list', [1, 2, 3, 4, False, 'mystr', [{}, {}, [7, 8, 9, nested_doc]]])
self.assertEqual(doc.as_dictionary(), {'test_list': [1,
2,
3,
4,
False,
'mystr',
[{}, {}, [7, 8, 9,
{
'nested_str': 'strstr',
'nested_int': 11
}
]
]
]
}
)
def test_doc_get(self):
doc = OJAIDocument().set_id('121212') \
.set('test_int', 123) \
.set('first.test_int', 1235) \
.set('first.test_long', 123456789) \
.set('first.test_float', 123456789.123) \
.set('first.test_time', OTime(timestamp=1518689532)) \
.set('test_float', 11.1) \
.set('first.test_timestamp', OTimestamp(millis_since_epoch=29877132000)) \
.set('first.test_date', ODate(days_since_epoch=3456)) \
.set('first.test_interval', OInterval(milli_seconds=172800000)) \
.set('first.test_bool', True) \
.set('first.test_bool_false', False) \
.set('first.test_invalid', ODate(days_since_epoch=3457)) \
.set('first.test_str', 'strstr') \
.set('first.test_dict', {'a': 1, 'b': 2}) \
.set('first.test_dict2', {}) \
.set('first.test_list', [1, 2, 'str', False, ODate(days_since_epoch=3457)]) \
self.assertEqual(doc.get_int('test_int'), 123)
self.assertEqual(doc.get_int('first.test_int'), 1235)
self.assertEqual(doc.get_int('first.test_long'), 123456789)
self.assertEqual(doc.get_long('first.test_long'), 123456789)
self.assertEqual(doc.get_float('first.test_float'), 123456789.123)
self.assertEqual(doc.get_time('first.test_time').time_to_str(), '12:12:12')
self.assertEqual(doc.get_interval('first.test_interval').time_duration, 172800000)
self.assertEqual(doc.get_timestamp('first.test_timestamp').__str__(), '1970-12-12T19:12:12.000000Z')
self.assertEqual(doc.get_date('first.test_date').to_date_str(), '1979-06-19')
self.assertEqual(doc.get_boolean('first.test_bool'), True)
self.assertEqual(doc.get_boolean('first.test_bool_false'), False)
self.assertEqual(doc.get_boolean('first.test_invalid'), None)
self.assertEqual(doc.get_str('first.test_str'), 'strstr')
self.assertEqual(doc.get_dictionary('first.test_dict'), {'a': 1, 'b': 2})
self.assertEqual(doc.get_dictionary('first.test_dict2'), {})
# MAPRDB-779
def test_document_change_value_type(self):
doc = OJAIDocument().set_id('121212') \
.set('test_int', 123) \
.set('test_float', 11.1)
self.assertEqual(doc.as_dictionary(), {'_id': '121212',
'test_int': 123,
'test_float': 11.1})
doc.set('test_int', 12.2)
self.assertEqual(doc.as_dictionary(), {'_id': '121212',
'test_int': 12.2,
'test_float': 11.1})
def test_get_bytearray(self):
b = bytearray([0x13, 0x00, 0x00, 0x00, 0x08, 0x00])
doc = OJAIDocument()
doc.set('f1', b)
self.assertEqual(doc.get_binary('f1'), b)
def test_set_list_instead_of_list(self):
doc = OJAIDocument()
field = 'list_field'
doc.set(field, value=[1, 1])
doc.set(field, value=[2, 2])
self.assertEqual(doc.as_dictionary(), {field: [2, 2]})
doc.set(field, 5)
self.assertEqual(doc.as_dictionary(), {field: 5})
doc.set(field, value=[1, 1])
self.assertEqual(doc.as_dictionary(), {field: [1, 1]})
def test_set_array_element(self):
doc = OJAIDocument().set('a.b.c', [1, 2, 3])
self.assertEqual(doc.as_dictionary(), {'a': {'b': {'c': [1, 2, 3]}}})
doc.set('a.b.c[1]', 9)
self.assertEqual(doc.as_dictionary(), {'a': {'b': {'c': [1, 9, 3]}}})
doc.set('a.b.c[12]', 55)
self.assertEqual(doc.as_dictionary(), {'a': {'b': {'c': [1, 9, 3, None,
None, None,
None, None,
None, None,
None, None,
55]}}})
self.assertEqual(doc.get('a.b.c[12]'), 55)
self.assertEqual(doc.get('a.b.c[0]'), 1)
self.assertEqual(doc.get('a.b.c[1]'), 9)
self.assertEqual(doc.get('a.b.c[3]'), None)
def test_document_to_json_str_without_tags(self):
doc = OJAIDocument().set_id('121212') \
.set('test_int', 123) \
.set('first.test_int', 1235) \
.set('first.test_long', 123456789) \
.set('first.test_float', 123456789.123) \
.set('first.test_time', OTime(timestamp=1518689532)) \
.set('test_float', 11.1) \
.set('first.test_timestamp', OTimestamp(millis_since_epoch=29877132000)) \
.set('first.test_date', ODate(days_since_epoch=3456)) \
.set('first.test_bool', True) \
.set('first.test_bool_false', False) \
.set('first.test_invalid', ODate(days_since_epoch=3457)) \
.set('first.test_str', 'strstr') \
.set('first.test_dict', {'a': 1, 'b': 2}) \
.set('first.test_dict2', {}) \
.set('first.test_list', [1, 2, 'str', False, ODate(days_since_epoch=3457)])\
.set('test_bytearray', bytearray(b'\x06\x06'))
self.assertEqual({"_id": "121212", "test_int": 123,
"first": {"test_int": 1235, "test_long": 123456789,
"test_float": 123456789.123, "test_time": "12:12:12",
"test_timestamp": "1970-12-12T19:12:12.000000Z",
"test_date": "1979-06-19", "test_bool": True,
"test_bool_false": False, "test_invalid": "1979-06-20",
"test_str": "strstr", "test_dict": {"a": 1, "b": 2},
"test_dict2": {}, "test_list": [1, 2, "str", False, "1979-06-20"]},
"test_float": 11.1, "test_bytearray": "\u0006\u0006"},
json.loads(doc.as_json_str(with_tags=False)))