Skip to content

Commit

Permalink
tests: [#56] for torrents info and announce urls tables in upgrader
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Nov 30, 2022
1 parent cd95987 commit 0063289
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub struct TorrentRecordV1 {
pub info_hash: String,
pub title: String,
pub category_id: i64,
pub description: String,
pub description: Option<String>,
pub upload_date: i64,
pub file_size: i64,
pub seeders: i64,
Expand Down
38 changes: 38 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 @@ -35,6 +35,20 @@ pub struct TrackerKeyRecordV2 {
pub date_expiry: i64,
}

#[derive(Debug, Serialize, Deserialize, sqlx::FromRow)]
pub struct TorrentInfoRecordV2 {
pub torrent_id: i64,
pub title: String,
pub description: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, sqlx::FromRow, PartialEq)]
pub struct TorrentAnnounceUrlV2 {
pub announce_url_id: i64,
pub torrent_id: i64,
pub tracker_url: String,
}

pub struct SqliteDatabaseV2_0_0 {
pub pool: SqlitePool,
}
Expand Down Expand Up @@ -95,4 +109,28 @@ impl SqliteDatabaseV2_0_0 {
.fetch_one(&self.pool)
.await
}

pub async fn get_torrent_info(
&self,
torrent_id: i64,
) -> Result<TorrentInfoRecordV2, sqlx::Error> {
query_as::<_, TorrentInfoRecordV2>(
"SELECT * FROM torrust_torrent_info WHERE torrent_id = ?",
)
.bind(torrent_id)
.fetch_one(&self.pool)
.await
}

pub async fn get_torrent_announce_urls(
&self,
torrent_id: i64,
) -> Result<Vec<TorrentAnnounceUrlV2>, sqlx::Error> {
query_as::<_, TorrentAnnounceUrlV2>(
"SELECT * FROM torrust_torrent_announce_urls WHERE torrent_id = ?",
)
.bind(torrent_id)
.fetch_all(&self.pool)
.await
}
}
43 changes: 40 additions & 3 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 @@ -31,7 +31,7 @@ impl TorrentTester {
info_hash: "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_string(),
title: "title".to_string(),
category_id: 1,
description: "description".to_string(),
description: Some("description".to_string()),
upload_date: 1667546358, // 2022-11-04 07:19:18
file_size: 9219566,
seeders: 0,
Expand Down Expand Up @@ -60,10 +60,10 @@ impl TorrentTester {
let torrent_file = read_torrent_from_file(&filepath).unwrap();

self.assert_torrent(&torrent_file).await;
self.assert_torrent_info().await;
self.assert_torrent_announce_urls(&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 {
Expand Down Expand Up @@ -112,4 +112,41 @@ impl TorrentTester {
convert_timestamp_to_datetime(self.test_data.torrent.upload_date)
);
}

/// Table `torrust_torrent_info`
async fn assert_torrent_info(&self) {
let torrent_info = self
.destiny_database
.get_torrent_info(self.test_data.torrent.torrent_id)
.await
.unwrap();

assert_eq!(torrent_info.torrent_id, self.test_data.torrent.torrent_id);
assert_eq!(torrent_info.title, self.test_data.torrent.title);
assert_eq!(torrent_info.description, self.test_data.torrent.description);
}

/// Table `torrust_torrent_announce_urls`
async fn assert_torrent_announce_urls(&self, torrent_file: &Torrent) {
let torrent_announce_urls = self
.destiny_database
.get_torrent_announce_urls(self.test_data.torrent.torrent_id)
.await
.unwrap();

let urls: Vec<String> = torrent_announce_urls
.iter()
.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>>();

assert_eq!(urls, expected_urls);
}
}

0 comments on commit 0063289

Please sign in to comment.