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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
|
from reactivex import Observer
from reactivex.notification import OnCompleted, OnError, OnNext, from_notifier
class MyObserver(Observer):
def __init__(self):
super().__init__()
self.has_on_next = None
self.has_on_completed = None
self.has_on_error = None
def _on_next_core(self, value):
self.has_on_next = value
def _on_error_core(self, error: Exception):
self.has_on_error = str(error)
def _on_completed_core(self):
self.has_on_completed = True
def test_to_observer_notification_on_next():
i = 0
def next(n):
assert i == 0
assert n.kind == "N"
assert n.value == 42
assert not hasattr(n, "exception")
assert n.has_value
from_notifier(next).on_next(42)
def test_to_observer_notification_on_error():
ex = "ex"
i = 0
def next(n):
assert i == 0
assert n.kind == "E"
assert str(n.exception) == ex
assert not n.has_value
from_notifier(next).on_error(ex)
def test_to_observer_notification_completed():
i = 0
def next(n):
assert i == 0
assert n.kind == "C"
assert not n.has_value
from_notifier(next).on_completed()
def test_to_notifier_forwards():
obsn = MyObserver()
obsn.to_notifier()(OnNext(42))
assert obsn.has_on_next == 42
ex = "ex"
obse = MyObserver()
obse.to_notifier()(OnError(ex))
assert ex == obse.has_on_error
obsc = MyObserver()
obsc.to_notifier()(OnCompleted())
assert obsc.has_on_completed
def test_create_on_next():
next = [False]
def on_next(x):
assert 42 == x
next[0] = True
res = Observer(on_next)
res.on_next(42)
assert next[0]
return res.on_completed()
def test_create_on_next_has_error():
ex = "ex"
next = [False]
_e = None
def on_next(x):
assert 42 == x
next[0] = True
res = Observer(on_next)
res.on_next(42)
assert next[0]
try:
res.on_error(ex)
assert False
except Exception as e:
e_ = e.args[0]
assert ex == e_
def test_create_on_next_on_completed():
next = [False]
completed = [False]
def on_next(x):
assert 42 == x
next[0] = True
return next[0]
def on_completed():
completed[0] = True
return completed[0]
res = Observer(on_next, None, on_completed)
res.on_next(42)
assert next[0]
assert not completed[0]
res.on_completed()
assert completed[0]
def test_create_on_next_close_has_error():
e_ = None
ex = "ex"
next = [False]
completed = [False]
def on_next(x):
assert 42 == x
next[0] = True
def on_completed():
completed[0] = True
res = Observer(on_next, None, on_completed)
res.on_next(42)
assert next[0]
assert not completed[0]
try:
res.on_error(ex)
assert False
except Exception as e:
e_ = e.args[0]
assert ex == e_
assert not completed[0]
def test_create_on_next_on_error():
ex = "ex"
next = [True]
error = [False]
def on_next(x):
assert 42 == x
next[0] = True
def on_error(e):
assert ex == e
error[0] = True
res = Observer(on_next, on_error)
res.on_next(42)
assert next[0]
assert not error[0]
res.on_error(ex)
assert error[0]
def test_create_on_next_throw_hit_completed():
ex = "ex"
next = [True]
error = [False]
def on_next(x):
assert 42 == x
next[0] = True
def on_error(e):
assert ex == e
error[0] = True
res = Observer(on_next, on_error)
res.on_next(42)
assert next[0]
assert not error[0]
res.on_completed()
assert not error[0]
def test_create_on_next_throw_close1():
ex = "ex"
next = [True]
error = [False]
completed = [False]
def on_next(x):
assert 42 == x
next[0] = True
def on_error(e):
assert ex == e
error[0] = True
def on_completed():
completed[0] = True
res = Observer(on_next, on_error, on_completed)
res.on_next(42)
assert next[0]
assert not error[0]
assert not completed[0]
res.on_completed()
assert completed[0]
assert not error[0]
def test_create_on_next_throw_close2():
ex = "ex"
next = [True]
error = [False]
completed = [False]
def on_next(x):
assert 42 == x
next[0] = True
def on_error(e):
assert ex == e
error[0] = True
def on_completed():
completed[0] = True
res = Observer(on_next, on_error, on_completed)
res.on_next(42)
assert next[0]
assert not error[0]
assert not completed[0]
res.on_error(ex)
assert not completed[0]
assert error[0]
def test_as_observer_hides():
obs = MyObserver()
res = obs.as_observer()
assert res != obs
assert not isinstance(res, obs.__class__)
def test_as_observer_forwards():
obsn = MyObserver()
obsn.as_observer().on_next(42)
assert obsn.has_on_next == 42
ex = "ex"
obse = MyObserver()
obse.as_observer().on_error(ex)
assert obse.has_on_error == ex
obsc = MyObserver()
obsc.as_observer().on_completed()
assert obsc.has_on_completed
if __name__ == "__main__":
test_to_notifier_forwards()
|