|
| 1 | +import pytest |
| 2 | +from unittest import mock |
| 3 | + |
| 4 | +import pythonbits.config as config |
| 5 | +c = config.config |
| 6 | + |
| 7 | +c.register('Foo', 'bar', 'bar?') |
| 8 | +c.register('Foo', 'bar2', 'bar2?') |
| 9 | +c.register('Foo', 'bar3', 'bar3?', ask=True) |
| 10 | +c.register('Foo', 'bar4', 'bar4?', ask=True) |
| 11 | +c.register('Foo', 'ham', 'ham?') |
| 12 | +c.register('Foo', 'eggs', 'eggs?') |
| 13 | + |
| 14 | + |
| 15 | +def test_get(): |
| 16 | + # todo: atomic tests with fresh config file |
| 17 | + |
| 18 | + # registered option, present in config file |
| 19 | + assert c.get('Foo', 'bar') == 'baz' |
| 20 | + assert c.get('Foo', 'bar', 'bar_default') == 'baz' |
| 21 | + with mock.patch('builtins.input', lambda q: 'some_input'): |
| 22 | + assert c.get('Foo', 'ham') == 'some_input' |
| 23 | + |
| 24 | + # registered option, present in config file, but empty |
| 25 | + assert c.get('Foo', 'bar2', 'bar2_default') == 'bar2_default' |
| 26 | + with mock.patch('builtins.input', lambda q: 'bar2_input'): |
| 27 | + assert c.get('Foo', 'bar2') == 'bar2_input' |
| 28 | + # assert c.get('Foo', 'bar2') == 'bar2_input' # see todo: non-mocked _write |
| 29 | + |
| 30 | + # registered option, not present in config file, ask=True |
| 31 | + assert c.get('Foo', 'bar3', 'bar3_default') == 'bar3_default' |
| 32 | + with mock.patch('builtins.input', side_effect=['bar3_input', 'y']) as m: |
| 33 | + assert c.get('Foo', 'bar3') == 'bar3_input' |
| 34 | + with pytest.raises(StopIteration): # make sure all consumed |
| 35 | + next(m.side_effect) |
| 36 | + assert c.get('Foo', 'bar3') == 'bar3_input' |
| 37 | + |
| 38 | + # lifecycle test: registered option, not present, ask=True |
| 39 | + assert c.get('Foo', 'bar4', 'bar4_default') == 'bar4_default' |
| 40 | + with mock.patch('builtins.input', side_effect=['bar4_input', 'n']) as m: |
| 41 | + assert c.get('Foo', 'bar4') == 'bar4_input' |
| 42 | + with pytest.raises(StopIteration): # make sure all consumed |
| 43 | + next(m.side_effect) |
| 44 | + assert c.get('Foo', 'bar4', 'bar4_default2') == 'bar4_default2' |
| 45 | + with mock.patch('builtins.input', side_effect=['bar4_input2', 'nr']) as m: |
| 46 | + assert c.get('Foo', 'bar4') == 'bar4_input2' |
| 47 | + with pytest.raises(StopIteration): # make sure all consumed |
| 48 | + next(m.side_effect) |
| 49 | + assert c.get('Foo', 'bar4', 'bar4_default3') == 'bar4_default3' |
| 50 | + with mock.patch('builtins.input', side_effect=['bar4_input3']) as m: |
| 51 | + assert c.get('Foo', 'bar4') == 'bar4_input3' |
| 52 | + with pytest.raises(StopIteration): # make sure all consumed |
| 53 | + next(m.side_effect) |
| 54 | + |
| 55 | + # unregistered option, present in config file, but empty |
| 56 | + assert c.get('Foo', 'spam', 'spam_default') == 'spam_default' |
| 57 | + with pytest.raises(config.UnregisteredOption): |
| 58 | + c.get('Foo', 'spam') |
| 59 | + |
| 60 | + # unregistered option, present in config file |
| 61 | + assert c.get('Foo', 'spam2') == 'spam2_value' |
| 62 | + assert c.get('Foo', 'spam2', 'spam2_default') == 'spam2_value' |
| 63 | + |
| 64 | + # unregistered option, not present in config file |
| 65 | + assert c.get('Foo', 'spam3', 'spam3_default') == 'spam3_default' |
| 66 | + with pytest.raises(config.UnregisteredOption): |
| 67 | + c.get('Foo', 'spam3') |
| 68 | + |
| 69 | + # registered option, not present in config file |
| 70 | + assert c.get('Foo', 'eggs', default='default_input') == 'default_input' |
| 71 | + |
| 72 | + with mock.patch('builtins.input', lambda q: 'more_input'): |
| 73 | + assert c.get('Foo', 'eggs') == 'more_input' |
| 74 | + |
| 75 | + assert c.get('Foo', 'eggs') == 'more_input' |
0 commit comments