File: test_with_serde_path_to_err.rs

package info (click to toggle)
rust-pythonize 0.21.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 232 kB
  • sloc: makefile: 2
file content (208 lines) | stat: -rw-r--r-- 6,231 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use std::collections::BTreeMap;

use pyo3::{
    prelude::*,
    types::{PyDict, PyList},
    Py, PyAny, Python,
};
use pythonize::PythonizeTypes;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
struct Root<T> {
    root_key: String,
    root_map: BTreeMap<String, Nested<T>>,
}

impl<T> PythonizeTypes for Root<T> {
    type Map = PyDict;
    type List = PyList;
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
struct Nested<T> {
    nested_key: T,
}

#[derive(Deserialize, Debug, PartialEq, Eq)]
struct CannotSerialize {}

impl Serialize for CannotSerialize {
    fn serialize<S>(&self, _serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        Err(serde::ser::Error::custom(
            "something went intentionally wrong",
        ))
    }
}

#[test]
fn test_de_valid() {
    Python::with_gil(|py| {
        let pyroot = PyDict::new_bound(py);
        pyroot.set_item("root_key", "root_value").unwrap();

        let nested = PyDict::new_bound(py);
        let nested_0 = PyDict::new_bound(py);
        nested_0.set_item("nested_key", "nested_value_0").unwrap();
        nested.set_item("nested_0", nested_0).unwrap();
        let nested_1 = PyDict::new_bound(py);
        nested_1.set_item("nested_key", "nested_value_1").unwrap();
        nested.set_item("nested_1", nested_1).unwrap();

        pyroot.set_item("root_map", nested).unwrap();

        let de = &mut pythonize::Depythonizer::from_object_bound(pyroot.into_any());
        let root: Root<String> = serde_path_to_error::deserialize(de).unwrap();

        assert_eq!(
            root,
            Root {
                root_key: String::from("root_value"),
                root_map: BTreeMap::from([
                    (
                        String::from("nested_0"),
                        Nested {
                            nested_key: String::from("nested_value_0")
                        }
                    ),
                    (
                        String::from("nested_1"),
                        Nested {
                            nested_key: String::from("nested_value_1")
                        }
                    )
                ])
            }
        );
    })
}

#[test]
fn test_de_invalid() {
    Python::with_gil(|py| {
        let pyroot = PyDict::new_bound(py);
        pyroot.set_item("root_key", "root_value").unwrap();

        let nested = PyDict::new_bound(py);
        let nested_0 = PyDict::new_bound(py);
        nested_0.set_item("nested_key", "nested_value_0").unwrap();
        nested.set_item("nested_0", nested_0).unwrap();
        let nested_1 = PyDict::new_bound(py);
        nested_1.set_item("nested_key", 1).unwrap();
        nested.set_item("nested_1", nested_1).unwrap();

        pyroot.set_item("root_map", nested).unwrap();

        let de = &mut pythonize::Depythonizer::from_object_bound(pyroot.into_any());
        let err = serde_path_to_error::deserialize::<_, Root<String>>(de).unwrap_err();

        assert_eq!(err.path().to_string(), "root_map.nested_1.nested_key");
        assert_eq!(err.to_string(), "root_map.nested_1.nested_key: unexpected type: 'int' object cannot be converted to 'PyString'");
    })
}

#[test]
fn test_ser_valid() {
    Python::with_gil(|py| {
        let root = Root {
            root_key: String::from("root_value"),
            root_map: BTreeMap::from([
                (
                    String::from("nested_0"),
                    Nested {
                        nested_key: String::from("nested_value_0"),
                    },
                ),
                (
                    String::from("nested_1"),
                    Nested {
                        nested_key: String::from("nested_value_1"),
                    },
                ),
            ]),
        };

        let ser = pythonize::Pythonizer::<Root<String>>::from(py);
        let pyroot: Py<PyAny> = serde_path_to_error::serialize(&root, ser).unwrap();

        let pyroot = pyroot.bind(py).downcast::<PyDict>().unwrap();
        assert_eq!(pyroot.len(), 2);

        let root_value: String = pyroot
            .get_item("root_key")
            .unwrap()
            .unwrap()
            .extract()
            .unwrap();
        assert_eq!(root_value, "root_value");

        let root_map = pyroot
            .get_item("root_map")
            .unwrap()
            .unwrap()
            .downcast_into::<PyDict>()
            .unwrap();
        assert_eq!(root_map.len(), 2);

        let nested_0 = root_map
            .get_item("nested_0")
            .unwrap()
            .unwrap()
            .downcast_into::<PyDict>()
            .unwrap();
        assert_eq!(nested_0.len(), 1);
        let nested_key_0: String = nested_0
            .get_item("nested_key")
            .unwrap()
            .unwrap()
            .extract()
            .unwrap();
        assert_eq!(nested_key_0, "nested_value_0");

        let nested_1 = root_map
            .get_item("nested_1")
            .unwrap()
            .unwrap()
            .downcast_into::<PyDict>()
            .unwrap();
        assert_eq!(nested_1.len(), 1);
        let nested_key_1: String = nested_1
            .get_item("nested_key")
            .unwrap()
            .unwrap()
            .extract()
            .unwrap();
        assert_eq!(nested_key_1, "nested_value_1");
    });
}

#[test]
fn test_ser_invalid() {
    Python::with_gil(|py| {
        let root = Root {
            root_key: String::from("root_value"),
            root_map: BTreeMap::from([
                (
                    String::from("nested_0"),
                    Nested {
                        nested_key: CannotSerialize {},
                    },
                ),
                (
                    String::from("nested_1"),
                    Nested {
                        nested_key: CannotSerialize {},
                    },
                ),
            ]),
        };

        let ser = pythonize::Pythonizer::<Root<String>>::from(py);
        let err = serde_path_to_error::serialize(&root, ser).unwrap_err();

        assert_eq!(err.path().to_string(), "root_map.nested_0.nested_key");
    });
}