-
Notifications
You must be signed in to change notification settings - Fork 15
/
test_markdown.py
185 lines (170 loc) · 5.78 KB
/
test_markdown.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
"""This is the test module for the markdown module"""
import unittest
from unittest.mock import mock_open, patch
import contributor_stats
from markdown import write_to_markdown
class TestMarkdown(unittest.TestCase):
"""
Test case for the markdown module.
"""
@patch("builtins.open", new_callable=mock_open)
def test_write_to_markdown(self, mock_file):
"""
Test the write_to_markdown function.
"""
person1 = contributor_stats.ContributorStats(
"user1",
False,
"url",
100,
"commit url",
"sponsor_url_1",
)
person2 = contributor_stats.ContributorStats(
"user2",
False,
"url2",
200,
"commit url2",
"sponsor_url_2",
)
# Set person2 as a new contributor since this cannot be set on initiatization of the object
person2.new_contributor = True
collaborators = [
person1,
person2,
]
ghe = ""
write_to_markdown(
collaborators,
"filename",
"2023-01-01",
"2023-01-02",
None,
"org/repo",
"false",
"true",
ghe,
)
mock_file.assert_called_once_with("filename", "w", encoding="utf-8")
mock_file().write.assert_any_call("# Contributors\n\n")
mock_file().write.assert_any_call(
"- Date range for contributor list: 2023-01-01 to 2023-01-02\n"
)
mock_file().write.assert_any_call(
"| Total Contributors | Total Contributions | % New Contributors |\n| --- | --- | --- |\n| 2 | 300 | 50.0% |\n\n"
)
mock_file().write.assert_any_call(
"| Username | All Time Contribution Count | New Contributor | Commits between 2023-01-01 and 2023-01-02 |\n"
"| --- | --- | --- | --- |\n"
"| @user1 | 100 | False | commit url |\n"
"| @user2 | 200 | True | commit url2 |\n"
)
@patch("builtins.open", new_callable=mock_open)
def test_write_to_markdown_with_sponsors(self, mock_file):
"""
Test the write_to_markdown function with sponsors info turned on.
"""
person1 = contributor_stats.ContributorStats(
"user1",
False,
"url",
100,
"commit url",
"sponsor_url_1",
)
person2 = contributor_stats.ContributorStats(
"user2",
False,
"url2",
200,
"commit url2",
"",
)
# Set person2 as a new contributor since this cannot be set on initiatization of the object
person2.new_contributor = True
collaborators = [
person1,
person2,
]
ghe = ""
write_to_markdown(
collaborators,
"filename",
"2023-01-01",
"2023-01-02",
None,
"org/repo",
"true",
"true",
ghe,
)
mock_file.assert_called_once_with("filename", "w", encoding="utf-8")
mock_file().write.assert_any_call("# Contributors\n\n")
mock_file().write.assert_any_call(
"- Date range for contributor list: 2023-01-01 to 2023-01-02\n"
)
mock_file().write.assert_any_call(
"| Total Contributors | Total Contributions | % New Contributors |\n| --- | --- | --- |\n| 2 | 300 | 50.0% |\n\n"
)
mock_file().write.assert_any_call(
"| Username | All Time Contribution Count | New Contributor | Sponsor URL | Commits between 2023-01-01 and 2023-01-02 |\n"
"| --- | --- | --- | --- | --- |\n"
"| @user1 | 100 | False | [Sponsor Link](sponsor_url_1) | commit url |\n"
"| @user2 | 200 | True | not sponsorable | commit url2 |\n"
)
@patch("builtins.open", new_callable=mock_open)
def test_write_to_markdown_without_link_to_profile(self, mock_file):
"""
Test the write_to_markdown function with link to profile turned off.
"""
person1 = contributor_stats.ContributorStats(
"user1",
False,
"url",
100,
"commit url",
"sponsor_url_1",
)
person2 = contributor_stats.ContributorStats(
"user2",
False,
"url2",
200,
"commit url2",
"sponsor_url_2",
)
# Set person2 as a new contributor since this cannot be set on initiatization of the object
person2.new_contributor = True
collaborators = [
person1,
person2,
]
ghe = ""
write_to_markdown(
collaborators,
"filename",
"2023-01-01",
"2023-01-02",
None,
"org/repo",
"false",
"false",
ghe,
)
mock_file.assert_called_once_with("filename", "w", encoding="utf-8")
mock_file().write.assert_any_call("# Contributors\n\n")
mock_file().write.assert_any_call(
"- Date range for contributor list: 2023-01-01 to 2023-01-02\n"
)
mock_file().write.assert_any_call(
"| Total Contributors | Total Contributions | % New Contributors |\n| --- | --- | --- |\n| 2 | 300 | 50.0% |\n\n"
)
mock_file().write.assert_any_call(
"| Username | All Time Contribution Count | New Contributor | Commits between 2023-01-01 and 2023-01-02 |\n"
"| --- | --- | --- | --- |\n"
"| user1 | 100 | False | commit url |\n"
"| user2 | 200 | True | commit url2 |\n"
)
if __name__ == "__main__":
unittest.main()