Skip to content

Commit f155195

Browse files
Pierre-Luc GagnéCopilot
andcommitted
refactor(tests): add RAII scope_guard for test cleanup, remove Cleanup suite
- Add scope_guard<F> template inside anonymous namespace so every integration test's DROP TABLE runs even when the test throws mid-way - Apply scope_guard to all 36 tests that create/own a table, wrapping the drop call with (void) to silence [[nodiscard]] in destructors - Remove the fragile end-of-suite "Cleanup" suite that was racing with individual test teardown and polluting unrelated runs - All 36 tests in the integration suite now self-clean independently Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent eb17212 commit f155195

1 file changed

Lines changed: 62 additions & 18 deletions

File tree

tests/integration/mysql/test_query_builder.cpp

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,20 @@ struct trade {
6868
};
6969
}
7070

71+
template <typename F>
72+
struct scope_guard {
73+
explicit scope_guard(F fn) : fn_(std::move(fn)) {}
74+
~scope_guard() noexcept { fn_(); }
75+
scope_guard(scope_guard const&) = delete;
76+
scope_guard& operator=(scope_guard const&) = delete;
77+
scope_guard(scope_guard&&) = delete;
78+
scope_guard& operator=(scope_guard&&) = delete;
79+
private:
80+
F fn_;
81+
};
82+
template <typename F>
83+
scope_guard(F) -> scope_guard<F>;
84+
7185
// A database struct registering `trade` for validate_database<> tests.
7286
struct trade_db : ds_mysql::database_schema {
7387
using tables = std::tuple<trade>;
@@ -150,6 +164,7 @@ suite<"DDL Integration"> ddl_integration_suite = [] {
150164

151165
auto const db = mysql_database::connect(*config);
152166
expect(fatal(db));
167+
auto _ = scope_guard{[&] { (void)(db->execute(drop_table<trade>().if_exists())); }};
153168

154169
expect(db->execute(drop_table<trade>().if_exists()).has_value()) << "Failed to drop table";
155170
expect(db->execute(create_table<trade>()).has_value()) << "Failed to create trade table";
@@ -165,6 +180,7 @@ suite<"DDL Integration"> ddl_integration_suite = [] {
165180

166181
auto const db = mysql_database::connect(*config);
167182
expect(fatal(db));
183+
auto _ = scope_guard{[&] { (void)(db->execute(drop_table<trade>().if_exists())); }};
168184

169185
expect(db->execute(create_table<trade>().if_not_exists()).has_value());
170186
expect(db->execute(create_table<trade>().if_not_exists()).has_value());
@@ -176,6 +192,7 @@ suite<"DDL Integration"> ddl_integration_suite = [] {
176192

177193
auto const db = mysql_database::connect(*config);
178194
expect(fatal(db));
195+
auto _ = scope_guard{[&] { (void)(db->execute(drop_table<trade>().if_exists())); }};
179196

180197
expect(db->execute(drop_table<trade>().if_exists().then().create_table<trade>()).has_value())
181198
<< "Chained DROP/CREATE should succeed";
@@ -197,6 +214,7 @@ suite<"INSERT Integration"> insert_integration_suite = [] {
197214

198215
auto const db = mysql_database::connect(*config);
199216
expect(fatal(db));
217+
auto _ = scope_guard{[&] { (void)(db->execute(drop_table<trade>().if_exists())); }};
200218

201219
expect(db->execute(drop_table<trade>().if_exists().then().create_table<trade>()).has_value());
202220

@@ -220,6 +238,7 @@ suite<"INSERT Integration"> insert_integration_suite = [] {
220238

221239
auto const db = mysql_database::connect(*config);
222240
expect(fatal(db));
241+
auto _ = scope_guard{[&] { (void)(db->execute(drop_table<trade>().if_exists())); }};
223242

224243
expect(db->execute(drop_table<trade>().if_exists().then().create_table<trade>()).has_value());
225244

@@ -243,6 +262,7 @@ suite<"INSERT Integration"> insert_integration_suite = [] {
243262

244263
auto const db = mysql_database::connect(*config);
245264
expect(fatal(db));
265+
auto _ = scope_guard{[&] { (void)(db->execute(drop_table<temporal_precision_trade>().if_exists())); }};
246266

247267
expect(db->execute(drop_table<temporal_precision_trade>().if_exists()).has_value());
248268
expect(db->execute(create_table<temporal_precision_trade>()).has_value());
@@ -280,6 +300,7 @@ suite<"SELECT Integration"> select_integration_suite = [] {
280300

281301
auto const db = mysql_database::connect(*config);
282302
expect(fatal(db));
303+
auto _ = scope_guard{[&] { (void)(db->execute(drop_table<trade>().if_exists())); }};
283304

284305
expect(db->execute(create_table<trade>().if_not_exists()).has_value());
285306
expect(db->execute(delete_from<trade>()).has_value());
@@ -305,6 +326,7 @@ suite<"SELECT Integration"> select_integration_suite = [] {
305326

306327
auto const db = mysql_database::connect(*config);
307328
expect(fatal(db));
329+
auto _ = scope_guard{[&] { (void)(db->execute(drop_table<trade>().if_exists())); }};
308330

309331
expect(db->execute(create_table<trade>().if_not_exists()).has_value());
310332
expect(db->execute(delete_from<trade>()).has_value());
@@ -327,6 +349,7 @@ suite<"SELECT Integration"> select_integration_suite = [] {
327349

328350
auto const db = mysql_database::connect(*config);
329351
expect(fatal(db));
352+
auto _ = scope_guard{[&] { (void)(db->execute(drop_table<trade>().if_exists())); }};
330353

331354
expect(db->execute(create_table<trade>().if_not_exists()).has_value());
332355
expect(db->execute(delete_from<trade>()).has_value());
@@ -355,6 +378,7 @@ suite<"SELECT Integration"> select_integration_suite = [] {
355378

356379
auto const db = mysql_database::connect(*config);
357380
expect(fatal(db));
381+
auto _ = scope_guard{[&] { (void)(db->execute(drop_table<trade>().if_exists())); }};
358382

359383
expect(db->execute(create_table<trade>().if_not_exists()).has_value());
360384
expect(db->execute(delete_from<trade>()).has_value());
@@ -387,6 +411,7 @@ suite<"UPDATE Integration"> update_integration_suite = [] {
387411

388412
auto const db = mysql_database::connect(*config);
389413
expect(fatal(db));
414+
auto _ = scope_guard{[&] { (void)(db->execute(drop_table<trade>().if_exists())); }};
390415

391416
expect(db->execute(create_table<trade>().if_not_exists()).has_value());
392417
expect(db->execute(delete_from<trade>()).has_value());
@@ -416,6 +441,7 @@ suite<"UPDATE Integration"> update_integration_suite = [] {
416441

417442
auto const db = mysql_database::connect(*config);
418443
expect(fatal(db));
444+
auto _ = scope_guard{[&] { (void)(db->execute(drop_table<trade>().if_exists())); }};
419445

420446
expect(db->execute(create_table<trade>().if_not_exists()).has_value());
421447
expect(db->execute(delete_from<trade>()).has_value());
@@ -446,6 +472,7 @@ suite<"DELETE Integration"> delete_integration_suite = [] {
446472

447473
auto const db = mysql_database::connect(*config);
448474
expect(fatal(db));
475+
auto _ = scope_guard{[&] { (void)(db->execute(drop_table<trade>().if_exists())); }};
449476

450477
expect(db->execute(create_table<trade>().if_not_exists()).has_value());
451478
expect(db->execute(delete_from<trade>()).has_value());
@@ -477,6 +504,7 @@ suite<"DELETE Integration"> delete_integration_suite = [] {
477504

478505
auto const db = mysql_database::connect(*config);
479506
expect(fatal(db));
507+
auto _ = scope_guard{[&] { (void)(db->execute(drop_table<trade>().if_exists())); }};
480508

481509
expect(db->execute(create_table<trade>().if_not_exists()).has_value());
482510
expect(db->execute(delete_from<trade>()).has_value());
@@ -498,6 +526,7 @@ suite<"WHERE Operator Syntax Integration"> where_operator_integration_suite = []
498526

499527
auto const db = mysql_database::connect(*config);
500528
expect(fatal(db));
529+
auto _ = scope_guard{[&] { (void)(db->execute(drop_table<trade>().if_exists())); }};
501530

502531
expect(db->execute(create_table<trade>().if_not_exists()).has_value());
503532
expect(db->execute(delete_from<trade>()).has_value());
@@ -520,6 +549,7 @@ suite<"WHERE Operator Syntax Integration"> where_operator_integration_suite = []
520549

521550
auto const db = mysql_database::connect(*config);
522551
expect(fatal(db));
552+
auto _ = scope_guard{[&] { (void)(db->execute(drop_table<trade>().if_exists())); }};
523553

524554
expect(db->execute(create_table<trade>().if_not_exists()).has_value());
525555
expect(db->execute(delete_from<trade>()).has_value());
@@ -543,6 +573,7 @@ suite<"WHERE Operator Syntax Integration"> where_operator_integration_suite = []
543573

544574
auto const db = mysql_database::connect(*config);
545575
expect(fatal(db));
576+
auto _ = scope_guard{[&] { (void)(db->execute(drop_table<trade>().if_exists())); }};
546577

547578
expect(db->execute(create_table<trade>().if_not_exists()).has_value());
548579
expect(db->execute(delete_from<trade>()).has_value());
@@ -567,6 +598,7 @@ suite<"WHERE Operator Syntax Integration"> where_operator_integration_suite = []
567598

568599
auto const db = mysql_database::connect(*config);
569600
expect(fatal(db));
601+
auto _ = scope_guard{[&] { (void)(db->execute(drop_table<trade>().if_exists())); }};
570602

571603
expect(db->execute(create_table<trade>().if_not_exists()).has_value());
572604
expect(db->execute(delete_from<trade>()).has_value());
@@ -590,6 +622,7 @@ suite<"WHERE Operator Syntax Integration"> where_operator_integration_suite = []
590622

591623
auto const db = mysql_database::connect(*config);
592624
expect(fatal(db));
625+
auto _ = scope_guard{[&] { (void)(db->execute(drop_table<trade>().if_exists())); }};
593626

594627
expect(db->execute(create_table<trade>().if_not_exists()).has_value());
595628
expect(db->execute(delete_from<trade>()).has_value());
@@ -613,6 +646,7 @@ suite<"WHERE Operator Syntax Integration"> where_operator_integration_suite = []
613646

614647
auto const db = mysql_database::connect(*config);
615648
expect(fatal(db));
649+
auto _ = scope_guard{[&] { (void)(db->execute(drop_table<trade>().if_exists())); }};
616650

617651
expect(db->execute(create_table<trade>().if_not_exists()).has_value());
618652
expect(db->execute(delete_from<trade>()).has_value());
@@ -646,6 +680,7 @@ suite<"Schema Validation Integration"> schema_validation_suite = [] {
646680

647681
auto const db = mysql_database::connect(*config);
648682
expect(fatal(db));
683+
auto _ = scope_guard{[&] { (void)(db->execute(drop_table<trade>().if_exists())); }};
649684

650685
expect(db->execute(create_table<trade>().if_not_exists()).has_value());
651686

@@ -660,6 +695,7 @@ suite<"Schema Validation Integration"> schema_validation_suite = [] {
660695

661696
auto const db = mysql_database::connect(*config);
662697
expect(fatal(db));
698+
auto _ = scope_guard{[&] { (void)(db->execute(drop_table<trade>().if_exists())); }};
663699

664700
expect(db->execute(drop_table<trade>().if_exists()).has_value());
665701

@@ -673,6 +709,10 @@ suite<"Schema Validation Integration"> schema_validation_suite = [] {
673709

674710
auto const db = mysql_database::connect(*config);
675711
expect(fatal(db));
712+
auto _ = scope_guard{[&] {
713+
(void)(db->execute(drop_table<trade_few_columns>().if_exists()));
714+
(void)(db->execute(drop_table<trade>().if_exists()));
715+
}};
676716

677717
// Create the trade_few_columns DB table with the same 9 columns as trade —
678718
// the C++ struct only defines 2, so validate_table must detect the count mismatch.
@@ -692,6 +732,7 @@ suite<"Schema Validation Integration"> schema_validation_suite = [] {
692732

693733
auto const db = mysql_database::connect(*config);
694734
expect(fatal(db));
735+
auto _ = scope_guard{[&] { (void)(db->execute(drop_table<trade>().if_exists())); }};
695736

696737
expect(db->execute(create_table<trade>().if_not_exists()).has_value());
697738

@@ -706,6 +747,7 @@ suite<"Schema Validation Integration"> schema_validation_suite = [] {
706747

707748
auto const db = mysql_database::connect(*config);
708749
expect(fatal(db));
750+
auto _ = scope_guard{[&] { (void)(db->execute(drop_table<trade>().if_exists())); }};
709751

710752
expect(db->execute(drop_table<trade>().if_exists()).has_value());
711753

@@ -719,6 +761,10 @@ suite<"Schema Validation Integration"> schema_validation_suite = [] {
719761

720762
auto const db = mysql_database::connect(*config);
721763
expect(fatal(db));
764+
auto _ = scope_guard{[&] {
765+
(void)(db->execute(drop_table<trade_few_columns>().if_exists()));
766+
(void)(db->execute(drop_table<trade>().if_exists()));
767+
}};
722768

723769
// Ensure trade_few_columns DB table has 9 columns; the C++ struct defines only 2.
724770
expect(db->execute(create_table<trade>().if_not_exists()).has_value());
@@ -737,6 +783,10 @@ suite<"Schema Validation Integration"> schema_validation_suite = [] {
737783

738784
auto const db = mysql_database::connect(*config);
739785
expect(fatal(db));
786+
auto _ = scope_guard{[&] {
787+
(void)(db->execute(drop_table<trade>().if_exists()));
788+
(void)(db->execute(drop_table<account>().if_exists()));
789+
}};
740790

741791
expect(db->execute(create_table<trade>().if_not_exists()).has_value());
742792
expect(db->execute(create_table<account>().if_not_exists()).has_value());
@@ -752,6 +802,10 @@ suite<"Schema Validation Integration"> schema_validation_suite = [] {
752802

753803
auto const db = mysql_database::connect(*config);
754804
expect(fatal(db));
805+
auto _ = scope_guard{[&] {
806+
(void)(db->execute(drop_table<trade>().if_exists()));
807+
(void)(db->execute(drop_table<account>().if_exists()));
808+
}};
755809

756810
expect(db->execute(create_table<trade>().if_not_exists()).has_value());
757811
expect(db->execute(drop_table<account>().if_exists()).has_value());
@@ -761,24 +815,6 @@ suite<"Schema Validation Integration"> schema_validation_suite = [] {
761815
};
762816
};
763817

764-
// ===================================================================
765-
// Cleanup — drop table at end
766-
// ===================================================================
767-
768-
suite<"Cleanup"> cleanup_suite = [] {
769-
"drop trade table"_test = [] {
770-
auto const config = mysql_config_from_env();
771-
expect(fatal(config.has_value()));
772-
773-
auto const db = mysql_database::connect(*config);
774-
expect(fatal(db));
775-
776-
expect(db->execute(drop_table<trade>().if_exists()).has_value()) << "Table drop should succeed";
777-
expect(db->execute(drop_table<trade_few_columns>().if_exists()).has_value());
778-
expect(db->execute(drop_table<account>().if_exists()).has_value());
779-
};
780-
};
781-
782818
// ===================================================================
783819
// Connection Error
784820
// ===================================================================
@@ -808,6 +844,7 @@ suite<"Execute Failure"> execute_failure_suite = [] {
808844

809845
auto const db = mysql_database::connect(*config);
810846
expect(fatal(db));
847+
auto _ = scope_guard{[&] { (void)(db->execute(drop_table<trade>().if_exists())); }};
811848

812849
expect(db->execute(drop_table<trade>().if_exists()).has_value());
813850
expect(db->execute(create_table<trade>()).has_value());
@@ -832,6 +869,7 @@ suite<"Raw SQL DML"> raw_sql_dml_suite = [] {
832869

833870
auto const db = mysql_database::connect(*config);
834871
expect(fatal(db));
872+
auto _ = scope_guard{[&] { (void)(db->execute(drop_table<trade>().if_exists())); }};
835873

836874
expect(db->execute(create_table<trade>().if_not_exists()).has_value());
837875

@@ -847,6 +885,7 @@ suite<"Raw SQL DML"> raw_sql_dml_suite = [] {
847885

848886
auto const db = mysql_database::connect(*config);
849887
expect(fatal(db));
888+
auto _ = scope_guard{[&] { (void)(db->query(raw_sql{"DROP TABLE IF EXISTS raw_ddl_test"})); }};
850889

851890
// DDL via raw_sql also returns no result set.
852891
auto const result =
@@ -880,6 +919,7 @@ suite<"Boolean Column Coverage"> boolean_column_suite = [] {
880919

881920
auto const db = mysql_database::connect(*config);
882921
expect(fatal(db));
922+
auto _ = scope_guard{[&] { (void)(db->execute(drop_table<bool_table>().if_exists())); }};
883923

884924
expect(db->execute(drop_table<bool_table>().if_exists()).has_value());
885925
expect(db->execute(create_table<bool_table>()).has_value());
@@ -951,6 +991,7 @@ suite<"validate_field Error Paths"> validate_field_errors_suite = [] {
951991

952992
auto const db = mysql_database::connect(*config);
953993
expect(fatal(db));
994+
auto _ = scope_guard{[&] { (void)(db->query(raw_sql{"DROP TABLE IF EXISTS mismatch_name_tbl"})); }};
954995

955996
// Create a table where column 2 is 'wrong_col_name' but struct expects 'right_name'.
956997
auto drop = db->query(raw_sql{"DROP TABLE IF EXISTS mismatch_name_tbl"});
@@ -977,6 +1018,7 @@ suite<"validate_field Error Paths"> validate_field_errors_suite = [] {
9771018

9781019
auto const db = mysql_database::connect(*config);
9791020
expect(fatal(db));
1021+
auto _ = scope_guard{[&] { (void)(db->query(raw_sql{"DROP TABLE IF EXISTS type_mismatch_tbl"})); }};
9801022

9811023
// Create a table where 'code' is INT but struct expects VARCHAR(32).
9821024
auto drop = db->query(raw_sql{"DROP TABLE IF EXISTS type_mismatch_tbl"});
@@ -1003,6 +1045,7 @@ suite<"validate_field Error Paths"> validate_field_errors_suite = [] {
10031045

10041046
auto const db = mysql_database::connect(*config);
10051047
expect(fatal(db));
1048+
auto _ = scope_guard{[&] { (void)(db->query(raw_sql{"DROP TABLE IF EXISTS nullable_mismatch_tbl"})); }};
10061049

10071050
// Create a table where 'code' is nullable but struct expects NOT NULL.
10081051
auto drop = db->query(raw_sql{"DROP TABLE IF EXISTS nullable_mismatch_tbl"});
@@ -1045,6 +1088,7 @@ suite<"Typed Query Column Count Coverage"> typed_query_col_count_suite = [] {
10451088

10461089
auto const db = mysql_database::connect(*config);
10471090
expect(fatal(db));
1091+
auto _ = scope_guard{[&] { (void)(db->execute(drop_table<trade>().if_exists())); }};
10481092

10491093
// Ensure at least one row exists so the result set is non-empty.
10501094
expect(db->execute(create_table<trade>().if_not_exists()).has_value());

0 commit comments

Comments
 (0)