0a8b5dc8f84b17e23810b353ba90d6603c2c00e5
[utils.git] / src / budget_sync / test / test_write_budget_markdown.py
1 import unittest
2 from budget_sync.config import Config
3 from budget_sync.test.mock_bug import MockBug
4 from budget_sync.test.mock_path import MockFilesystem, MockPath, DIR
5 from budget_sync.test.test_mock_path import make_filesystem_and_report_if_error
6 from budget_sync.budget_graph import BudgetGraph
7 from budget_sync.write_budget_markdown import (
8 write_budget_markdown, DisplayStatus, markdown_escape)
9 from budget_sync.util import BugStatus
10
11
12 class TestWriteBudgetMarkdown(unittest.TestCase):
13 maxDiff = None
14
15 def test_display_status(self):
16 for status in BugStatus:
17 DisplayStatus.from_status(status)
18
19 def test_markdown_escape(self):
20 self.assertEqual(markdown_escape("abc * def_k < &k"),
21 r"abc \* def\_k &lt; &amp;k")
22
23 def format_files_dict(self, files):
24 assert isinstance(files, dict)
25 files_list: "list[str]" = []
26 for path, contents in files.items():
27 assert isinstance(path, str)
28 if contents is DIR:
29 files_list.append(f" {path!r}: DIR,")
30 continue
31 assert isinstance(contents, bytes)
32 lines: "list[str]" = []
33 for line in contents.splitlines(keepends=True):
34 lines.append(f" {line!r}")
35 if len(lines) == 0:
36 files_list.append(f" {path!r}: b'',")
37 else:
38 lines_str = '\n'.join(lines)
39 files_list.append(f" {path!r}: (\n{lines_str}\n ),")
40 if len(files_list) == 0:
41 return "{}"
42 return "{\n" + "\n".join(files_list) + "\n}"
43
44 def assertFiles(self, expected_files, filesystem: MockFilesystem):
45 files = filesystem.files
46 self.assertIsInstance(expected_files, dict)
47 if files == expected_files:
48 return
49 files_str = self.format_files_dict(files)
50 expected_files_str = self.format_files_dict(expected_files)
51 self.assertEqual(
52 files, expected_files,
53 msg=f"\nfiles:\n{files_str}\nexpected:\n{expected_files_str}")
54
55 def test(self):
56 config = Config.from_str(
57 """
58 bugzilla_url = "https://bugzilla.example.com/"
59 [milestones]
60 [people."person1"]
61 email = "person1@example.com"
62 full_name = "Person One"
63 [people."person2"]
64 full_name = "Person Two"
65 """)
66 budget_graph = BudgetGraph([
67 MockBug(bug_id=1,
68 cf_budget_parent=None,
69 cf_budget="0",
70 cf_total_budget="0",
71 cf_nlnet_milestone=None,
72 cf_payees_list="",
73 summary="",
74 assigned_to="person1@example.com"),
75 ], config)
76 self.assertEqual([], budget_graph.get_errors())
77 with make_filesystem_and_report_if_error(self) as filesystem:
78 output_dir = MockPath("/output_dir/", filesystem=filesystem)
79 write_budget_markdown(budget_graph, output_dir)
80 self.assertFiles({
81 '/': DIR,
82 '/output_dir': DIR,
83 '/output_dir/person1.mdwn': (
84 b'<!-- autogenerated by budget-sync -->\n'
85 b'\n'
86 b'# Person One (person1)\n'
87 b'\n'
88 b'\n'
89 b'\n'
90 b'# Status Tracking\n'
91 b'\n'
92 ),
93 '/output_dir/person2.mdwn': (
94 b'<!-- autogenerated by budget-sync -->\n'
95 b'\n'
96 b'# Person Two (person2)\n'
97 b'\n'
98 b'\n'
99 b'\n'
100 b'# Status Tracking\n'
101 b'\n'
102 ),
103 }, filesystem)
104
105 def test2(self):
106 config = Config.from_str(
107 """
108 bugzilla_url = "https://bugzilla.example.com/"
109 [milestones]
110 "milestone 1" = { canonical_bug_id = 1 }
111 [people."person1"]
112 email = "person1@example.com"
113 full_name = "Person One"
114 [people."person2"]
115 full_name = "Person Two"
116 """)
117 budget_graph = BudgetGraph([
118 MockBug(bug_id=1,
119 cf_budget_parent=None,
120 cf_budget="700",
121 cf_total_budget="1000",
122 cf_nlnet_milestone="milestone 1",
123 cf_payees_list="",
124 summary="",
125 assigned_to="person1@example.com",
126 cf_is_in_nlnet_mou2="Yes"),
127 MockBug(bug_id=2,
128 cf_budget_parent=1,
129 cf_budget="100",
130 cf_total_budget="300",
131 cf_nlnet_milestone="milestone 1",
132 cf_payees_list="person2 = 100",
133 summary="",
134 assigned_to="person1@example.com",
135 cf_is_in_nlnet_mou2="Yes"),
136 MockBug(bug_id=3,
137 cf_budget_parent=2,
138 cf_budget="100",
139 cf_total_budget="200",
140 cf_nlnet_milestone="milestone 1",
141 cf_payees_list="person1 = 100",
142 summary="",
143 assigned_to="person1@example.com"),
144 MockBug(bug_id=4,
145 cf_budget_parent=3,
146 cf_budget="100",
147 cf_total_budget="100",
148 cf_nlnet_milestone="milestone 1",
149 cf_payees_list="person2 = 100",
150 summary="",
151 assigned_to="person1@example.com"),
152 ], config)
153 self.assertEqual([], budget_graph.get_errors())
154 with make_filesystem_and_report_if_error(self) as filesystem:
155 output_dir = MockPath("/output_dir/", filesystem=filesystem)
156 write_budget_markdown(budget_graph, output_dir)
157 self.assertFiles({
158 '/': DIR,
159 '/output_dir': DIR,
160 '/output_dir/person1.mdwn': (
161 b'<!-- autogenerated by budget-sync -->\n'
162 b'\n'
163 b'# Person One (person1)\n'
164 b'\n'
165 b'\n'
166 b'\n'
167 b'# Status Tracking\n'
168 b'\n'
169 b'\n'
170 b'## Payment not yet submitted\n'
171 b'\n'
172 b'\n'
173 b'### milestone 1\n'
174 b'\n'
175 b'* [Bug #3](https://bugzilla.example.com/show_bug.cgi?id=3):\n'
176 b' \n'
177 b' * &euro;100 which is the total amount\n'
178 b' * the closest parent task which is in the MoU is\n'
179 b' [Bug #2](https://bugzilla.example.com/show_bug.cgi?id=2)\n'
180 ),
181 '/output_dir/person2.mdwn': (
182 b'<!-- autogenerated by budget-sync -->\n'
183 b'\n'
184 b'# Person Two (person2)\n'
185 b'\n'
186 b'\n'
187 b'\n'
188 b'# Status Tracking\n'
189 b'\n'
190 b'\n'
191 b'## Payment not yet submitted\n'
192 b'\n'
193 b'\n'
194 b'### milestone 1\n'
195 b'\n'
196 b'* [Bug #2](https://bugzilla.example.com/show_bug.cgi?id=2):\n'
197 b' \n'
198 b' * &euro;100 which is the total amount\n'
199 b' * this task is in the MoU\n'
200 b'* [Bug #4](https://bugzilla.example.com/show_bug.cgi?id=4):\n'
201 b' \n'
202 b' * &euro;100 which is the total amount\n'
203 b' * the closest parent task which is in the MoU is\n'
204 b' [Bug #2](https://bugzilla.example.com/show_bug.cgi?id=2)\n'
205 ),
206 }, filesystem)
207 # TODO: add more test cases
208
209
210 if __name__ == "__main__":
211 unittest.main()