Skip to content

Commit 700482d

Browse files
committedFeb 11, 2025
test: [torrust#1251] reset database before running db driver tests
1 parent b9188c7 commit 700482d

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed
 

‎packages/tracker-core/src/databases/driver/mod.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,7 @@ mod tests {
9696
// tests, we share the same database. If we want to isolate the tests in
9797
// the future, we can create a new database for each test.
9898

99-
create_database_tables(driver).await.unwrap();
100-
101-
// todo: truncate tables otherwise they will increase in size over time.
102-
// That's not a problem on CI when the database is always newly created.
99+
database_setup(driver).await;
103100

104101
// Persistent torrents (stats)
105102

@@ -121,8 +118,22 @@ mod tests {
121118
handling_the_whitelist::it_should_remove_an_infohash_from_the_whitelist(driver);
122119
handling_the_whitelist::it_should_fail_trying_to_add_the_same_infohash_twice(driver);
123120
handling_the_whitelist::it_load_the_whitelist(driver);
121+
}
124122

125-
driver.drop_database_tables().unwrap();
123+
/// It initializes the database schema.
124+
///
125+
/// Since the drop SQL queries don't check if the tables already exist,
126+
/// we have to create them first, and then drop them.
127+
///
128+
/// The method to drop tables does not use "DROP TABLE IF EXISTS". We can
129+
/// change this function when we update the `Database::drop_database_tables`
130+
/// method to use "DROP TABLE IF EXISTS".
131+
async fn database_setup(driver: &Arc<Box<dyn Database>>) {
132+
create_database_tables(driver).await.expect("database tables creation failed");
133+
driver.drop_database_tables().expect("old database tables deletion failed");
134+
create_database_tables(driver)
135+
.await
136+
.expect("database tables creation from empty schema failed");
126137
}
127138

128139
async fn create_database_tables(driver: &Arc<Box<dyn Database>>) -> Result<(), Box<dyn std::error::Error>> {

‎packages/tracker-core/src/databases/driver/mysql.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ mod tests {
264264
265265
`TORRUST_TRACKER_CORE_RUN_MYSQL_DRIVER_TEST=true cargo test`
266266
267-
The `Database`` trait is very simple and we only have one driver that needs
267+
The `Database` trait is very simple and we only have one driver that needs
268268
a container. In the future we might want to use different approaches like:
269269
270270
- https://github.com/testcontainers/testcontainers-rs/issues/707

0 commit comments

Comments
 (0)
Please sign in to comment.