forked from piccolo-orm/piccolo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_run.py
More file actions
73 lines (54 loc) · 2.23 KB
/
test_run.py
File metadata and controls
73 lines (54 loc) · 2.23 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
import os
from unittest import TestCase
from unittest.mock import MagicMock, patch
from piccolo.apps.tester.commands.run import run, set_env_var
class TestSetEnvVar(TestCase):
def test_no_existing_value(self):
"""
Make sure the environment variable is set correctly, when there is
no existing value.
"""
var_name = "PICCOLO_TEST_1"
# Make sure it definitely doesn't exist already
if os.environ.get(var_name) is not None:
del os.environ[var_name]
new_value = "hello world"
with set_env_var(var_name=var_name, temp_value=new_value):
self.assertEqual(os.environ.get(var_name), new_value)
self.assertEqual(os.environ.get(var_name), None)
def test_existing_value(self):
"""
Make sure the environment variable is set correctly, when there is
an existing value.
"""
var_name = "PICCOLO_TEST_2"
initial_value = "hello"
new_value = "goodbye"
os.environ[var_name] = initial_value
with set_env_var(var_name=var_name, temp_value=new_value):
self.assertEqual(os.environ.get(var_name), new_value)
self.assertEqual(os.environ.get(var_name), initial_value)
def test_raise_exception(self):
"""
Make sure the environment variable is still reset, even if an exception
is raised within the context manager body.
"""
var_name = "PICCOLO_TEST_3"
initial_value = "hello"
new_value = "goodbye"
os.environ[var_name] = initial_value
class FakeException(Exception):
pass
try:
with set_env_var(var_name=var_name, temp_value=new_value):
self.assertEqual(os.environ.get(var_name), new_value)
raise FakeException("Something went wrong ...")
except FakeException:
pass
self.assertEqual(os.environ.get(var_name), initial_value)
class TestRun(TestCase):
@patch("piccolo.apps.tester.commands.run.run_pytest")
def test_success(self, pytest: MagicMock):
with self.assertRaises(SystemExit):
run(pytest_args="-s foo", piccolo_conf="my_piccolo_conf")
pytest.assert_called_once_with(["-s", "foo"])