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
12 class TestWriteBudgetMarkdown(unittest
.TestCase
):
15 def test_display_status(self
):
16 for status
in BugStatus
:
17 DisplayStatus
.from_status(status
)
19 def test_markdown_escape(self
):
20 self
.assertEqual(markdown_escape("abc * def_k < &k"),
21 r
"abc \* def\_k < &k")
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)
29 files_list
.append(f
" {path!r}: DIR,")
31 assert isinstance(contents
, bytes
)
32 lines
: "list[str]" = []
33 for line
in contents
.splitlines(keepends
=True):
34 lines
.append(f
" {line!r}")
36 files_list
.append(f
" {path!r}: b'',")
38 lines_str
= '\n'.join(lines
)
39 files_list
.append(f
" {path!r}: (\n{lines_str}\n ),")
40 if len(files_list
) == 0:
42 return "{\n" + "\n".join(files_list
) + "\n}"
44 def assertFiles(self
, expected_files
, filesystem
: MockFilesystem
):
45 files
= filesystem
.files
46 self
.assertIsInstance(expected_files
, dict)
47 if files
== expected_files
:
49 files_str
= self
.format_files_dict(files
)
50 expected_files_str
= self
.format_files_dict(expected_files
)
52 files
, expected_files
,
53 msg
=f
"\nfiles:\n{files_str}\nexpected:\n{expected_files_str}")
56 config
= Config
.from_str(
58 bugzilla_url = "https://bugzilla.example.com/"
61 email = "person1@example.com"
62 full_name = "Person One"
64 full_name = "Person Two"
66 budget_graph
= BudgetGraph([
68 cf_budget_parent
=None,
71 cf_nlnet_milestone
=None,
74 assigned_to
="person1@example.com"),
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
)
83 '/output_dir/person1.mdwn': (
84 b
'<!-- autogenerated by budget-sync -->\n'
86 b
'# Person One (person1)\n'
90 b
'# Status Tracking\n'
93 '/output_dir/person2.mdwn': (
94 b
'<!-- autogenerated by budget-sync -->\n'
96 b
'# Person Two (person2)\n'
100 b
'# Status Tracking\n'
106 config
= Config
.from_str(
108 bugzilla_url = "https://bugzilla.example.com/"
110 "milestone 1" = { canonical_bug_id = 1 }
112 email = "person1@example.com"
113 full_name = "Person One"
115 full_name = "Person Two"
117 budget_graph
= BudgetGraph([
119 cf_budget_parent
=None,
121 cf_total_budget
="1000",
122 cf_nlnet_milestone
="milestone 1",
125 assigned_to
="person1@example.com",
126 cf_is_in_nlnet_mou2
="Yes"),
130 cf_total_budget
="300",
131 cf_nlnet_milestone
="milestone 1",
132 cf_payees_list
="person2 = 100",
134 assigned_to
="person1@example.com",
135 cf_is_in_nlnet_mou2
="Yes"),
139 cf_total_budget
="200",
140 cf_nlnet_milestone
="milestone 1",
141 cf_payees_list
="person1 = 100",
143 assigned_to
="person1@example.com"),
147 cf_total_budget
="100",
148 cf_nlnet_milestone
="milestone 1",
149 cf_payees_list
="person2 = 100",
151 assigned_to
="person1@example.com"),
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
)
160 '/output_dir/person1.mdwn': (
161 b
'<!-- autogenerated by budget-sync -->\n'
163 b
'# Person One (person1)\n'
167 b
'# Status Tracking\n'
170 b
'## Payment not yet submitted\n'
175 b
'* [Bug #3](https://bugzilla.example.com/show_bug.cgi?id=3):\n'
177 b
' * €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'
181 '/output_dir/person2.mdwn': (
182 b
'<!-- autogenerated by budget-sync -->\n'
184 b
'# Person Two (person2)\n'
188 b
'# Status Tracking\n'
191 b
'## Payment not yet submitted\n'
196 b
'* [Bug #2](https://bugzilla.example.com/show_bug.cgi?id=2):\n'
198 b
' * €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'
202 b
' * €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'
207 # TODO: add more test cases
210 if __name__
== "__main__":