|
1 | 1 | import pytest
|
2 | 2 |
|
3 |
| -def test_exception_in_generator(): |
4 |
| - from dataflows import Flow, printer |
5 | 3 |
|
6 |
| - class MyException(Exception): |
7 |
| - pass |
| 4 | +class MyException(Exception): |
| 5 | + pass |
| 6 | + |
| 7 | + |
| 8 | +def test_exception_in_generator(): |
| 9 | + from dataflows import Flow, printer, exceptions |
8 | 10 |
|
9 | 11 | def generator():
|
10 | 12 | for i in range(5):
|
11 | 13 | raise MyException()
|
12 | 14 | yield {"i": i}
|
13 | 15 |
|
14 |
| - with pytest.raises(MyException): |
| 16 | + with pytest.raises(exceptions.ProcessorError) as excinfo: |
15 | 17 | Flow(generator(), printer()).process()
|
| 18 | + assert isinstance(excinfo.value.cause, MyException) |
| 19 | + |
| 20 | + |
| 21 | +def test_exception_information(): |
| 22 | + from dataflows import Flow, load, exceptions |
| 23 | + flow = Flow( |
| 24 | + load('data/bad-path1.csv'), |
| 25 | + ) |
| 26 | + with pytest.raises(exceptions.ProcessorError) as excinfo: |
| 27 | + flow.results() |
| 28 | + assert str(excinfo.value.cause) == "Failed to load source 'data/bad-path1.csv' and options {'custom_parsers': {'xml': <class 'dataflows.processors.load.XMLParser'>}, 'ignore_blank_headers': True, 'headers': 1}: [Errno 2] No such file or directory: 'data/bad-path1.csv'" |
| 29 | + assert excinfo.value.processor_name == 'load' |
| 30 | + assert excinfo.value.processor_object.load_source == 'data/bad-path1.csv' |
| 31 | + assert excinfo.value.processor_position == 1 |
| 32 | + |
| 33 | + |
| 34 | +def test_exception_information_multiple_processors_simple(): |
| 35 | + from dataflows import Flow, load, exceptions |
| 36 | + flow = Flow( |
| 37 | + load('data/bad-path1.csv'), |
| 38 | + load('data/bad-path2.csv'), |
| 39 | + ) |
| 40 | + with pytest.raises(exceptions.ProcessorError) as excinfo: |
| 41 | + flow.results() |
| 42 | + assert str(excinfo.value.cause) == "Failed to load source 'data/bad-path1.csv' and options {'custom_parsers': {'xml': <class 'dataflows.processors.load.XMLParser'>}, 'ignore_blank_headers': True, 'headers': 1}: [Errno 2] No such file or directory: 'data/bad-path1.csv'" |
| 43 | + assert excinfo.value.processor_name == 'load' |
| 44 | + assert excinfo.value.processor_object.load_source == 'data/bad-path1.csv' |
| 45 | + assert excinfo.value.processor_position == 1 |
| 46 | + |
| 47 | + |
| 48 | +def test_exception_information_multiple_processors_last_errored(): |
| 49 | + from dataflows import Flow, load, exceptions |
| 50 | + flow = Flow( |
| 51 | + load('data/academy.csv'), |
| 52 | + load('data/bad-path2.csv'), |
| 53 | + ) |
| 54 | + with pytest.raises(exceptions.ProcessorError) as excinfo: |
| 55 | + flow.results() |
| 56 | + assert str(excinfo.value.cause) == "Failed to load source 'data/bad-path2.csv' and options {'custom_parsers': {'xml': <class 'dataflows.processors.load.XMLParser'>}, 'ignore_blank_headers': True, 'headers': 1}: [Errno 2] No such file or directory: 'data/bad-path2.csv'" |
| 57 | + assert excinfo.value.processor_name == 'load' |
| 58 | + assert excinfo.value.processor_object.load_source == 'data/bad-path2.csv' |
| 59 | + assert excinfo.value.processor_position == 2 |
| 60 | + |
| 61 | + |
| 62 | +def test_exception_information_multiple_processors_function_error(): |
| 63 | + from dataflows import Flow, load, exceptions |
| 64 | + |
| 65 | + def func(rows): |
| 66 | + for i, row in enumerate(rows): |
| 67 | + if i == 1: |
| 68 | + raise MyException('custom-error') |
| 69 | + yield row |
| 70 | + |
| 71 | + flow = Flow( |
| 72 | + load('data/academy.csv'), |
| 73 | + func |
| 74 | + ) |
| 75 | + with pytest.raises(exceptions.ProcessorError) as excinfo: |
| 76 | + flow.results() |
| 77 | + assert str(excinfo.value.cause) == 'custom-error' |
| 78 | + assert excinfo.value.processor_name == 'rows_processor' |
| 79 | + assert excinfo.value.processor_position == 2 |
| 80 | + |
| 81 | + |
| 82 | +def test_exception_information_multiple_processors_iterable_error(): |
| 83 | + from dataflows import Flow, printer, exceptions |
| 84 | + |
| 85 | + def func(): |
| 86 | + for i in range(10): |
| 87 | + if i == 1: |
| 88 | + raise MyException('custom-iterable-error') |
| 89 | + yield dict(a=i) |
| 90 | + |
| 91 | + flow = Flow( |
| 92 | + func(), |
| 93 | + printer() |
| 94 | + ) |
| 95 | + with pytest.raises(exceptions.ProcessorError) as excinfo: |
| 96 | + flow.results() |
| 97 | + assert str(excinfo.value.cause) == 'custom-iterable-error' |
| 98 | + assert excinfo.value.processor_name == 'iterable_loader' |
| 99 | + assert excinfo.value.processor_position == 1 |
0 commit comments