1
- from unittest .mock import call , patch , Mock
1
+ from unittest .mock import call , patch
2
2
import getpass
3
- import os
4
- from pathlib import Path
5
- import pytest
6
3
7
- from scripts .pa_autoconfigure_django import main , download_repo
8
-
9
-
10
- @pytest .fixture
11
- def mock_main_functions ():
12
- mocks = Mock ()
13
- patchers = []
14
- functions = [
15
- 'download_repo' ,
16
- 'DjangoProject' ,
17
- ]
18
- for function in functions :
19
- mock = getattr (mocks , function )
20
- patcher = patch (
21
- 'scripts.pa_autoconfigure_django.{}' .format (function ),
22
- mock
23
- )
24
- patchers .append (patcher )
25
- patcher .start ()
26
-
27
- yield mocks
28
-
29
- for patcher in patchers :
30
- patcher .stop ()
4
+ from scripts .pa_autoconfigure_django import main
31
5
32
6
33
7
34
8
class TestMain :
35
9
36
- def test_calls_all_stuff_in_right_order (self , mock_main_functions ):
37
- main ('https://github.com/pythonanywhere.com/example-django-project.git' , 'www.domain.com' , 'python.version' , nuke = 'nuke option' )
38
- mock_django_project = mock_main_functions .DjangoProject .return_value
39
- assert mock_main_functions .method_calls == [
40
- call .download_repo ('https://github.com/pythonanywhere.com/example-django-project.git' , 'www.domain.com' , nuke = 'nuke option' ),
41
- call .DjangoProject ('www.domain.com' ),
42
- ]
43
- assert mock_django_project .method_calls == [
10
+ def test_calls_all_stuff_in_right_order (self ):
11
+ with patch ('scripts.pa_autoconfigure_django.DjangoProject' ) as mock_DjangoProject :
12
+ main (
13
+ 'https://github.com/pythonanywhere.com/example-django-project.git' ,
14
+ 'www.domain.com' ,
15
+ 'python.version' ,
16
+ nuke = 'nuke option'
17
+ )
18
+ assert mock_DjangoProject .call_args == call ('www.domain.com' )
19
+ assert mock_DjangoProject .return_value .method_calls == [
44
20
call .sanity_checks (nuke = 'nuke option'),
21
+ call .download_repo ('https://github.com/pythonanywhere.com/example-django-project.git' , nuke = 'nuke option' ),
45
22
call .create_virtualenv ('python.version' , nuke = 'nuke option' ),
46
23
call .update_settings_file (),
47
24
call .run_collectstatic (),
@@ -52,52 +29,27 @@ def test_calls_all_stuff_in_right_order(self, mock_main_functions):
52
29
]
53
30
54
31
55
- def test_domain_defaults_to_using_current_username (self , mock_main_functions ):
32
+ def test_domain_defaults_to_using_current_username (self ):
56
33
username = getpass .getuser ()
57
- main ('a-repo' , 'your-username.pythonanywhere.com' , 'python.version' , nuke = False )
58
- assert mock_main_functions .DjangoProject .call_args == call (
34
+ with patch ('scripts.pa_autoconfigure_django.DjangoProject' ) as mock_DjangoProject :
35
+ main ('a-repo' , 'your-username.pythonanywhere.com' , 'python.version' , nuke = False )
36
+ assert mock_DjangoProject .call_args == call (
59
37
username + '.pythonanywhere.com'
60
38
)
61
39
62
40
63
- def test_lowercases_username (self , mock_main_functions ):
41
+ def test_lowercases_username (self ):
64
42
with patch ('scripts.pa_autoconfigure_django.getpass' ) as mock_getpass :
65
43
mock_getpass .getuser .return_value = 'UserName1'
66
- main ('a-url' , 'your-username.pythonanywhere.com' , 'python.version' , 'nukey' )
67
- assert mock_main_functions .DjangoProject .call_args == call (
44
+ with patch ('scripts.pa_autoconfigure_django.DjangoProject' ) as mock_DjangoProject :
45
+ main ('a-url' , 'your-username.pythonanywhere.com' , 'python.version' , 'nukey' )
46
+ assert mock_DjangoProject .call_args == call (
68
47
'username1.pythonanywhere.com' ,
69
48
)
70
49
71
50
72
- class TestDownloadRepo :
73
-
74
- @pytest .mark .slowtest
75
- def test_actually_downloads_repo (self , fake_home ):
76
- new_folder = download_repo ('https://gist.github.com/hjwp/4173bcface139beb7632ec93726f91ea' , 'a-domain.com' , nuke = False )
77
- print (os .listdir (fake_home ))
78
- assert new_folder .is_dir ()
79
- assert 'file1.py' in os .listdir (new_folder )
80
- assert 'file2.py' in os .listdir (new_folder )
81
-
82
-
83
- def test_calls_git_subprocess (self , mock_subprocess , fake_home ):
84
- new_folder = download_repo ('repo' , 'a-domain.com' , nuke = False )
85
- assert new_folder == Path (fake_home ) / 'a-domain.com'
86
- assert mock_subprocess .check_call .call_args == call (
87
- ['git' , 'clone' , 'repo' , new_folder ]
88
- )
89
-
90
-
91
- def test_nuke_option (self , mock_subprocess , fake_home ):
92
- mock_subprocess .check_call .side_effect = lambda * _ , ** __ : Path (fake_home / 'a-domain.com' ).mkdir ()
93
- Path (fake_home / 'a-domain.com' ).mkdir ()
94
- Path (fake_home / 'a-domain.com' / 'old-thing.txt' ).touch ()
95
- new_folder = download_repo ('repo' , 'a-domain.com' , nuke = True )
96
- assert 'old-thing.txt' not in new_folder .iterdir ()
97
-
98
51
99
52
def test_todos ():
100
- assert not 'move download_repo onto djangoproject'
101
53
assert not 'existing-project sanity checks eg settings.py not found, requirements empty'
102
54
assert not 'SECRET_KEY'
103
55
assert not 'database stuff?'
0 commit comments