Skip to content

Commit e552144

Browse files
committed
Remove duplicate code from the raise type error and type null tests
1 parent b212aaa commit e552144

File tree

2 files changed

+34
-116
lines changed

2 files changed

+34
-116
lines changed

bindings/python/pymongoarrow/lib.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ cdef void process_raw_bson_stream(const uint8_t * docstream, size_t length, obje
416416
elif value_t == BSON_TYPE_NULL:
417417
if context.raise_on_type_null:
418418
raise PyMongoArrowError(f"Null value for {key}!")
419-
date32_builder.append_null()
419+
date32_builder.append_null()
420420
else:
421421
if context.raise_on_type_error:
422422
raise PyMongoArrowError(f"Type mismatch! {key} is not a data32")
@@ -1063,4 +1063,4 @@ cdef class BinaryBuilder(_ArrayBuilderBase):
10631063
return pyarrow_wrap_array(out).cast(BinaryType(self._subtype))
10641064

10651065
cdef shared_ptr[CBinaryBuilder] unwrap(self):
1066-
return self.builder
1066+
return self.builder

bindings/python/test/test_bson.py

Lines changed: 32 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -263,10 +263,10 @@ def test_nested(self):
263263
self._run_test(docs, as_dict)
264264

265265

266-
class TestTypeErrorBsonToArrowConversion(TestBsonToArrowConversionBase):
266+
class TestSimpleBsonToArrowConversion(TestBsonToArrowConversionBase):
267267
def setUp(self):
268268
self.schema = Schema(
269-
{"_id": ObjectId, "data": int64(), "title": string()}, raise_on_type_error=True
269+
{"_id": ObjectId, "data": int64(), "title": string()},
270270
)
271271
self.context = PyMongoArrowContext.from_schema(self.schema)
272272

@@ -286,12 +286,12 @@ def test_simple(self):
286286

287287
self._run_test(docs, as_dict)
288288

289-
def test_with_missing_data(self):
289+
def test_missing_data(self):
290290
ids = [ObjectId() for i in range(4)]
291291
docs = [
292292
{"_id": ids[0], "data": 10, "title": "a"},
293293
{"_id": ids[1], "data": 20},
294-
{"_id": ids[2]},
294+
{"_id": ids[2]},
295295
{"_id": ids[3], "data": 40},
296296
{"foo": 1},
297297
{},
@@ -304,28 +304,14 @@ def test_with_missing_data(self):
304304

305305
self._run_test(docs, as_dict)
306306

307-
def test_with_missmatch_data_and_rising(self):
307+
def test_mismatch_data(self):
308308
ids = [ObjectId() for i in range(4)]
309309
docs = [
310310
{"_id": ids[0], "data": 10, "title": "ä"},
311311
{"_id": ids[1], "data": 20, "title": "b"},
312312
{"_id": ids[2], "data": "30", "title": "č"},
313313
{"_id": ids[3], "data": 40, "title": "ê"},
314314
]
315-
316-
payload = type(self)._generate_payload(docs)
317-
318-
with pytest.raises(PyMongoArrowError, match="Type mismatch! b'data' is not an int64"):
319-
process_bson_stream(payload, self.context)
320-
321-
def test_with_null_data(self):
322-
ids = [ObjectId() for i in range(4)]
323-
docs = [
324-
{"_id": ids[0], "data": 10, "title": "ä"},
325-
{"_id": ids[1], "data": 20, "title": "b"},
326-
{"_id": ids[2], "data": None, "title": "č"},
327-
{"_id": ids[3], "data": 40, "title": "ê"},
328-
]
329315
as_dict = {
330316
"_id": ids,
331317
"data": [10, 20, None, 40],
@@ -334,53 +320,12 @@ def test_with_null_data(self):
334320

335321
self._run_test(docs, as_dict)
336322

337-
class TestTypeNullBsonToArrowConversion(TestBsonToArrowConversionBase):
338-
def setUp(self):
339-
self.schema = Schema(
340-
{"_id": ObjectId, "data": int64(), "title": string()}, raise_on_type_null=True
341-
)
342-
self.context = PyMongoArrowContext.from_schema(self.schema)
343-
344-
def test_simple(self):
323+
def test_null_data(self):
345324
ids = [ObjectId() for i in range(4)]
346325
docs = [
347326
{"_id": ids[0], "data": 10, "title": "ä"},
348327
{"_id": ids[1], "data": 20, "title": "b"},
349-
{"_id": ids[2], "data": 30, "title": "č"},
350-
{"_id": ids[3], "data": 40, "title": "ê"},
351-
]
352-
as_dict = {
353-
"_id": ids,
354-
"data": [10, 20, 30, 40],
355-
"title": ["ä", "b", "č", "ê"],
356-
}
357-
358-
self._run_test(docs, as_dict)
359-
360-
def test_with_missing_data(self):
361-
ids = [ObjectId() for i in range(4)]
362-
docs = [
363-
{"_id": ids[0], "data": 10, "title": "a"},
364-
{"_id": ids[1], "data": 20},
365-
{"_id": ids[2]},
366-
{"_id": ids[3], "data": 40},
367-
{"foo": 1},
368-
{},
369-
]
370-
as_dict = {
371-
"_id": ids + [None, None],
372-
"data": [10, 20, None, 40, None, None],
373-
"title": ["a", None, None, None, None, None],
374-
}
375-
376-
self._run_test(docs, as_dict)
377-
378-
def test_with_missmatch_data(self):
379-
ids = [ObjectId() for i in range(4)]
380-
docs = [
381-
{"_id": ids[0], "data": 10, "title": "ä"},
382-
{"_id": ids[1], "data": 20, "title": "b"},
383-
{"_id": ids[2], "data": "30", "title": "č"},
328+
{"_id": ids[2], "data": None, "title": "č"},
384329
{"_id": ids[3], "data": 40, "title": "ê"},
385330
]
386331
as_dict = {
@@ -391,62 +336,15 @@ def test_with_missmatch_data(self):
391336

392337
self._run_test(docs, as_dict)
393338

394-
def test_with_null_data_and_rising(self):
395-
ids = [ObjectId() for i in range(4)]
396-
docs = [
397-
{"_id": ids[0], "data": 10, "title": "ä"},
398-
{"_id": ids[1], "data": 20, "title": "b"},
399-
{"_id": ids[2], "data": None, "title": "č"},
400-
{"_id": ids[3], "data": 40, "title": "ê"},
401-
]
402-
403-
payload = type(self)._generate_payload(docs)
404339

405-
with pytest.raises(PyMongoArrowError, match="Null value for b'data'!"):
406-
process_bson_stream(payload, self.context)
407-
408-
class TestTypeNullAndTypeErrorBsonToArrowConversion(TestBsonToArrowConversionBase):
340+
class TestTypeErrorBsonToArrowConversion(TestSimpleBsonToArrowConversion):
409341
def setUp(self):
410342
self.schema = Schema(
411-
{"_id": ObjectId, "data": int64(), "title": string()}, raise_on_type_null=True, raise_on_type_error=True
343+
{"_id": ObjectId, "data": int64(), "title": string()}, raise_on_type_error=True
412344
)
413345
self.context = PyMongoArrowContext.from_schema(self.schema)
414346

415-
def test_simple(self):
416-
ids = [ObjectId() for i in range(4)]
417-
docs = [
418-
{"_id": ids[0], "data": 10, "title": "ä"},
419-
{"_id": ids[1], "data": 20, "title": "b"},
420-
{"_id": ids[2], "data": 30, "title": "č"},
421-
{"_id": ids[3], "data": 40, "title": "ê"},
422-
]
423-
as_dict = {
424-
"_id": ids,
425-
"data": [10, 20, 30, 40],
426-
"title": ["ä", "b", "č", "ê"],
427-
}
428-
429-
self._run_test(docs, as_dict)
430-
431-
def test_with_missing_data(self):
432-
ids = [ObjectId() for i in range(4)]
433-
docs = [
434-
{"_id": ids[0], "data": 10, "title": "a"},
435-
{"_id": ids[1], "data": 20},
436-
{"_id": ids[2]},
437-
{"_id": ids[3], "data": 40},
438-
{"foo": 1},
439-
{},
440-
]
441-
as_dict = {
442-
"_id": ids + [None, None],
443-
"data": [10, 20, None, 40, None, None],
444-
"title": ["a", None, None, None, None, None],
445-
}
446-
447-
self._run_test(docs, as_dict)
448-
449-
def test_with_missmatch_data_and_rising(self):
347+
def test_mismatch_data(self):
450348
ids = [ObjectId() for i in range(4)]
451349
docs = [
452350
{"_id": ids[0], "data": 10, "title": "ä"},
@@ -460,7 +358,15 @@ def test_with_missmatch_data_and_rising(self):
460358
with pytest.raises(PyMongoArrowError, match="Type mismatch! b'data' is not an int64"):
461359
process_bson_stream(payload, self.context)
462360

463-
def test_with_null_data_and_rising(self):
361+
362+
class TestTypeNullBsonToArrowConversion(TestSimpleBsonToArrowConversion):
363+
def setUp(self):
364+
self.schema = Schema(
365+
{"_id": ObjectId, "data": int64(), "title": string()}, raise_on_type_null=True
366+
)
367+
self.context = PyMongoArrowContext.from_schema(self.schema)
368+
369+
def test_null_data(self):
464370
ids = [ObjectId() for i in range(4)]
465371
docs = [
466372
{"_id": ids[0], "data": 10, "title": "ä"},
@@ -472,4 +378,16 @@ def test_with_null_data_and_rising(self):
472378
payload = type(self)._generate_payload(docs)
473379

474380
with pytest.raises(PyMongoArrowError, match="Null value for b'data'!"):
475-
process_bson_stream(payload, self.context)
381+
process_bson_stream(payload, self.context)
382+
383+
384+
class TestTypeNullAndTypeErrorBsonToArrowConversion(
385+
TestTypeNullBsonToArrowConversion, TestTypeErrorBsonToArrowConversion
386+
):
387+
def setUp(self):
388+
self.schema = Schema(
389+
{"_id": ObjectId, "data": int64(), "title": string()},
390+
raise_on_type_null=True,
391+
raise_on_type_error=True,
392+
)
393+
self.context = PyMongoArrowContext.from_schema(self.schema)

0 commit comments

Comments
 (0)