commit b59c17b8a9a0fc19ea12492df4163a329f23cadc Author: aagbsn aagbsn@extc.org Date: Tue Jan 15 12:23:28 2013 +0000
Move Managers Mocks into mocks.py --- tests/mocks.py | 109 ++++++++++++++++++++++++++++++++++++++--------- tests/test_managers.py | 91 +++------------------------------------- 2 files changed, 94 insertions(+), 106 deletions(-)
diff --git a/tests/mocks.py b/tests/mocks.py index eae3fad..687e247 100644 --- a/tests/mocks.py +++ b/tests/mocks.py @@ -1,14 +1,8 @@ -from ooni.tasks import BaseTask +from ooni.tasks import BaseTask, TaskWithTimeout +from twisted.python import failure from ooni.nettest import NetTest from ooni.managers import TaskManager - -class MockMeasurement(BaseTask): - def run(self): - f = open('foo.txt', 'w') - f.write('testing') - f.close() - - return defer.succeed(self) +from twisted.internet import defer
class MockMeasurementFailOnce(BaseTask): def run(self): @@ -20,18 +14,6 @@ class MockMeasurementFailOnce(BaseTask): else: return defer.fail()
-class MockNetTest(NetTest): - def __init__(self, num_measurements=1): - NetTest.__init__(self, StringIO(net_test_string), dummyOptions) - self.num_measurements = num_measurements - def generateMeasurements(self): - for i in range(self.num_measurements): - yield MockMeasurement() - -class MockDirector(object): - def __init__(self): - pass - class MockMeasurementManager(TaskManager): def __init__(self): self.successes = [] @@ -46,4 +28,89 @@ class MockReporter(object): def write(self, measurement): pass
+## from test_managers +mockFailure = failure.Failure(Exception('mock')) + +class MockSuccessTask(BaseTask): + def run(self): + return defer.succeed(42) + +class MockFailTask(BaseTask): + def run(self): + return defer.fail(mockFailure) + +class MockFailOnceTask(BaseTask): + def run(self): + if self.failures >= 1: + return defer.succeed(42) + else: + return defer.fail(mockFailure) + +class MockSuccessTaskWithTimeout(TaskWithTimeout): + def run(self): + return defer.succeed(42) + +class MockFailTaskThatTimesOut(TaskWithTimeout): + def run(self): + return defer.Deferred() + +class MockTimeoutOnceTask(TaskWithTimeout): + def run(self): + if self.failures >= 1: + return defer.succeed(42) + else: + return defer.Deferred() + +class MockFailTaskWithTimeout(TaskWithTimeout): + def run(self): + return defer.fail(mockFailure) + +class MockTaskManager(TaskManager): + def __init__(self): + self.successes = [] + + def failed(self, failure, task): + pass + + def succeeded(self, result, task): + self.successes.append((result, task)) + +class MockNetTest(object): + def __init__(self): + self.successes = [] + + def succeeded(self, measurement): + self.successes.append(measurement) + +class MockMeasurement(TaskWithTimeout): + def __init__(self, net_test): + TaskWithTimeout.__init__(self) + self.netTest = net_test + + def succeeded(self, result): + return self.netTest.succeeded(self) + +class MockSuccessMeasurement(MockMeasurement): + def run(self): + return defer.succeed(42) + +class MockFailMeasurement(MockMeasurement): + def run(self): + return defer.fail(mockFailure) + +class MockFailOnceMeasurement(MockMeasurement): + def run(self): + if self.failures >= 1: + return defer.succeed(42) + else: + return defer.fail(mockFailure) + +class MockDirector(object): + def __init__(self): + self.successes = [] + + def measurementFailed(self, failure, measurement): + pass
+ def measurementSucceeded(self, measurement): + self.successes.append(measurement) diff --git a/tests/test_managers.py b/tests/test_managers.py index 9bc0698..1e469c7 100644 --- a/tests/test_managers.py +++ b/tests/test_managers.py @@ -5,51 +5,12 @@ from twisted.internet import defer, task from ooni.tasks import BaseTask, TaskWithTimeout from ooni.managers import TaskManager, MeasurementManager
-mockFailure = failure.Failure(Exception('mock')) - -class MockSuccessTask(BaseTask): - def run(self): - return defer.succeed(42) - -class MockFailTask(BaseTask): - def run(self): - return defer.fail(mockFailure) - -class MockFailOnceTask(BaseTask): - def run(self): - if self.failures >= 1: - return defer.succeed(42) - else: - return defer.fail(mockFailure) - -class MockSuccessTaskWithTimeout(TaskWithTimeout): - def run(self): - return defer.succeed(42) - -class MockFailTaskThatTimesOut(TaskWithTimeout): - def run(self): - return defer.Deferred() - -class MockTimeoutOnceTask(TaskWithTimeout): - def run(self): - if self.failures >= 1: - return defer.succeed(42) - else: - return defer.Deferred() - -class MockFailTaskWithTimeout(TaskWithTimeout): - def run(self): - return defer.fail(mockFailure) - -class MockTaskManager(TaskManager): - def __init__(self): - self.successes = [] - - def failed(self, failure, task): - pass - - def succeeded(self, result, task): - self.successes.append((result, task)) +from tests.mocks import MockSuccessTask, MockFailTask, MockFailOnceTask +from tests.mocks import MockSuccessTaskWithTimeout, MockFailTaskThatTimesOut +from tests.mocks import MockTimeoutOnceTask, MockFailTaskWithTimeout +from tests.mocks import MockTaskManager, mockFailure, MockDirector +from tests.mocks import MockNetTest, MockMeasurement, MockSuccessMeasurement +from tests.mocks import MockFailMeasurement, MockFailOnceMeasurement
class TestTaskManager(unittest.TestCase): timeout = 1 @@ -200,46 +161,6 @@ class TestTaskManager(unittest.TestCase):
return d
-class MockNetTest(object): - def __init__(self): - self.successes = [] - - def succeeded(self, measurement): - self.successes.append(measurement) - -class MockMeasurement(TaskWithTimeout): - def __init__(self, net_test): - TaskWithTimeout.__init__(self) - self.netTest = net_test - - def succeeded(self, result): - return self.netTest.succeeded(self) - -class MockSuccessMeasurement(MockMeasurement): - def run(self): - return defer.succeed(42) - -class MockFailMeasurement(MockMeasurement): - def run(self): - return defer.fail(mockFailure) - -class MockFailOnceMeasurement(MockMeasurement): - def run(self): - if self.failures >= 1: - return defer.succeed(42) - else: - return defer.fail(mockFailure) - -class MockDirector(object): - def __init__(self): - self.successes = [] - - def measurementFailed(self, failure, measurement): - pass - - def measurementSucceeded(self, measurement): - self.successes.append(measurement) - class TestMeasurementManager(unittest.TestCase): def setUp(self): mock_director = MockDirector()