Skip to content

Commit

Permalink
tests: [#56] for torrents files table in upgrader
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Nov 30, 2022
1 parent 82b84a3 commit afffaef
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 13 deletions.
25 changes: 25 additions & 0 deletions src/models/torrent_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ impl TorrentInfo {
Some(root_hash) => root_hash.parse::<i64>().unwrap(),
}
}

pub fn is_a_single_file_torrent(&self) -> bool {
self.length.is_some()
}

pub fn is_a_multiple_file_torrent(&self) -> bool {
self.files.is_some()
}
}

#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -192,6 +200,23 @@ impl Torrent {
}
}
}

pub fn announce_urls(&self) -> Vec<String> {
self.announce_list
.clone()
.unwrap()
.into_iter()
.flatten()
.collect::<Vec<String>>()
}

pub fn is_a_single_file_torrent(&self) -> bool {
self.info.is_a_single_file_torrent()
}

pub fn is_a_multiple_file_torrent(&self) -> bool {
self.info.is_a_multiple_file_torrent()
}
}

#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize, sqlx::FromRow)]
Expand Down
5 changes: 1 addition & 4 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 @@ -359,10 +359,7 @@ async fn transfer_torrents(

println!("[v2][torrust_torrent_files] adding torrent files");

let _is_torrent_with_multiple_files = torrent_from_file.info.files.is_some();
let is_torrent_with_a_single_file = torrent_from_file.info.length.is_some();

if is_torrent_with_a_single_file {
if torrent_from_file.is_a_single_file_torrent() {
// The torrent contains only one file then:
// - "path" is NULL
// - "md5sum" can be NULL
Expand Down
19 changes: 19 additions & 0 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
Expand Up @@ -49,6 +49,15 @@ pub struct TorrentAnnounceUrlV2 {
pub tracker_url: String,
}

#[derive(Debug, Serialize, Deserialize, sqlx::FromRow, PartialEq)]
pub struct TorrentFileV2 {
pub file_id: i64,
pub torrent_id: i64,
pub md5sum: Option<String>,
pub length: i64,
pub path: Option<String>,
}

pub struct SqliteDatabaseV2_0_0 {
pub pool: SqlitePool,
}
Expand Down Expand Up @@ -133,4 +142,14 @@ impl SqliteDatabaseV2_0_0 {
.fetch_all(&self.pool)
.await
}

pub async fn get_torrent_files(
&self,
torrent_id: i64,
) -> Result<Vec<TorrentFileV2>, sqlx::Error> {
query_as::<_, TorrentFileV2>("SELECT * FROM torrust_torrent_files WHERE torrent_id = ?")
.bind(torrent_id)
.fetch_all(&self.pool)
.await
}
}
33 changes: 24 additions & 9 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
Expand Up @@ -62,8 +62,7 @@ impl TorrentTester {
self.assert_torrent(&torrent_file).await;
self.assert_torrent_info().await;
self.assert_torrent_announce_urls(&torrent_file).await;
// TODO
// `torrust_torrent_files`,
self.assert_torrent_files(&torrent_file).await;
}

pub fn torrent_file_path(&self, upload_path: &str, torrent_id: i64) -> String {
Expand Down Expand Up @@ -139,14 +138,30 @@ impl TorrentTester {
.map(|torrent_announce_url| torrent_announce_url.tracker_url.to_string())
.collect();

let expected_urls = torrent_file
.announce_list
.clone()
.unwrap()
.into_iter()
.flatten()
.collect::<Vec<String>>();
let expected_urls = torrent_file.announce_urls();

assert_eq!(urls, expected_urls);
}

/// Table `torrust_torrent_files`
async fn assert_torrent_files(&self, torrent_file: &Torrent) {
let db_torrent_files = self
.destiny_database
.get_torrent_files(self.test_data.torrent.torrent_id)
.await
.unwrap();

if torrent_file.is_a_single_file_torrent() {
let db_torrent_file = &db_torrent_files[0];
assert_eq!(
db_torrent_file.torrent_id,
self.test_data.torrent.torrent_id
);
assert!(db_torrent_file.md5sum.is_none());
assert_eq!(db_torrent_file.length, torrent_file.info.length.unwrap());
assert!(db_torrent_file.path.is_none());
} else {
todo!();
}
}
}

0 comments on commit afffaef

Please sign in to comment.