Skip to content

Commit

Permalink
tests: [torrust#56] for torrents table in upgrader
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Nov 30, 2022
1 parent eef980c commit f0f581f
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 46 deletions.
4 changes: 2 additions & 2 deletions src/models/torrent_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<i64>().unwrap()
Some(root_hash) => root_hash.parse::<i64>().unwrap(),
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions src/upgrades/from_v1_0_0_to_v2_0_0/databases/sqlite_v2_0_0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -136,7 +137,7 @@ impl SqliteDatabaseV2_0_0 {
user_id: i64,
username: &str,
email: &str,
email_verified: bool
email_verified: bool,
) -> Result<i64, sqlx::Error> {
query("INSERT INTO torrust_user_profiles (user_id, username, email, email_verified, bio, avatar) VALUES (?, ?, ?, ?, ?, ?)")
.bind(user_id)
Expand Down
10 changes: 8 additions & 2 deletions src/upgrades/from_v1_0_0_to_v2_0_0/upgrader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -463,7 +469,7 @@ async fn transfer_torrents(
println!("Torrents transferred");
}

fn read_torrent_from_file(path: &str) -> Result<Torrent, Box<dyn error::Error>> {
pub fn read_torrent_from_file(path: &str) -> Result<Torrent, Box<dyn error::Error>> {
let contents = match fs::read(path) {
Ok(contents) => contents,
Err(e) => return Err(e.into()),
Expand Down
Binary file not shown.
32 changes: 31 additions & 1 deletion tests/upgrades/from_v1_0_0_to_v2_0_0/sqlite_v1_0_0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use sqlx::sqlite::SqlitePoolOptions;
use sqlx::{query, SqlitePool};
use std::fs;
use torrust_index_backend::upgrades::from_v1_0_0_to_v2_0_0::databases::sqlite_v1_0_0::{
TrackerKeyRecordV1, UserRecordV1,
TorrentRecordV1, TrackerKeyRecordV1, UserRecordV1,
};

pub struct SqliteDatabaseV1_0_0 {
Expand Down Expand Up @@ -81,4 +81,34 @@ impl SqliteDatabaseV1_0_0 {
.await
.map(|v| v.last_insert_rowid())
}

pub async fn insert_torrent(&self, torrent: &TorrentRecordV1) -> Result<i64, sqlx::Error> {
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())
}
}
18 changes: 12 additions & 6 deletions tests/upgrades/from_v1_0_0_to_v2_0_0/sqlite_v2_0_0.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -82,11 +83,16 @@ impl SqliteDatabaseV2_0_0 {
&self,
tracker_key_id: i64,
) -> Result<TrackerKeyRecordV2, sqlx::Error> {
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<TorrentRecordV2, sqlx::Error> {
query_as::<_, TorrentRecordV2>("SELECT * FROM torrust_torrents WHERE torrent_id = ?")
.bind(torrent_id)
.fetch_one(&self.pool)
.await
}
}
1 change: 1 addition & 0 deletions tests/upgrades/from_v1_0_0_to_v2_0_0/testers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod torrent_tester;
pub mod tracker_keys_tester;
pub mod user_data_tester;
115 changes: 115 additions & 0 deletions tests/upgrades/from_v1_0_0_to_v2_0_0/testers/torrent_tester.rs
Original file line number Diff line number Diff line change
@@ -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<SqliteDatabaseV1_0_0>,
destiny_database: Arc<SqliteDatabaseV2_0_0>,
test_data: TestData,
}

pub struct TestData {
pub torrent: TorrentRecordV1,
pub user: UserRecordV1,
}

impl TorrentTester {
pub fn new(
source_database: Arc<SqliteDatabaseV1_0_0>,
destiny_database: Arc<SqliteDatabaseV2_0_0>,
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)
);
}
}
42 changes: 10 additions & 32 deletions tests/upgrades/from_v1_0_0_to_v2_0_0/upgrader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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<SqliteDatabaseV1_0_0> {
Expand All @@ -113,7 +92,6 @@ async fn destiny_db_connection(destiny_database_file: &str) -> Arc<SqliteDatabas

/// Reset databases from previous executions
fn reset_databases(source_database_file: &str, destiny_database_file: &str) {
// TODO: use a unique temporary dir
fs::remove_file(&source_database_file).expect("Can't remove source DB file.");
fs::remove_file(&destiny_database_file).expect("Can't remove destiny DB file.");
}

0 comments on commit f0f581f

Please sign in to comment.