• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

WenjieDu / PyPOTS / 4845620836

pending completion
4845620836

Pull #77

github

GitHub
Merge fd7b05c7e into 39b2bbebd
Pull Request #77: Fix dependency error in daily testing

3112 of 3668 relevant lines covered (84.84%)

0.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.82
/pypots/tests/test_cli.py
1
"""
1✔
2
Test cases for the functions and classes in package `pypots.cli`.
3
"""
4

5
# Created by Wenjie Du <wenjay.du@gmail.com>
6
# License: GLP-v3
7

8
import os
1✔
9
import threading
1✔
10
import unittest
1✔
11
from argparse import Namespace
1✔
12
from copy import copy
1✔
13

14
import pytest
1✔
15

16
from pypots.cli.dev import dev_command_factory
1✔
17
from pypots.cli.doc import doc_command_factory
1✔
18
from pypots.cli.env import env_command_factory
1✔
19
from pypots.utils.logging import logger
1✔
20

21
PROJECT_ROOT_DIR = os.path.abspath(os.path.join(os.path.abspath(__file__), "../../.."))
1✔
22

23

24
def callback_func():
1✔
25
    raise TimeoutError("Time out.")
×
26

27

28
def time_out(interval, callback):
1✔
29
    def decorator(func):
1✔
30
        def wrapper(*args, **kwargs):
1✔
31
            t = threading.Thread(target=func, args=args, kwargs=kwargs)
1✔
32
            t.setDaemon(True)
1✔
33
            t.start()
1✔
34
            t.join(interval)  # wait for interval seconds
1✔
35
            if t.is_alive():
1✔
36
                return threading.Timer(0, callback).start()  # invoke callback()
×
37
            else:
38
                return
1✔
39

40
        return wrapper
1✔
41

42
    return decorator
1✔
43

44

45
class TestPyPOTSCLIDev(unittest.TestCase):
1✔
46
    # set up the default arguments
47
    default_arguments = {
1✔
48
        "build": False,
49
        "cleanup": False,
50
        "run_tests": False,
51
        "k": None,
52
        "show_coverage": False,
53
        "lint_code": False,
54
    }
55
    # `pypots-cli dev` must run under the project root dir
56
    os.chdir(PROJECT_ROOT_DIR)
1✔
57

58
    @pytest.mark.xdist_group(name="cli-dev")
1✔
59
    def test_0_build(self):
60
        arguments = copy(self.default_arguments)
1✔
61
        arguments["build"] = True
1✔
62
        args = Namespace(**arguments)
1✔
63
        dev_command_factory(args).run()
1✔
64

65
    @pytest.mark.xdist_group(name="cli-dev")
1✔
66
    def test_1_run_tests(self):
67
        arguments = copy(self.default_arguments)
1✔
68
        arguments["run_tests"] = True
1✔
69
        arguments["k"] = "try_to_find_a_non_existing_test_case"
1✔
70
        args = Namespace(**arguments)
1✔
71
        try:
1✔
72
            dev_command_factory(args).run()
1✔
73
        except RuntimeError:  # try to find a non-existing test case, so RuntimeError will be raised
1✔
74
            pass
1✔
75
        except Exception as e:  # other exceptions will cause an error and result in failed testing
×
76
            raise e
×
77

78
    # Don't test --lint-code because Black will reformat the code and cause error when generating the coverage report
79
    # @pytest.mark.xdist_group(name="cli-dev")
80
    # def test_2_lint_code(self):
81
    #     arguments = copy(self.default_arguments)
82
    #     arguments["lint_code"] = True
83
    #     args = Namespace(**arguments)
84
    #     dev_command_factory(args).run()
85

86
    @pytest.mark.xdist_group(name="cli-dev")
1✔
87
    def test_3_cleanup(self):
88
        arguments = copy(self.default_arguments)
1✔
89
        arguments["cleanup"] = True
1✔
90
        args = Namespace(**arguments)
1✔
91
        dev_command_factory(args).run()
1✔
92

93

94
class TestPyPOTSCLIDoc(unittest.TestCase):
1✔
95
    # set up the default arguments
96
    default_arguments = {
1✔
97
        "gene_rst": False,
98
        "branch": "main",
99
        "gene_html": False,
100
        "view_doc": False,
101
        "port": 9075,
102
        "cleanup": False,
103
    }
104
    # `pypots-cli doc` must run under the project root dir
105
    os.chdir(PROJECT_ROOT_DIR)
1✔
106

107
    @pytest.mark.xdist_group(name="cli-doc")
1✔
108
    def test_0_gene_rst(self):
109
        arguments = copy(self.default_arguments)
1✔
110
        arguments["gene_rst"] = True
1✔
111
        args = Namespace(**arguments)
1✔
112
        doc_command_factory(args).run()
1✔
113

114
        logger.info("run again under a non-root dir")
1✔
115
        try:
1✔
116
            os.chdir(os.path.abspath(os.path.join(PROJECT_ROOT_DIR, "pypots")))
1✔
117
            doc_command_factory(args).run()
1✔
118
        except RuntimeError:  # try to run under a non-root dir, so RuntimeError will be raised
1✔
119
            pass
1✔
120
        except Exception as e:  # other exceptions will cause an error and result in failed testing
×
121
            raise e
×
122
        finally:
123
            os.chdir(PROJECT_ROOT_DIR)
1✔
124

125
    @pytest.mark.xdist_group(name="cli-doc")
1✔
126
    def test_1_gene_html(self):
127
        arguments = copy(self.default_arguments)
1✔
128
        arguments["gene_html"] = True
1✔
129
        args = Namespace(**arguments)
1✔
130
        try:
1✔
131
            doc_command_factory(args).run()
1✔
132
        except Exception as e:  # somehow we have some error when testing on Windows, so just print and pass below
×
133
            logger.error(e)
×
134

135
    @pytest.mark.xdist_group(name="cli-doc")
1✔
136
    @time_out(2, callback_func)  # wait for two seconds
1✔
137
    def test_2_view_doc(self):
138
        arguments = copy(self.default_arguments)
1✔
139
        arguments["view_doc"] = True
1✔
140
        args = Namespace(**arguments)
1✔
141
        try:
1✔
142
            doc_command_factory(args).run()
1✔
143
        except Exception as e:  # somehow we have some error when testing on Windows, so just print and pass below
1✔
144
            logger.error(e)
1✔
145

146
    @pytest.mark.xdist_group(name="cli-doc")
1✔
147
    def test_3_cleanup(self):
148
        arguments = copy(self.default_arguments)
1✔
149
        arguments["cleanup"] = True
1✔
150
        args = Namespace(**arguments)
1✔
151
        doc_command_factory(args).run()
1✔
152

153

154
class TestPyPOTSCLIEnv(unittest.TestCase):
1✔
155
    # set up the default arguments
156
    default_arguments = {
1✔
157
        "install": "optional",
158
        "tool": "conda",
159
    }
160

161
    # `pypots-cli env` must run under the project root dir
162
    os.chdir(PROJECT_ROOT_DIR)
1✔
163

164
    @pytest.mark.xdist_group(name="cli-env")
1✔
165
    def test_0_install_with_conda(self):
166
        arguments = copy(self.default_arguments)
1✔
167
        arguments["tool"] = "conda"
1✔
168
        args = Namespace(**arguments)
1✔
169
        try:
1✔
170
            env_command_factory(args).run()
1✔
171
        except Exception as e:  # somehow we have some error when testing on Windows, so just print and pass below
1✔
172
            logger.error(e)
1✔
173

174
    @pytest.mark.xdist_group(name="cli-env")
1✔
175
    def test_1_install_with_pip(self):
176
        arguments = copy(self.default_arguments)
1✔
177
        arguments["tool"] = "pip"
1✔
178
        args = Namespace(**arguments)
1✔
179
        try:
1✔
180
            env_command_factory(args).run()
1✔
181
        except Exception as e:  # somehow we have some error when testing on Windows, so just print and pass below
1✔
182
            logger.error(e)
1✔
183

184

185
if __name__ == "__main__":
1✔
186
    unittest.main()
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc