[Tests] Inherit from BaseTestCase in testcases

* Testcases in test_torrent.py and test_torrentmanager.py creates
  components and should therefore inherit from BaseTestCase.
* Cleanup in test_json_api.py
This commit is contained in:
bendikro 2016-05-11 19:42:31 +02:00
parent a8dac9bd3a
commit 260268f62b
3 changed files with 23 additions and 61 deletions

View File

@ -215,8 +215,6 @@ class JSONRequestFailedTestCase(JSONBase):
self.assertTrue("in test_raise_error" in self.core.stderr_out) self.assertTrue("in test_raise_error" in self.core.stderr_out)
extra_callback["deferred"].addCallback(on_test_raise) extra_callback["deferred"].addCallback(on_test_raise)
self.d_test_raise_error_log = extra_callback["deferred"]
d.addCallback(self.start_core, custom_script=custom_script, print_stderr=False, d.addCallback(self.start_core, custom_script=custom_script, print_stderr=False,
timeout=5, extra_callbacks=[extra_callback]) timeout=5, extra_callbacks=[extra_callback])
d.addCallbacks(self.connect_client, self.terminate_core) d.addCallbacks(self.connect_client, self.terminate_core)

View File

@ -2,12 +2,10 @@ from __future__ import print_function
import base64 import base64
import os import os
import sys
import time import time
from twisted.internet import defer, reactor from twisted.internet import defer, reactor
from twisted.internet.task import deferLater from twisted.internet.task import deferLater
from twisted.trial import unittest
import deluge.component as component import deluge.component as component
import deluge.core.torrent import deluge.core.torrent
@ -18,55 +16,27 @@ from deluge.core.rpcserver import RPCServer
from deluge.core.torrent import Torrent from deluge.core.torrent import Torrent
from deluge.core.torrentmanager import TorrentState from deluge.core.torrentmanager import TorrentState
config_setup = False from .basetest import BaseTestCase
core = None
rpcserver = None
eventmanager = None
# This is called by torrent.py when calling component.get("...") class TorrentTestCase(BaseTestCase):
def get(key):
if key is "Core":
return core
elif key is "RPCServer":
return rpcserver
elif key is "EventManager":
return core.eventmanager
elif key is "TorrentManager":
return core.torrentmanager
else:
return None
class TorrentTestCase(unittest.TestCase):
def setup_config(self): def setup_config(self):
global config_setup
config_setup = True
config_dir = common.set_tmp_config_dir() config_dir = common.set_tmp_config_dir()
core_config = deluge.config.Config("core.conf", defaults=deluge.core.preferencesmanager.DEFAULT_PREFS, core_config = deluge.config.Config("core.conf", defaults=deluge.core.preferencesmanager.DEFAULT_PREFS,
config_dir=config_dir) config_dir=config_dir)
core_config.save() core_config.save()
def setUp(self): # NOQA def set_up(self):
# Save component and set back on teardown
self.original_component = deluge.core.torrent.component
deluge.core.torrent.component = sys.modules[__name__]
self.setup_config() self.setup_config()
global rpcserver RPCServer(listen=False)
global core self.core = Core()
rpcserver = RPCServer(listen=False)
core = Core()
self.session = lt.session() self.session = lt.session()
self.torrent = None self.torrent = None
return component.start() return component.start()
def tearDown(self): # NOQA def tear_down(self):
deluge.core.torrent.component = self.original_component return component.shutdown()
def on_shutdown(result):
component._ComponentRegistry.components = {}
return component.shutdown().addCallback(on_shutdown)
def print_priority_list(self, priorities): def print_priority_list(self, priorities):
tmp = '' tmp = ''
@ -148,8 +118,8 @@ class TorrentTestCase(unittest.TestCase):
def test_torrent_error_data_missing(self): def test_torrent_error_data_missing(self):
options = {"seed_mode": True} options = {"seed_mode": True}
filename = os.path.join(os.path.dirname(__file__), "test_torrent.file.torrent") filename = os.path.join(os.path.dirname(__file__), "test_torrent.file.torrent")
torrent_id = yield core.add_torrent_file(filename, base64.encodestring(open(filename).read()), options) torrent_id = yield self.core.add_torrent_file(filename, base64.encodestring(open(filename).read()), options)
torrent = core.torrentmanager.torrents[torrent_id] torrent = self.core.torrentmanager.torrents[torrent_id]
self.assert_state(torrent, "Seeding") self.assert_state(torrent, "Seeding")
@ -162,8 +132,8 @@ class TorrentTestCase(unittest.TestCase):
def test_torrent_error_resume_original_state(self): def test_torrent_error_resume_original_state(self):
options = {"seed_mode": True, "add_paused": True} options = {"seed_mode": True, "add_paused": True}
filename = os.path.join(os.path.dirname(__file__), "test_torrent.file.torrent") filename = os.path.join(os.path.dirname(__file__), "test_torrent.file.torrent")
torrent_id = yield core.add_torrent_file(filename, base64.encodestring(open(filename).read()), options) torrent_id = yield self.core.add_torrent_file(filename, base64.encodestring(open(filename).read()), options)
torrent = core.torrentmanager.torrents[torrent_id] torrent = self.core.torrentmanager.torrents[torrent_id]
orig_state = "Paused" orig_state = "Paused"
self.assert_state(torrent, orig_state) self.assert_state(torrent, orig_state)
@ -206,13 +176,13 @@ class TorrentTestCase(unittest.TestCase):
filename = os.path.join(os.path.dirname(__file__), "test_torrent.file.torrent") filename = os.path.join(os.path.dirname(__file__), "test_torrent.file.torrent")
filedump = open(filename).read() filedump = open(filename).read()
torrent_id = yield core.torrentmanager.add(state=torrent_state, filedump=filedump, torrent_id = yield self.core.torrentmanager.add(state=torrent_state, filedump=filedump,
resume_data=lt.bencode(resume_data)) resume_data=lt.bencode(resume_data))
torrent = core.torrentmanager.torrents[torrent_id] torrent = self.core.torrentmanager.torrents[torrent_id]
def assert_resume_data(): def assert_resume_data():
self.assert_state(torrent, "Error") self.assert_state(torrent, "Error")
tm_resume_data = lt.bdecode(core.torrentmanager.resume_data[torrent.torrent_id]) tm_resume_data = lt.bdecode(self.core.torrentmanager.resume_data[torrent.torrent_id])
self.assertEquals(tm_resume_data, resume_data) self.assertEquals(tm_resume_data, resume_data)
yield deferLater(reactor, 0.5, assert_resume_data) yield deferLater(reactor, 0.5, assert_resume_data)

View File

@ -3,7 +3,6 @@ import os
import warnings import warnings
from twisted.internet import defer from twisted.internet import defer
from twisted.trial import unittest
from deluge import component from deluge import component
from deluge.core.core import Core from deluge.core.core import Core
@ -11,37 +10,32 @@ from deluge.core.rpcserver import RPCServer
from deluge.error import InvalidTorrentError from deluge.error import InvalidTorrentError
from . import common from . import common
from .basetest import BaseTestCase
warnings.filterwarnings("ignore", category=RuntimeWarning) warnings.filterwarnings("ignore", category=RuntimeWarning)
warnings.resetwarnings() warnings.resetwarnings()
class TorrentmanagerTestCase(unittest.TestCase): class TorrentmanagerTestCase(BaseTestCase):
def setUp(self): # NOQA def set_up(self):
common.set_tmp_config_dir() common.set_tmp_config_dir()
self.rpcserver = RPCServer(listen=False) RPCServer(listen=False)
self.core = Core() self.core = Core()
self.torrentManager = self.core.torrentmanager
return component.start() return component.start()
def tearDown(self): # NOQA def tear_down(self):
def on_shutdown(result): return component.shutdown()
component._ComponentRegistry.components = {}
del self.rpcserver
del self.core
del self.torrentManager
return component.shutdown().addCallback(on_shutdown)
@defer.inlineCallbacks @defer.inlineCallbacks
def test_remove_torrent(self): def test_remove_torrent(self):
filename = os.path.join(os.path.dirname(__file__), "test.torrent") filename = os.path.join(os.path.dirname(__file__), "test.torrent")
torrent_id = yield self.core.add_torrent_file(filename, base64.encodestring(open(filename).read()), {}) torrent_id = yield self.core.add_torrent_file(filename, base64.encodestring(open(filename).read()), {})
self.assertTrue(self.torrentManager.remove(torrent_id, False)) self.assertTrue(self.core.torrentmanager.remove(torrent_id, False))
def test_remove_torrent_false(self): def test_remove_torrent_false(self):
"""Test when remove_torrent returns False""" """Test when remove_torrent returns False"""
raise unittest.SkipTest("") raise unittest.SkipTest("")
def test_remove_invalid_torrent(self): def test_remove_invalid_torrent(self):
self.assertRaises(InvalidTorrentError, self.torrentManager.remove, "torrentidthatdoesntexist") self.assertRaises(InvalidTorrentError, self.core.torrentmanager.remove, "torrentidthatdoesntexist")