Files
Citorrent/tests/data/test_database_manager.py

61 lines
1.9 KiB
Python

import pytest
import os
from src.data.database_manager import DatabaseManager
def test_add_and_get_torrent(tmp_path):
db_path = tmp_path / "test_citorrent.db"
db = DatabaseManager(str(db_path))
torrent_data = {
"info_hash": "abc123hash",
"name": "Test Torrent",
"save_path": "/downloads/test",
"status": "downloading",
"progress": 0.5
}
db.add_torrent(torrent_data)
retrieved = db.get_torrent("abc123hash")
assert retrieved is not None
assert retrieved["name"] == "Test Torrent"
assert retrieved["info_hash"] == "abc123hash"
def test_get_all_torrents(tmp_path):
db_path = tmp_path / "test_citorrent.db"
db = DatabaseManager(str(db_path))
torrents = [
{"info_hash": "h1", "name": "T1", "save_path": "/p1", "status": "s1", "progress": 0.1},
{"info_hash": "h2", "name": "T2", "save_path": "/p2", "status": "s2", "progress": 0.2},
]
for t in torrents:
db.add_torrent(t)
all_torrents = db.get_all_torrents()
assert len(all_torrents) == 2
hashes = [t["info_hash"] for t in all_torrents]
assert "h1" in hashes
assert "h2" in hashes
def test_update_torrent(tmp_path):
db_path = tmp_path / "test_citorrent.db"
db = DatabaseManager(str(db_path))
db.add_torrent({"info_hash": "h1", "name": "T1", "save_path": "/p1", "status": "s1", "progress": 0.1})
db.update_torrent("h1", {"progress": 0.6, "status": "completed"})
retrieved = db.get_torrent("h1")
assert retrieved["progress"] == 0.6
assert retrieved["status"] == "completed"
def test_remove_torrent(tmp_path):
db_path = tmp_path / "test_citorrent.db"
db = DatabaseManager(str(db_path))
db.add_torrent({"info_hash": "h1", "name": "T1", "save_path": "/p1", "status": "s1", "progress": 0.1})
db.remove_torrent("h1")
assert db.get_torrent("h1") is None