[VVP] Generated completed preload from env files
[vvp/validation-scripts.git] / ice_validator / app_tests / test_helpers.py
1 # -*- coding: utf8 -*-
2 # ============LICENSE_START====================================================
3 # org.onap.vvp/validation-scripts
4 # ===================================================================
5 # Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 # ===================================================================
7 #
8 # Unless otherwise specified, all software contained herein is licensed
9 # under the Apache License, Version 2.0 (the "License");
10 # you may not use this software except in compliance with the License.
11 # You may obtain a copy of the License at
12 #
13 #             http://www.apache.org/licenses/LICENSE-2.0
14 #
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS,
17 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 # See the License for the specific language governing permissions and
19 # limitations under the License.
20 #
21 #
22 #
23 # Unless otherwise specified, all documentation contained herein is licensed
24 # under the Creative Commons License, Attribution 4.0 Intl. (the "License");
25 # you may not use this documentation except in compliance with the License.
26 # You may obtain a copy of the License at
27 #
28 #             https://creativecommons.org/licenses/by/4.0/
29 #
30 # Unless required by applicable law or agreed to in writing, documentation
31 # distributed under the License is distributed on an "AS IS" BASIS,
32 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33 # See the License for the specific language governing permissions and
34 # limitations under the License.
35 #
36 # ============LICENSE_END============================================
37 from pathlib import Path
38
39 import pytest
40
41 from tests.helpers import check, first, unzip, remove
42
43 THIS_DIR = Path(__file__).parent
44
45
46 def test_check_fail():
47     with pytest.raises(RuntimeError, match="pre-condition failed"):
48         check(False, "pre-condition failed")
49
50
51 def test_check_pass():
52     check(True, "pre-condition failed")
53
54
55 def test_first_found():
56     result = first(range(1, 10), lambda x: x % 4 == 0)
57     assert result == 4
58
59
60 def test_first_not_found():
61     result = first(range(1, 3), lambda x: x % 4 == 0)
62     assert result is None
63
64
65 def test_first_custom_default():
66     result = first(range(1, 3), lambda x: x % 4 == 0, default="not found")
67     assert result == "not found"
68
69
70 def test_unzip_success(tmpdir):
71     test_zip = THIS_DIR / "test_data.zip"
72     target_dir = tmpdir.join("sub-dir")
73     unzip(test_zip, target_dir)
74     assert "data.txt" in (p.basename for p in target_dir.listdir())
75
76
77 def test_unzip_not_found(tmpdir):
78     test_zip = THIS_DIR / "test_data1.zip"
79     with pytest.raises(RuntimeError, match="not a valid zipfile"):
80         unzip(test_zip, tmpdir)
81
82
83 def test_remove_with_no_key():
84     assert remove([1, 2, 3, 4], [3]) == [1, 2, 4]
85
86
87 def test_remove_with_key():
88     assert remove(["a", "b", "c", "d"], ["A"], lambda s: s.upper()) == ["b", "c", "d"]