1
1
from django .test import TestCase , override_settings
2
- from django .core .exceptions import ImproperlyConfigured
3
2
from django .test .utils import captured_stdout
4
3
5
- from peps .converters import get_pep0_page , get_pep_page , add_pep_image
4
+ from unittest .mock import Mock , patch
5
+
6
+ from peps .converters import (
7
+ get_pep_page ,
8
+ add_pep_image ,
9
+ get_commit_history_info ,
10
+ )
6
11
7
12
from . import FAKE_PEP_REPO
8
13
9
14
10
15
class PEPConverterTests (TestCase ):
11
16
12
- def test_source_link (self ):
17
+ @patch ('peps.converters.get_commit_history_info' )
18
+ def test_source_link (self , mock_get_commit_history ):
19
+ mock_get_commit_history .return_value = ''
13
20
pep = get_pep_page (FAKE_PEP_REPO , '0525' )
14
21
self .assertEqual (pep .title , 'PEP 525 -- Asynchronous Generators' )
15
22
self .assertIn (
@@ -18,12 +25,63 @@ def test_source_link(self):
18
25
pep .content .rendered
19
26
)
20
27
21
- def test_source_link_rst (self ):
28
+ @patch ('peps.converters.get_commit_history_info' )
29
+ def test_source_link_rst (self , mock_get_commit_history ):
30
+ mock_get_commit_history .return_value = ''
22
31
pep = get_pep_page (FAKE_PEP_REPO , '0012' )
23
32
self .assertEqual (pep .title , 'PEP 12 -- Sample reStructuredText PEP Template' )
24
33
self .assertIn (
25
- 'Source: <a href="https://github.com/python/peps/blob/master/'
26
- 'pep-0012.rst">https://github.com/python/peps/blob/master/pep-0012.rst</a>' ,
34
+ '<div>Source: <a href="https://github.com/python/peps/blob/master/'
35
+ 'pep-0012.rst">https://github.com/python/peps/blob/master/pep-0012.rst</a></div>' ,
36
+ pep .content .rendered
37
+ )
38
+
39
+ @patch ('requests.get' )
40
+ def test_get_commit_history_info_with_data (self , mocked_gh_request ):
41
+
42
+ mocked_gh_request .return_value = Mock (ok = True )
43
+ mocked_gh_request .return_value .json .return_value = [
44
+ {
45
+ "commit" : {
46
+ "committer" : {
47
+ "name" : "miss-islington" ,
48
+ "date" : "2020-02-19T04:06:01Z" ,
49
+ }
50
+ }
51
+ }
52
+ ]
53
+
54
+ info = get_commit_history_info ('pep-0012.txt' )
55
+ self .assertEqual (
56
+ info ,
57
+ '<div>Last modified: <a href="https://github.com/python/peps/commits/master/pep-0012.txt">2020-02-19T04:06:01Z</a></div>'
58
+ )
59
+
60
+ @patch ('requests.get' )
61
+ def test_get_commit_history_info_no_data (self , mocked_gh_request ):
62
+ mocked_gh_request .return_value = Mock (ok = True )
63
+ mocked_gh_request .return_value .json .return_value = []
64
+
65
+ info = get_commit_history_info ('pep-0012.txt' )
66
+ self .assertEqual (info , '' )
67
+
68
+ @patch ('requests.get' )
69
+ def test_get_page_page_includes_last_modified (self , mocked_gh_request ):
70
+ mocked_gh_request .return_value = Mock (ok = True )
71
+ mocked_gh_request .return_value .json .return_value = [
72
+ {
73
+ "commit" : {
74
+ "committer" : {
75
+ "name" : "miss-islington" ,
76
+ "date" : "2020-02-19T04:06:01Z" ,
77
+ }
78
+ }
79
+ }
80
+ ]
81
+
82
+ pep = get_pep_page (FAKE_PEP_REPO , '0012' )
83
+ self .assertIn (
84
+ '<div>Last modified: <a href="https://github.com/python/peps/commits/master/pep-0012.rst">2020-02-19T04:06:01Z</a></div>' ,
27
85
pep .content .rendered
28
86
)
29
87
@@ -43,7 +101,10 @@ def test_add_image_not_found(self):
43
101
r"Image Path '(.*)/path/that/does/not/exist(.*)' does not exist, skipping"
44
102
)
45
103
46
- def test_html_do_not_prettify (self ):
104
+ @patch ('peps.converters.get_commit_history_info' )
105
+ def test_html_do_not_prettify (self , mock_get_commit_history ):
106
+ mock_get_commit_history .return_value = ''
107
+
47
108
pep = get_pep_page (FAKE_PEP_REPO , '3001' )
48
109
self .assertEqual (
49
110
pep .title ,
@@ -56,7 +117,10 @@ def test_html_do_not_prettify(self):
56
117
pep .content .rendered
57
118
)
58
119
59
- def test_strip_html_and_body_tags (self ):
120
+ @patch ('peps.converters.get_commit_history_info' )
121
+ def test_strip_html_and_body_tags (self , mock_get_commit_history ):
122
+ mock_get_commit_history .return_value = ''
123
+
60
124
pep = get_pep_page (FAKE_PEP_REPO , '0525' )
61
125
self .assertNotIn ('<html>' , pep .content .rendered )
62
126
self .assertNotIn ('</html>' , pep .content .rendered )
0 commit comments