reinhardt-query 0.1.0

SQL query builder for Reinhardt framework
Documentation
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
548
549
550
551
552
//! # CreateRoleStatement Unit Tests
//!
//! Comprehensive unit tests for CreateRoleStatement covering:
//! - Happy Path: Normal operations
//! - Error Path: Validation and error handling
//! - Edge Cases: Boundary values and special cases
//! - Combinations: Attribute/option combinations
//!
//! ## Test Coverage
//!
//! - Statements tested: CreateRoleStatement
//! - Code coverage target: 97%
//! - Total tests: ~40

use crate::backend::{MySqlQueryBuilder, PostgresQueryBuilder, QueryBuilder};
use crate::dcl::{CreateRoleStatement, RoleAttribute, UserOption};
use rstest::rstest;

// ============================================================================
// Happy Path Tests
// ============================================================================

#[rstest]
#[case("basic_role")]
#[case("role_with_underscores")]
#[case("RoleWithCamelCase")]
#[case("role123")]
#[case("UPPERCASE_ROLE")]
fn test_basic_creation(#[case] role_name: &str) {
	let stmt = CreateRoleStatement::new().role(role_name);

	assert_eq!(stmt.role_name, role_name);
	assert!(!stmt.if_not_exists);
	assert!(stmt.attributes.is_empty());
	assert!(stmt.options.is_empty());
}

#[rstest]
#[case(RoleAttribute::Login)]
#[case(RoleAttribute::CreateDb)]
#[case(RoleAttribute::CreateRole)]
#[case(RoleAttribute::Inherit)]
#[case(RoleAttribute::Replication)]
#[case(RoleAttribute::BypassRls)]
fn test_single_attribute(#[case] attr: RoleAttribute) {
	let stmt = CreateRoleStatement::new()
		.role("test_role")
		.attribute(attr.clone());

	assert_eq!(stmt.attributes.len(), 1);
	assert_eq!(stmt.attributes[0], attr);
}

#[rstest]
fn test_multiple_attributes() {
	let stmt = CreateRoleStatement::new()
		.role("test_role")
		.attribute(RoleAttribute::Login)
		.attribute(RoleAttribute::CreateDb)
		.attribute(RoleAttribute::ConnectionLimit(10));

	assert_eq!(stmt.attributes.len(), 3);
}

#[rstest]
fn test_attributes_method() {
	let attrs = vec![
		RoleAttribute::Login,
		RoleAttribute::CreateDb,
		RoleAttribute::Inherit,
	];

	let stmt = CreateRoleStatement::new()
		.role("test_role")
		.attributes(attrs.clone());

	assert_eq!(stmt.attributes, attrs);
}

#[rstest]
fn test_if_not_exists() {
	let stmt = CreateRoleStatement::new()
		.role("test_role")
		.if_not_exists(true);

	assert!(stmt.if_not_exists);
}

#[rstest]
fn test_single_option() {
	let opt = UserOption::Comment("Test role".to_string());
	let stmt = CreateRoleStatement::new()
		.role("test_role")
		.option(opt.clone());

	assert_eq!(stmt.options.len(), 1);
	assert_eq!(stmt.options[0], opt);
}

#[rstest]
fn test_multiple_options() {
	let stmt = CreateRoleStatement::new()
		.role("test_role")
		.option(UserOption::AccountLock)
		.option(UserOption::PasswordExpireNever);

	assert_eq!(stmt.options.len(), 2);
}

#[rstest]
fn test_options_method() {
	let opts = vec![
		UserOption::Comment("Test".to_string()),
		UserOption::AccountUnlock,
	];

	let stmt = CreateRoleStatement::new()
		.role("test_role")
		.options(opts.clone());

	assert_eq!(stmt.options, opts);
}

#[rstest]
fn test_attributes_and_options() {
	let stmt = CreateRoleStatement::new()
		.role("test_role")
		.attribute(RoleAttribute::Login)
		.option(UserOption::Comment("Test".to_string()));

	assert_eq!(stmt.attributes.len(), 1);
	assert_eq!(stmt.options.len(), 1);
}

#[rstest]
fn test_connection_limit_attribute() {
	let stmt = CreateRoleStatement::new()
		.role("test_role")
		.attribute(RoleAttribute::ConnectionLimit(100));

	assert_eq!(stmt.attributes.len(), 1);
	match &stmt.attributes[0] {
		RoleAttribute::ConnectionLimit(limit) => assert_eq!(*limit, 100),
		_ => panic!("Expected ConnectionLimit attribute"),
	}
}

#[rstest]
fn test_password_attribute() {
	let stmt = CreateRoleStatement::new()
		.role("test_role")
		.attribute(RoleAttribute::Password("secret123".to_string()));

	assert_eq!(stmt.attributes.len(), 1);
	match &stmt.attributes[0] {
		RoleAttribute::Password(pwd) => assert_eq!(pwd, "secret123"),
		_ => panic!("Expected Password attribute"),
	}
}

// ============================================================================
// Error Path Tests
// ============================================================================

#[rstest]
fn test_empty_role_name_validation() {
	let stmt = CreateRoleStatement::new();

	let result = stmt.validate();
	assert!(result.is_err());
	assert_eq!(
		result.unwrap_err(),
		"Role name cannot be empty or whitespace only"
	);
}

#[rstest]
fn test_whitespace_only_role_name() {
	let stmt = CreateRoleStatement::new().role("   ");

	let result = stmt.validate();
	assert!(result.is_err());
}

#[rstest]
fn test_empty_string_role_name() {
	let stmt = CreateRoleStatement::new().role("");

	let result = stmt.validate();
	assert!(result.is_err());
	assert_eq!(
		result.unwrap_err(),
		"Role name cannot be empty or whitespace only"
	);
}

#[rstest]
fn test_validate_with_valid_role() {
	let stmt = CreateRoleStatement::new().role("valid_role");

	let result = stmt.validate();
	assert!(result.is_ok());
}

#[rstest]
fn test_validate_with_valid_role_and_attributes() {
	let stmt = CreateRoleStatement::new()
		.role("valid_role")
		.attribute(RoleAttribute::Login)
		.attribute(RoleAttribute::CreateDb);

	let result = stmt.validate();
	assert!(result.is_ok());
}

#[rstest]
fn test_validate_with_valid_role_and_options() {
	let stmt = CreateRoleStatement::new()
		.role("valid_role")
		.option(UserOption::Comment("Test".to_string()));

	let result = stmt.validate();
	assert!(result.is_ok());
}

#[rstest]
fn test_validate_with_complete_statement() {
	let stmt = CreateRoleStatement::new()
		.role("valid_role")
		.attribute(RoleAttribute::Login)
		.option(UserOption::AccountUnlock);

	let result = stmt.validate();
	assert!(result.is_ok());
}

// ============================================================================
// Edge Case Tests
// ============================================================================

#[rstest]
#[case(0, "empty string")]
#[case(1, "single character")]
#[case(63, "boundary before limit")]
#[case(64, "boundary at limit")]
#[case(255, "long name")]
fn test_role_name_length(#[case] length: usize, #[case] _desc: &str) {
	let role_name = "a".repeat(length);
	let stmt = CreateRoleStatement::new().role(role_name.clone());

	assert_eq!(stmt.role_name, role_name);
	assert_eq!(stmt.role_name.len(), length);
}

#[rstest]
fn test_very_long_role_name() {
	// 256 characters - likely exceeds database limits
	let role_name = "a".repeat(256);
	let stmt = CreateRoleStatement::new().role(role_name);

	assert_eq!(stmt.role_name.len(), 256);
}

#[rstest]
#[case(-1, "unlimited")]
#[case(0, "no connections")]
#[case(1, "single connection")]
#[case(100, "moderate limit")]
#[case(i32::MAX, "maximum limit")]
fn test_connection_limit_values(#[case] limit: i32, #[case] _desc: &str) {
	let stmt = CreateRoleStatement::new()
		.role("test_role")
		.attribute(RoleAttribute::ConnectionLimit(limit));

	assert_eq!(stmt.attributes.len(), 1);
}

#[rstest]
fn test_empty_password() {
	let stmt = CreateRoleStatement::new()
		.role("test_role")
		.attribute(RoleAttribute::Password("".to_string()));

	assert_eq!(stmt.attributes.len(), 1);
	match &stmt.attributes[0] {
		RoleAttribute::Password(pwd) => assert_eq!(pwd, ""),
		_ => panic!("Expected Password attribute"),
	}
}

#[rstest]
fn test_password_with_special_characters() {
	let password = "P@ssw0rd!#$%^&*()";
	let stmt = CreateRoleStatement::new()
		.role("test_role")
		.attribute(RoleAttribute::Password(password.to_string()));

	assert_eq!(stmt.attributes.len(), 1);
	match &stmt.attributes[0] {
		RoleAttribute::Password(pwd) => assert_eq!(pwd, password),
		_ => panic!("Expected Password attribute"),
	}
}

#[rstest]
fn test_valid_until_attribute() {
	let timestamp = "2025-12-31T23:59:59Z";
	let stmt = CreateRoleStatement::new()
		.role("test_role")
		.attribute(RoleAttribute::ValidUntil(timestamp.to_string()));

	assert_eq!(stmt.attributes.len(), 1);
	match &stmt.attributes[0] {
		RoleAttribute::ValidUntil(ts) => assert_eq!(ts, timestamp),
		_ => panic!("Expected ValidUntil attribute"),
	}
}

#[rstest]
fn test_in_role_attribute() {
	let roles = vec!["role1".to_string(), "role2".to_string()];
	let stmt = CreateRoleStatement::new()
		.role("test_role")
		.attribute(RoleAttribute::InRole(roles.clone()));

	assert_eq!(stmt.attributes.len(), 1);
	match &stmt.attributes[0] {
		RoleAttribute::InRole(r) => assert_eq!(r, &roles),
		_ => panic!("Expected InRole attribute"),
	}
}

#[rstest]
fn test_encrypted_password_attribute() {
	let encrypted = "md5a1b2c3d4e5f6";
	let stmt = CreateRoleStatement::new()
		.role("test_role")
		.attribute(RoleAttribute::EncryptedPassword(encrypted.to_string()));

	assert_eq!(stmt.attributes.len(), 1);
	match &stmt.attributes[0] {
		RoleAttribute::EncryptedPassword(pwd) => assert_eq!(pwd, encrypted),
		_ => panic!("Expected EncryptedPassword attribute"),
	}
}

// ============================================================================
// Combination Tests
// ============================================================================

#[rstest]
fn test_boolean_attribute_pairs() {
	// SuperUser vs NoSuperUser
	let stmt1 = CreateRoleStatement::new()
		.role("role1")
		.attribute(RoleAttribute::SuperUser);
	let stmt2 = CreateRoleStatement::new()
		.role("role2")
		.attribute(RoleAttribute::NoSuperUser);

	assert_ne!(stmt1.attributes, stmt2.attributes);
}

#[rstest]
fn test_all_boolean_attributes() {
	let attrs = vec![
		RoleAttribute::SuperUser,
		RoleAttribute::CreateDb,
		RoleAttribute::CreateRole,
		RoleAttribute::Inherit,
		RoleAttribute::Login,
		RoleAttribute::Replication,
		RoleAttribute::BypassRls,
	];

	let stmt = CreateRoleStatement::new()
		.role("test_role")
		.attributes(attrs.clone());

	assert_eq!(stmt.attributes.len(), 7);
}

#[rstest]
fn test_mixed_attributes() {
	let stmt = CreateRoleStatement::new()
		.role("test_role")
		.attribute(RoleAttribute::Login)
		.attribute(RoleAttribute::ConnectionLimit(10))
		.attribute(RoleAttribute::Password("secret".to_string()))
		.attribute(RoleAttribute::ValidUntil("2025-12-31".to_string()));

	assert_eq!(stmt.attributes.len(), 4);
}

#[rstest]
fn test_all_user_option_types() {
	let stmt = CreateRoleStatement::new()
		.role("test_role")
		.option(UserOption::Password("secret".to_string()))
		.option(UserOption::AccountLock)
		.option(UserOption::PasswordExpireNever)
		.option(UserOption::Comment("Test".to_string()));

	assert_eq!(stmt.options.len(), 4);
}

#[rstest]
fn test_comprehensive_role_statement() {
	let stmt = CreateRoleStatement::new()
		.role("app_user")
		.if_not_exists(true)
		.attributes(vec![
			RoleAttribute::Login,
			RoleAttribute::CreateDb,
			RoleAttribute::ConnectionLimit(10),
			RoleAttribute::Password("secret123".to_string()),
		])
		.options(vec![
			UserOption::Comment("Application user".to_string()),
			UserOption::AccountUnlock,
		]);

	assert_eq!(stmt.role_name, "app_user");
	assert!(stmt.if_not_exists);
	assert_eq!(stmt.attributes.len(), 4);
	assert_eq!(stmt.options.len(), 2);
}

#[rstest]
fn test_clone_statement() {
	let stmt1 = CreateRoleStatement::new()
		.role("test_role")
		.attribute(RoleAttribute::Login);
	let stmt2 = stmt1.clone();

	assert_eq!(stmt1.role_name, stmt2.role_name);
	assert_eq!(stmt1.attributes, stmt2.attributes);
}

#[rstest]
fn test_default_statement() {
	let stmt = CreateRoleStatement::new();

	assert_eq!(stmt.role_name, "");
	assert!(!stmt.if_not_exists);
	assert!(stmt.attributes.is_empty());
	assert!(stmt.options.is_empty());
}

#[rstest]
fn test_builder_pattern_chaining() {
	let stmt = CreateRoleStatement::new()
		.role("test_role")
		.if_not_exists(true)
		.attribute(RoleAttribute::Login)
		.attribute(RoleAttribute::CreateDb)
		.option(UserOption::Comment("Test".to_string()))
		.option(UserOption::AccountUnlock);

	assert_eq!(stmt.role_name, "test_role");
	assert!(stmt.if_not_exists);
	assert_eq!(stmt.attributes.len(), 2);
	assert_eq!(stmt.options.len(), 2);
}

// ============================================================================
// SQL Generation Tests (PostgreSQL)
// ============================================================================

#[rstest]
fn test_postgres_basic_create_role() {
	let builder = PostgresQueryBuilder::new();
	let stmt = CreateRoleStatement::new().role("test_role");

	let (sql, values) = builder.build_create_role(&stmt);

	assert_eq!(sql, r#"CREATE ROLE "test_role""#);
	assert!(values.is_empty());
}

#[rstest]
fn test_postgres_create_role_with_login() {
	let builder = PostgresQueryBuilder::new();
	let stmt = CreateRoleStatement::new()
		.role("test_role")
		.attribute(RoleAttribute::Login);

	let (sql, values) = builder.build_create_role(&stmt);

	assert!(sql.contains(r#"CREATE ROLE "test_role""#));
	assert!(sql.contains("WITH"));
	assert!(sql.contains("LOGIN"));
	assert!(values.is_empty());
}

#[rstest]
fn test_postgres_create_role_with_password() {
	let builder = PostgresQueryBuilder::new();
	let stmt = CreateRoleStatement::new()
		.role("test_role")
		.attribute(RoleAttribute::Password("secret".to_string()));

	let (sql, values) = builder.build_create_role(&stmt);

	assert!(sql.contains(r#"CREATE ROLE "test_role""#));
	assert!(sql.contains("PASSWORD"));
	// Password should be parameterized
	assert!(!sql.contains("secret"));
	assert_eq!(values.len(), 1);
}

// ============================================================================
// SQL Generation Tests (MySQL)
// ============================================================================

#[rstest]
fn test_mysql_basic_create_role() {
	let builder = MySqlQueryBuilder::new();
	let stmt = CreateRoleStatement::new().role("test_role");

	let (sql, values) = builder.build_create_role(&stmt);

	assert_eq!(sql, r#"CREATE ROLE `test_role`"#);
	assert!(values.is_empty());
}

#[rstest]
fn test_mysql_create_role_if_not_exists() {
	let builder = MySqlQueryBuilder::new();
	let stmt = CreateRoleStatement::new()
		.role("test_role")
		.if_not_exists(true);

	let (sql, values) = builder.build_create_role(&stmt);

	assert!(sql.contains(r#"CREATE ROLE IF NOT EXISTS `test_role`"#));
	assert!(values.is_empty());
}

#[rstest]
fn test_mysql_create_role_with_comment() {
	let builder = MySqlQueryBuilder::new();
	let stmt = CreateRoleStatement::new()
		.role("test_role")
		.option(UserOption::Comment("Test role".to_string()));

	let (sql, values) = builder.build_create_role(&stmt);

	assert!(sql.contains(r#"CREATE ROLE `test_role`"#));
	assert!(sql.contains("COMMENT"));
	assert!(values.is_empty());
}