Skip to content

Commit 18fb41e

Browse files
author
AGAEV Denis E
committed
Add values for serialization tests
1 parent 6a1a37e commit 18fb41e

File tree

3 files changed

+61
-49
lines changed

3 files changed

+61
-49
lines changed

python/psqlpy/_internal/extra_types.pyi

Lines changed: 16 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,18 @@ class PyCustomType:
130130
Coordinates: typing.TypeAlias = typing.Union[
131131
list[int | float],
132132
set[int | float],
133-
tuple[int | float],
133+
tuple[int | float, int | float],
134+
]
135+
PairsOfCoordinates: typing.TypeAlias = typing.Union[
136+
list[Coordinates | int | float],
137+
set[Coordinates | int | float],
138+
tuple[Coordinates | int | float, ...],
134139
]
135140

136141
class PyPoint:
137142
"""Represent point field in PostgreSQL and Point in Rust."""
138143

139-
def __init__(
140-
self: Self,
141-
value: Coordinates,
142-
) -> None:
144+
def __init__(self: Self, value: Coordinates) -> None:
143145
"""Create new instance of PyPoint.
144146
145147
It accepts any pair(List, Tuple or Set)
@@ -152,13 +154,7 @@ class PyPoint:
152154
class PyBox:
153155
"""Represent box field in PostgreSQL and Rect in Rust."""
154156

155-
def __init__(
156-
self: Self,
157-
value: typing.Union[
158-
typing.Union[list[Coordinates], set[Coordinates], tuple[Coordinates]],
159-
Coordinates,
160-
],
161-
) -> None:
157+
def __init__(self: Self, value: PairsOfCoordinates) -> None:
162158
"""Create new instance of PyBox.
163159
164160
You need to pass any of this structures:
@@ -174,13 +170,7 @@ class PyBox:
174170
class PyPath:
175171
"""Represent path field in PostgreSQL and LineString in Rust."""
176172

177-
def __init__(
178-
self: Self,
179-
value: typing.Union[
180-
typing.Union[list[Coordinates], set[Coordinates], tuple[Coordinates]],
181-
Coordinates,
182-
],
183-
) -> None:
173+
def __init__(self: Self, value: PairsOfCoordinates) -> None:
184174
"""Create new instance of PyPath.
185175
186176
You need to pass any of this structures:
@@ -196,13 +186,7 @@ class PyPath:
196186
class PyLine:
197187
"""Represent line field in PostgreSQL and Line in Rust."""
198188

199-
def __init__(
200-
self: Self,
201-
value: typing.Union[
202-
typing.Union[list[Coordinates], set[Coordinates], tuple[Coordinates]],
203-
Coordinates,
204-
],
205-
) -> None:
189+
def __init__(self: Self, value: PairsOfCoordinates) -> None:
206190
"""Create new instance of PyLine.
207191
208192
You need to pass any of this structures:
@@ -218,13 +202,7 @@ class PyLine:
218202
class PyLineSegment:
219203
"""Represent lseg field in PostgreSQL and Line in Rust."""
220204

221-
def __init__(
222-
self: Self,
223-
value: typing.Union[
224-
typing.Union[list[Coordinates], set[Coordinates], tuple[Coordinates]],
225-
Coordinates,
226-
],
227-
) -> None:
205+
def __init__(self: Self, value: PairsOfCoordinates) -> None:
228206
"""Create new instance of PyLineSegment.
229207
230208
You need to pass any of this structures:
@@ -240,13 +218,7 @@ class PyLineSegment:
240218
class PyPolygon:
241219
"""Represent polygon field in PostgreSQL and Polygon in Rust."""
242220

243-
def __init__(
244-
self: Self,
245-
value: typing.Union[
246-
typing.Union[list[Coordinates], set[Coordinates], tuple[Coordinates]],
247-
Coordinates,
248-
],
249-
) -> None:
221+
def __init__(self: Self, value: PairsOfCoordinates) -> None:
250222
"""Create new instance of PyPolygon.
251223
252224
You need to pass any of this structures:
@@ -265,18 +237,15 @@ class PyCircle:
265237
def __init__(
266238
self: Self,
267239
value: typing.Union[
268-
typing.Union[list[Coordinates], set[Coordinates], tuple[Coordinates]],
269-
Coordinates,
240+
list[int | float],
241+
set[int | float],
242+
tuple[int | float, int | float, int | float],
270243
],
271244
) -> None:
272245
"""Create new instance of PyLine.
273246
274247
You need to pass any of this structures:
275-
- sequence of three int/float numbers
276-
- sequence of two sequences,
277-
each with pair of int/float numbers in every combination
278-
- sequence(List, Tuple or Set) with two pairs
279-
of int/float numbers in every combination
248+
- sequence of three int/float numbers(x, y, r)
280249
281250
### Parameters:
282251
- `value`: any valid structure with int/float numbers.

python/tests/test_value_converter.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,15 @@
1212
from psqlpy.extra_types import (
1313
BigInt,
1414
Integer,
15+
PyBox,
16+
PyCircle,
1517
PyJSON,
1618
PyJSONB,
19+
PyLine,
20+
PyLineSegment,
21+
PyPath,
22+
PyPoint,
23+
PyPolygon,
1724
PyText,
1825
PyUUID,
1926
SmallInt,
@@ -111,6 +118,42 @@ async def test_as_class(
111118
PyJSON([{"array": "json"}, {"one more": "test"}]),
112119
[{"array": "json"}, {"one more": "test"}],
113120
),
121+
("POINT", [1.5, 2], (1.5, 2.0)),
122+
("POINT", PyPoint([1.5, 2]), (1.5, 2.0)),
123+
("POINT", PyPoint({1, 2.3}), (1.0, 2.3)),
124+
("POINT", PyPoint((1.7, 2.8)), (1.7, 2.8)),
125+
("BOX", PyBox([3.5, 3, 9, 9]), ((9.0, 9.0), (3.5, 3.0))),
126+
("BOX", PyBox({[1, 2], [9, 9]}), ((9.0, 9.0), (1.0, 2.0))),
127+
("BOX", PyBox(((1.7, 2.8), (9, 9))), ((9.0, 9.0), (1.7, 2.8))),
128+
(
129+
"PATH",
130+
PyPath([(3.5, 3), (9, 9), (8, 8)]),
131+
[(3.5, 3.0), (9.0, 9.0), (8.0, 8.0)],
132+
),
133+
(
134+
"PATH",
135+
PyPath(((1.7, 2.8), (3.3, 2.5), (9, 9), (1.7, 2.8))),
136+
((1.7, 2.8), (3.3, 2.5), (9.0, 9.0), (1.7, 2.8)),
137+
),
138+
("LINE", PyLine({[1, 2], [9, 9]}), [(1.0, 2.0), (9.0, 9.0)]),
139+
("LINE", PyLine(((1.7, 2.8), (9, 9))), [(1.7, 2.8), (9.0, 9.0)]),
140+
("LSEG", PyLineSegment({[1, 2], [9, 9]}), [(1.0, 2.0), (9.0, 9.0)]),
141+
("LSEG", PyLineSegment(((1.7, 2.8), (9, 9))), [(1.7, 2.8), (9.0, 9.0)]),
142+
(
143+
"POLYGON",
144+
PyPolygon((1.7, 2.8, 3.3, 2.5, 9, 9, 1.7, 2.8)),
145+
((1.7, 2.8), (3.3, 2.5), (9.0, 9.0), (1.7, 2.8)),
146+
),
147+
(
148+
"POLYGON",
149+
PyPolygon(((1.7, 2.8), (3.3, 2.5), (9, 9), (1.7, 2.8))),
150+
((1.7, 2.8), (3.3, 2.5), (9.0, 9.0), (1.7, 2.8)),
151+
),
152+
(
153+
"CIRCLE",
154+
PyCircle((1.7, 2.8, 3)),
155+
(1.7, 2.8, 3.0),
156+
),
114157
(
115158
"VARCHAR ARRAY",
116159
["Some String", "Some String"],

src/additional_types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,9 @@ impl<'a> IntoPy<PyObject> for &'a RustLineString {
185185
}
186186

187187
if inner_value.is_closed() {
188-
return PyList::new_bound(py, result_vec).into();
188+
return PyTuple::new_bound(py, result_vec).into();
189189
}
190-
return PyTuple::new_bound(py, result_vec).into();
190+
return PyList::new_bound(py, result_vec).into();
191191
}
192192
}
193193

0 commit comments

Comments
 (0)