Debian Codegraph @

tests/mysql/derives.rs

raw symbols ◀ ▶
Source reconstructed at patch state 1 of 3 — not the installed (fully patched) source.
3 Debian patches touch this file
1 use sqlx_mysql::MySql;
2 use sqlx_test::new;
3
4 #[sqlx::test]
5 async fn test_derive_strong_enum() -> anyhow::Result<()> {
6 #[derive(sqlx::Type, PartialEq, Eq, Debug)]
7 #[sqlx(rename_all = "PascalCase")]
8 enum PascalCaseEnum {
9 FooFoo,
10 BarBar,
11 BazBaz,
12 }
13
14 #[derive(sqlx::Type, PartialEq, Eq, Debug)]
15 #[sqlx(rename_all = "camelCase")]
16 enum CamelCaseEnum {
17 FooFoo,
18 BarBar,
19 BazBaz,
20 }
21
22 #[derive(sqlx::Type, PartialEq, Eq, Debug)]
23 #[sqlx(rename_all = "snake_case")]
24 enum SnakeCaseEnum {
25 FooFoo,
26 BarBar,
27 BazBaz,
28 }
29
30 #[derive(sqlx::Type, PartialEq, Eq, Debug)]
31 #[sqlx(rename_all = "SCREAMING_SNAKE_CASE")]
32 enum ScreamingSnakeCaseEnum {
33 FooFoo,
34 BarBar,
35 BazBaz,
36 }
37
38 #[derive(sqlx::Type, PartialEq, Eq, Debug)]
39 #[sqlx(rename_all = "kebab-case")]
40 enum KebabCaseEnum {
41 FooFoo,
42 BarBar,
43 BazBaz,
44 }
45
46 #[derive(sqlx::Type, PartialEq, Eq, Debug)]
47 #[sqlx(rename_all = "lowercase")]
48 enum LowerCaseEnum {
49 FooFoo,
50 BarBar,
51 BazBaz,
52 }
53
54 #[derive(sqlx::Type, PartialEq, Eq, Debug)]
55 #[sqlx(rename_all = "UPPERCASE")]
56 enum UpperCaseEnum {
57 FooFoo,
58 BarBar,
59 BazBaz,
60 }
61
62 #[derive(sqlx::Type, PartialEq, Eq, Debug)]
63 enum DefaultCaseEnum {
64 FooFoo,
65 BarBar,
66 BazBaz,
67 }
68
69 #[derive(sqlx::FromRow, PartialEq, Eq, Debug)]
70 struct StrongEnumRow {
71 pascal_case: PascalCaseEnum,
72 camel_case: CamelCaseEnum,
73 snake_case: SnakeCaseEnum,
74 screaming_snake_case: ScreamingSnakeCaseEnum,
75 kebab_case: KebabCaseEnum,
76 lowercase: LowerCaseEnum,
77 uppercase: UpperCaseEnum,
78 default_case: DefaultCaseEnum,
79 }
80
81 let mut conn = new::<MySql>().await?;
82
83 sqlx::raw_sql(
84 r#"
85 CREATE TEMPORARY TABLE strong_enum (
86 pascal_case ENUM('FooFoo', 'BarBar', 'BazBaz'),
87 camel_case ENUM('fooFoo', 'barBar', 'bazBaz'),
88 snake_case ENUM('foo_foo', 'bar_bar', 'baz_baz'),
89 screaming_snake_case ENUM('FOO_FOO', 'BAR_BAR', 'BAZ_BAZ'),
90 kebab_case ENUM('foo-foo', 'bar-bar', 'baz-baz'),
91 lowercase ENUM('foofoo', 'barbar', 'bazbaz'),
92 uppercase ENUM('FOOFOO', 'BARBAR', 'BAZBAZ'),
93 default_case ENUM('FooFoo', 'BarBar', 'BazBaz')
94 );
95 "#,
96 )
97 .execute(&mut conn)
98 .await?;
99
100 let input = StrongEnumRow {
101 pascal_case: PascalCaseEnum::FooFoo,
102 camel_case: CamelCaseEnum::BarBar,
103 snake_case: SnakeCaseEnum::BazBaz,
104 screaming_snake_case: ScreamingSnakeCaseEnum::FooFoo,
105 kebab_case: KebabCaseEnum::BarBar,
106 lowercase: LowerCaseEnum::BazBaz,
107 uppercase: UpperCaseEnum::FooFoo,
108 default_case: DefaultCaseEnum::BarBar,
109 };
110
111 sqlx::query(
112 r#"
113 INSERT INTO strong_enum(
114 pascal_case,
115 camel_case,
116 snake_case,
117 screaming_snake_case,
118 kebab_case,
119 lowercase,
120 uppercase,
121 default_case
122 )
123 VALUES (?, ?, ?, ?, ?, ?, ?, ?)
124 "#,
125 )
126 .bind(&input.pascal_case)
127 .bind(&input.camel_case)
128 .bind(&input.snake_case)
129 .bind(&input.screaming_snake_case)
130 .bind(&input.kebab_case)
131 .bind(&input.lowercase)
132 .bind(&input.uppercase)
133 .bind(&input.default_case)
134 .execute(&mut conn)
135 .await?;
136
137 let output: StrongEnumRow = sqlx::query_as("SELECT * FROM strong_enum")
138 .fetch_one(&mut conn)
139 .await?;
140
141 assert_eq!(input, output);
142
143 Ok(())
144 }
145
146 #[sqlx::test]
147 async fn test_derive_weak_enum() -> anyhow::Result<()> {
148 #[derive(sqlx::Type, Debug, PartialEq, Eq)]
149 #[repr(i8)]
150 enum WeakEnumI8 {
151 Foo = i8::MIN,
152 Bar = 0,
153 Baz = i8::MAX,
154 }
155
156 #[derive(sqlx::Type, Debug, PartialEq, Eq)]
157 #[repr(i16)]
158 enum WeakEnumI16 {
159 Foo = i16::MIN,
160 Bar = 0,
161 Baz = i16::MAX,
162 }
163
164 #[derive(sqlx::Type, Debug, PartialEq, Eq)]
165 #[repr(i32)]
166 enum WeakEnumI32 {
167 Foo = i32::MIN,
168 Bar = 0,
169 Baz = i32::MAX,
170 }
171
172 #[derive(sqlx::Type, Debug, PartialEq, Eq)]
173 #[repr(i64)]
174 enum WeakEnumI64 {
175 Foo = i64::MIN,
176 Bar = 0,
177 Baz = i64::MAX,
178 }
179
180 #[derive(sqlx::Type, Debug, PartialEq, Eq)]
181 #[repr(u8)]
182 enum WeakEnumU8 {
183 Foo = 0,
184 Bar = 1,
185 Baz = u8::MAX,
186 }
187
188 #[derive(sqlx::Type, Debug, PartialEq, Eq)]
189 #[sqlx(no_pg_array)]
190 #[repr(u16)]
191 enum WeakEnumU16 {
192 Foo = 0,
193 Bar = 1,
194 Baz = u16::MAX,
195 }
196
197 #[derive(sqlx::Type, Debug, PartialEq, Eq)]
198 #[sqlx(no_pg_array)]
199 #[repr(u32)]
200 enum WeakEnumU32 {
201 Foo = 0,
202 Bar = 1,
203 Baz = u32::MAX,
204 }
205
206 #[derive(sqlx::Type, Debug, PartialEq, Eq)]
207 #[sqlx(no_pg_array)]
208 #[repr(u64)]
209 enum WeakEnumU64 {
210 Foo = 0,
211 Bar = 1,
212 Baz = u64::MAX,
213 }
214
215 #[derive(sqlx::FromRow, Debug, PartialEq, Eq)]
216 struct WeakEnumRow {
217 i8: WeakEnumI8,
218 i16: WeakEnumI16,
219 i32: WeakEnumI32,
220 i64: WeakEnumI64,
221 u8: WeakEnumU8,
222 u16: WeakEnumU16,
223 u32: WeakEnumU32,
224 u64: WeakEnumU64,
225 }
226
227 let mut conn = new::<MySql>().await?;
228
229 sqlx::raw_sql(
230 r#"
231 CREATE TEMPORARY TABLE weak_enum (
232 i8 TINYINT,
233 i16 SMALLINT,
234 i32 INT,
235 i64 BIGINT,
236 u8 TINYINT UNSIGNED,
237 u16 SMALLINT UNSIGNED,
238 u32 INT UNSIGNED,
239 u64 BIGINT UNSIGNED
240 )
241 "#,
242 )
243 .execute(&mut conn)
244 .await?;
245
246 let rows_in = vec![
247 WeakEnumRow {
248 i8: WeakEnumI8::Foo,
249 i16: WeakEnumI16::Foo,
250 i32: WeakEnumI32::Foo,
251 i64: WeakEnumI64::Foo,
252 u8: WeakEnumU8::Foo,
253 u16: WeakEnumU16::Foo,
254 u32: WeakEnumU32::Foo,
255 u64: WeakEnumU64::Foo,
256 },
257 WeakEnumRow {
258 i8: WeakEnumI8::Bar,
259 i16: WeakEnumI16::Bar,
260 i32: WeakEnumI32::Bar,
261 i64: WeakEnumI64::Bar,
262 u8: WeakEnumU8::Bar,
263 u16: WeakEnumU16::Bar,
264 u32: WeakEnumU32::Bar,
265 u64: WeakEnumU64::Bar,
266 },
267 WeakEnumRow {
268 i8: WeakEnumI8::Baz,
269 i16: WeakEnumI16::Baz,
270 i32: WeakEnumI32::Baz,
271 i64: WeakEnumI64::Baz,
272 u8: WeakEnumU8::Baz,
273 u16: WeakEnumU16::Baz,
274 u32: WeakEnumU32::Baz,
275 u64: WeakEnumU64::Baz,
276 },
277 ];
278
279 for row in &rows_in {
280 sqlx::query(
281 r#"
282 INSERT INTO weak_enum(i8, i16, i32, i64, u8, u16, u32, u64)
283 VALUES (?, ?, ?, ?, ?, ?, ?, ?)
284 "#,
285 )
286 .bind(&row.i8)
287 .bind(&row.i16)
288 .bind(&row.i32)
289 .bind(&row.i64)
290 .bind(&row.u8)
291 .bind(&row.u16)
292 .bind(&row.u32)
293 .bind(&row.u64)
294 .execute(&mut conn)
295 .await?;
296 }
297
298 let rows_out: Vec<WeakEnumRow> = sqlx::query_as("SELECT * FROM weak_enum")
299 .fetch_all(&mut conn)
300 .await?;
301
302 assert_eq!(rows_in, rows_out);
303
304 Ok(())
305 }