Skip to content

Commit 8bc297e

Browse files
Flat is better then nested in tests of project.
1 parent 879134a commit 8bc297e

File tree

1 file changed

+94
-99
lines changed

1 file changed

+94
-99
lines changed

tests/test_project.py

Lines changed: 94 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -9,103 +9,98 @@
99
from pythonanywhere.virtualenvs import Virtualenv
1010

1111

12-
class TestProject:
13-
def test_domain_and_python_version(self, fake_home, virtualenvs_folder):
14-
project = Project('mydomain.com', 'python.version')
15-
assert project.domain == 'mydomain.com'
16-
assert project.python_version == 'python.version'
17-
18-
def test_project_path(self, fake_home, virtualenvs_folder):
19-
project = Project('mydomain.com', 'python.version')
20-
assert project.project_path == fake_home / 'mydomain.com'
21-
22-
def test_wsgi_file_path(self, fake_home, virtualenvs_folder):
23-
project = Project('mydomain.com', 'python.version')
24-
assert project.wsgi_file_path == Path('/var/www/mydomain_com_wsgi.py')
25-
26-
def test_webapp(self, fake_home, virtualenvs_folder):
27-
project = Project('mydomain.com', 'python.version')
28-
assert project.webapp == Webapp('mydomain.com')
29-
30-
def test_virtualenv(self, fake_home, virtualenvs_folder):
31-
project = Project('mydomain.com', 'python.version')
32-
assert project.virtualenv == Virtualenv('mydomain.com', 'python.version')
33-
34-
35-
class TestSanityChecks:
36-
def test_calls_webapp_sanity_checks(self, fake_home, virtualenvs_folder):
37-
project = Project('mydomain.com', 'python.version')
38-
project.webapp.sanity_checks = Mock()
39-
project.sanity_checks(nuke='nuke.option')
40-
assert project.webapp.sanity_checks.call_args == call(nuke='nuke.option')
41-
42-
def test_raises_if_virtualenv_exists(self, fake_home, virtualenvs_folder):
43-
project = Project('mydomain.com', 'python.version')
44-
project.webapp.sanity_checks = Mock()
45-
project.virtualenv.path.mkdir()
46-
47-
with pytest.raises(SanityException) as e:
48-
project.sanity_checks(nuke=False)
49-
50-
assert "You already have a virtualenv for mydomain.com" in str(e.value)
51-
assert "nuke" in str(e.value)
52-
53-
def test_raises_if_project_path_exists(self, fake_home, virtualenvs_folder):
54-
project = Project('mydomain.com', 'python.version')
55-
project.webapp.sanity_checks = Mock()
56-
project.project_path.mkdir()
57-
58-
with pytest.raises(SanityException) as e:
59-
project.sanity_checks(nuke=False)
60-
61-
expected_msg = f"You already have a project folder at {fake_home}/mydomain.com"
62-
assert expected_msg in str(e.value)
63-
assert "nuke" in str(e.value)
64-
65-
def test_nuke_option_overrides_directory_checks(self, fake_home, virtualenvs_folder):
66-
project = Project('mydomain.com', 'python.version')
67-
project.webapp.sanity_checks = Mock()
68-
project.project_path.mkdir()
69-
project.virtualenv.path.mkdir()
70-
71-
project.sanity_checks(nuke=True) # should not raise
72-
73-
74-
class TestCreateWebapp:
75-
def test_calls_webapp_create(self, virtualenvs_folder):
76-
project = Project('mydomain.com', 'python.version')
77-
project.webapp.create = Mock()
78-
project.python_version = 'python.version'
79-
80-
project.create_webapp(nuke=True)
81-
assert project.webapp.create.call_args == call(
82-
'python.version', project.virtualenv.path, project.project_path, nuke=True
83-
)
84-
85-
86-
class TestAddStaticFilesMappings:
87-
def test_calls_webapp_add_default_static_files_mappings(self, virtualenvs_folder):
88-
project = Project('mydomain.com', 'python.version')
89-
project.webapp.add_default_static_files_mappings = Mock()
90-
project.add_static_file_mappings()
91-
assert project.webapp.add_default_static_files_mappings.call_args == call(
92-
project.project_path,
93-
)
94-
95-
96-
class TestStartBash:
97-
def test_calls_launch_bash_in_virtualenv_with_virtualenv_and_project_path(self, fake_home, virtualenvs_folder):
98-
project = Project('mydomain.com', 'python.version')
99-
with patch('pythonanywhere.project.launch_bash_in_virtualenv') as mock_launch_bash_in_virtualenv:
12+
def test_project_domain_and_python_version(fake_home, virtualenvs_folder):
13+
project = Project('mydomain.com', 'python.version')
14+
assert project.domain == 'mydomain.com'
15+
assert project.python_version == 'python.version'
16+
17+
def test_project_path(fake_home, virtualenvs_folder):
18+
project = Project('mydomain.com', 'python.version')
19+
assert project.project_path == fake_home / 'mydomain.com'
20+
21+
def test_project_wsgi_file_path(fake_home, virtualenvs_folder):
22+
project = Project('mydomain.com', 'python.version')
23+
assert project.wsgi_file_path == Path('/var/www/mydomain_com_wsgi.py')
24+
25+
def test_project_webapp(fake_home, virtualenvs_folder):
26+
project = Project('mydomain.com', 'python.version')
27+
assert project.webapp == Webapp('mydomain.com')
28+
29+
def test_project_virtualenv(fake_home, virtualenvs_folder):
30+
project = Project('mydomain.com', 'python.version')
31+
assert project.virtualenv == Virtualenv('mydomain.com', 'python.version')
32+
33+
34+
def test_sanity_checks_calls_webapp_sanity_checks(fake_home, virtualenvs_folder):
35+
project = Project('mydomain.com', 'python.version')
36+
project.webapp.sanity_checks = Mock()
37+
project.sanity_checks(nuke='nuke.option')
38+
assert project.webapp.sanity_checks.call_args == call(nuke='nuke.option')
39+
40+
def test_sanity_checks_raises_if_virtualenv_exists(fake_home, virtualenvs_folder):
41+
project = Project('mydomain.com', 'python.version')
42+
project.webapp.sanity_checks = Mock()
43+
project.virtualenv.path.mkdir()
44+
45+
with pytest.raises(SanityException) as e:
46+
project.sanity_checks(nuke=False)
47+
48+
assert "You already have a virtualenv for mydomain.com" in str(e.value)
49+
assert "nuke" in str(e.value)
50+
51+
def test_sanity_checks_raises_if_project_path_exists(fake_home, virtualenvs_folder):
52+
project = Project('mydomain.com', 'python.version')
53+
project.webapp.sanity_checks = Mock()
54+
project.project_path.mkdir()
55+
56+
with pytest.raises(SanityException) as e:
57+
project.sanity_checks(nuke=False)
58+
59+
expected_msg = f"You already have a project folder at {fake_home}/mydomain.com"
60+
assert expected_msg in str(e.value)
61+
assert "nuke" in str(e.value)
62+
63+
def test_sanity_checks_nuke_option_overrides_directory_checks(fake_home, virtualenvs_folder):
64+
project = Project('mydomain.com', 'python.version')
65+
project.webapp.sanity_checks = Mock()
66+
project.project_path.mkdir()
67+
project.virtualenv.path.mkdir()
68+
69+
project.sanity_checks(nuke=True) # should not raise
70+
71+
72+
def test_create_webapp_calls_webapp_create(virtualenvs_folder):
73+
project = Project('mydomain.com', 'python.version')
74+
project.webapp.create = Mock()
75+
project.python_version = 'python.version'
76+
77+
project.create_webapp(nuke=True)
78+
assert project.webapp.create.call_args == call(
79+
'python.version', project.virtualenv.path, project.project_path, nuke=True
80+
)
81+
82+
83+
def test_add_static_file_mappings_calls_webapp_add_default_static_files_mappings(virtualenvs_folder):
84+
project = Project('mydomain.com', 'python.version')
85+
project.webapp.add_default_static_files_mappings = Mock()
86+
project.add_static_file_mappings()
87+
assert project.webapp.add_default_static_files_mappings.call_args == call(
88+
project.project_path,
89+
)
90+
91+
92+
def test_start_bash_calls_launch_bash_in_virtualenv_with_virtualenv_and_project_path(fake_home, virtualenvs_folder):
93+
project = Project('mydomain.com', 'python.version')
94+
with patch('pythonanywhere.project.launch_bash_in_virtualenv') as mock_launch_bash_in_virtualenv:
95+
project.start_bash()
96+
args, kwargs = mock_launch_bash_in_virtualenv.call_args
97+
assert args[0] == project.virtualenv.path
98+
assert args[2] == project.project_path
99+
100+
def test_start_bash_calls_launch_bash_in_virtualenv_with_unique_id(fake_home, virtualenvs_folder):
101+
project = Project('mydomain.com', 'python.version')
102+
with patch('pythonanywhere.project.launch_bash_in_virtualenv') as mock_launch_bash_in_virtualenv:
103+
for _ in range(100):
100104
project.start_bash()
101-
args, kwargs = mock_launch_bash_in_virtualenv.call_args
102-
assert args[0] == project.virtualenv.path
103-
assert args[2] == project.project_path
104-
105-
def test_calls_launch_bash_in_virtualenv_with_unique_id(self, fake_home, virtualenvs_folder):
106-
project = Project('mydomain.com', 'python.version')
107-
with patch('pythonanywhere.project.launch_bash_in_virtualenv') as mock_launch_bash_in_virtualenv:
108-
for _ in range(100):
109-
project.start_bash()
110-
unique_ids = [args[1] for args, kwargs in mock_launch_bash_in_virtualenv.call_args_list]
111-
assert len(set(unique_ids)) == len(unique_ids)
105+
unique_ids = [args[1] for args, kwargs in mock_launch_bash_in_virtualenv.call_args_list]
106+
assert len(set(unique_ids)) == len(unique_ids)

0 commit comments

Comments
 (0)