Debian Codegraph @

tests/test_with_serde_path_to_err.rs

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 use std::collections::BTreeMap;
2
3 use pyo3::{
4 prelude::*,
5 types::{PyDict, PyList},
6 };
7 use pythonize::{PythonizeTypes, PythonizeUnnamedMappingAdapter};
8 use serde::{Deserialize, Serialize};
9
10 #[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
11 struct Root<T> {
12 root_key: String,
13 root_map: BTreeMap<String, Nested<T>>,
14 }
15
16 impl<'py, T> PythonizeTypes for Root<T> {
17 type Map = PyDict;
18 type NamedMap = PythonizeUnnamedMappingAdapter<PyDict>;
19 type List = PyList;
20 }
21
22 #[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
23 struct Nested<T> {
24 nested_key: T,
25 }
26
27 #[derive(Deserialize, Debug, PartialEq, Eq)]
28 struct CannotSerialize {}
29
30 impl Serialize for CannotSerialize {
31 fn serialize<S>(&self, _serializer: S) -> Result<S::Ok, S::Error>
32 where
33 S: serde::Serializer,
34 {
35 Err(serde::ser::Error::custom(
36 "something went intentionally wrong",
37 ))
38 }
39 }
40
41 #[test]
42 fn test_de_valid() {
43 Python::attach(|py| {
44 let pyroot = PyDict::new(py);
45 pyroot.set_item("root_key", "root_value").unwrap();
46
47 let nested = PyDict::new(py);
48 let nested_0 = PyDict::new(py);
49 nested_0.set_item("nested_key", "nested_value_0").unwrap();
50 nested.set_item("nested_0", nested_0).unwrap();
51 let nested_1 = PyDict::new(py);
52 nested_1.set_item("nested_key", "nested_value_1").unwrap();
53 nested.set_item("nested_1", nested_1).unwrap();
54
55 pyroot.set_item("root_map", nested).unwrap();
56
57 let de = &mut pythonize::Depythonizer::from_object(&pyroot);
58 let root: Root<String> = serde_path_to_error::deserialize(de).unwrap();
59
60 assert_eq!(
61 root,
62 Root {
63 root_key: String::from("root_value"),
64 root_map: BTreeMap::from([
65 (
66 String::from("nested_0"),
67 Nested {
68 nested_key: String::from("nested_value_0")
69 }
70 ),
71 (
72 String::from("nested_1"),
73 Nested {
74 nested_key: String::from("nested_value_1")
75 }
76 )
77 ])
78 }
79 );
80 })
81 }
82
83 #[test]
84 fn test_de_invalid() {
85 Python::attach(|py| {
86 let pyroot = PyDict::new(py);
87 pyroot.set_item("root_key", "root_value").unwrap();
88
89 let nested = PyDict::new(py);
90 let nested_0 = PyDict::new(py);
91 nested_0.set_item("nested_key", "nested_value_0").unwrap();
92 nested.set_item("nested_0", nested_0).unwrap();
93 let nested_1 = PyDict::new(py);
94 nested_1.set_item("nested_key", 1).unwrap();
95 nested.set_item("nested_1", nested_1).unwrap();
96
97 pyroot.set_item("root_map", nested).unwrap();
98
99 let de = &mut pythonize::Depythonizer::from_object(&pyroot);
100 let err = serde_path_to_error::deserialize::<_, Root<String>>(de).unwrap_err();
101
102 assert_eq!(err.path().to_string(), "root_map.nested_1.nested_key");
•103 if err.to_string() != "root_map.nested_1.nested_key: unexpected type: 'int' object cannot be cast as 'str'" {
104 assert_eq!(
105 err.to_string(),
106 "root_map.nested_1.nested_key: unexpected type: 'int' object is not an instance of 'str'"
•107 );
108 }
109 })
110 }
111
112 #[test]
113 fn test_ser_valid() {
114 Python::attach(|py| {
115 let root = Root {
116 root_key: String::from("root_value"),
117 root_map: BTreeMap::from([
118 (
119 String::from("nested_0"),
120 Nested {
121 nested_key: String::from("nested_value_0"),
122 },
123 ),
124 (
125 String::from("nested_1"),
126 Nested {
127 nested_key: String::from("nested_value_1"),
128 },
129 ),
130 ]),
131 };
132
133 let ser = pythonize::Pythonizer::<Root<String>>::from(py);
134 let pyroot: Bound<'_, PyAny> = serde_path_to_error::serialize(&root, ser).unwrap();
135
136 let pyroot = pyroot.cast::<PyDict>().unwrap();
137 assert_eq!(pyroot.len(), 2);
138
139 let root_value: String = pyroot
140 .get_item("root_key")
141 .unwrap()
142 .unwrap()
143 .extract()
144 .unwrap();
145 assert_eq!(root_value, "root_value");
146
147 let root_map = pyroot
148 .get_item("root_map")
149 .unwrap()
150 .unwrap()
151 .cast_into::<PyDict>()
152 .unwrap();
153 assert_eq!(root_map.len(), 2);
154
155 let nested_0 = root_map
156 .get_item("nested_0")
157 .unwrap()
158 .unwrap()
159 .cast_into::<PyDict>()
160 .unwrap();
161 assert_eq!(nested_0.len(), 1);
162 let nested_key_0: String = nested_0
163 .get_item("nested_key")
164 .unwrap()
165 .unwrap()
166 .extract()
167 .unwrap();
168 assert_eq!(nested_key_0, "nested_value_0");
169
170 let nested_1 = root_map
171 .get_item("nested_1")
172 .unwrap()
173 .unwrap()
174 .cast_into::<PyDict>()
175 .unwrap();
176 assert_eq!(nested_1.len(), 1);
177 let nested_key_1: String = nested_1
178 .get_item("nested_key")
179 .unwrap()
180 .unwrap()
181 .extract()
182 .unwrap();
183 assert_eq!(nested_key_1, "nested_value_1");
184 });
185 }
186
187 #[test]
188 fn test_ser_invalid() {
189 Python::attach(|py| {
190 let root = Root {
191 root_key: String::from("root_value"),
192 root_map: BTreeMap::from([
193 (
194 String::from("nested_0"),
195 Nested {
196 nested_key: CannotSerialize {},
197 },
198 ),
199 (
200 String::from("nested_1"),
201 Nested {
202 nested_key: CannotSerialize {},
203 },
204 ),
205 ]),
206 };
207
208 let ser = pythonize::Pythonizer::<Root<String>>::from(py);
209 let err = serde_path_to_error::serialize(&root, ser).unwrap_err();
210
211 assert_eq!(err.path().to_string(), "root_map.nested_0.nested_key");
212 });
213 }