|
| 1 | +from pyper import task, AsyncPipeline, Pipeline |
| 2 | + |
| 3 | + |
| 4 | +def func(x): |
| 5 | + return x |
| 6 | + |
| 7 | +def gen(x): |
| 8 | + yield x |
| 9 | + |
| 10 | +async def afunc(x): |
| 11 | + return x |
| 12 | + |
| 13 | +async def agen(x): |
| 14 | + yield x |
| 15 | + |
| 16 | +class Func: |
| 17 | + def __call__(self, x): |
| 18 | + return x |
| 19 | + |
| 20 | +class Gen: |
| 21 | + def __call__(self, x): |
| 22 | + yield x |
| 23 | + |
| 24 | +class AFunc: |
| 25 | + async def __call__(self, x): |
| 26 | + return x |
| 27 | + |
| 28 | +class AGen: |
| 29 | + async def __call__(self, x): |
| 30 | + yield x |
| 31 | + |
| 32 | +def test_as_decorator(): |
| 33 | + p = task(func) |
| 34 | + assert isinstance(p, Pipeline) |
| 35 | + |
| 36 | +def test_as_decorator_with_params(): |
| 37 | + p = task(join=True, concurrency=2, throttle=2)(func) |
| 38 | + assert isinstance(p, Pipeline) |
| 39 | + |
| 40 | +def test_as_wrapper_with_params(): |
| 41 | + p = task(func, join=True, concurrency=2, throttle=2) |
| 42 | + assert isinstance(p, Pipeline) |
| 43 | + |
| 44 | +def _try_invalid_concurrency(value, exc_type): |
| 45 | + try: |
| 46 | + task(func, concurrency=value) |
| 47 | + except Exception as e: |
| 48 | + return isinstance(e, exc_type) |
| 49 | + return False |
| 50 | + |
| 51 | +def test_raise_for_invalid_concurrency(): |
| 52 | + assert _try_invalid_concurrency(0, ValueError) |
| 53 | + assert _try_invalid_concurrency(-1, ValueError) |
| 54 | + assert _try_invalid_concurrency("1",TypeError) |
| 55 | + assert _try_invalid_concurrency(1.5, TypeError) |
| 56 | + |
| 57 | +def _try_invalid_throttle(value, exc_type): |
| 58 | + try: |
| 59 | + task(func, throttle=value) |
| 60 | + except Exception as e: |
| 61 | + return isinstance(e, exc_type) |
| 62 | + return False |
| 63 | + |
| 64 | +def test_raise_for_invalid_throttle(): |
| 65 | + assert _try_invalid_throttle(-1, ValueError) |
| 66 | + assert _try_invalid_throttle("1",TypeError) |
| 67 | + assert _try_invalid_throttle(1.5, TypeError) |
| 68 | + |
| 69 | +def test_raise_for_invalid_daemon(): |
| 70 | + try: |
| 71 | + task(afunc, daemon=True) |
| 72 | + except Exception as e: |
| 73 | + assert isinstance(e, ValueError) |
| 74 | + else: |
| 75 | + raise AssertionError |
| 76 | + |
| 77 | +def test_raise_for_invalid_func(): |
| 78 | + try: |
| 79 | + task(1) |
| 80 | + except Exception as e: |
| 81 | + assert isinstance(e, TypeError) |
| 82 | + else: |
| 83 | + raise AssertionError |
| 84 | + |
| 85 | +def test_async_task(): |
| 86 | + p = task(afunc) |
| 87 | + assert isinstance(p, AsyncPipeline) |
| 88 | + |
| 89 | +def test_piped_async_task(): |
| 90 | + p = task(afunc) >> task(func) |
| 91 | + assert isinstance(p, AsyncPipeline) |
| 92 | + |
| 93 | +def test_invalid_pipe(): |
| 94 | + try: |
| 95 | + task(func) >> 1 |
| 96 | + except Exception as e: |
| 97 | + assert isinstance(e, TypeError) |
| 98 | + else: |
| 99 | + raise AssertionError |
| 100 | + |
| 101 | +def test_invalid_async_pipe(): |
| 102 | + try: |
| 103 | + task(afunc) >> 1 |
| 104 | + except Exception as e: |
| 105 | + assert isinstance(e, TypeError) |
| 106 | + else: |
| 107 | + raise AssertionError |
| 108 | + |
| 109 | +def test_invalid_consumer(): |
| 110 | + try: |
| 111 | + task(func) & 1 |
| 112 | + except Exception as e: |
| 113 | + assert isinstance(e, TypeError) |
| 114 | + else: |
| 115 | + raise AssertionError |
| 116 | + |
| 117 | +def test_invalid_async_consumer(): |
| 118 | + try: |
| 119 | + task(afunc) & func |
| 120 | + except Exception as e: |
| 121 | + assert isinstance(e, TypeError) |
| 122 | + else: |
| 123 | + raise AssertionError |
| 124 | + |
| 125 | +def test_gen_inspect(): |
| 126 | + is_gen = lambda f: task(f).tasks[0].is_gen |
| 127 | + assert is_gen(gen) |
| 128 | + assert is_gen(agen) |
| 129 | + assert is_gen(Gen()) |
| 130 | + assert is_gen(AGen()) |
| 131 | + assert not is_gen(func) |
| 132 | + assert not is_gen(afunc) |
| 133 | + assert not is_gen(Func()) |
| 134 | + assert not is_gen(AFunc()) |
| 135 | + assert not is_gen(lambda x: x) |
| 136 | + |
| 137 | +def test_async_inspect(): |
| 138 | + is_async = lambda f: task(f).tasks[0].is_async |
| 139 | + assert is_async(afunc) |
| 140 | + assert is_async(agen) |
| 141 | + assert is_async(AFunc()) |
| 142 | + assert is_async(AGen()) |
| 143 | + assert not is_async(func) |
| 144 | + assert not is_async(Func()) |
| 145 | + assert not is_async(gen) |
| 146 | + assert not is_async(Gen()) |
| 147 | + assert not is_async(lambda x: x) |
| 148 | + |
| 149 | +def test_repr(): |
| 150 | + p = task(func) |
| 151 | + assert "Pipeline" in repr(p) |
0 commit comments