From afffaefc62f21070ee9a9aa4e3f98367142dad39 Mon Sep 17 00:00:00 2001
From: Jose Celano <josecelano@gmail.com>
Date: Fri, 11 Nov 2022 10:15:16 +0000
Subject: [PATCH] tests: [#56] for torrents files table in upgrader

---
 src/models/torrent_file.rs                    | 25 ++++++++++++++
 .../from_v1_0_0_to_v2_0_0/upgrader.rs         |  5 +--
 .../from_v1_0_0_to_v2_0_0/sqlite_v2_0_0.rs    | 19 +++++++++++
 .../testers/torrent_tester.rs                 | 33 ++++++++++++++-----
 4 files changed, 69 insertions(+), 13 deletions(-)

diff --git a/src/models/torrent_file.rs b/src/models/torrent_file.rs
index 62319036..6e015d1a 100644
--- a/src/models/torrent_file.rs
+++ b/src/models/torrent_file.rs
@@ -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)]
@@ -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)]
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 cfb17be9..91e42931 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
@@ -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
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 2f0ba395..20a55daa 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
@@ -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,
 }
@@ -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
+    }
 }
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
index 3f636506..2b6d92b4 100644
--- 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
@@ -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 {
@@ -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!();
+        }
+    }
 }