@@ -94,3 +94,134 @@ def test_rss_database(db_session):
9494 db .add (RSSItem (url = rss_url , name = "Test RSS" ))
9595 result = db .search_id (1 )
9696 assert result .url == rss_url
97+
98+
99+ # ---------------------------------------------------------------------------
100+ # TorrentDatabase qb_hash methods
101+ # ---------------------------------------------------------------------------
102+
103+
104+ def test_torrent_search_by_qb_hash (db_session ):
105+ """Test searching torrent by qBittorrent hash."""
106+ db = TorrentDatabase (db_session )
107+
108+ # Create torrent with qb_hash
109+ torrent = Torrent (
110+ name = "[SubGroup] Test Anime - 01 [1080p].mkv" ,
111+ url = "https://example.com/torrent1" ,
112+ qb_hash = "abc123def456" ,
113+ )
114+ db .add (torrent )
115+
116+ # Search by qb_hash
117+ result = db .search_by_qb_hash ("abc123def456" )
118+ assert result is not None
119+ assert result .name == torrent .name
120+ assert result .qb_hash == "abc123def456"
121+
122+
123+ def test_torrent_search_by_qb_hash_not_found (db_session ):
124+ """Test searching non-existent qb_hash returns None."""
125+ db = TorrentDatabase (db_session )
126+
127+ result = db .search_by_qb_hash ("nonexistent_hash" )
128+ assert result is None
129+
130+
131+ def test_torrent_search_by_url (db_session ):
132+ """Test searching torrent by URL."""
133+ db = TorrentDatabase (db_session )
134+
135+ url = "https://mikanani.me/Download/torrent123.torrent"
136+ torrent = Torrent (
137+ name = "[SubGroup] Test Anime - 02 [1080p].mkv" ,
138+ url = url ,
139+ )
140+ db .add (torrent )
141+
142+ # Search by URL
143+ result = db .search_by_url (url )
144+ assert result is not None
145+ assert result .url == url
146+ assert result .name == torrent .name
147+
148+
149+ def test_torrent_search_by_url_not_found (db_session ):
150+ """Test searching non-existent URL returns None."""
151+ db = TorrentDatabase (db_session )
152+
153+ result = db .search_by_url ("https://nonexistent.com/torrent.torrent" )
154+ assert result is None
155+
156+
157+ def test_torrent_update_qb_hash (db_session ):
158+ """Test updating qb_hash for existing torrent."""
159+ db = TorrentDatabase (db_session )
160+
161+ # Create torrent without qb_hash
162+ torrent = Torrent (
163+ name = "[SubGroup] Test Anime - 03 [1080p].mkv" ,
164+ url = "https://example.com/torrent3" ,
165+ )
166+ db .add (torrent )
167+ assert torrent .qb_hash is None
168+
169+ # Update qb_hash
170+ success = db .update_qb_hash (torrent .id , "new_hash_value" )
171+ assert success is True
172+
173+ # Verify update
174+ result = db .search (torrent .id )
175+ assert result .qb_hash == "new_hash_value"
176+
177+
178+ def test_torrent_update_qb_hash_nonexistent (db_session ):
179+ """Test updating qb_hash for non-existent torrent returns False."""
180+ db = TorrentDatabase (db_session )
181+
182+ success = db .update_qb_hash (99999 , "some_hash" )
183+ assert success is False
184+
185+
186+ def test_torrent_with_bangumi_id (db_session ):
187+ """Test torrent with bangumi_id for offset lookup."""
188+ db = TorrentDatabase (db_session )
189+
190+ # Create torrent linked to a bangumi
191+ torrent = Torrent (
192+ name = "[SubGroup] Test Anime - 04 [1080p].mkv" ,
193+ url = "https://example.com/torrent4" ,
194+ bangumi_id = 42 ,
195+ qb_hash = "hash_for_bangumi_42" ,
196+ )
197+ db .add (torrent )
198+
199+ # Search and verify bangumi_id is preserved
200+ result = db .search_by_qb_hash ("hash_for_bangumi_42" )
201+ assert result is not None
202+ assert result .bangumi_id == 42
203+
204+
205+ def test_torrent_qb_hash_index_efficient (db_session ):
206+ """Test that qb_hash lookups work correctly with multiple torrents."""
207+ db = TorrentDatabase (db_session )
208+
209+ # Add multiple torrents
210+ torrents = [
211+ Torrent (name = f"Torrent { i } " , url = f"https://example.com/{ i } " , qb_hash = f"hash_{ i } " )
212+ for i in range (10 )
213+ ]
214+ db .add_all (torrents )
215+
216+ # Verify we can find specific torrents by hash
217+ result = db .search_by_qb_hash ("hash_5" )
218+ assert result is not None
219+ assert result .name == "Torrent 5"
220+
221+ result = db .search_by_qb_hash ("hash_9" )
222+ assert result is not None
223+ assert result .name == "Torrent 9"
224+
225+ # Non-existent hash
226+ result = db .search_by_qb_hash ("hash_100" )
227+ assert result is None
0 commit comments