diff options
Diffstat (limited to 'deluge/tests/test_component.py')
-rw-r--r-- | deluge/tests/test_component.py | 122 |
1 files changed, 54 insertions, 68 deletions
diff --git a/deluge/tests/test_component.py b/deluge/tests/test_component.py index 26f24ad..0345e24 100644 --- a/deluge/tests/test_component.py +++ b/deluge/tests/test_component.py @@ -1,19 +1,15 @@ -# -*- coding: utf-8 -*- # # This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with # the additional special exception to link portions of this program with the OpenSSL library. # See LICENSE for more details. # -from __future__ import unicode_literals - +import pytest +import pytest_twisted from twisted.internet import defer, threads -from twisted.trial.unittest import SkipTest import deluge.component as component -from .basetest import BaseTestCase - class ComponentTester(component.Component): def __init__(self, name, depend=None): @@ -70,14 +66,15 @@ class ComponentTesterShutdown(component.Component): self.stop_count += 1 -class ComponentTestClass(BaseTestCase): +@pytest.mark.usefixtures('component') +class TestComponent: def tear_down(self): return component.shutdown() def test_start_component(self): def on_start(result, c): - self.assertEqual(c._component_state, 'Started') - self.assertEqual(c.start_count, 1) + assert c._component_state == 'Started' + assert c.start_count == 1 c = ComponentTester('test_start_c1') d = component.start(['test_start_c1']) @@ -86,16 +83,16 @@ class ComponentTestClass(BaseTestCase): def test_start_stop_depends(self): def on_stop(result, c1, c2): - self.assertEqual(c1._component_state, 'Stopped') - self.assertEqual(c2._component_state, 'Stopped') - self.assertEqual(c1.stop_count, 1) - self.assertEqual(c2.stop_count, 1) + assert c1._component_state == 'Stopped' + assert c2._component_state == 'Stopped' + assert c1.stop_count == 1 + assert c2.stop_count == 1 def on_start(result, c1, c2): - self.assertEqual(c1._component_state, 'Started') - self.assertEqual(c2._component_state, 'Started') - self.assertEqual(c1.start_count, 1) - self.assertEqual(c2.start_count, 1) + assert c1._component_state == 'Started' + assert c2._component_state == 'Started' + assert c1.start_count == 1 + assert c2.start_count == 1 return component.stop(['test_start_depends_c1']).addCallback( on_stop, c1, c2 ) @@ -126,8 +123,8 @@ class ComponentTestClass(BaseTestCase): def test_start_all(self): def on_start(*args): for c in args[1:]: - self.assertEqual(c._component_state, 'Started') - self.assertEqual(c.start_count, 1) + assert c._component_state == 'Started' + assert c.start_count == 1 ret = self.start_with_depends() ret[0].addCallback(on_start, *ret[1:]) @@ -136,20 +133,19 @@ class ComponentTestClass(BaseTestCase): def test_register_exception(self): ComponentTester('test_register_exception_c1') - self.assertRaises( - component.ComponentAlreadyRegistered, - ComponentTester, - 'test_register_exception_c1', - ) + with pytest.raises(component.ComponentAlreadyRegistered): + ComponentTester( + 'test_register_exception_c1', + ) def test_stop_component(self): def on_stop(result, c): - self.assertEqual(c._component_state, 'Stopped') - self.assertFalse(c._component_timer.running) - self.assertEqual(c.stop_count, 1) + assert c._component_state == 'Stopped' + assert not c._component_timer.running + assert c.stop_count == 1 def on_start(result, c): - self.assertEqual(c._component_state, 'Started') + assert c._component_state == 'Started' return component.stop(['test_stop_component_c1']).addCallback(on_stop, c) c = ComponentTesterUpdate('test_stop_component_c1') @@ -160,12 +156,12 @@ class ComponentTestClass(BaseTestCase): def test_stop_all(self): def on_stop(result, *args): for c in args: - self.assertEqual(c._component_state, 'Stopped') - self.assertEqual(c.stop_count, 1) + assert c._component_state == 'Stopped' + assert c.stop_count == 1 def on_start(result, *args): for c in args: - self.assertEqual(c._component_state, 'Started') + assert c._component_state == 'Started' return component.stop().addCallback(on_stop, *args) ret = self.start_with_depends() @@ -175,9 +171,9 @@ class ComponentTestClass(BaseTestCase): def test_update(self): def on_start(result, c1, counter): - self.assertTrue(c1._component_timer) - self.assertTrue(c1._component_timer.running) - self.assertNotEqual(c1.counter, counter) + assert c1._component_timer + assert c1._component_timer.running + assert c1.counter != counter return component.stop() c1 = ComponentTesterUpdate('test_update_c1') @@ -189,13 +185,13 @@ class ComponentTestClass(BaseTestCase): def test_pause(self): def on_pause(result, c1, counter): - self.assertEqual(c1._component_state, 'Paused') - self.assertNotEqual(c1.counter, counter) - self.assertFalse(c1._component_timer.running) + assert c1._component_state == 'Paused' + assert c1.counter != counter + assert not c1._component_timer.running def on_start(result, c1, counter): - self.assertTrue(c1._component_timer) - self.assertNotEqual(c1.counter, counter) + assert c1._component_timer + assert c1.counter != counter d = component.pause(['test_pause_c1']) d.addCallback(on_pause, c1, counter) return d @@ -207,23 +203,16 @@ class ComponentTestClass(BaseTestCase): d.addCallback(on_start, c1, cnt) return d - @defer.inlineCallbacks + @pytest_twisted.inlineCallbacks def test_component_start_error(self): ComponentTesterUpdate('test_pause_c1') yield component.start(['test_pause_c1']) yield component.pause(['test_pause_c1']) test_comp = component.get('test_pause_c1') - try: - result = self.failureResultOf(test_comp._component_start()) - except AttributeError: - raise SkipTest( - 'This test requires trial failureResultOf() in Twisted version >= 13' - ) - self.assertEqual( - result.check(component.ComponentException), component.ComponentException - ) + with pytest.raises(component.ComponentException, match='Current state: Paused'): + yield test_comp._component_start() - @defer.inlineCallbacks + @pytest_twisted.inlineCallbacks def test_start_paused_error(self): ComponentTesterUpdate('test_pause_c1') yield component.start(['test_pause_c1']) @@ -232,29 +221,26 @@ class ComponentTestClass(BaseTestCase): # Deferreds that fail in component have to error handler which results in # twisted doing a log.err call which causes the test to fail. # Prevent failure by ignoring the exception - self._observer._ignoreErrors(component.ComponentException) + # self._observer._ignoreErrors(component.ComponentException) result = yield component.start() - self.assertEqual( - [(result[0][0], result[0][1].value)], - [ - ( - defer.FAILURE, - component.ComponentException( - 'Trying to start component "%s" but it is ' - 'not in a stopped state. Current state: %s' - % ('test_pause_c1', 'Paused'), - '', - ), - ) - ], - ) + assert [(result[0][0], result[0][1].value)] == [ + ( + defer.FAILURE, + component.ComponentException( + 'Trying to start component "%s" but it is ' + 'not in a stopped state. Current state: %s' + % ('test_pause_c1', 'Paused'), + '', + ), + ) + ] def test_shutdown(self): def on_shutdown(result, c1): - self.assertTrue(c1.shutdowned) - self.assertEqual(c1._component_state, 'Stopped') - self.assertEqual(c1.stop_count, 1) + assert c1.shutdowned + assert c1._component_state == 'Stopped' + assert c1.stop_count == 1 def on_start(result, c1): d = component.shutdown() |