Publishing 2019 R1 content
[platform/upstream/dldt.git] / tools / accuracy_checker / tests / test_model_conversion.py
1 """
2 Copyright (c) 2019 Intel Corporation
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 """
16
17 import sys
18 import pytest
19
20 from accuracy_checker.launcher.model_conversion import (exec_mo_binary, find_dlsdk_ir, find_mo, prepare_args)
21 from tests.common import mock_filesystem
22
23
24 def test_mock_file_system():
25     with mock_filesystem(['foo/bar', 'foo/baz/']) as prefix:
26         assert (prefix / 'foo' / 'bar').is_file()
27         assert (prefix / 'foo' / 'baz').is_dir()
28
29
30 def test_find_mo():
31     with mock_filesystem(['deployment_tools/model_optimizer/mo.py']) as prefix:
32         assert find_mo([prefix / 'deployment_tools' / 'model_optimizer'])
33
34
35 def test_find_mo_is_none_when_not_exist():
36     with mock_filesystem(['deployment_tools/model_optimizer/mo.py']) as prefix:
37         assert find_mo([prefix / 'deployment_tools']) is None
38
39
40 def test_find_mo_list_not_corrupted():
41     with mock_filesystem(['deployment_tools/model_optimizer/mo.py']) as prefix:
42         search_paths = [prefix]
43         find_mo(search_paths)
44         assert len(search_paths) == 1
45
46
47 def test_find_ir__in_root():
48     with mock_filesystem(['model.xml', 'model.bin']) as root:
49         model, weights = find_dlsdk_ir(root, 'model')
50         assert model == root / 'model.xml'
51         assert weights == root / 'model.bin'
52
53
54 def test_find_ir_raises_file_not_found_error_when_ir_not_found():
55     with mock_filesystem(['foo/']) as root:
56         with pytest.raises(FileNotFoundError):
57             find_dlsdk_ir(root, 'model')
58
59
60 def test_prepare_args():
61     args = prepare_args('foo', ['a', 'b'], {'bar': 123, 'x': 'baz'})
62     assert args[0] == sys.executable
63     assert args[1] == 'foo'
64     assert '--a' in args
65     assert '--b' in args
66     assert '--bar' in args
67     assert '--x' in args
68
69     assert args[args.index('--bar') + 1] == '123'
70     assert args[args.index('--x') + 1] == 'baz'
71
72
73 def test_exec_mo_binary(mocker):
74     subprocess_run = mocker.patch('subprocess.run')
75     mocker.patch('os.chdir')
76
77     args = prepare_args('ModelOptimizer', value_options={'--foo': 'bar'})
78     exec_mo_binary(args)
79
80     subprocess_run.assert_called_once_with(args, check=False, timeout=None)