Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / mo / utils / cli_parser_test.py
1 """
2  Copyright (c) 2018-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 import argparse
17 import imp
18 import os
19 import tempfile
20 import unittest
21 from unittest.mock import patch
22
23 import numpy as np
24
25 from mo.utils.cli_parser import get_placeholder_shapes, get_tuple_values, get_mean_scale_dictionary, get_model_name, \
26     get_absolute_path, parse_tuple_pairs, check_positive, writable_dir, readable_dirs, \
27     readable_file
28 from mo.utils.error import Error
29
30
31 class TestingMeanScaleGetter(unittest.TestCase):
32     def test_tuple_parser(self):
33         tuple_values = "data(1.1,22.22,333.333),info[2.2,33.33,444.444]"
34         result = parse_tuple_pairs(tuple_values)
35         exp_res = {
36             'data': np.array([1.1, 22.22, 333.333]),
37             'info': np.array([2.2, 33.33, 444.444])
38         }
39         for el in exp_res.keys():
40             np.array_equal(result[el], exp_res[el])
41
42     def test_tuple_parser_same_values(self):
43         tuple_values = "data(1.1,22.22,333.333),info[1.1,22.22,333.333]"
44         result = parse_tuple_pairs(tuple_values)
45         exp_res = {
46             'data': np.array([1.1, 22.22, 333.333]),
47             'info': np.array([1.1, 22.22, 333.333])
48         }
49         for el in exp_res.keys():
50             np.array_equal(result[el], exp_res[el])
51
52     def test_tuple_parser_no_inputs(self):
53         tuple_values = "(1.1,22.22,333.333),[2.2,33.33,444.444]"
54         result = parse_tuple_pairs(tuple_values)
55         exp_res = [np.array([1.1, 22.22, 333.333]),
56                    np.array([2.2, 33.33, 444.444])]
57         for i in range(0, len(exp_res)):
58             np.array_equal(result[i], exp_res[i])
59
60     def test_tuple_parser_error(self):
61         tuple_values = "(1.1,22.22,333.333),data[2.2,33.33,444.444]"
62         self.assertRaises(Error, parse_tuple_pairs, tuple_values)
63
64     def test_mean_scale_no_input(self):
65         mean_values = "data(1.1,22.22,333.333)"
66         scale_values = "info[1.1,22.22,333.333]"
67         result = get_mean_scale_dictionary(parse_tuple_pairs(mean_values), parse_tuple_pairs(scale_values), None)
68         exp_res = {
69             'data': {
70                 'mean': np.array([1.1, 22.22, 333.333]),
71                 'scale': None
72             },
73             'info': {
74                 'mean': None,
75                 'scale': np.array([1.1, 22.22, 333.333])
76             }
77         }
78         for input in exp_res.keys():
79             for key in exp_res[input].keys():
80                 if type(exp_res[input][key]) is np.ndarray:
81                     np.array_equal(exp_res[input][key], result[input][key])
82                 else:
83                     self.assertEqual(exp_res[input][key], result[input][key])
84
85     def test_mean_scale_no_input_diff_len(self):
86         mean_values = "data(1.1,22.22,333.333),info(2.1,33.22,333.333)"
87         scale_values = "info[1.1,22.22,333.333]"
88         result = get_mean_scale_dictionary(parse_tuple_pairs(mean_values), parse_tuple_pairs(scale_values), None)
89         exp_res = {
90             'data': {
91                 'mean': np.array([1.1, 22.22, 333.333]),
92                 'scale': None
93             },
94             'info': {
95                 'mean': np.array([2.1, 33.22, 333.333]),
96                 'scale': np.array([1.1, 22.22, 333.333])
97             }
98         }
99         for input in exp_res.keys():
100             for key in exp_res[input].keys():
101                 if type(exp_res[input][key]) is np.ndarray:
102                     np.array_equal(exp_res[input][key], result[input][key])
103                 else:
104                     self.assertEqual(exp_res[input][key], result[input][key])
105
106     def test_mean_only_input(self):
107         mean_values = "data(1.1,22.22,333.333)"
108         result = get_mean_scale_dictionary(parse_tuple_pairs(mean_values), parse_tuple_pairs(''), None)
109         exp_res = {
110             'data': {
111                 'mean': np.array([1.1, 22.22, 333.333]),
112                 'scale': None
113             }
114         }
115         for input in exp_res.keys():
116             for key in exp_res[input].keys():
117                 if type(exp_res[input][key]) is np.ndarray:
118                     np.array_equal(exp_res[input][key], result[input][key])
119                 else:
120                     self.assertEqual(exp_res[input][key], result[input][key])
121
122     def test_scale_only_input(self):
123         scale_values = "data(1.1,22.22,333.333)"
124         result = get_mean_scale_dictionary(parse_tuple_pairs(''), parse_tuple_pairs(scale_values), None)
125         exp_res = {
126             'data': {
127                 'mean': None,
128                 'scale': np.array([1.1, 22.22, 333.333])
129             }
130         }
131         for input in exp_res.keys():
132             for key in exp_res[input].keys():
133                 if type(exp_res[input][key]) is np.ndarray:
134                     np.array_equal(exp_res[input][key], result[input][key])
135                 else:
136                     self.assertEqual(exp_res[input][key], result[input][key])
137
138     def test_scale_only_no_input(self):
139         scale_values = "(1.1,22.22,333.333)"
140         mean_values = ""
141         mean = parse_tuple_pairs(mean_values)
142         scale = parse_tuple_pairs(scale_values)
143         result = get_mean_scale_dictionary(mean, scale, None)
144         exp_res = [
145             [
146                 None,
147                 np.array([1.1, 22.22, 333.333])
148             ]
149         ]
150         for i in range(len(exp_res)):
151             for j in range(len(exp_res[i])):
152                 if type(exp_res[i][j]) is np.ndarray:
153                     np.array_equal(exp_res[i][j], result[i][j])
154                 else:
155                     self.assertEqual(exp_res[i][j], result[i][j])
156
157     def test_scale_only_with_input(self):
158         scale_values = "(1.1,22.22,333.333)"
159         mean_values = ""
160         mean = parse_tuple_pairs(mean_values)
161         scale = parse_tuple_pairs(scale_values)
162         result = get_mean_scale_dictionary(mean, scale, 'data')
163         exp_res = {
164             'data': {
165                 'mean': None,
166                 'scale': np.array([1.1, 22.22, 333.333])
167             }
168         }
169         for input in exp_res.keys():
170             for key in exp_res[input].keys():
171                 if type(exp_res[input][key]) is np.ndarray:
172                     np.array_equal(exp_res[input][key], result[input][key])
173                 else:
174                     self.assertEqual(exp_res[input][key], result[input][key])
175
176     def test_2_scale_only_with_input(self):
177         scale_values = "(1.1,22.22,333.333),(1.2,22.33,333.444)"
178         mean_values = ""
179         mean = parse_tuple_pairs(mean_values)
180         scale = parse_tuple_pairs(scale_values)
181         result = get_mean_scale_dictionary(mean, scale, 'data,info')
182         exp_res = {
183             'data': {
184                 'mean': None,
185                 'scale': np.array([1.1, 22.22, 333.333])
186             },
187             'info': {
188                 'mean': None,
189                 'scale': np.array([1.2, 22.33, 333.444])
190             }
191         }
192         for input in exp_res.keys():
193             for key in exp_res[input].keys():
194                 if type(exp_res[input][key]) is np.ndarray:
195                     np.array_equal(exp_res[input][key], result[input][key])
196                 else:
197                     self.assertEqual(exp_res[input][key], result[input][key])
198
199     def test_2_mean_only_with_input(self):
200         scale_values = ""
201         mean_values = "(1.1,22.22,333.333),(1.2,22.33,333.444)"
202         mean = parse_tuple_pairs(mean_values)
203         scale = parse_tuple_pairs(scale_values)
204         result = get_mean_scale_dictionary(mean, scale, 'data,info')
205         exp_res = {
206             'data': {
207                 'mean': np.array([1.1, 22.22, 333.333]),
208                 'scale': None,
209             },
210             'info': {
211                 'mean': np.array([1.2, 22.33, 333.444]),
212                 'scale': None,
213             }
214         }
215         for input in exp_res.keys():
216             for key in exp_res[input].keys():
217                 if type(exp_res[input][key]) is np.ndarray:
218                     np.array_equal(exp_res[input][key], result[input][key])
219                 else:
220                     self.assertEqual(exp_res[input][key], result[input][key])
221
222     def test_mean_only_with_input(self):
223         scale_values = ""
224         mean_values = "(1.1,22.22,333.333)"
225         mean = parse_tuple_pairs(mean_values)
226         scale = parse_tuple_pairs(scale_values)
227         result = get_mean_scale_dictionary(mean, scale, 'data')
228         exp_res = {
229             'data': {
230                 'mean': np.array([1.1, 22.22, 333.333]),
231                 'scale': None
232             }
233         }
234         for input in exp_res.keys():
235             for key in exp_res[input].keys():
236                 if type(exp_res[input][key]) is np.ndarray:
237                     np.array_equal(exp_res[input][key], result[input][key])
238                 else:
239                     self.assertEqual(exp_res[input][key], result[input][key])
240
241     def test_mean_scale_diff_no_input(self):
242         scale_values = "(1.1,22.22,333.333),(1.1,22.22,333.333)"
243         mean_values = "(2.1,11.22,444.333)"
244         mean = parse_tuple_pairs(mean_values)
245         scale = parse_tuple_pairs(scale_values)
246         result = get_mean_scale_dictionary(mean, scale, None)
247         exp_res = [
248             [
249                 np.array([2.1, 11.22, 444.333]),  # mean
250                 np.array([1.1, 22.22, 333.333])  # scale
251             ],
252             [
253                 None,  # mean
254                 np.array([1.1, 22.22, 333.333])  # scale
255             ]
256         ]
257         for i in range(len(exp_res)):
258             for j in range(len(exp_res[i])):
259                 if type(exp_res[i][j]) is np.ndarray:
260                     np.array_equal(exp_res[i][j], result[i][j])
261                 else:
262                     self.assertEqual(exp_res[i][j], result[i][j])
263
264     def test_multi_mean_scale_no_input(self):
265         mean_values = "data(1.1,22.22,333.333),info(2.1,33.22,444.333)"
266         scale_values = "data[1.1,22.22,333.333],info[2.1,33.22,444.333]"
267         result = get_mean_scale_dictionary(parse_tuple_pairs(mean_values), parse_tuple_pairs(scale_values), None)
268         exp_res = {
269             'data': {
270                 'mean': np.array([1.1, 22.22, 333.333]),
271                 'scale': np.array([1.1, 22.22, 333.333])
272             },
273             'info': {
274                 'mean': np.array([2.1, 33.22, 444.333]),
275                 'scale': np.array([2.1, 33.22, 444.333])
276             }
277         }
278         for input in exp_res.keys():
279             for key in exp_res[input].keys():
280                 if type(exp_res[input][key]) is np.ndarray:
281                     np.array_equal(exp_res[input][key], result[input][key])
282                 else:
283                     self.assertEqual(exp_res[input][key], result[input][key])
284
285     def test_multi_mean_scale_input(self):
286         mean_values = "data(1.1,22.22,333.333),info(2.1,33.22,444.333)"
287         scale_values = "data[1.1,22.22,333.333],info[2.1,33.22,444.333]"
288         input_names = 'data,info'
289         result = get_mean_scale_dictionary(parse_tuple_pairs(mean_values), parse_tuple_pairs(scale_values), input_names)
290         exp_res = {
291             'data': {
292                 'mean': np.array([1.1, 22.22, 333.333]),
293                 'scale': np.array([1.1, 22.22, 333.333])
294             },
295             'info': {
296                 'mean': np.array([2.1, 33.22, 444.333]),
297                 'scale': np.array([2.1, 33.22, 444.333])
298             }
299         }
300         for input in exp_res.keys():
301             for key in exp_res[input].keys():
302                 if type(exp_res[input][key]) is np.ndarray:
303                     np.array_equal(exp_res[input][key], result[input][key])
304                 else:
305                     self.assertEqual(exp_res[input][key], result[input][key])
306
307     def test_multi_mean_scale_input_arrays(self):
308         mean_values = "(1.1,22.22,333.333),(2.1,33.22,444.333)"
309         scale_values = "[1.1,22.22,333.333],[2.1,33.22,444.333]"
310         input_names = 'data,info'
311         result = get_mean_scale_dictionary(parse_tuple_pairs(mean_values), parse_tuple_pairs(scale_values), input_names)
312         exp_res = {
313             'data': {
314                 'mean': np.array([1.1, 22.22, 333.333]),
315                 'scale': np.array([1.1, 22.22, 333.333])
316             },
317             'info': {
318                 'mean': np.array([2.1, 33.22, 444.333]),
319                 'scale': np.array([2.1, 33.22, 444.333])
320             }
321         }
322         for input in exp_res.keys():
323             for key in exp_res[input].keys():
324                 if type(exp_res[input][key]) is np.ndarray:
325                     np.array_equal(exp_res[input][key], result[input][key])
326                 else:
327                     self.assertEqual(exp_res[input][key], result[input][key])
328
329     def test_multi_mean_scale_arrays_no_input(self):
330         mean_values = "(1.1,22.22,333.333),(2.1,33.22,444.333)"
331         scale_values = "[1.1,22.22,333.333],[2.1,33.22,444.333]"
332         result = get_mean_scale_dictionary(parse_tuple_pairs(mean_values), parse_tuple_pairs(scale_values), None)
333         exp_res = [
334             [
335                 np.array([1.1, 22.22, 333.333]),  # mean
336                 np.array([1.1, 22.22, 333.333])  # scale
337             ],
338             [
339                 np.array([2.1, 33.22, 444.333]),  # mean
340                 np.array([2.1, 33.22, 444.333])  # scale
341             ]
342         ]
343         for i in range(0, len(exp_res)):
344             for j in range(0, len(exp_res[i])):
345                 np.array_equal(exp_res[i][j], result[i][j])
346
347
348 class TestSingleTupleParsing(unittest.TestCase):
349     def test_get_values_ideal(self):
350         values = "(1.11, 22.22, 333.333)"
351         result = get_tuple_values(values)
352         exp_res = ['1.11, 22.22, 333.333']
353         self.assertEqual(exp_res, result)
354
355     def test_get_values_ideal_spaces(self):
356         values = "(1    , 22 ,333)"
357         result = get_tuple_values(values)
358         exp_res = ['1    , 22 ,333']
359         self.assertEqual(exp_res, result)
360
361     def test_get_values_ideal_square(self):
362         values = "[1,22,333]"
363         result = get_tuple_values(values)
364         exp_res = ['1,22,333']
365         self.assertEqual(exp_res, result)
366
367     def test_get_values_ideal_square_spaces(self):
368         values = "[1    , 22 ,333]"
369         result = get_tuple_values(values)
370         exp_res = ['1    , 22 ,333']
371         self.assertEqual(exp_res, result)
372
373     def test_get_neg_values_ideal(self):
374         values = "(-1,-22,-333)"
375         result = get_tuple_values(values)
376         exp_res = ['-1,-22,-333']
377         self.assertEqual(exp_res, result)
378
379     def test_get_neg_values_minus(self):
380         values = "(-1,--22,-3-33)"
381         self.assertRaises(Error, get_tuple_values, values)
382
383     def test_get_values_unbalanced(self):
384         values = "(1,22,333]"
385         self.assertRaises(Error, get_tuple_values, values)
386
387     def test_get_values_unbalanced2(self):
388         values = "[1,22,333)"
389         self.assertRaises(Error, get_tuple_values, values)
390
391     def test_get_values_exactly_3(self):
392         values = "[1,22,333,22]"
393         self.assertRaises(Error, get_tuple_values, values)
394
395     def test_get_values_exactly_3_1(self):
396         values = "[1,22]"
397         self.assertRaises(Error, get_tuple_values, values)
398
399     def test_get_values_empty(self):
400         values = ""
401         self.assertRaises(Error, get_tuple_values, values)
402
403     def test_get_values_empty_tuple(self):
404         values = ()
405         result = get_tuple_values(values)
406         exp_res = ()
407         self.assertEqual(exp_res, result)
408
409
410 class TestShapesParsing(unittest.TestCase):
411     def test_get_shapes_several_inputs_several_shapes(self):
412         argv_input = "inp1,inp2"
413         input_shapes = "(1,22,333,123), (-1,45,7,1)"
414         result = get_placeholder_shapes(argv_input, input_shapes)
415         exp_res = {'inp1': np.array([1, 22, 333, 123]), 'inp2': np.array([-1, 45, 7, 1])}
416         self.assertEqual(list(exp_res.keys()), list(result.keys()))
417         for i in exp_res.keys():
418             np.testing.assert_array_equal(result[i], exp_res[i])
419
420     def test_get_shapes_several_inputs_several_shapes_not_equal(self):
421         argv_input = "inp1,inp2,inp3"
422         input_shapes = "(1,22,333,123), (-1,45,7,1)"
423         self.assertRaises(Error, get_placeholder_shapes, argv_input, input_shapes)
424
425     def test_get_shapes_several_shapes_one_input(self):
426         argv_input = "inp1"
427         input_shapes = "(1,22,333,123), (-1,45,7,1), (-1,456,7,1)"
428         self.assertRaises(Error, get_placeholder_shapes, argv_input, input_shapes)
429
430     def test_get_shapes_several_shapes_no_input(self):
431         argv_input = ""
432         input_shapes = "(1,22,333,123), (-1,45,7,1), (-1,456,7,1)"
433         self.assertRaises(Error, get_placeholder_shapes, argv_input, input_shapes)
434
435     def test_get_shapes_one_input_one_shape(self):
436         argv_input = "inp1"
437         input_shapes = "(1,22,333,123)"
438         result = get_placeholder_shapes(argv_input, input_shapes)
439         exp_res = {'inp1': np.array([1, 22, 333, 123])}
440         self.assertEqual(list(exp_res.keys()), list(result.keys()))
441         for i in exp_res.keys():
442             np.testing.assert_array_equal(result[i], exp_res[i])
443
444     def test_get_shapes_no_input_no_shape(self):
445         argv_input = ""
446         input_shapes = ""
447         result = get_placeholder_shapes(argv_input, input_shapes)
448         exp_res = np.array([None])
449         np.testing.assert_array_equal(result, exp_res)
450
451     def test_get_shapes_no_input_one_shape(self):
452         argv_input = ""
453         input_shapes = "(12,4,1)"
454         result = get_placeholder_shapes(argv_input, input_shapes)
455         exp_res = np.array([12, 4, 1])
456         np.testing.assert_array_equal(result, exp_res)
457
458     def test_get_shapes_no_input_one_shape2(self):
459         argv_input = ""
460         input_shapes = "[12,4,1]"
461         result = get_placeholder_shapes(argv_input, input_shapes)
462         exp_res = np.array([12, 4, 1])
463         np.testing.assert_array_equal(result, exp_res)
464
465     def test_get_shapes_no_input_two_shapes(self):
466         argv_input = ""
467         input_shapes = "(12,4,1),(5,4,3)"
468         self.assertRaises(Error, get_placeholder_shapes, argv_input, input_shapes)
469
470     def test_get_shapes_one_input_no_shape(self):
471         argv_input = "inp1"
472         input_shapes = ""
473         result = get_placeholder_shapes(argv_input, input_shapes)
474         exp_res = {'inp1': np.array([None])}
475         self.assertEqual(list(exp_res.keys()), list(result.keys()))
476         for i in exp_res.keys():
477             np.testing.assert_array_equal(result[i], exp_res[i])
478
479     def test_get_shapes_one_input_wrong_shape8(self):
480         argv_input = "inp1"
481         input_shapes = "[2,4,1)"
482         self.assertRaises(Error, get_placeholder_shapes, argv_input, input_shapes)
483
484     def test_get_shapes_one_input_wrong_shape9(self):
485         argv_input = "inp1"
486         input_shapes = "(2,4,1]"
487         self.assertRaises(Error, get_placeholder_shapes, argv_input, input_shapes)
488
489     def test_get_shapes_one_input_wrong_shape10(self):
490         argv_input = "inp1"
491         input_shapes = "(2,,,4,1]"
492         self.assertRaises(Error, get_placeholder_shapes, argv_input, input_shapes)
493
494     def test_get_shapes_one_input_wrong_shape2(self):
495         argv_input = "inp1"
496         input_shapes = "(2,4,1"
497         self.assertRaises(Error, get_placeholder_shapes, argv_input, input_shapes)
498
499     def test_get_shapes_one_input_wrong_shape3(self):
500         argv_input = "inp1"
501         input_shapes = "2,4,1"
502         self.assertRaises(Error, get_placeholder_shapes, argv_input, input_shapes)
503
504     def test_get_shapes_one_input_wrong_shape4(self):
505         argv_input = "inp1"
506         input_shapes = "2;4;1"
507         self.assertRaises(Error, get_placeholder_shapes, argv_input, input_shapes)
508
509     def test_get_shapes_one_input_wrong_shape5(self):
510         argv_input = "inp1"
511         input_shapes = "2,         4,1"
512         self.assertRaises(Error, get_placeholder_shapes, argv_input, input_shapes)
513
514     def test_get_shapes_one_input_wrong_shape6(self):
515         argv_input = "inp1"
516         input_shapes = "(2,         4,1),[4,6,8]"
517         self.assertRaises(Error, get_placeholder_shapes, argv_input, input_shapes)
518
519     def test_get_shapes_one_input_wrong_shape7(self):
520         argv_input = "inp1"
521         input_shapes = "[2,4,1],(4,6,8)"
522         self.assertRaises(Error, get_placeholder_shapes, argv_input, input_shapes)
523
524     def test_get_shapes_one_input_several_shapes(self):
525         argv_input = "inp1"
526         input_shapes = "(2,4,1),(4,6,8)"
527         self.assertRaises(Error, get_placeholder_shapes, argv_input, input_shapes)
528
529     def test_get_shapes_one_input_first_neg_shape1(self):
530         argv_input = "inp1,inp2"
531         input_shapes = "(-1,4,1),(4,6,8)"
532         result = get_placeholder_shapes(argv_input, input_shapes)
533         exp_res = {'inp1': np.array([-1, 4, 1]), 'inp2': np.array([4, 6, 8])}
534         self.assertEqual(list(exp_res.keys()), list(result.keys()))
535         for i in exp_res.keys():
536             np.testing.assert_array_equal(result[i], exp_res[i])
537
538     def test_get_shapes_one_input_first_neg_shape_not_one(self):
539         argv_input = "inp1"
540         input_shapes = "(-12,4,1),(4,6,8)"
541         self.assertRaises(Error, get_placeholder_shapes, argv_input, input_shapes)
542
543     def test_get_shapes_one_input_any_neg_shape(self):
544         argv_input = "inp1, inp2"
545         input_shapes = "(12,4,1),(4,-6,8)"
546         self.assertRaises(Error, get_placeholder_shapes, argv_input, input_shapes)
547
548
549 class TestModelNameParsing(unittest.TestCase):
550     def test_model_name_ideal(self):
551         model_name = '/home/models/mymodel.caffemodel'
552         res = get_model_name(model_name)
553         exp_res = 'mymodel'
554         self.assertEqual(exp_res, res)
555
556     def test_model_name_no_name(self):
557         model_name = '/home/models/.caffemodel'
558         res = get_model_name(model_name)
559         exp_res = 'model'
560         self.assertEqual(exp_res, res)
561
562     def test_model_name_no_ext(self):
563         model_name = '/home/models/caffemodel'
564         res = get_model_name(model_name)
565         exp_res = 'caffemodel'
566         self.assertEqual(exp_res, res)
567
568     def test_model_name_no_name_no_path(self):
569         model_name = '.caffemodel'
570         res = get_model_name(model_name)
571         exp_res = 'model'
572         self.assertEqual(exp_res, res)
573
574     @patch("mo.utils.cli_parser.os")
575     def test_model_name_win(self, old_os):
576         old_os.path.basename.return_value = "caffemodel"
577         old_os.path.splitext.return_value = ("caffemodel", "")
578         model_name = r'\home\models\caffemodel'
579         res = get_model_name(model_name)
580
581         exp_res = 'caffemodel'
582         self.assertEqual(exp_res, res)
583
584     def test_model_name_dots(self):
585         model_name = r'/home/models/squeezenet_v1.1.caffemodel'
586         res = get_model_name(model_name)
587         exp_res = 'squeezenet_v1.1'
588         self.assertEqual(exp_res, res)
589
590
591 class PositiveChecker(unittest.TestCase):
592     def test_positive_checker_batch(self):
593         res = check_positive('1')
594         self.assertEqual(res, 1)
595
596     def test_positive_checker_batch_negative(self):
597         self.assertRaises(argparse.ArgumentTypeError, check_positive, '-1')
598
599     def test_positive_checker_batch_not_int(self):
600         self.assertRaises(argparse.ArgumentTypeError, check_positive, 'qwe')
601
602
603 class PathCheckerFunctions(unittest.TestCase):
604     READABLE_DIR = tempfile.gettempdir()
605     WRITABLE_DIR = os.path.join(tempfile.gettempdir(), 'writable_dir')
606     WRITABLE_NON_EXISTING_DIR = os.path.join(WRITABLE_DIR, 'non_existing_dir')
607     NOT_WRITABLE_DIR = os.path.join(tempfile.gettempdir(), 'not_writable_dir')
608     NOT_WRITABLE_SUB_DIR = os.path.join(tempfile.gettempdir(), 'another_not_writable_dir', 'not_existing_dir')
609     EXISTING_FILE = tempfile.NamedTemporaryFile(mode='r+', delete=False).name
610     NOT_EXISTING_FILE = '/abcd/efgh/ijkl'
611
612     @classmethod
613     def setUpClass(cls):
614         if not os.path.exists(__class__.WRITABLE_DIR):
615             os.makedirs(__class__.WRITABLE_DIR)
616         if os.path.exists(__class__.WRITABLE_NON_EXISTING_DIR):
617             os.removedirs(__class__.WRITABLE_NON_EXISTING_DIR)
618
619         if not os.path.exists(__class__.NOT_WRITABLE_DIR):
620             os.makedirs(__class__.NOT_WRITABLE_DIR)
621         os.chmod(__class__.NOT_WRITABLE_DIR, 0)
622
623         if not os.path.exists(os.path.dirname(__class__.NOT_WRITABLE_SUB_DIR)):
624             os.makedirs(os.path.dirname(__class__.NOT_WRITABLE_SUB_DIR))
625         os.chmod(os.path.dirname(__class__.NOT_WRITABLE_SUB_DIR), 0)
626         if os.path.exists(__class__.NOT_EXISTING_FILE):
627             os.remove(__class__.NOT_EXISTING_FILE)
628
629     @classmethod
630     def tearDownClass(cls):
631         if os.path.exists(__class__.WRITABLE_DIR):
632             os.removedirs(__class__.WRITABLE_DIR)
633         if os.path.exists(__class__.NOT_WRITABLE_DIR):
634             os.removedirs(__class__.NOT_WRITABLE_DIR)
635         if os.path.exists(os.path.dirname(__class__.NOT_WRITABLE_SUB_DIR)):
636             os.removedirs(os.path.dirname(__class__.NOT_WRITABLE_SUB_DIR))
637         if os.path.exists(__class__.EXISTING_FILE):
638             os.remove(__class__.EXISTING_FILE)
639
640     def test_single_writable_dir(self):
641         self.assertEqual(__class__.WRITABLE_DIR, writable_dir(__class__.WRITABLE_DIR))
642
643     def test_single_non_writable_dir(self):
644         with self.assertRaises(Error) as cm:
645             writable_dir(__class__.NOT_WRITABLE_DIR)
646
647     def test_single_non_writable_sub_dir(self):
648         with self.assertRaises(Error) as cm:
649             writable_dir(__class__.NOT_WRITABLE_SUB_DIR)
650
651     def test_multiple_writable_dirs(self):
652         dirs_str = ','.join([__class__.WRITABLE_DIR, __class__.WRITABLE_NON_EXISTING_DIR])
653         self.assertEqual(dirs_str, writable_dir(dirs_str))
654
655     def test_single_writable_non_existing_dir(self):
656         self.assertEqual(__class__.WRITABLE_NON_EXISTING_DIR, writable_dir(__class__.WRITABLE_NON_EXISTING_DIR))
657
658     def test_readable_dirs(self):
659         dirs_str = ','.join([__class__.WRITABLE_DIR, __class__.READABLE_DIR])
660         self.assertEqual(dirs_str, readable_dirs(dirs_str))
661
662     def test_not_readable_dirs(self):
663         dirs_str = ','.join([__class__.WRITABLE_DIR, __class__.WRITABLE_NON_EXISTING_DIR])
664         with self.assertRaises(Error) as cm:
665             readable_dirs(dirs_str)
666
667     def test_readable_file(self):
668         self.assertEqual(__class__.EXISTING_FILE, readable_file(__class__.EXISTING_FILE))
669
670     def test_non_readable_file(self):
671         with self.assertRaises(Error) as cm:
672             readable_file(__class__.NOT_EXISTING_FILE)