commit aca4ed4fde29d004cb466aa0288846dc9614b5f5 Author: aagbsn aagbsn@extc.org Date: Mon Feb 18 19:32:27 2013 +0100
Implement Report unit tests --- tests/mocks.py | 35 ++++++++-- tests/test_reporter.py | 169 +++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 182 insertions(+), 22 deletions(-)
diff --git a/tests/mocks.py b/tests/mocks.py index c7feceb..fed683e 100644 --- a/tests/mocks.py +++ b/tests/mocks.py @@ -10,9 +10,9 @@ class MockMeasurementFailOnce(BaseTask): f.write('fail') f.close() if self.failure >= 1: - return defer.succeed() + return defer.succeed(self) else: - return defer.fail() + return defer.fail(failure.Failure)
class MockMeasurementManager(TaskManager): def __init__(self): @@ -33,8 +33,7 @@ class MockReporter(object): pass
def createReport(self): - self.created.callback(True) - return self.created + self.created.callback(self)
def finish(self): pass @@ -93,7 +92,7 @@ class MockMeasurement(TaskWithTimeout): self.netTest = net_test
def succeeded(self, result): - return self.netTest.succeeded(self) + return self.netTest.succeeded(42)
class MockSuccessMeasurement(MockMeasurement): def run(self): @@ -126,13 +125,35 @@ class MockOReporter(object): self.created = defer.Deferred()
def writeReportEntry(self, entry): - pass + return defer.succeed(42)
def finish(self): pass
def createReport(self): - pass + from ooni.utils import log + log.debug("Creating report with %s" % self) + self.created.callback(self) + +class MockOReporterThatFailsWrite(MockOReporter): + def writeReportEntry(self, entry): + raise MockFailure + +class MockOReporterThatFailsOpen(MockOReporter): + def createReport(self): + self.created.errback(failure.Failure(MockFailure())) + +class MockOReporterThatFailsWriteOnce(MockOReporter): + def __init__(self): + self.failure = 0 + MockOReporter.__init__(self) + + def writeReportEntry(self, entry): + if self.failure >= 1: + return defer.succeed(42) + else: + self.failure += 1 + raise MockFailure
class MockTaskManager(TaskManager): def __init__(self): diff --git a/tests/test_reporter.py b/tests/test_reporter.py index e99debb..d2fcc4e 100644 --- a/tests/test_reporter.py +++ b/tests/test_reporter.py @@ -8,25 +8,164 @@ from ooni.nettest import NetTest, NetTestState from ooni.tasks import TaskWithTimeout from tests.mocks import MockOReporter, MockTaskManager from tests.mocks import MockMeasurement, MockNetTest +from tests.mocks import MockOReporterThatFailsWrite +from tests.mocks import MockOReporterThatFailsWriteOnce +from tests.mocks import MockOReporterThatFailsOpen
-mockReportOptions = {'name':'foo_test', 'version': '0.1'} - -class MockState(NetTestState): - pass +from twisted.python import failure
class TestReport(unittest.TestCase): def setUp(self): - self.taskManager = MockTaskManager() - self.report = Report([MockOReporter], self.taskManager) - self.state = MockState() + pass + def tearDown(self): + pass + def test_create_report_with_no_reporter(self): + report = Report([],ReportEntryManager()) + self.assertIsInstance(report, Report) + + def test_create_report_with_single_reporter(self): + report = Report([MockOReporter()], ReportEntryManager()) + self.assertIsInstance(report, Report) + + def test_create_report_with_multiple_reporters(self): + report = Report([MockOReporter() for x in xrange(3)], + ReportEntryManager()) + self.assertIsInstance(report, Report) + + def test_report_open_with_single_reporter(self): + report = Report([MockOReporter()],ReportEntryManager()) + d = report.open() + return d + + def test_report_open_with_multiple_reporter(self): + report = Report([MockOReporter() for x in xrange(3)], + ReportEntryManager()) + d = report.open() + return d + + def test_fail_to_open_report_with_single_reporter(self): + report = Report([MockOReporterThatFailsOpen()], + ReportEntryManager()) + d = report.open() + def f(x): + self.assertEquals(len(report.reporters), 0) + d.addCallback(f) + return d + + def test_fail_to_open_single_report_with_multiple_reporter(self): + report = Report([MockOReporterThatFailsOpen(), MockOReporter(), + MockOReporter()], ReportEntryManager()) + d = report.open() + def f(x): + self.assertEquals(len(report.reporters),2) + d.addCallback(f) + return d + + def test_fail_to_open_all_reports_with_multiple_reporter(self): + report = Report([MockOReporterThatFailsOpen() for x in xrange(3)], + ReportEntryManager()) + d = report.open() + def f(x): + self.assertEquals(len(report.reporters),0) + d.addCallback(f) + return d + + def test_write_report_with_single_reporter_and_succeed(self): + #XXX: verify that the MockOReporter writeReportEntry succeeds + report = Report([MockOReporter()], ReportEntryManager()) + report.open() + d = report.write(MockMeasurement(MockNetTest())) + return d + + def test_write_report_with_single_reporter_and_fail_after_timeout(self): + report = Report([MockOReporterThatFailsWrite()], ReportEntryManager()) + report.open() + d = report.write(MockMeasurement(MockNetTest())) + def f(err): + self.assertEquals(len(report.reporters),0) + d.addBoth(f) + return d + + def test_write_report_with_single_reporter_and_succeed_after_timeout(self): + report = Report([MockOReporterThatFailsWriteOnce()], ReportEntryManager()) + report.open() + d = report.write(MockMeasurement(MockNetTest())) + return d + + def test_write_report_with_multiple_reporter_and_succeed(self): + report = Report([MockOReporter() for x in xrange(3)], ReportEntryManager()) + report.open() + d = report.write(MockMeasurement(MockNetTest())) + return d + + def test_write_report_with_multiple_reporter_and_fail_a_single_reporter(self): + report = Report([MockOReporter(), MockOReporter(), MockOReporterThatFailsWrite()], ReportEntryManager()) + d = report.open() + + self.assertEquals(len(report.reporters),3) + d = report.write(MockMeasurement(MockNetTest())) + + def f(x): + # one of the reporters should have been removed + self.assertEquals(len(report.reporters), 2) + d.addBoth(f) + return d
- def test_report_alltasksdone_callback_fires(self): - for m in range(10): - measurement = MockMeasurement(MockNetTest()) - self.report.write(measurement) + def test_write_report_with_multiple_reporter_and_fail_all_reporter(self): + report = Report([MockOReporterThatFailsWrite() for x in xrange(3)], ReportEntryManager()) + report.open() + d = report.write(MockMeasurement(MockNetTest())) + def f(err): + self.assertEquals(len(report.reporters),0) + d.addErrback(f) + return d
- @self.report.done.addCallback - def done(reporters): - self.assertEqual(len(reporters), 1) +#class TestYAMLReporter(unittest.TestCase): +# def setUp(self): +# pass +# def tearDown(self): +# pass +# def test_create_yaml_reporter(self): +# raise NotImplementedError +# def test_open_yaml_report_and_succeed(self): +# raise NotImplementedError +# def test_open_yaml_report_and_fail(self): +# raise NotImplementedError +# def test_write_yaml_report_entry(self): +# raise NotImplementedError +# def test_write_multiple_yaml_report_entry(self): +# raise NotImplementedError +# def test_close_yaml_report(self): +# raise NotImplementedError +# def test_write_yaml_report_after_close(self): +# raise NotImplementedError +# def test_write_yaml_report_before_open(self): +# raise NotImplementedError +# def test_close_yaml_report_after_task_complete(self): +# raise NotImplementedError
- return self.report.done +#class TestOONIBReporter(unittest.TestCase): +# def setUp(self): +# pass +# def tearDown(self): +# pass +# def test_create_oonib_reporter(self): +# raise NotImplementedError +# def test_open_oonib_report_and_succeed(self): +# raise NotImplementedError +# def test_open_oonib_report_and_fail(self): +# raise NotImplementedError +# def test_write_oonib_report_entry_and_succeed(self): +# raise NotImplementedError +# def test_write_oonib_report_entry_and_succeed_after_timeout(self): +# raise NotImplementedError +# def test_write_oonib_report_entry_and_fail_after_timeout(self): +# raise NotImplementedError +# def test_write_oonib_report_after_close(self): +# raise NotImplementedError +# def test_write_oonib_report_before_open(self): +# raise NotImplementedError +# def test_close_oonib_report_and_succeed(self): +# raise NotImplementedError +# def test_close_oonib_report_and_fail(self): +# raise NotImplementedError