__author__ = 'dev' import unittest from feed.torrent_cli_entry_handler import TorrentCliEntryHandler from feed.feed import Feed import urllib import os from config.config import Config import shutil import subprocess class TestTorrentCliEntryHandler(unittest.TestCase): def setUp(self): self.__feed = Feed("sbk", "podcast", "application/x-bittorrent", "testfeeds/podcast/sbk/sbk.xml") self.__test_torrent = "http://bitlove.org/nitramred/staatsbuergerkunde-mp3/SBK028_Arbeit.mp3.torrent" self.__test_torrent2 = "testfeeds/podcast/SBK028_Arbeit.mp3.torrent" self.__test_torrent3 = "http://bitlove.org/nitramred/staatsbuergerkunde-mp3/SBK028_Arbeit.mp3.torren" self.__test_torrent4 = "testfeeds/podcast/SBK028_Arbeit.mp3.trrent" self.__torrent_file = "/home/dev/projects/feedcrawler/testenv/testtorrents/SBK028_Arbeit.mp3.torrent" self.__torrent_folder = Config["TorrentStorageFolder"] self.__tceh = TorrentCliEntryHandler(self.__feed) def tearDown(self): if os.path.exists(self.__torrent_file): os.remove(self.__torrent_file) if os.path.exists(self.__torrent_folder): shutil.rmtree(self.__torrent_folder) def test_download_torrent(self): self.__tceh.download_torrent(self.__test_torrent) self.assertTrue(os.path.exists(self.__torrent_file)) os.remove(self.__torrent_file) self.__tceh.download_torrent(self.__test_torrent2) self.assertTrue(os.path.exists(self.__torrent_file)) def test_download_torrent_wrong_url(self): self.assertRaises(urllib.error.HTTPError, self.__tceh.download_torrent,self.__test_torrent3) self.assertRaises(ValueError, self.__tceh.download_torrent, self.__test_torrent4) def test_init_torrent_folder(self): self.__tceh.init_torrent_folder(self.__torrent_folder) self.assertTrue(os.path.exists(self.__torrent_folder)) self.assertTrue(os.path.exists(TorrentCliEntryHandler.SettingFile)) def test_start_torrent(self): tc = self.__tceh.start_torrent("/home/dev/projects/feedcrawler/testenv/testfeeds/podcast/SBK028_Arbeit.mp3.torrent", "testtorrents/", "testtorrents/") self.assertTrue(isinstance(tc, subprocess.Popen)) tc.terminate() if __name__ == '__main__': unittest.main()