|
| 1 | +import pytest |
| 2 | +import mock |
| 3 | + |
| 4 | +import openpathsampling as paths |
| 5 | + |
| 6 | +from paths_cli.tests.wizard.mock_wizard import mock_wizard |
| 7 | + |
| 8 | +from paths_cli.wizard.load_from_ops import ( |
| 9 | + named_objs_helper, _get_ops_storage, _get_ops_object, load_from_ops |
| 10 | +) |
| 11 | + |
| 12 | +# for some reason I couldn't get these to work with MagicMock |
| 13 | +class NamedObj: |
| 14 | + def __init__(self, name): |
| 15 | + self.name = name |
| 16 | + self.is_named = True |
| 17 | + |
| 18 | +class FakeStore: |
| 19 | + def __init__(self, objects): |
| 20 | + self._objects = objects |
| 21 | + self._named_objects = {obj.name: obj for obj in objects} |
| 22 | + |
| 23 | + def __getitem__(self, key): |
| 24 | + if isinstance(key, int): |
| 25 | + return self._objects[key] |
| 26 | + elif isinstance(key, str): |
| 27 | + return self._named_objects[key] |
| 28 | + else: |
| 29 | + raise TypeError("Huh?") |
| 30 | + |
| 31 | + def __iter__(self): |
| 32 | + return iter(self._objects) |
| 33 | + |
| 34 | +class FakeStorage: |
| 35 | + def __init__(self, foo): |
| 36 | + self.foo = foo |
| 37 | + |
| 38 | +@pytest.fixture |
| 39 | +def ops_file_fixture(): |
| 40 | + # store name 'foo', objects named 'bar', 'baz' |
| 41 | + bar = NamedObj('bar') |
| 42 | + baz = NamedObj('baz') |
| 43 | + foo = FakeStore([bar, baz]) |
| 44 | + storage = FakeStorage(foo) |
| 45 | + return storage |
| 46 | + |
| 47 | +def test_named_objs_helper(ops_file_fixture): |
| 48 | + wizard = mock_wizard([]) |
| 49 | + helper_func = named_objs_helper(ops_file_fixture, 'foo') |
| 50 | + helper_func(wizard, 'any') |
| 51 | + assert "what I found" in wizard.console.log_text |
| 52 | + assert "bar" in wizard.console.log_text |
| 53 | + assert "baz" in wizard.console.log_text |
| 54 | + |
| 55 | +@pytest.mark.parametrize('with_failure', [False, True]) |
| 56 | +def test_get_ops_storage(tmpdir, with_failure): |
| 57 | + fake_file = tmpdir / 'foo.db' |
| 58 | + fake_file.write_text('bar', 'utf-8') |
| 59 | + |
| 60 | + failure_text = ['baz.db'] if with_failure else [] |
| 61 | + wizard = mock_wizard(failure_text + [str(fake_file)]) |
| 62 | + |
| 63 | + with mock.patch('paths_cli.wizard.load_from_ops.INPUT_FILE', |
| 64 | + new=mock.Mock(get=open)): |
| 65 | + storage = _get_ops_storage(wizard) |
| 66 | + assert storage.read() == 'bar' |
| 67 | + if with_failure: |
| 68 | + assert 'something went wrong' in wizard.console.log_text |
| 69 | + else: |
| 70 | + assert 'something went wrong' not in wizard.console.log_text |
| 71 | + |
| 72 | +@pytest.mark.parametrize('with_failure', [False, True]) |
| 73 | +def test_get_ops_object(ops_file_fixture, with_failure): |
| 74 | + failure_text = ['qux'] if with_failure else [] |
| 75 | + wizard = mock_wizard(failure_text + ['bar']) |
| 76 | + obj = _get_ops_object(wizard, ops_file_fixture, |
| 77 | + store_name='foo', |
| 78 | + obj_name='FOOMAGIC') |
| 79 | + assert isinstance(obj, NamedObj) |
| 80 | + assert obj.name == 'bar' |
| 81 | + log = wizard.console.log_text |
| 82 | + assert 'name of the FOOMAGIC' in log |
| 83 | + fail_msg = 'Something went wrong' |
| 84 | + if with_failure: |
| 85 | + assert fail_msg in log |
| 86 | + else: |
| 87 | + assert fail_msg not in log |
| 88 | + |
| 89 | +def test_load_from_ops(ops_file_fixture): |
| 90 | + wizard = mock_wizard(['anyfile.db', 'bar']) |
| 91 | + with mock.patch('paths_cli.wizard.load_from_ops.INPUT_FILE.get', |
| 92 | + mock.Mock(return_value=ops_file_fixture)): |
| 93 | + obj = load_from_ops(wizard, 'foo', 'FOOMAGIC') |
| 94 | + |
| 95 | + assert isinstance(obj, NamedObj) |
| 96 | + assert obj.name == 'bar' |
| 97 | + |
| 98 | + pass |
0 commit comments