Skip to content

Commit afffaef

Browse files
committed
tests: [#56] for torrents files table in upgrader
1 parent 82b84a3 commit afffaef

File tree

4 files changed

+69
-13
lines changed

4 files changed

+69
-13
lines changed

src/models/torrent_file.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ impl TorrentInfo {
5555
Some(root_hash) => root_hash.parse::<i64>().unwrap(),
5656
}
5757
}
58+
59+
pub fn is_a_single_file_torrent(&self) -> bool {
60+
self.length.is_some()
61+
}
62+
63+
pub fn is_a_multiple_file_torrent(&self) -> bool {
64+
self.files.is_some()
65+
}
5866
}
5967

6068
#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
@@ -192,6 +200,23 @@ impl Torrent {
192200
}
193201
}
194202
}
203+
204+
pub fn announce_urls(&self) -> Vec<String> {
205+
self.announce_list
206+
.clone()
207+
.unwrap()
208+
.into_iter()
209+
.flatten()
210+
.collect::<Vec<String>>()
211+
}
212+
213+
pub fn is_a_single_file_torrent(&self) -> bool {
214+
self.info.is_a_single_file_torrent()
215+
}
216+
217+
pub fn is_a_multiple_file_torrent(&self) -> bool {
218+
self.info.is_a_multiple_file_torrent()
219+
}
195220
}
196221

197222
#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize, sqlx::FromRow)]

src/upgrades/from_v1_0_0_to_v2_0_0/upgrader.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -359,10 +359,7 @@ async fn transfer_torrents(
359359

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

362-
let _is_torrent_with_multiple_files = torrent_from_file.info.files.is_some();
363-
let is_torrent_with_a_single_file = torrent_from_file.info.length.is_some();
364-
365-
if is_torrent_with_a_single_file {
362+
if torrent_from_file.is_a_single_file_torrent() {
366363
// The torrent contains only one file then:
367364
// - "path" is NULL
368365
// - "md5sum" can be NULL

tests/upgrades/from_v1_0_0_to_v2_0_0/sqlite_v2_0_0.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ pub struct TorrentAnnounceUrlV2 {
4949
pub tracker_url: String,
5050
}
5151

52+
#[derive(Debug, Serialize, Deserialize, sqlx::FromRow, PartialEq)]
53+
pub struct TorrentFileV2 {
54+
pub file_id: i64,
55+
pub torrent_id: i64,
56+
pub md5sum: Option<String>,
57+
pub length: i64,
58+
pub path: Option<String>,
59+
}
60+
5261
pub struct SqliteDatabaseV2_0_0 {
5362
pub pool: SqlitePool,
5463
}
@@ -133,4 +142,14 @@ impl SqliteDatabaseV2_0_0 {
133142
.fetch_all(&self.pool)
134143
.await
135144
}
145+
146+
pub async fn get_torrent_files(
147+
&self,
148+
torrent_id: i64,
149+
) -> Result<Vec<TorrentFileV2>, sqlx::Error> {
150+
query_as::<_, TorrentFileV2>("SELECT * FROM torrust_torrent_files WHERE torrent_id = ?")
151+
.bind(torrent_id)
152+
.fetch_all(&self.pool)
153+
.await
154+
}
136155
}

tests/upgrades/from_v1_0_0_to_v2_0_0/testers/torrent_tester.rs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ impl TorrentTester {
6262
self.assert_torrent(&torrent_file).await;
6363
self.assert_torrent_info().await;
6464
self.assert_torrent_announce_urls(&torrent_file).await;
65-
// TODO
66-
// `torrust_torrent_files`,
65+
self.assert_torrent_files(&torrent_file).await;
6766
}
6867

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

142-
let expected_urls = torrent_file
143-
.announce_list
144-
.clone()
145-
.unwrap()
146-
.into_iter()
147-
.flatten()
148-
.collect::<Vec<String>>();
141+
let expected_urls = torrent_file.announce_urls();
149142

150143
assert_eq!(urls, expected_urls);
151144
}
145+
146+
/// Table `torrust_torrent_files`
147+
async fn assert_torrent_files(&self, torrent_file: &Torrent) {
148+
let db_torrent_files = self
149+
.destiny_database
150+
.get_torrent_files(self.test_data.torrent.torrent_id)
151+
.await
152+
.unwrap();
153+
154+
if torrent_file.is_a_single_file_torrent() {
155+
let db_torrent_file = &db_torrent_files[0];
156+
assert_eq!(
157+
db_torrent_file.torrent_id,
158+
self.test_data.torrent.torrent_id
159+
);
160+
assert!(db_torrent_file.md5sum.is_none());
161+
assert_eq!(db_torrent_file.length, torrent_file.info.length.unwrap());
162+
assert!(db_torrent_file.path.is_none());
163+
} else {
164+
todo!();
165+
}
166+
}
152167
}

0 commit comments

Comments
 (0)