Debian Codegraph @

tests/test_with_serde_path_to_err.rs

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
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 assert_eq!(
104 err.to_string(),
105 "root_map.nested_1.nested_key: unexpected type: 'int' object is not an instance of 'str'"
106 );
107 })
108 }
109
110 #[test]
111 fn test_ser_valid() {
112 Python::attach(|py| {
113 let root = Root {
114 root_key: String::from("root_value"),
115 root_map: BTreeMap::from([
116 (
117 String::from("nested_0"),
118 Nested {
119 nested_key: String::from("nested_value_0"),
120 },
121 ),
122 (
123 String::from("nested_1"),
124 Nested {
125 nested_key: String::from("nested_value_1"),
126 },
127 ),
128 ]),
129 };
130
131 let ser = pythonize::Pythonizer::<Root<String>>::from(py);
132 let pyroot: Bound<'_, PyAny> = serde_path_to_error::serialize(&root, ser).unwrap();
133
134 let pyroot = pyroot.cast::<PyDict>().unwrap();
135 assert_eq!(pyroot.len(), 2);
136
137 let root_value: String = pyroot
138 .get_item("root_key")
139 .unwrap()
140 .unwrap()
141 .extract()
142 .unwrap();
143 assert_eq!(root_value, "root_value");
144
145 let root_map = pyroot
146 .get_item("root_map")
147 .unwrap()
148 .unwrap()
149 .cast_into::<PyDict>()
150 .unwrap();
151 assert_eq!(root_map.len(), 2);
152
153 let nested_0 = root_map
154 .get_item("nested_0")
155 .unwrap()
156 .unwrap()
157 .cast_into::<PyDict>()
158 .unwrap();
159 assert_eq!(nested_0.len(), 1);
160 let nested_key_0: String = nested_0
161 .get_item("nested_key")
162 .unwrap()
163 .unwrap()
164 .extract()
165 .unwrap();
166 assert_eq!(nested_key_0, "nested_value_0");
167
168 let nested_1 = root_map
169 .get_item("nested_1")
170 .unwrap()
171 .unwrap()
172 .cast_into::<PyDict>()
173 .unwrap();
174 assert_eq!(nested_1.len(), 1);
175 let nested_key_1: String = nested_1
176 .get_item("nested_key")
177 .unwrap()
178 .unwrap()
179 .extract()
180 .unwrap();
181 assert_eq!(nested_key_1, "nested_value_1");
182 });
183 }
184
185 #[test]
186 fn test_ser_invalid() {
187 Python::attach(|py| {
188 let root = Root {
189 root_key: String::from("root_value"),
190 root_map: BTreeMap::from([
191 (
192 String::from("nested_0"),
193 Nested {
194 nested_key: CannotSerialize {},
195 },
196 ),
197 (
198 String::from("nested_1"),
199 Nested {
200 nested_key: CannotSerialize {},
201 },
202 ),
203 ]),
204 };
205
206 let ser = pythonize::Pythonizer::<Root<String>>::from(py);
207 let err = serde_path_to_error::serialize(&root, ser).unwrap_err();
208
209 assert_eq!(err.path().to_string(), "root_map.nested_0.nested_key");
210 });
211 }