|
1 | 1 | from typing import Any, List, Optional, Tuple |
2 | 2 |
|
3 | 3 | import numpy as np |
| 4 | +import orjson |
4 | 5 | import pytest |
5 | 6 |
|
6 | 7 | from docarray import DocList, DocVec |
7 | 8 | from docarray.base_doc.doc import BaseDoc |
| 9 | +from docarray.base_doc.io.json import orjson_dumps_and_decode |
8 | 10 | from docarray.typing import NdArray |
| 11 | +from docarray.typing.tensor.abstract_tensor import AbstractTensor |
| 12 | +from docarray.utils._internal.pydantic import is_pydantic_v2 |
9 | 13 |
|
10 | 14 |
|
11 | 15 | def test_base_document_init(): |
@@ -146,3 +150,40 @@ class MyDoc(BaseDoc): |
146 | 150 | field_type = MyDoc._get_field_inner_type("tuple_") |
147 | 151 |
|
148 | 152 | assert field_type == Any |
| 153 | + |
| 154 | + |
| 155 | +@pytest.mark.skipif( |
| 156 | + is_pydantic_v2, reason="syntax only working with pydantic v1 for now" |
| 157 | +) |
| 158 | +def test_subclass_config(): |
| 159 | + class MyDoc(BaseDoc): |
| 160 | + x: str |
| 161 | + |
| 162 | + class Config(BaseDoc.Config): |
| 163 | + arbitrary_types_allowed = True # just an example setting |
| 164 | + |
| 165 | + assert MyDoc.Config.json_loads == orjson.loads |
| 166 | + assert MyDoc.Config.json_dumps == orjson_dumps_and_decode |
| 167 | + assert ( |
| 168 | + MyDoc.Config.json_encoders[AbstractTensor](3) == 3 |
| 169 | + ) # dirty check that it is identity |
| 170 | + assert MyDoc.Config.validate_assignment |
| 171 | + assert not MyDoc.Config._load_extra_fields_from_protobuf |
| 172 | + assert MyDoc.Config.arbitrary_types_allowed |
| 173 | + |
| 174 | + |
| 175 | +@pytest.mark.skipif(not (is_pydantic_v2), reason="syntax only working with pydantic v2") |
| 176 | +def test_subclass_config_v2(): |
| 177 | + class MyDoc(BaseDoc): |
| 178 | + x: str |
| 179 | + |
| 180 | + model_config = BaseDoc.ConfigDocArray( |
| 181 | + arbitrary_types_allowed=True |
| 182 | + ) # just an example setting |
| 183 | + |
| 184 | + assert ( |
| 185 | + MyDoc.model_config['json_encoders'][AbstractTensor](3) == 3 |
| 186 | + ) # dirty check that it is identity |
| 187 | + assert MyDoc.model_config['validate_assignment'] |
| 188 | + assert not MyDoc.model_config['_load_extra_fields_from_protobuf'] |
| 189 | + assert MyDoc.model_config['arbitrary_types_allowed'] |
0 commit comments