Skip to content

Commit f565519

Browse files
committed
Adding tests for multi-dimensional arrays
Signed-off-by: chandr-andr (Kiselev Aleksandr) <chandr@chandr.net>
1 parent e30bef8 commit f565519

File tree

4 files changed

+243
-146
lines changed

4 files changed

+243
-146
lines changed

python/psqlpy/_internal/extra_types.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ class PyJSONB:
9494
value: typing.Union[
9595
dict[str, typing.Any],
9696
list[dict[str, typing.Any]],
97+
list[typing.Any],
9798
],
9899
) -> None:
99100
"""Create new instance of PyJSON.B.

python/tests/test_value_converter.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,54 +185,103 @@ async def test_as_class(
185185
["Some String", "Some String"],
186186
),
187187
("BOOL ARRAY", [True, False], [True, False]),
188+
("BOOL ARRAY", [[True], [False]], [[True], [False]]),
188189
("INT2 ARRAY", [SmallInt(12), SmallInt(100)], [12, 100]),
190+
("INT2 ARRAY", [[SmallInt(12)], [SmallInt(100)]], [[12], [100]]),
189191
("INT4 ARRAY", [Integer(121231231), Integer(121231231)], [121231231, 121231231]),
192+
("INT4 ARRAY", [[Integer(121231231)], [Integer(121231231)]], [[121231231], [121231231]]),
190193
(
191194
"INT8 ARRAY",
192195
[BigInt(99999999999999999), BigInt(99999999999999999)],
193196
[99999999999999999, 99999999999999999],
194197
),
198+
(
199+
"INT8 ARRAY",
200+
[[BigInt(99999999999999999)], [BigInt(99999999999999999)]],
201+
[[99999999999999999], [99999999999999999]],
202+
),
195203
(
196204
"MONEY ARRAY",
197205
[Money(99999999999999999), Money(99999999999999999)],
198206
[99999999999999999, 99999999999999999],
199207
),
208+
(
209+
"MONEY ARRAY",
210+
[[Money(99999999999999999)], [Money(99999999999999999)]],
211+
[[99999999999999999], [99999999999999999]],
212+
),
200213
(
201214
"NUMERIC(5, 2) ARRAY",
202215
[Decimal("121.23"), Decimal("188.99")],
203216
[Decimal("121.23"), Decimal("188.99")],
204217
),
218+
(
219+
"NUMERIC(5, 2) ARRAY",
220+
[[Decimal("121.23")], [Decimal("188.99")]],
221+
[[Decimal("121.23")], [Decimal("188.99")]],
222+
),
205223
(
206224
"FLOAT8 ARRAY",
207225
[32.12329864501953, 32.12329864501953],
208226
[32.12329864501953, 32.12329864501953],
209227
),
228+
(
229+
"FLOAT8 ARRAY",
230+
[[32.12329864501953], [32.12329864501953]],
231+
[[32.12329864501953], [32.12329864501953]],
232+
),
210233
(
211234
"DATE ARRAY",
212235
[now_datetime.date(), now_datetime.date()],
213236
[now_datetime.date(), now_datetime.date()],
214237
),
238+
(
239+
"DATE ARRAY",
240+
[[now_datetime.date()], [now_datetime.date()]],
241+
[[now_datetime.date()], [now_datetime.date()]],
242+
),
215243
(
216244
"TIME ARRAY",
217245
[now_datetime.time(), now_datetime.time()],
218246
[now_datetime.time(), now_datetime.time()],
219247
),
248+
(
249+
"TIME ARRAY",
250+
[[now_datetime.time()], [now_datetime.time()]],
251+
[[now_datetime.time()], [now_datetime.time()]],
252+
),
220253
("TIMESTAMP ARRAY", [now_datetime, now_datetime], [now_datetime, now_datetime]),
254+
("TIMESTAMP ARRAY", [[now_datetime], [now_datetime]], [[now_datetime], [now_datetime]]),
221255
(
222256
"TIMESTAMPTZ ARRAY",
223257
[now_datetime_with_tz, now_datetime_with_tz],
224258
[now_datetime_with_tz, now_datetime_with_tz],
225259
),
260+
(
261+
"TIMESTAMPTZ ARRAY",
262+
[[now_datetime_with_tz], [now_datetime_with_tz]],
263+
[[now_datetime_with_tz], [now_datetime_with_tz]],
264+
),
226265
(
227266
"UUID ARRAY",
228267
[uuid_, uuid_],
229268
[str(uuid_), str(uuid_)],
230269
),
270+
(
271+
"UUID ARRAY",
272+
[[uuid_], [uuid_]],
273+
[[str(uuid_)], [str(uuid_)]],
274+
),
231275
(
232276
"INET ARRAY",
233277
[IPv4Address("192.0.0.1"), IPv4Address("192.0.0.1")],
234278
[IPv4Address("192.0.0.1"), IPv4Address("192.0.0.1")],
235279
),
280+
(
281+
"INET ARRAY",
282+
[[IPv4Address("192.0.0.1")], [IPv4Address("192.0.0.1")]],
283+
[[IPv4Address("192.0.0.1")], [IPv4Address("192.0.0.1")]],
284+
),
236285
(
237286
"JSONB ARRAY",
238287
[
@@ -256,6 +305,37 @@ async def test_as_class(
256305
},
257306
],
258307
),
308+
(
309+
"JSONB ARRAY",
310+
[
311+
[
312+
{
313+
"test": ["something", 123, "here"],
314+
"nested": ["JSON"],
315+
},
316+
],
317+
[
318+
{
319+
"test": ["something", 123, "here"],
320+
"nested": ["JSON"],
321+
},
322+
],
323+
],
324+
[
325+
[
326+
{
327+
"test": ["something", 123, "here"],
328+
"nested": ["JSON"],
329+
},
330+
],
331+
[
332+
{
333+
"test": ["something", 123, "here"],
334+
"nested": ["JSON"],
335+
},
336+
],
337+
],
338+
),
259339
(
260340
"JSONB ARRAY",
261341
[
@@ -267,6 +347,17 @@ async def test_as_class(
267347
[{"array": "json"}, {"one more": "test"}],
268348
],
269349
),
350+
(
351+
"JSONB ARRAY",
352+
[
353+
PyJSONB([[{"array": "json"}], [{"one more": "test"}]]),
354+
PyJSONB([[{"array": "json"}], [{"one more": "test"}]]),
355+
],
356+
[
357+
[[{"array": "json"}], [{"one more": "test"}]],
358+
[[{"array": "json"}], [{"one more": "test"}]],
359+
],
360+
),
270361
(
271362
"JSON ARRAY",
272363
[

src/additional_types.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use postgres_protocol::types;
88
use postgres_types::{to_sql_checked, IsNull, ToSql};
99
use pyo3::{
1010
types::{PyList, PyTuple},
11-
IntoPy, Py, PyAny, PyObject, Python,
11+
IntoPy, Py, PyAny, PyObject, Python, ToPyObject,
1212
};
1313
use serde::{Deserialize, Serialize};
1414
use tokio_postgres::types::{FromSql, Type};
@@ -78,6 +78,42 @@ build_additional_rust_type!(RustRect, Rect);
7878
build_additional_rust_type!(RustLineString, LineString);
7979
build_additional_rust_type!(RustLineSegment, LineSegment);
8080

81+
impl ToPyObject for RustPoint {
82+
fn to_object(&self, py: Python<'_>) -> PyObject {
83+
self.into_py(py)
84+
}
85+
}
86+
87+
impl ToPyObject for RustRect {
88+
fn to_object(&self, py: Python<'_>) -> PyObject {
89+
self.into_py(py)
90+
}
91+
}
92+
93+
impl ToPyObject for RustLineString {
94+
fn to_object(&self, py: Python<'_>) -> PyObject {
95+
self.into_py(py)
96+
}
97+
}
98+
99+
impl ToPyObject for RustLineSegment {
100+
fn to_object(&self, py: Python<'_>) -> PyObject {
101+
self.into_py(py)
102+
}
103+
}
104+
105+
impl ToPyObject for Line {
106+
fn to_object(&self, py: Python<'_>) -> PyObject {
107+
self.into_py(py)
108+
}
109+
}
110+
111+
impl ToPyObject for Circle {
112+
fn to_object(&self, py: Python<'_>) -> PyObject {
113+
self.into_py(py)
114+
}
115+
}
116+
81117
impl<'a> IntoPy<PyObject> for &'a RustPoint {
82118
#[inline]
83119
fn into_py(self, py: Python<'_>) -> PyObject {

0 commit comments

Comments
 (0)