forked from phenom-films/dayu_widgets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_utils_get_static_file.py
More file actions
101 lines (88 loc) · 3.07 KB
/
test_utils_get_static_file.py
File metadata and controls
101 lines (88 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Test get_static_file function.
"""
# Import future modules
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
# Import built-in modules
import os
# Import third-party modules
import pytest
import six
# Import local modules
from dayu_widgets import CUSTOM_STATIC_FOLDERS
from dayu_widgets import DEFAULT_STATIC_FOLDER
from dayu_widgets import utils
@pytest.fixture(scope="module", name="custom_folder")
def setup_custom_folder(tmpdir_factory):
"""Create a folder to represent user's custom static folder"""
# user has his custom static folder
# put this icons or images in it
tmp_folder = tmpdir_factory.mktemp("my_static")
# create a file in base dir
tmp_folder.join("add_line.svg").ensure()
# create a sub folder and a file in the sub folder
tmp_folder.join("sub_folder", "sub_file.png").ensure()
return tmp_folder
@pytest.mark.parametrize(
"input_path, output_path",
(
("add_line.svg", os.path.join(DEFAULT_STATIC_FOLDER, "add_line.svg")),
("check.svg", os.path.join(DEFAULT_STATIC_FOLDER, "check.svg")),
("", None),
("a_not_exists_file", None),
(
os.path.join(os.path.dirname(__file__), "for_test.txt"),
os.path.join(os.path.dirname(__file__), "for_test.txt"),
), # user give a full path file, return
("main.qss", os.path.join(DEFAULT_STATIC_FOLDER, "main.qss")),
),
)
def test_get_static_file(input_path, output_path):
"""Only default static file. Test different situation input."""
assert utils.get_static_file(input_path) == output_path
def test_custom_static_folder(custom_folder):
"""Test when user append a custom static folder."""
CUSTOM_STATIC_FOLDERS.append(str(custom_folder))
for input_file, result in (
("add_line.svg", os.path.join(DEFAULT_STATIC_FOLDER, "add_line.svg")),
("check.svg", os.path.join(DEFAULT_STATIC_FOLDER, "check.svg")),
("", None),
("a_not_exists_file", None),
(
os.path.join(os.path.dirname(__file__), "for_test.txt"),
# user give a full path file, return
os.path.join(os.path.dirname(__file__), "for_test.txt"),
),
(
"sub_folder/sub_file.png",
os.path.join(str(custom_folder), "sub_folder/sub_file.png"),
),
):
assert utils.get_static_file(input_file) == result
@pytest.mark.parametrize(
"input_file, error_type",
(
(3, int),
(set(), set),
({}, dict),
(["g"], list),
((2,), tuple),
(object(), object),
),
)
def test_with_wrong_type(input_file, error_type):
"""Make sure when user give a wrong type arg, raise TypeError"""
with pytest.raises(TypeError) as exc_info:
utils.get_static_file(input_file)
exception_msg = exc_info.value.args[0]
print(exception_msg)
assert (
exception_msg
== "Input argument 'path' should be six.string_types type, but get {}".format(
error_type
)
)