Changeset 90d1e90 in trunk


Ignore:
Timestamp:
2021-12-01T20:05:29Z (3 years ago)
Author:
Jean-Paul Calderone <exarkun@…>
Branches:
master
Children:
eee1f09
Parents:
7626a02b
Message:

rewrite the Eliot interaction tests to make expected behavior clearer

and to have explicit assertions about that behavior

File:
1 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified src/allmydata/test/test_eliotutil.py

    r7626a02b r90d1e90  
    2828from testtools import (
    2929    TestCase,
    30 )
    31 from testtools import (
    3230    TestResult,
    3331)
     
    3533    Is,
    3634    IsInstance,
     35    Not,
    3736    MatchesStructure,
    3837    Equals,
     
    7877
    7978
    80 class EliotLoggedTestTests(AsyncTestCase):
    81     """
    82     Tests for the automatic log-related provided by ``EliotLoggedRunTest``.
    83     """
     79def passes():
     80    """
     81    Create a matcher that matches a ``TestCase`` that runs without failures or
     82    errors.
     83    """
     84    def run(case):
     85        result = TestResult()
     86        case.run(result)
     87        return result.wasSuccessful()
     88    return AfterPreprocessing(run, Equals(True))
     89
     90
     91class EliotLoggedTestTests(TestCase):
     92    """
     93    Tests for the automatic log-related provided by ``AsyncTestCase``.
     94
     95    This class uses ``testtools.TestCase`` because it is inconvenient to nest
     96    ``AsyncTestCase`` inside ``AsyncTestCase`` (in particular, Eliot messages
     97    emitted by the inner test case get observed by the outer test case and if
     98    an inner case emits invalid messages they cause the outer test case to
     99    fail).
     100    """
     101    def test_fails(self):
     102        """
     103        A test method of an ``AsyncTestCase`` subclass can fail.
     104        """
     105        class UnderTest(AsyncTestCase):
     106            def test_it(self):
     107                self.fail("make sure it can fail")
     108
     109        self.assertThat(UnderTest("test_it"), Not(passes()))
     110
     111    def test_unserializable_fails(self):
     112        """
     113        A test method of an ``AsyncTestCase`` subclass that logs an unserializable
     114        value with Eliot fails.
     115        """
     116        class world(object):
     117            """
     118            an unserializable object
     119            """
     120
     121        class UnderTest(AsyncTestCase):
     122            def test_it(self):
     123                Message.log(hello=world)
     124
     125        self.assertThat(UnderTest("test_it"), Not(passes()))
     126
     127    def test_logs_non_utf_8_byte(self):
     128        """
     129        A test method of an ``AsyncTestCase`` subclass can log a message that
     130        contains a non-UTF-8 byte string and return ``None`` and pass.
     131        """
     132        class UnderTest(AsyncTestCase):
     133            def test_it(self):
     134                Message.log(hello=b"\xFF")
     135
     136        self.assertThat(UnderTest("test_it"), passes())
     137
    84138    def test_returns_none(self):
    85         Message.log(hello="world")
     139        """
     140        A test method of an ``AsyncTestCase`` subclass can log a message and
     141        return ``None`` and pass.
     142        """
     143        class UnderTest(AsyncTestCase):
     144            def test_it(self):
     145                Message.log(hello="world")
     146
     147        self.assertThat(UnderTest("test_it"), passes())
    86148
    87149    def test_returns_fired_deferred(self):
    88         Message.log(hello="world")
    89         return succeed(None)
     150        """
     151        A test method of an ``AsyncTestCase`` subclass can log a message and
     152        return an already-fired ``Deferred`` and pass.
     153        """
     154        class UnderTest(AsyncTestCase):
     155            def test_it(self):
     156                Message.log(hello="world")
     157                return succeed(None)
     158
     159        self.assertThat(UnderTest("test_it"), passes())
    90160
    91161    def test_returns_unfired_deferred(self):
    92         Message.log(hello="world")
    93         # @eliot_logged_test automatically gives us an action context but it's
    94         # still our responsibility to maintain it across stack-busting
    95         # operations.
    96         d = DeferredContext(deferLater(reactor, 0.0, lambda: None))
    97         d.addCallback(lambda ignored: Message.log(goodbye="world"))
    98         # We didn't start an action.  We're not finishing an action.
    99         return d.result
    100 
    101     def test_logs_non_utf_8_byte(self):
    102         """
    103         If an Eliot message is emitted that contains a non-UTF-8 byte string then
    104         the test nevertheless passes.
    105         """
    106         Message.log(hello=b"\xFF")
     162        """
     163        A test method of an ``AsyncTestCase`` subclass can log a message and
     164        return an unfired ``Deferred`` and pass when the ``Deferred`` fires.
     165        """
     166        class UnderTest(AsyncTestCase):
     167            def test_it(self):
     168                Message.log(hello="world")
     169                # @eliot_logged_test automatically gives us an action context
     170                # but it's still our responsibility to maintain it across
     171                # stack-busting operations.
     172                d = DeferredContext(deferLater(reactor, 0.0, lambda: None))
     173                d.addCallback(lambda ignored: Message.log(goodbye="world"))
     174                # We didn't start an action.  We're not finishing an action.
     175                return d.result
     176
     177        self.assertThat(UnderTest("test_it"), passes())
    107178
    108179
Note: See TracChangeset for help on using the changeset viewer.