-
Notifications
You must be signed in to change notification settings - Fork 11
/
lib.rs
547 lines (433 loc) · 15.4 KB
/
lib.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
#[allow(clippy::derive_partial_eq_without_eq)]
pub mod generated;
#[derive(
Clone, catalytic_macro::Json, serde::Serialize, serde::Deserialize, Debug, Eq, PartialEq,
)]
pub struct MyJsonType {
pub age: i32,
}
#[derive(
Clone, catalytic_macro::Json, serde::Serialize, serde::Deserialize, Debug, Eq, PartialEq,
)]
pub enum MyJsonEnum {
Something,
SomethingElse,
}
#[cfg(test)]
mod test {
use crate::generated::child::{truncate, Child};
use crate::generated::person::{PersonRef, UpdatableColumnRef};
use crate::generated::{FieldNameDifferentCombined, Person};
use crate::{MyJsonEnum, MyJsonType};
use catalytic::runtime::create_connection;
use catalytic::scylla;
use catalytic::scylla::transport::{PagingState, PagingStateResponse};
use catalytic_macro::{query, query_base_table};
use futures_util::StreamExt;
use scylla::frame::value::{LegacySerializedValues, SerializeValuesError};
use scylla::CachingSession;
#[tokio::test]
#[serial_test::serial]
async fn crud() {
let session = CachingSession::from(create_connection().await, 1);
crate::generated::child::truncate(&session).await.unwrap();
crate::generated::person::truncate(&session).await.unwrap();
let mut child = Child {
birthday: 1,
enum_json: MyJsonEnum::Something,
json: MyJsonType { age: 1 },
json_nullable: None,
};
// Inserts
// Insert with ttl
child.to_ref().insert_ttl(&session, 100).await.unwrap();
child.to_ref().insert(&session).await.unwrap();
// Selects
macro_rules! eq {
() => {
assert_eq!(
child
.primary_key()
.select_unique(&session)
.await
.unwrap()
.entity
.unwrap(),
child
);
assert_eq!(
child
.primary_key()
.select_unique_expect(&session)
.await
.unwrap()
.entity,
child
);
};
}
eq!();
let mut iterator = crate::generated::child::select_all(&session, Some(2))
.await
.unwrap();
while let Some(row) = iterator.next().await {
assert_eq!(child, row.unwrap());
}
let all = crate::generated::child::select_all_in_memory(&session, 123)
.await
.unwrap()
.entities;
assert_eq!(1, all.len());
assert_eq!(&child, &all[0]);
assert_eq!(
1,
crate::generated::child::select_all_count(&session)
.await
.unwrap()
.entity
);
// Select now for the base table from the mv
let person = Person {
name: "name".to_string(),
age: 1,
email: "2".to_string(),
row_type: "test".to_string(),
};
person.to_ref().insert(&session).await.unwrap();
let mut iterator = crate::generated::person_by_email::select_all_base_table(&session, None)
.await
.unwrap();
while let Some(row) = iterator.next().await {
assert_eq!(person, row.unwrap());
}
let pbe = crate::generated::person_by_email::PrimaryKeyRef {
email: &person.email,
name: &person.name,
age: &person.age,
};
assert_eq!(
pbe.select_unique_base_table(&session)
.await
.unwrap()
.entity
.unwrap(),
person
);
assert_eq!(
pbe.select_unique_expect_base_table(&session)
.await
.unwrap()
.entity,
person
);
// Single updates, with and without nullable values
child.json_nullable = Some(MyJsonType { age: 2 });
child
.primary_key()
.update_json_nullable(&session, &child.json_nullable)
.await
.unwrap();
eq!();
child.json_nullable = None;
child
.primary_key()
.update_json_nullable(&session, &child.json_nullable)
.await
.unwrap();
eq!();
// Single dynamic update
child.json = MyJsonType { age: 3 };
child
.primary_key()
.update_dyn(&session, child.updatable_column_json())
.await
.unwrap();
eq!();
// Single dynamic updates in vec
child.json = MyJsonType { age: 4 };
child
.primary_key()
.update_dyn_multiple(&session, &[child.updatable_column_json()])
.await
.unwrap();
eq!();
// Multiple dynamic updates in vec
child.json = MyJsonType { age: 5 };
child.json_nullable = Some(MyJsonType { age: 6 });
child
.primary_key()
.update_dyn_multiple(
&session,
&[
child.updatable_column_json(),
child.updatable_column_json_nullable(),
],
)
.await
.unwrap();
eq!();
// Delete
child.primary_key().delete(&session).await.unwrap();
macro_rules! empty {
() => {
assert!(child
.primary_key()
.select_unique(&session)
.await
.unwrap()
.entity
.is_none());
assert!(child
.primary_key()
.select_unique_expect(&session)
.await
.is_err());
};
}
empty!();
// Insert and check truncate
child.to_ref().insert(&session).await.unwrap();
eq!();
truncate(&session).await.unwrap();
empty!();
}
macro_rules! assert_serialized_values {
($sv: expr, $($val: expr),*) => {{
let mut serialized_values = LegacySerializedValues::new();
$(serialized_values.add_value(&$val).unwrap();)*
assert_eq!(serialized_values, $sv.qv.values);
}};
}
#[tokio::test]
#[serial_test::serial]
async fn paging() -> Result<(), SerializeValuesError> {
let session = CachingSession::from(create_connection().await, 1);
let rows_to_generate = 100;
let page_size = 7;
crate::generated::person::truncate(&session).await.unwrap();
let name = "doesnt_matter";
for index in 0..rows_to_generate {
PersonRef {
name,
age: &index,
email: "",
row_type: "34",
}
.insert(&session)
.await
.unwrap();
}
// Let's page
let select_multiple = query!("select * from person where name = ? order by age", name);
let mut paging_state = PagingState::start();
let mut counter_rows = 0;
let mut counter_loop = 0;
loop {
counter_loop += 1;
let result = select_multiple
.select_paged(&session, Some(page_size), paging_state.clone())
.await
.unwrap();
for row in result.entities {
assert_eq!(row.age, counter_rows);
counter_rows += 1;
}
if let PagingStateResponse::HasMorePages { state: next_state } = result.paging_result {
paging_state = next_state;
} else {
break;
}
}
assert_eq!(counter_rows, rows_to_generate);
assert_eq!(counter_loop, (rows_to_generate / 7) + 1);
Ok(())
}
/// Tests that when a custom field_name is provided, everything keeps working
#[tokio::test]
#[serial_test::serial]
async fn custom_field_name() {
let connection = CachingSession::from(create_connection().await, 1);
crate::generated::person::truncate(&connection)
.await
.unwrap();
let mut person = Person {
name: "name".to_string(),
age: 1,
email: "myemail".to_string(),
row_type: "4".to_string(),
};
person.to_ref().insert(&connection).await.unwrap();
macro_rules! check_values_person {
() => {
let all = crate::generated::person::select_all_in_memory(&connection, 2)
.await
.unwrap();
assert_eq!(1, all.entities.len());
assert_eq!(all.entities[0], person);
};
}
check_values_person!();
person.row_type += "a";
person
.primary_key()
.update_row_type(&connection, &person.row_type)
.await
.unwrap();
check_values_person!();
person.row_type += "a";
person
.primary_key()
.update_dyn(&connection, UpdatableColumnRef::RowType(&person.row_type))
.await
.unwrap();
check_values_person!();
person.row_type += "a";
person.email += "a";
person
.primary_key()
.update_dyn_multiple(
&connection,
&[
UpdatableColumnRef::RowType(&person.row_type),
UpdatableColumnRef::Email(&person.email),
],
)
.await
.unwrap();
check_values_person!();
crate::generated::field_name_different_combined::truncate(&connection)
.await
.unwrap();
let field_name = FieldNameDifferentCombined {
row_type: 1,
row_pub: "a".to_string(),
row_struct: "b".to_string(),
};
field_name.to_ref().insert(&connection).await.unwrap();
macro_rules! check_values_field_name {
() => {
let all = crate::generated::field_name_different_combined::select_all_in_memory(
&connection,
2,
)
.await
.unwrap();
assert_eq!(1, all.entities.len());
assert_eq!(all.entities[0], field_name);
};
}
check_values_field_name!();
field_name.primary_key().delete(&connection).await.unwrap();
let result =
crate::generated::field_name_different_combined::select_all_in_memory(&connection, 2)
.await
.unwrap();
assert!(result.entities.is_empty());
}
#[tokio::test]
#[serial_test::serial]
async fn qmd() -> Result<(), SerializeValuesError> {
let session = CachingSession::from(create_connection().await, 1);
crate::generated::person::truncate(&session).await.unwrap();
let person = Person {
name: "name".to_string(),
age: 1,
email: "myemail".to_string(),
row_type: "4".to_string(),
};
person.to_ref().insert(&session).await.unwrap();
let email = &person.email;
let transformed_type =
query_base_table!("select * from person_by_email where email = ?", email);
let mut rows = transformed_type.select(&session, None).await.unwrap();
while let Some(row) = rows.next().await {
assert_eq!(person, row.unwrap());
}
let c = &1;
let transformed_type = query!("select * from test_table where b = 1 and c = ?", c);
// The extracted query should be the columns and not * so it can be used in prepared statements
assert_eq!(
"select b, c, d, a, e from test_table where b = 1 and c = ?",
transformed_type.query
);
assert_serialized_values!(transformed_type, c);
let transformed_type = query!("select * from test_table where b = 1 and c = 2 and d = 3");
assert_eq!(
"select b, c, d, a, e from test_table where b = 1 and c = 2 and d = 3",
transformed_type.query
);
assert_eq!(LegacySerializedValues::new(), transformed_type.qv.values);
let _ = query!("select * from test_table where b = 1 and c = 2 and d > 3");
let val = &1;
let _ = query!(
"select * from test_table where b = 1 and c = 2 and d > ?",
val
);
let transformed_type = query!("select * from test_table limit 1");
assert_eq!(
"select b, c, d, a, e from test_table limit 1",
transformed_type.query
);
assert_eq!(LegacySerializedValues::new(), transformed_type.qv.values);
let transformed_type = query!("select count(*) from test_table where b = 1 and c = 2");
assert_eq!(
"select count(*) from test_table where b = 1 and c = 2",
transformed_type.query
);
assert_eq!(LegacySerializedValues::new(), transformed_type.qv.values);
let transformed_type = query!("delete from test_table where b = 1 and c = 3");
assert_eq!(
"delete from test_table where b = 1 and c = 3",
transformed_type.query
);
assert_eq!(LegacySerializedValues::new(), transformed_type.qv.values);
let a = &1;
let transformed_type = query!("select * from test_table where b = 1 and c = ?", a);
assert_eq!(
"select b, c, d, a, e from test_table where b = 1 and c = ?",
transformed_type.query
);
assert_serialized_values!(transformed_type, a);
let a = "sadas";
let transformed_type = query!("select * from another_test_table where a = 1 and b = ?", a);
assert_serialized_values!(transformed_type, a);
let a = &vec![1, 2, 3];
let transformed_type = query!("select * from test_table where b = 1 and c in ?", a);
assert_serialized_values!(transformed_type, a);
let a = &vec![1, 2];
let transformed_type = query!("select * from test_table where b = 1 and c in ? limit 1", a);
assert_serialized_values!(transformed_type, a);
let c = &vec![1, 2];
let b = &1;
let limit = &5;
let transformed_type = query!(
"select * from test_table where b = ? and c in ? limit ?",
b,
c,
limit
);
assert_serialized_values!(transformed_type, b, c, limit);
Ok(())
}
macro_rules! write_failing {
($file: ident) => {
#[test]
fn $file() {
catalytic::runtime::create_test_tables();
let t = trybuild::TestCases::new();
let current_dir = std::env::current_dir()
.unwrap()
.join("src")
.join("non_compiling_code");
t.compile_fail(current_dir.join(format!("{}.rs", stringify!($file))));
}
};
}
write_failing!(failing_wrong_type_primitive);
write_failing!(failing_wrong_type_vec);
write_failing!(non_complete_insert);
write_failing!(non_existing_column);
write_failing!(non_existing_table);
write_failing!(no_param_but_question_mark);
write_failing!(param_but_no_question_mark);
}