|
14 | 14 | from datetime import datetime
|
15 | 15 | from unittest import TestCase
|
16 | 16 |
|
17 |
| -from bson import Int64 |
18 |
| -from pyarrow import float64, int64, timestamp |
| 17 | +from bson import Decimal128, Int64, ObjectId |
| 18 | +from pyarrow import Table, float64, int64 |
| 19 | +from pyarrow import schema as ArrowSchema |
| 20 | +from pyarrow import timestamp |
19 | 21 | from pymongoarrow.schema import Schema
|
| 22 | +from pymongoarrow.types import _TYPE_NORMALIZER_FACTORY |
20 | 23 |
|
21 | 24 |
|
22 | 25 | class TestSchema(TestCase):
|
| 26 | + def test_as_py(self): |
| 27 | + # Some of the classes want special things in their constructors. |
| 28 | + instantiated_objs = { |
| 29 | + datetime: datetime(1, 1, 1), |
| 30 | + str: "hell0", |
| 31 | + bool: True, |
| 32 | + } |
| 33 | + # The extension types need to be provided to from_pydict as strings or binary, |
| 34 | + # but we also need the original object for the assertion at the end of the test. |
| 35 | + oid = ObjectId() |
| 36 | + dec = Decimal128("1.000") |
| 37 | + lookup = {Decimal128: dec, ObjectId: oid} |
| 38 | + instantiated_objs.update({Decimal128: str(dec), ObjectId: oid.binary}) |
| 39 | + |
| 40 | + for k, v in _TYPE_NORMALIZER_FACTORY.items(): |
| 41 | + # Make an array of 4 elements with either the instantiated object or 1. |
| 42 | + column = [instantiated_objs.get(k, 1)] * 4 |
| 43 | + t = Table.from_pydict( |
| 44 | + {"value": column}, |
| 45 | + ArrowSchema([("value", v(True))]), |
| 46 | + ) |
| 47 | + self.assertEqual(t.to_pylist(), [{"value": lookup.get(k, i)} for i in column]) |
| 48 | + |
23 | 49 | def test_initialization(self):
|
24 | 50 | dict_schema = Schema({"field1": int, "field2": datetime, "field3": float})
|
25 | 51 | self.assertEqual(
|
|
0 commit comments