2 # ============LICENSE_START====================================================
3 # org.onap.vvp/validation-scripts
4 # ===================================================================
5 # Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 # ===================================================================
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
13 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
28 # https://creativecommons.org/licenses/by/4.0/
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.
36 # ============LICENSE_END============================================
37 from pathlib import Path
41 from tests.helpers import check, first, unzip, remove
43 THIS_DIR = Path(__file__).parent
46 def test_check_fail():
47 with pytest.raises(RuntimeError, match="pre-condition failed"):
48 check(False, "pre-condition failed")
51 def test_check_pass():
52 check(True, "pre-condition failed")
55 def test_first_found():
56 result = first(range(1, 10), lambda x: x % 4 == 0)
60 def test_first_not_found():
61 result = first(range(1, 3), lambda x: x % 4 == 0)
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"
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())
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)
83 def test_remove_with_no_key():
84 assert remove([1, 2, 3, 4], [3]) == [1, 2, 4]
87 def test_remove_with_key():
88 assert remove(["a", "b", "c", "d"], ["A"], lambda s: s.upper()) == ["b", "c", "d"]