| 1 |
import sys |
| 2 |
from importlib.util import find_spec |
| 3 |
from unittest.mock import Mock |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from celery.local import PromiseProxy, Proxy, maybe_evaluate, try_import |
| 8 |
|
| 9 |
|
| 10 |
class test_try_import: |
| 11 |
|
| 12 |
def test_imports(self): |
| 13 |
assert try_import(__name__) |
| 14 |
|
| 15 |
def test_when_default(self): |
| 16 |
default = object() |
| 17 |
assert try_import('foobar.awqewqe.asdwqewq', default) is default |
| 18 |
|
| 19 |
|
| 20 |
class test_Proxy: |
| 21 |
|
| 22 |
def test_std_class_attributes(self): |
| 23 |
assert Proxy.__name__ == 'Proxy' |
| 24 |
assert Proxy.__module__ == 'celery.local' |
| 25 |
assert isinstance(Proxy.__doc__, str) |
| 26 |
|
| 27 |
def test_doc(self): |
| 28 |
def real(): |
| 29 |
pass |
| 30 |
x = Proxy(real, __doc__='foo') |
| 31 |
assert x.__doc__ == 'foo' |
| 32 |
|
| 33 |
def test_name(self): |
| 34 |
|
| 35 |
def real(): |
| 36 |
"""real function""" |
| 37 |
return 'REAL' |
| 38 |
|
| 39 |
x = Proxy(lambda: real, name='xyz') |
| 40 |
assert x.__name__ == 'xyz' |
| 41 |
|
| 42 |
y = Proxy(lambda: real) |
| 43 |
assert y.__name__ == 'real' |
| 44 |
|
| 45 |
assert x.__doc__ == 'real function' |
| 46 |
|
| 47 |
assert x.__class__ == type(real) |
| 48 |
assert x.__dict__ == real.__dict__ |
| 49 |
assert repr(x) == repr(real) |
| 50 |
assert x.__module__ |
| 51 |
|
| 52 |
def test_get_current_local(self): |
| 53 |
x = Proxy(lambda: 10) |
| 54 |
object.__setattr__(x, '_Proxy_local', Mock()) |
| 55 |
assert x._get_current_object() |
| 56 |
|
| 57 |
def test_bool(self): |
| 58 |
|
| 59 |
class X: |
| 60 |
|
| 61 |
def __bool__(self): |
| 62 |
return False |
| 63 |
__nonzero__ = __bool__ |
| 64 |
|
| 65 |
x = Proxy(lambda: X()) |
| 66 |
assert not x |
| 67 |
|
| 68 |
def test_slots(self): |
| 69 |
|
| 70 |
class X: |
| 71 |
__slots__ = () |
| 72 |
|
| 73 |
x = Proxy(X) |
| 74 |
with pytest.raises(AttributeError): |
| 75 |
x.__dict__ |
| 76 |
|
| 77 |
def test_dir(self): |
| 78 |
|
| 79 |
class X: |
| 80 |
|
| 81 |
def __dir__(self): |
| 82 |
return ['a', 'b', 'c'] |
| 83 |
|
| 84 |
x = Proxy(lambda: X()) |
| 85 |
assert dir(x) == ['a', 'b', 'c'] |
| 86 |
|
| 87 |
class Y: |
| 88 |
|
| 89 |
def __dir__(self): |
| 90 |
raise RuntimeError() |
| 91 |
y = Proxy(lambda: Y()) |
| 92 |
assert dir(y) == [] |
| 93 |
|
| 94 |
def test_getsetdel_attr(self): |
| 95 |
|
| 96 |
class X: |
| 97 |
a = 1 |
| 98 |
b = 2 |
| 99 |
c = 3 |
| 100 |
|
| 101 |
def __dir__(self): |
| 102 |
return ['a', 'b', 'c'] |
| 103 |
|
| 104 |
v = X() |
| 105 |
|
| 106 |
x = Proxy(lambda: v) |
| 107 |
assert x.__members__ == ['a', 'b', 'c'] |
| 108 |
assert x.a == 1 |
| 109 |
assert x.b == 2 |
| 110 |
assert x.c == 3 |
| 111 |
|
| 112 |
setattr(x, 'a', 10) |
| 113 |
assert x.a == 10 |
| 114 |
|
| 115 |
del (x.a) |
| 116 |
assert x.a == 1 |
| 117 |
|
| 118 |
def test_dictproxy(self): |
| 119 |
v = {} |
| 120 |
x = Proxy(lambda: v) |
| 121 |
x['foo'] = 42 |
| 122 |
assert x['foo'] == 42 |
| 123 |
assert len(x) == 1 |
| 124 |
assert 'foo' in x |
| 125 |
del (x['foo']) |
| 126 |
with pytest.raises(KeyError): |
| 127 |
x['foo'] |
| 128 |
assert iter(x) |
| 129 |
|
| 130 |
def test_listproxy(self): |
| 131 |
v = [] |
| 132 |
x = Proxy(lambda: v) |
| 133 |
x.append(1) |
| 134 |
x.extend([2, 3, 4]) |
| 135 |
assert x[0] == 1 |
| 136 |
assert x[:-1] == [1, 2, 3] |
| 137 |
del (x[-1]) |
| 138 |
assert x[:-1] == [1, 2] |
| 139 |
x[0] = 10 |
| 140 |
assert x[0] == 10 |
| 141 |
assert 10 in x |
| 142 |
assert len(x) == 3 |
| 143 |
assert iter(x) |
| 144 |
x[0:2] = [1, 2] |
| 145 |
del (x[0:2]) |
| 146 |
assert str(x) |
| 147 |
|
| 148 |
def test_complex_cast(self): |
| 149 |
|
| 150 |
class O: |
| 151 |
|
| 152 |
def __complex__(self): |
| 153 |
return complex(10.333) |
| 154 |
|
| 155 |
o = Proxy(O) |
| 156 |
assert o.__complex__() == complex(10.333) |
| 157 |
|
| 158 |
def test_index(self): |
| 159 |
|
| 160 |
class O: |
| 161 |
|
| 162 |
def __index__(self): |
| 163 |
return 1 |
| 164 |
|
| 165 |
o = Proxy(O) |
| 166 |
assert o.__index__() == 1 |
| 167 |
|
| 168 |
def test_coerce(self): |
| 169 |
|
| 170 |
class O: |
| 171 |
|
| 172 |
def __coerce__(self, other): |
| 173 |
return self, other |
| 174 |
|
| 175 |
o = Proxy(O) |
| 176 |
assert o.__coerce__(3) |
| 177 |
|
| 178 |
def test_int(self): |
| 179 |
assert Proxy(lambda: 10) + 1 == Proxy(lambda: 11) |
| 180 |
assert Proxy(lambda: 10) - 1 == Proxy(lambda: 9) |
| 181 |
assert Proxy(lambda: 10) * 2 == Proxy(lambda: 20) |
| 182 |
assert Proxy(lambda: 10) ** 2 == Proxy(lambda: 100) |
| 183 |
assert Proxy(lambda: 20) / 2 == Proxy(lambda: 10) |
| 184 |
assert Proxy(lambda: 20) // 2 == Proxy(lambda: 10) |
| 185 |
assert Proxy(lambda: 11) % 2 == Proxy(lambda: 1) |
| 186 |
assert Proxy(lambda: 10) << 2 == Proxy(lambda: 40) |
| 187 |
assert Proxy(lambda: 10) >> 2 == Proxy(lambda: 2) |
| 188 |
assert Proxy(lambda: 10) ^ 7 == Proxy(lambda: 13) |
| 189 |
assert Proxy(lambda: 10) | 40 == Proxy(lambda: 42) |
| 190 |
assert Proxy(lambda: 10) != Proxy(lambda: -11) |
| 191 |
assert Proxy(lambda: 10) != Proxy(lambda: -10) |
| 192 |
assert Proxy(lambda: -10) == Proxy(lambda: -10) |
| 193 |
|
| 194 |
assert Proxy(lambda: 10) < Proxy(lambda: 20) |
| 195 |
assert Proxy(lambda: 20) > Proxy(lambda: 10) |
| 196 |
assert Proxy(lambda: 10) >= Proxy(lambda: 10) |
| 197 |
assert Proxy(lambda: 10) <= Proxy(lambda: 10) |
| 198 |
assert Proxy(lambda: 10) == Proxy(lambda: 10) |
| 199 |
assert Proxy(lambda: 20) != Proxy(lambda: 10) |
| 200 |
assert Proxy(lambda: 100).__divmod__(30) |
| 201 |
assert Proxy(lambda: 100).__truediv__(30) |
| 202 |
assert abs(Proxy(lambda: -100)) |
| 203 |
|
| 204 |
x = Proxy(lambda: 10) |
| 205 |
x -= 1 |
| 206 |
assert x == 9 |
| 207 |
x = Proxy(lambda: 9) |
| 208 |
x += 1 |
| 209 |
assert x == 10 |
| 210 |
x = Proxy(lambda: 10) |
| 211 |
x *= 2 |
| 212 |
assert x == 20 |
| 213 |
x = Proxy(lambda: 20) |
| 214 |
x /= 2 |
| 215 |
assert x == 10 |
| 216 |
x = Proxy(lambda: 10) |
| 217 |
x %= 2 |
| 218 |
assert x == 0 |
| 219 |
x = Proxy(lambda: 10) |
| 220 |
x <<= 3 |
| 221 |
assert x == 80 |
| 222 |
x = Proxy(lambda: 80) |
| 223 |
x >>= 4 |
| 224 |
assert x == 5 |
| 225 |
x = Proxy(lambda: 5) |
| 226 |
x ^= 1 |
| 227 |
assert x == 4 |
| 228 |
x = Proxy(lambda: 4) |
| 229 |
x **= 4 |
| 230 |
assert x == 256 |
| 231 |
x = Proxy(lambda: 256) |
| 232 |
x //= 2 |
| 233 |
assert x == 128 |
| 234 |
x = Proxy(lambda: 128) |
| 235 |
x |= 2 |
| 236 |
assert x == 130 |
| 237 |
x = Proxy(lambda: 130) |
| 238 |
x &= 10 |
| 239 |
assert x == 2 |
| 240 |
|
| 241 |
x = Proxy(lambda: 10) |
| 242 |
assert type(x.__float__()) == float |
| 243 |
assert type(x.__int__()) == int |
| 244 |
assert hex(x) |
| 245 |
assert oct(x) |
| 246 |
|
| 247 |
def test_hash(self): |
| 248 |
|
| 249 |
class X: |
| 250 |
|
| 251 |
def __hash__(self): |
| 252 |
return 1234 |
| 253 |
|
| 254 |
assert hash(Proxy(lambda: X())) == 1234 |
| 255 |
|
| 256 |
def test_call(self): |
| 257 |
|
| 258 |
class X: |
| 259 |
|
| 260 |
def __call__(self): |
| 261 |
return 1234 |
| 262 |
|
| 263 |
assert Proxy(lambda: X())() == 1234 |
| 264 |
|
| 265 |
def test_context(self): |
| 266 |
|
| 267 |
class X: |
| 268 |
entered = exited = False |
| 269 |
|
| 270 |
def __enter__(self): |
| 271 |
self.entered = True |
| 272 |
return 1234 |
| 273 |
|
| 274 |
def __exit__(self, *exc_info): |
| 275 |
self.exited = True |
| 276 |
|
| 277 |
v = X() |
| 278 |
x = Proxy(lambda: v) |
| 279 |
with x as val: |
| 280 |
assert val == 1234 |
| 281 |
assert x.entered |
| 282 |
assert x.exited |
| 283 |
|
| 284 |
def test_reduce(self): |
| 285 |
|
| 286 |
class X: |
| 287 |
|
| 288 |
def __reduce__(self): |
| 289 |
return 123 |
| 290 |
|
| 291 |
x = Proxy(lambda: X()) |
| 292 |
assert x.__reduce__() == 123 |
| 293 |
|
| 294 |
|
| 295 |
class test_PromiseProxy: |
| 296 |
|
| 297 |
def test_only_evaluated_once(self): |
| 298 |
|
| 299 |
class X: |
| 300 |
attr = 123 |
| 301 |
evals = 0 |
| 302 |
|
| 303 |
def __init__(self): |
| 304 |
self.__class__.evals += 1 |
| 305 |
|
| 306 |
p = PromiseProxy(X) |
| 307 |
assert p.attr == 123 |
| 308 |
assert p.attr == 123 |
| 309 |
assert X.evals == 1 |
| 310 |
|
| 311 |
def test_callbacks(self): |
| 312 |
source = Mock(name='source') |
| 313 |
p = PromiseProxy(source) |
| 314 |
cbA = Mock(name='cbA') |
| 315 |
cbB = Mock(name='cbB') |
| 316 |
cbC = Mock(name='cbC') |
| 317 |
p.__then__(cbA, p) |
| 318 |
p.__then__(cbB, p) |
| 319 |
assert not p.__evaluated__() |
| 320 |
assert object.__getattribute__(p, '__pending__') |
| 321 |
|
| 322 |
assert repr(p) |
| 323 |
assert p.__evaluated__() |
| 324 |
with pytest.raises(AttributeError): |
| 325 |
object.__getattribute__(p, '__pending__') |
| 326 |
cbA.assert_called_with(p) |
| 327 |
cbB.assert_called_with(p) |
| 328 |
|
| 329 |
assert p.__evaluated__() |
| 330 |
p.__then__(cbC, p) |
| 331 |
cbC.assert_called_with(p) |
| 332 |
|
| 333 |
with pytest.raises(AttributeError): |
| 334 |
object.__getattribute__(p, '__pending__') |
| 335 |
|
| 336 |
def test_maybe_evaluate(self): |
| 337 |
x = PromiseProxy(lambda: 30) |
| 338 |
assert not x.__evaluated__() |
| 339 |
assert maybe_evaluate(x) == 30 |
| 340 |
assert maybe_evaluate(x) == 30 |
| 341 |
|
| 342 |
assert maybe_evaluate(30) == 30 |
| 343 |
assert x.__evaluated__() |
| 344 |
|
| 345 |
|
| 346 |
class test_celery_import: |
| 347 |
def test_import_celery(self, monkeypatch): |
| 348 |
monkeypatch.delitem(sys.modules, "celery", raising=False) |
| 349 |
spec = find_spec("celery") |
| 350 |
assert spec |
| 351 |
|
| 352 |
import celery |
| 353 |
|
| 354 |
assert celery.__spec__ == spec |
| 355 |
assert find_spec("celery") == spec |