From f0f581faebb48823e46edb0408214591152d0225 Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Thu, 10 Nov 2022 17:46:27 +0000 Subject: [PATCH] tests: [#56] for torrents table in upgrader --- src/models/torrent_file.rs | 4 +- .../databases/sqlite_v1_0_0.rs | 2 +- .../databases/sqlite_v2_0_0.rs | 5 +- .../from_v1_0_0_to_v2_0_0/upgrader.rs | 10 +- .../fixtures/uploads/1.torrent | Bin 0 -> 1128 bytes .../from_v1_0_0_to_v2_0_0/sqlite_v1_0_0.rs | 32 ++++- .../from_v1_0_0_to_v2_0_0/sqlite_v2_0_0.rs | 18 ++- .../from_v1_0_0_to_v2_0_0/testers/mod.rs | 1 + .../testers/torrent_tester.rs | 115 ++++++++++++++++++ .../from_v1_0_0_to_v2_0_0/upgrader.rs | 42 ++----- 10 files changed, 183 insertions(+), 46 deletions(-) create mode 100644 tests/upgrades/from_v1_0_0_to_v2_0_0/fixtures/uploads/1.torrent create mode 100644 tests/upgrades/from_v1_0_0_to_v2_0_0/testers/torrent_tester.rs diff --git a/src/models/torrent_file.rs b/src/models/torrent_file.rs index c7ab26a7..62319036 100644 --- a/src/models/torrent_file.rs +++ b/src/models/torrent_file.rs @@ -45,14 +45,14 @@ impl TorrentInfo { pub fn get_pieces_as_string(&self) -> String { match &self.pieces { None => "".to_string(), - Some(byte_buf) => bytes_to_hex(byte_buf.as_ref()) + Some(byte_buf) => bytes_to_hex(byte_buf.as_ref()), } } pub fn get_root_hash_as_i64(&self) -> i64 { match &self.root_hash { None => 0i64, - Some(root_hash) => root_hash.parse::().unwrap() + Some(root_hash) => root_hash.parse::().unwrap(), } } } diff --git a/src/upgrades/from_v1_0_0_to_v2_0_0/databases/sqlite_v1_0_0.rs b/src/upgrades/from_v1_0_0_to_v2_0_0/databases/sqlite_v1_0_0.rs index 3328fd43..3d42a4b3 100644 --- a/src/upgrades/from_v1_0_0_to_v2_0_0/databases/sqlite_v1_0_0.rs +++ b/src/upgrades/from_v1_0_0_to_v2_0_0/databases/sqlite_v1_0_0.rs @@ -10,7 +10,7 @@ pub struct CategoryRecord { pub name: String, } -#[derive(Debug, Serialize, Deserialize, sqlx::FromRow)] +#[derive(Debug, Serialize, Deserialize, sqlx::FromRow, Clone)] pub struct UserRecordV1 { pub user_id: i64, pub username: String, diff --git a/src/upgrades/from_v1_0_0_to_v2_0_0/databases/sqlite_v2_0_0.rs b/src/upgrades/from_v1_0_0_to_v2_0_0/databases/sqlite_v2_0_0.rs index b7d1a570..bee97bc2 100644 --- a/src/upgrades/from_v1_0_0_to_v2_0_0/databases/sqlite_v2_0_0.rs +++ b/src/upgrades/from_v1_0_0_to_v2_0_0/databases/sqlite_v2_0_0.rs @@ -14,6 +14,7 @@ pub struct CategoryRecordV2 { pub name: String, } +#[derive(Debug, Serialize, Deserialize, sqlx::FromRow)] pub struct TorrentRecordV2 { pub torrent_id: i64, pub uploader_id: i64, @@ -50,7 +51,7 @@ impl TorrentRecordV2 { } } -fn convert_timestamp_to_datetime(timestamp: i64) -> String { +pub fn convert_timestamp_to_datetime(timestamp: i64) -> String { // The expected format in database is: 2022-11-04 09:53:57 // MySQL uses a DATETIME column and SQLite uses a TEXT column. @@ -136,7 +137,7 @@ impl SqliteDatabaseV2_0_0 { user_id: i64, username: &str, email: &str, - email_verified: bool + email_verified: bool, ) -> Result { query("INSERT INTO torrust_user_profiles (user_id, username, email, email_verified, bio, avatar) VALUES (?, ?, ?, ?, ?, ?)") .bind(user_id) diff --git a/src/upgrades/from_v1_0_0_to_v2_0_0/upgrader.rs b/src/upgrades/from_v1_0_0_to_v2_0_0/upgrader.rs index 48048973..cfb17be9 100644 --- a/src/upgrades/from_v1_0_0_to_v2_0_0/upgrader.rs +++ b/src/upgrades/from_v1_0_0_to_v2_0_0/upgrader.rs @@ -326,7 +326,13 @@ async fn transfer_torrents( let filepath = format!("{}/{}.torrent", upload_path, &torrent.torrent_id); - let torrent_from_file = read_torrent_from_file(&filepath).unwrap(); + let torrent_from_file_result = read_torrent_from_file(&filepath); + + if torrent_from_file_result.is_err() { + panic!("Error torrent file not found: {:?}", &filepath); + } + + let torrent_from_file = torrent_from_file_result.unwrap(); let id = dest_database .insert_torrent(&TorrentRecordV2::from_v1_data( @@ -463,7 +469,7 @@ async fn transfer_torrents( println!("Torrents transferred"); } -fn read_torrent_from_file(path: &str) -> Result> { +pub fn read_torrent_from_file(path: &str) -> Result> { let contents = match fs::read(path) { Ok(contents) => contents, Err(e) => return Err(e.into()), diff --git a/tests/upgrades/from_v1_0_0_to_v2_0_0/fixtures/uploads/1.torrent b/tests/upgrades/from_v1_0_0_to_v2_0_0/fixtures/uploads/1.torrent new file mode 100644 index 0000000000000000000000000000000000000000..faa30f4cda2290bada6c2462f8e7782dae6246b3 GIT binary patch literal 1128 zcmYeXuu9C!%P-AKPBk&GDorV{($_C3N=(j9Ez$+@^wN_uOG@-I^V0IIEDZEvN)3(S zx^#0gi%W8HNYHF#3AHCDKRGccBfq%B%FNQt5^lJWIaJ=z$Xw4r&k$8MHPzfIIX^cy zHLt|T%*r=0FC{f6sVKihAvm=}!N|bGq5?z|R2UjqB^RY8mZYXABvl$(SQR<}J(6Ek z1k|TcW};`JX9)BJSVd-jo>eWrI%!68k!pcB~uLztqL+zlT(Woit_VIjE$_$-KiF4 ztKjTSVv9M{p>vJ(hyAt3*B_jVX)O|{y~?ASmztW2VTO@aZfZ%QLRo52G0>HnMnHGv zCFZ8$af+c4#99TIYs`!-On?SM+yZo&Rqez=-nqWJI%bC`gj`iC>Wyl%f2erB{P_3X zIpxz7(^M^v-aNayo8x_t%Ir;FkDS%I&wYUB!`k~r6P|8!^ij@npWQd#Z50o1-^JUU+ob&py3nhBbRX{CuSR^Gw6~5C)c?t(h|Aod?VOuC3aixa?5F%+=fC z6Z9QcsO~dbY&(~or!r&4mAT2D>lGr>)beL!=Ng%ut>qAnle)mb8NS-US|Q=!9ijGX zpP#$>7o>BV8HRpR_?EL&G+(E4YNl9lW3A!GZF|j~&IodE`lIc+v~}B`vJ#icsR-{U zRssVpg@|xBHZ`(Z?%Km>dsE(G;~L+@gzf$}B4VQ@nfr?F{}yw$H~#D>a^kZZyJX*t zi}nv&llRvAX)`ywz3zXq;Kj*`yt|GZ5U(-P^X)%#}n zZEMQkXtY&_&!Rv&L)-JlR|h$}Xx0_Xj-MCLbd>N{W$T{5Vr4x?K=}Xgq?naEzBQKh ztxj>R-kro?<9WNjDM;SoT-94uljNBAWeG>?*ZwcrfAiSI*(bB>?)83XxFly?GTC8H zLTC43DTcQ-jQYpePJ|Yp2v2R6xqs+c_t|eh&GPw6uI@Z;OCv#fOzfC z2FwaYvz<7PD13Z;rTF0I-TX}zq4N`8)r;iv{t- Result { + query( + "INSERT INTO torrust_torrents ( + torrent_id, + uploader, + info_hash, + title, + category_id, + description, + upload_date, + file_size, + seeders, + leechers + ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", + ) + .bind(torrent.torrent_id) + .bind(torrent.uploader.clone()) + .bind(torrent.info_hash.clone()) + .bind(torrent.title.clone()) + .bind(torrent.category_id) + .bind(torrent.description.clone()) + .bind(torrent.upload_date) + .bind(torrent.file_size) + .bind(torrent.seeders) + .bind(torrent.leechers) + .execute(&self.pool) + .await + .map(|v| v.last_insert_rowid()) + } } diff --git a/tests/upgrades/from_v1_0_0_to_v2_0_0/sqlite_v2_0_0.rs b/tests/upgrades/from_v1_0_0_to_v2_0_0/sqlite_v2_0_0.rs index 1f3c25a7..17331572 100644 --- a/tests/upgrades/from_v1_0_0_to_v2_0_0/sqlite_v2_0_0.rs +++ b/tests/upgrades/from_v1_0_0_to_v2_0_0/sqlite_v2_0_0.rs @@ -1,6 +1,7 @@ use serde::{Deserialize, Serialize}; use sqlx::sqlite::SqlitePoolOptions; use sqlx::{query_as, SqlitePool}; +use torrust_index_backend::upgrades::from_v1_0_0_to_v2_0_0::databases::sqlite_v2_0_0::TorrentRecordV2; #[derive(Debug, Serialize, Deserialize, sqlx::FromRow)] pub struct UserRecordV2 { @@ -82,11 +83,16 @@ impl SqliteDatabaseV2_0_0 { &self, tracker_key_id: i64, ) -> Result { - query_as::<_, TrackerKeyRecordV2>( - "SELECT * FROM torrust_tracker_keys WHERE user_id = ?", - ) - .bind(tracker_key_id) - .fetch_one(&self.pool) - .await + query_as::<_, TrackerKeyRecordV2>("SELECT * FROM torrust_tracker_keys WHERE user_id = ?") + .bind(tracker_key_id) + .fetch_one(&self.pool) + .await + } + + pub async fn get_torrent(&self, torrent_id: i64) -> Result { + query_as::<_, TorrentRecordV2>("SELECT * FROM torrust_torrents WHERE torrent_id = ?") + .bind(torrent_id) + .fetch_one(&self.pool) + .await } } diff --git a/tests/upgrades/from_v1_0_0_to_v2_0_0/testers/mod.rs b/tests/upgrades/from_v1_0_0_to_v2_0_0/testers/mod.rs index 7285ed3c..6445ec5b 100644 --- a/tests/upgrades/from_v1_0_0_to_v2_0_0/testers/mod.rs +++ b/tests/upgrades/from_v1_0_0_to_v2_0_0/testers/mod.rs @@ -1,2 +1,3 @@ +pub mod torrent_tester; pub mod tracker_keys_tester; pub mod user_data_tester; diff --git a/tests/upgrades/from_v1_0_0_to_v2_0_0/testers/torrent_tester.rs b/tests/upgrades/from_v1_0_0_to_v2_0_0/testers/torrent_tester.rs new file mode 100644 index 00000000..d6c14045 --- /dev/null +++ b/tests/upgrades/from_v1_0_0_to_v2_0_0/testers/torrent_tester.rs @@ -0,0 +1,115 @@ +use crate::upgrades::from_v1_0_0_to_v2_0_0::sqlite_v1_0_0::SqliteDatabaseV1_0_0; +use crate::upgrades::from_v1_0_0_to_v2_0_0::sqlite_v2_0_0::SqliteDatabaseV2_0_0; +use std::sync::Arc; +use torrust_index_backend::models::torrent_file::Torrent; +use torrust_index_backend::upgrades::from_v1_0_0_to_v2_0_0::databases::sqlite_v1_0_0::{ + TorrentRecordV1, UserRecordV1, +}; +use torrust_index_backend::upgrades::from_v1_0_0_to_v2_0_0::databases::sqlite_v2_0_0::convert_timestamp_to_datetime; +use torrust_index_backend::upgrades::from_v1_0_0_to_v2_0_0::upgrader::read_torrent_from_file; + +pub struct TorrentTester { + source_database: Arc, + destiny_database: Arc, + test_data: TestData, +} + +pub struct TestData { + pub torrent: TorrentRecordV1, + pub user: UserRecordV1, +} + +impl TorrentTester { + pub fn new( + source_database: Arc, + destiny_database: Arc, + user: &UserRecordV1, + ) -> Self { + let torrent = TorrentRecordV1 { + torrent_id: 1, + uploader: user.username.clone(), + info_hash: "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_string(), + title: "title".to_string(), + category_id: 1, + description: "description".to_string(), + upload_date: 1667546358, // 2022-11-04 07:19:18 + file_size: 9219566, + seeders: 0, + leechers: 0, + }; + + Self { + source_database, + destiny_database, + test_data: TestData { + torrent, + user: user.clone(), + }, + } + } + + pub async fn load_data_into_source_db(&self) { + self.source_database + .insert_torrent(&self.test_data.torrent) + .await + .unwrap(); + } + + pub async fn assert(&self, upload_path: &str) { + let filepath = self.torrent_file_path(upload_path, self.test_data.torrent.torrent_id); + let torrent_file = read_torrent_from_file(&filepath).unwrap(); + + self.assert_torrent(&torrent_file).await; + // TODO + // `torrust_torrent_files`, + // `torrust_torrent_info` + // `torrust_torrent_announce_urls` + } + + pub fn torrent_file_path(&self, upload_path: &str, torrent_id: i64) -> String { + format!("{}/{}.torrent", &upload_path, &torrent_id) + } + + /// Table `torrust_torrents` + async fn assert_torrent(&self, torrent_file: &Torrent) { + let imported_torrent = self + .destiny_database + .get_torrent(self.test_data.torrent.torrent_id) + .await + .unwrap(); + + assert_eq!( + imported_torrent.torrent_id, + self.test_data.torrent.torrent_id + ); + assert_eq!(imported_torrent.uploader_id, self.test_data.user.user_id); + assert_eq!( + imported_torrent.category_id, + self.test_data.torrent.category_id + ); + assert_eq!(imported_torrent.info_hash, self.test_data.torrent.info_hash); + assert_eq!(imported_torrent.size, self.test_data.torrent.file_size); + assert_eq!(imported_torrent.name, torrent_file.info.name); + assert_eq!( + imported_torrent.pieces, + torrent_file.info.get_pieces_as_string() + ); + assert_eq!( + imported_torrent.piece_length, + torrent_file.info.piece_length + ); + if torrent_file.info.private.is_none() { + assert_eq!(imported_torrent.private, Some(0)); + } else { + assert_eq!(imported_torrent.private, torrent_file.info.private); + } + assert_eq!( + imported_torrent.root_hash, + torrent_file.info.get_root_hash_as_i64() + ); + assert_eq!( + imported_torrent.date_uploaded, + convert_timestamp_to_datetime(self.test_data.torrent.upload_date) + ); + } +} diff --git a/tests/upgrades/from_v1_0_0_to_v2_0_0/upgrader.rs b/tests/upgrades/from_v1_0_0_to_v2_0_0/upgrader.rs index d0314328..22093624 100644 --- a/tests/upgrades/from_v1_0_0_to_v2_0_0/upgrader.rs +++ b/tests/upgrades/from_v1_0_0_to_v2_0_0/upgrader.rs @@ -13,6 +13,7 @@ //! to see the "upgrader" command output. use crate::upgrades::from_v1_0_0_to_v2_0_0::sqlite_v1_0_0::SqliteDatabaseV1_0_0; use crate::upgrades::from_v1_0_0_to_v2_0_0::sqlite_v2_0_0::SqliteDatabaseV2_0_0; +use crate::upgrades::from_v1_0_0_to_v2_0_0::testers::torrent_tester::TorrentTester; use crate::upgrades::from_v1_0_0_to_v2_0_0::testers::tracker_keys_tester::TrackerKeysTester; use crate::upgrades::from_v1_0_0_to_v2_0_0::testers::user_data_tester::UserDataTester; use std::fs; @@ -26,6 +27,7 @@ async fn upgrades_data_from_version_v1_0_0_to_v2_0_0() { // Directories let fixtures_dir = "./tests/upgrades/from_v1_0_0_to_v2_0_0/fixtures/".to_string(); let output_dir = "./tests/upgrades/from_v1_0_0_to_v2_0_0/output/".to_string(); + let upload_path = format!("{}uploads/", &fixtures_dir); // Files let source_database_file = format!("{}source.db", output_dir); @@ -44,63 +46,40 @@ async fn upgrades_data_from_version_v1_0_0_to_v2_0_0() { // Load data into database v1 - // `torrust_users`, `torrust_user_profiles` and `torrust_user_authentication` tables - let user_data_tester = UserDataTester::new( source_database.clone(), destiny_database.clone(), &execution_time, ); - user_data_tester.load_data_into_source_db().await; - // `torrust_tracker_keys` table - let tracker_keys_tester = TrackerKeysTester::new( source_database.clone(), destiny_database.clone(), user_data_tester.test_data.user.user_id, ); - tracker_keys_tester.load_data_into_source_db().await; - // `torrust_torrents` table - - // TODO + let torrent_tester = TorrentTester::new( + source_database.clone(), + destiny_database.clone(), + &user_data_tester.test_data.user, + ); + torrent_tester.load_data_into_source_db().await; // Run the upgrader let args = Arguments { source_database_file: source_database_file.clone(), destiny_database_file: destiny_database_file.clone(), - upload_path: format!("{}uploads/", fixtures_dir), + upload_path: upload_path.clone(), }; upgrade(&args, &execution_time).await; // Assertions in database v2 - // `torrust_users`, `torrust_user_profiles` and `torrust_user_authentication` tables - user_data_tester.assert().await; - - // `torrust_tracker_keys` table - tracker_keys_tester.assert().await; - - // `torrust_torrents` table - - // TODO - - // `torrust_torrent_files` table - - // TODO - - // `torrust_torrent_info` table - - // TODO - - // `torrust_torrent_announce_urls` table - - // TODO + torrent_tester.assert(&upload_path).await; } async fn source_db_connection(source_database_file: &str) -> Arc { @@ -113,7 +92,6 @@ async fn destiny_db_connection(destiny_database_file: &str) -> Arc