File: test_toasync.py

package info (click to toggle)
python-rx 4.0.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,056 kB
  • sloc: python: 39,070; javascript: 77; makefile: 24
file content (172 lines) | stat: -rw-r--r-- 4,074 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import unittest

import reactivex
from reactivex.testing import ReactiveTest, TestScheduler

on_next = ReactiveTest.on_next
on_completed = ReactiveTest.on_completed
on_error = ReactiveTest.on_error
subscribe = ReactiveTest.subscribe
subscribed = ReactiveTest.subscribed
disposed = ReactiveTest.disposed
created = ReactiveTest.created


class TestToAsync(unittest.TestCase):
    def test_to_async_context(self):
        class Context:
            def __init__(self):
                self.value = 42

            def func(self, x):
                return self.value + x

        scheduler = TestScheduler()

        def create():
            context = Context()
            return reactivex.to_async(context.func, scheduler)(42)

        res = scheduler.start(create)

        assert res.messages == [on_next(200, 84), on_completed(200)]

    def test_to_async0(self):
        scheduler = TestScheduler()

        def create():
            def func():
                return 0

            return reactivex.to_async(func, scheduler)()

        res = scheduler.start(create)

        assert res.messages == [on_next(200, 0), on_completed(200)]

    def test_to_async1(self):
        scheduler = TestScheduler()

        def create():
            def func(x):
                return x

            return reactivex.to_async(func, scheduler)(1)

        res = scheduler.start(create)

        assert res.messages == [on_next(200, 1), on_completed(200)]

    def test_to_async2(self):
        scheduler = TestScheduler()

        def create():
            def func(x, y):
                return x + y

            return reactivex.to_async(func, scheduler)(1, 2)

        res = scheduler.start(create)

        assert res.messages == [on_next(200, 3), on_completed(200)]

    def test_to_async3(self):
        scheduler = TestScheduler()

        def create():
            def func(x, y, z):
                return x + y + z

            return reactivex.to_async(func, scheduler)(1, 2, 3)

        res = scheduler.start(create)

        assert res.messages == [on_next(200, 6), on_completed(200)]

    def test_to_async4(self):
        scheduler = TestScheduler()

        def create():
            def func(a, b, c, d):
                return a + b + c + d

            return reactivex.to_async(func, scheduler)(1, 2, 3, 4)

        res = scheduler.start(create)

        assert res.messages == [on_next(200, 10), on_completed(200)]

    def test_to_async_error0(self):
        ex = Exception()

        scheduler = TestScheduler()

        def create():
            def func():
                raise ex

            return reactivex.to_async(func, scheduler)()

        res = scheduler.start(create)

        assert res.messages == [on_error(200, ex)]

    def test_to_async_error1(self):
        ex = Exception()

        scheduler = TestScheduler()

        def create():
            def func(a):
                raise ex

            return reactivex.to_async(func, scheduler)(1)

        res = scheduler.start(create)

        assert res.messages == [on_error(200, ex)]

    def test_to_async_error2(self):
        ex = Exception()

        scheduler = TestScheduler()

        def create():
            def func(a, b):
                raise ex

            return reactivex.to_async(func, scheduler)(1, 2)

        res = scheduler.start(create)

        assert res.messages == [on_error(200, ex)]

    def test_to_async_error3(self):
        ex = Exception()

        scheduler = TestScheduler()

        def create():
            def func(a, b, c):
                raise ex

            return reactivex.to_async(func, scheduler)(1, 2, 3)

        res = scheduler.start(create)

        assert res.messages == [on_error(200, ex)]

    def test_to_async_error4(self):
        ex = Exception()

        scheduler = TestScheduler()

        def create():
            def func(a, b, c, d):
                raise ex

            return reactivex.to_async(func, scheduler)(1, 2, 3, 4)

        res = scheduler.start(create)

        assert res.messages == [on_error(200, ex)]