-
Notifications
You must be signed in to change notification settings - Fork 216
/
mysql_types.rs
388 lines (352 loc) Β· 14.5 KB
/
mysql_types.rs
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
use super::test_api::*;
use datamodel::dml::ScalarType;
use indoc::indoc;
use pretty_assertions::assert_eq;
use serde_json::json;
use test_macros::*;
const CREATE_TYPES_TABLE: &str = indoc! {
r##"
CREATE TABLE `types` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`numeric_integer_tinyint` tinyint(4),
`numeric_integer_smallint` smallint(6),
`numeric_integer_int` int(11),
`numeric_integer_bigint` bigint(20),
`numeric_floating_decimal` decimal(10,2),
`numeric_floating_float` float,
`numeric_fixed_double` double,
`numeric_fixed_real` double,
`numeric_bit` bit(64),
`numeric_boolean` tinyint(1),
`date_date` date,
`date_datetime` datetime,
`date_timestamp` timestamp null DEFAULT null,
`date_time` time,
`date_year` year(4),
`string_char` char(255),
`string_varchar` varchar(255),
`string_text_tinytext` tinytext,
`string_text_text` text,
`string_text_mediumtext` mediumtext,
`string_text_longtext` longtext,
`string_binary_binary` binary(20),
`string_binary_varbinary` varbinary(255),
`string_blob_tinyblob` tinyblob,
`string_blob_mediumblob` mediumblob,
`string_blob_blob` blob,
`string_blob_longblob` longblob,
`string_enum` enum('pollicle_dogs','jellicle_cats'),
`string_set` set('a','b','c'),
`spatial_geometry` geometry,
`spatial_point` point,
`spatial_linestring` linestring,
`spatial_polygon` polygon,
`spatial_multipoint` multipoint,
`spatial_multilinestring` multilinestring,
`spatial_multipolygon` multipolygon,
`spatial_geometrycollection` geometrycollection,
`json` json,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
"##
};
#[test_each_connector(tags("mysql"))]
async fn mysql_types_roundtrip(api: &TestApi) -> TestResult {
api.execute_sql(CREATE_TYPES_TABLE).await?;
let (datamodel, engine) = api.introspect_and_start_query_engine().await?;
datamodel.assert_model("types", |model| {
model
.assert_field_type("numeric_integer_tinyint", ScalarType::Int)?
.assert_field_type("numeric_integer_smallint", ScalarType::Int)?
.assert_field_type("numeric_integer_int", ScalarType::Int)?
.assert_field_type("numeric_integer_bigint", ScalarType::Int)?
.assert_field_type("numeric_floating_decimal", ScalarType::Float)?
.assert_field_type("numeric_floating_float", ScalarType::Float)?
.assert_field_type("numeric_fixed_double", ScalarType::Float)?
.assert_field_type("numeric_fixed_real", ScalarType::Float)?
.assert_field_type("numeric_bit", ScalarType::Int)?
.assert_field_type("numeric_boolean", ScalarType::Boolean)?
.assert_field_type("date_date", ScalarType::DateTime)?
.assert_field_type("date_datetime", ScalarType::DateTime)?
.assert_field_type("date_timestamp", ScalarType::DateTime)?
.assert_field_type("date_time", ScalarType::DateTime)?
.assert_field_type("date_year", ScalarType::Int)?
.assert_field_type("string_char", ScalarType::String)?
.assert_field_type("string_varchar", ScalarType::String)?
.assert_field_type("string_text_tinytext", ScalarType::String)?
.assert_field_type("string_text_text", ScalarType::String)?
.assert_field_type("string_text_mediumtext", ScalarType::String)?
.assert_field_type("string_text_longtext", ScalarType::String)?
.assert_field_type("string_binary_binary", ScalarType::String)?
.assert_field_type("string_blob_tinyblob", ScalarType::String)?
.assert_field_type("string_blob_mediumblob", ScalarType::String)?
.assert_field_type("string_blob_blob", ScalarType::String)?
.assert_field_type("string_blob_longblob", ScalarType::String)?
.assert_field_enum_type("string_enum", "types_string_enum")?
.assert_field_type("string_set", ScalarType::String)?
.assert_field_type("spatial_geometry", ScalarType::String)?
.assert_field_type("spatial_point", ScalarType::String)?
.assert_field_type("spatial_linestring", ScalarType::String)?
.assert_field_type("spatial_polygon", ScalarType::String)?
.assert_field_type("spatial_multipoint", ScalarType::String)?
.assert_field_type("spatial_multilinestring", ScalarType::String)?
.assert_field_type("spatial_multipolygon", ScalarType::String)?
.assert_field_type("spatial_geometrycollection", ScalarType::String)?
.assert_field_type("json", ScalarType::String)
})?;
// Write the values.
{
let write = indoc! {
"
mutation {
createOnetypes(
data: {
numeric_integer_tinyint: 12,
numeric_integer_smallint: 350,
numeric_integer_int: 9002,
numeric_integer_bigint: 30000,
numeric_floating_decimal: 3.14
numeric_floating_float: -32.0
numeric_fixed_double: 0.14
numeric_fixed_real: 12.12
numeric_bit: 4
numeric_boolean: true
date_date: \"2020-02-27T00:00:00Z\"
date_datetime: \"2020-02-27T19:10:22Z\"
date_timestamp: \"2020-02-27T19:11:22Z\"
date_time: \"2020-02-20T12:50:01Z\"
date_year: 2012
string_char: \"make dolphins easy\"
string_varchar: \"dolphins of varying characters\"
string_text_tinytext: \"tiny dolphins\"
string_text_text: \"dolphins\"
string_text_mediumtext: \"medium dolphins\"
string_text_longtext: \"long dolphins\"
string_binary_binary: \"hello 2020\"
string_blob_tinyblob: \"smol blob\"
string_blob_mediumblob: \"average blob\"
string_blob_blob: \"very average blob\"
string_blob_longblob: \"loong looooong bloooooooob\"
string_enum: \"jellicle_cats\"
json: \"{\\\"name\\\": null}\"
}
) { id }
}
"
};
let write_response = engine.request(write).await;
let expected_write_response = json!({
"data": {
"createOnetypes": {
"id": 1,
}
}
});
assert_eq!(write_response, expected_write_response);
}
// Read the values back.
{
let read = indoc! {
"
query {
findManytypes {
numeric_integer_tinyint
numeric_integer_smallint
numeric_integer_int
numeric_integer_bigint
numeric_floating_decimal
numeric_floating_float
numeric_fixed_double
numeric_fixed_real
numeric_bit
numeric_boolean
date_date
date_datetime
date_timestamp
date_time
date_year
string_char
string_varchar
string_text_tinytext
string_text_text
string_text_mediumtext
string_text_longtext
string_binary_binary
string_blob_tinyblob
string_blob_mediumblob
string_blob_blob
string_blob_longblob
string_enum
# omitting spatial/geometry types
json
}
}
"
};
let read_response = engine.request(read).await;
let expected_read_response = json!({
"data": {
"findManytypes": [
{
"numeric_integer_tinyint": 12,
"numeric_integer_smallint": 350,
"numeric_integer_int": 9002,
"numeric_integer_bigint": 30000,
"numeric_floating_decimal": 3.14,
"numeric_floating_float": -32.0,
"numeric_fixed_double": 0.14,
"numeric_fixed_real": 12.12,
"numeric_bit": 4,
"numeric_boolean": true,
"date_date": "2020-02-27T00:00:00.000Z",
"date_datetime": "2020-02-27T19:10:22.000Z",
"date_timestamp": "2020-02-27T19:11:22.000Z",
"date_time": "1970-01-01T12:50:01.000Z",
"date_year": 2012,
"string_char": "make dolphins easy",
"string_varchar": "dolphins of varying characters",
"string_text_tinytext": "tiny dolphins",
"string_text_text": "dolphins",
"string_text_mediumtext": "medium dolphins",
"string_text_longtext": "long dolphins",
"string_binary_binary": "hello 2020\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}",
"string_blob_tinyblob": "smol blob",
"string_blob_mediumblob": "average blob",
"string_blob_blob": "very average blob",
"string_blob_longblob": "loong looooong bloooooooob",
"string_enum": "jellicle_cats",
"json": "{\"name\": null}",
},
]
},
});
assert_eq!(read_response, expected_read_response);
}
Ok(())
}
#[test_each_connector(tags("mysql"))]
async fn mysql_bit_columns_are_properly_mapped_to_signed_integers(api: &TestApi) -> TestResult {
api.execute_sql(CREATE_TYPES_TABLE).await?;
let (_datamodel, engine) = api.introspect_and_start_query_engine().await?;
let write = indoc! {
"
mutation {
createOnetypes(
data: {
numeric_bit: -12
}
) { id numeric_bit }
}
"
};
let write_response = engine.request(write).await;
let expected_write_response = json!({
"data": {
"createOnetypes": {
"id": 1,
"numeric_bit": -12,
}
}
});
assert_eq!(write_response, expected_write_response);
Ok(())
}
#[test_each_connector(tags("mysql"))]
async fn mysql_floats_do_not_lose_precision(api: &TestApi) -> TestResult {
api.execute_sql(CREATE_TYPES_TABLE).await?;
let (_datamodel, engine) = api.introspect_and_start_query_engine().await?;
let write = indoc! {
"
mutation {
createOnetypes(
data: {
numeric_floating_float: 6.4
numeric_floating_decimal: 6.4
}
) {
id
numeric_floating_float
numeric_floating_decimal
}
}
"
};
let write_response = engine.request(write).await;
let expected_write_response = json!({
"data": {
"createOnetypes": {
"id": 1,
// This is what we expect from a f64 -> f32 -> f64 conversion.
"numeric_floating_float": 6.400000095367432,
"numeric_floating_decimal": 6.4,
}
}
});
assert_eq!(write_response, expected_write_response);
Ok(())
}
#[test_each_connector(tags("mysql"), log = "debug")]
async fn all_mysql_identifier_types_work(api: &TestApi) -> TestResult {
let identifier_types = &[
("tinyint", "12", ""),
("smallint", "350", ""),
("int", "9002", ""),
("bigint", "30000", ""),
("decimal(4, 2)", "3.1", ""),
// ("float", "2.8", ""),
("double", "0.1", ""),
("real", "12.1", ""),
("bit(32)", "4", ""),
("boolean", "true", ""),
("date", "\"2020-02-27T00:00:00.000Z\"", ""),
("datetime", "\"2020-02-27T19:10:22.000Z\"", ""),
("timestamp", "\"2020-02-27T19:11:22.000Z\"", ""),
// ("time", "\"1970-01-01T12:50:01.000Z\"", ""),
("year", "2091", ""),
("char(18)", "\"make dolphins easy\"", ""),
("varchar(200)", "\"dolphins of varying characters\"", ""),
("tinytext", "\"tiny dolphins\"", "(20)"),
("text", "\"dolphins\"", "(100)"),
("mediumtext", "\"medium dolphins\"", "(100)"),
("longtext", "\"long dolphins\"", "(100)"),
(
"enum('pollicle_dogs','jellicle_cats')",
"\"jellicle_cats\"",
"",
),
// ("json", "\"{\\\"name\\\": null}\"", ""),
];
let drop_table = r#"DROP TABLE IF EXISTS `pk_test`"#;
for (identifier_type, identifier_value, prefix) in identifier_types {
for index_type in &["PRIMARY KEY", "CONSTRAINT UNIQUE INDEX"] {
let create_pk_table = format!(
r#"CREATE TABLE `pk_test` (id {} NOT NULL, {} (id{}))"#,
identifier_type, index_type, prefix
);
api.execute_sql(drop_table).await?;
api.execute_sql(&create_pk_table).await?;
let (_datamodel, engine) = api.introspect_and_start_query_engine().await?;
let query = format!(
r#"
mutation {{
createOnepk_test(
data: {{
id: {}
}}
) {{
id
}}
}}
"#,
identifier_value
);
let response = engine.request(query).await;
let expected_response = format!(
r#"{{"data":{{"createOnepk_test":{{"id":{}}}}}}}"#,
identifier_value
);
assert_eq!(response.to_string(), expected_response);
}
}
Ok(())
}