File: test_deferred.py

package info (click to toggle)
python-testtools 2.8.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,244 kB
  • sloc: python: 15,086; makefile: 127; sh: 3
file content (68 lines) | stat: -rw-r--r-- 1,896 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Copyright (c) testtools developers. See LICENSE for details.

"""Tests for testtools._deferred."""

from testtools.matchers import (
    Equals,
    MatchesException,
    Raises,
)

from ._helpers import NeedsTwistedTestCase

try:
    from testtools.twistedsupport._deferred import DeferredNotFired
except ImportError:
    DeferredNotFired = None  # type: ignore[misc,assignment]

try:
    from testtools.twistedsupport._deferred import extract_result
except ImportError:
    extract_result = None  # type: ignore[assignment]

try:
    from twisted.internet import defer
except ImportError:
    defer = None  # type: ignore[assignment]

try:
    from twisted.python.failure import Failure
except ImportError:
    Failure = None  # type: ignore[misc,assignment]


class TestExtractResult(NeedsTwistedTestCase):
    """Tests for ``extract_result``."""

    def test_not_fired(self):
        # _spinner.extract_result raises _spinner.DeferredNotFired if it's
        # given a Deferred that has not fired.
        self.assertThat(
            lambda: extract_result(defer.Deferred()),
            Raises(MatchesException(DeferredNotFired)),
        )

    def test_success(self):
        # _spinner.extract_result returns the value of the Deferred if it has
        # fired successfully.
        marker = object()
        d = defer.succeed(marker)
        self.assertThat(extract_result(d), Equals(marker))

    def test_failure(self):
        # _spinner.extract_result raises the failure's exception if it's given
        # a Deferred that is failing.
        try:
            1 / 0
        except ZeroDivisionError:
            f = Failure()
        d = defer.fail(f)
        self.assertThat(
            lambda: extract_result(d), Raises(MatchesException(ZeroDivisionError))
        )


def test_suite():
    from unittest import TestLoader

    return TestLoader().loadTestsFromName(__name__)