Setting the file. One moment.
Test Validate Heroku Migration Report Currency · Heroku To AWS · aws/agent-toolkit-for-aws · Skills Docs
ContentsBack to the top of the page 28.3
Clarify
81
Creating Amazon Aurora Db Cluster With Instances
104
Routing Traffic With Route53 And CloudFront
Resilience Program Design
Creating API Gateway Stage
def test_bad_monthly_figure_spelled_slash_month_fails
— line 114
This file
Number 28.48
Position 48 of 58
Type Python
Size 7 KB
Lines 203 scripts/ test_validate_heroku_migration_report_currency.py
Python · 203 lines · 7 KB
14
import
tempfile
15 from pathlib import Path
16
17 SCRIPT = Path( __file__ ).resolve().parent / "validate-heroku-migration-report.py"
18
19 GOOD = """<!DOCTYPE html>
20 <html lang="en"><body><div class="report">
21 <section id="decision-summary"><p class="verdict-headline">Go</p></section>
22 <section id="exec-costs"><p>Balanced Est. $112/mo</p></section>
23 <section id="cost-optimization"><p>No 1-year/3-year commitment product applies to this architecture.</p></section>
24 <section id="next-steps"><ol><li>See MIGRATION_GUIDE.md</li></ol></section>
25 <footer>draft for review</footer>
26 </div></body></html>
27 """
28
29
30 def run (html: str ) -> tuple[ int , str ]:
31 with tempfile.TemporaryDirectory() as tmp:
32 report = Path(tmp) / "migration-report.html"
33 report.write_text(html, encoding = "utf-8" )
34 cmd = [sys.executable, str ( SCRIPT ), str (report)]
35 result = subprocess.run(cmd, capture_output = True , text = True ) # nosec B603
36 return result.returncode, result.stdout + result.stderr
37
38
39 def test_good_report_has_no_currency_formatting_violations () -> None :
40 code, out = run( GOOD )
41 assert code == 0 , out
42 assert "currency formatting" not in out
43
44
45 def test_monthly_figure_with_cents_fails () -> None :
46 # Regression: the exact SF Beach report drift — a multi-thousand-dollar
47 # monthly figure rendered with cents.
48 html = GOOD .replace( "Est. $112/mo" , "Est. $25,684.89/mo" )
49 code, out = run(html)
50 assert code == 1 , out
51 assert "currency formatting" in out
52 assert "$25,684.89" in out
53
54
55 def test_small_monthly_total_under_two_dollars_with_cents_passes () -> None :
56 html = GOOD .replace( "Est. $112/mo" , "Est. $1.50/mo" )
57 code, out = run(html)
58 assert code == 0 , out
59 assert "currency formatting" not in out
60
61
62 def test_hourly_rate_with_cents_passes () -> None :
63 html = GOOD .replace( "Est. $112/mo" , "Est. $23.50/hr" )
64 code, out = run(html)
65 assert code == 0 , out
66 assert "currency formatting" not in out
67
68
69 def test_per_unit_commitment_rate_with_cents_passes () -> None :
70 html = GOOD .replace( "Est. $112/mo" , "Est. $21.18 (1-mo commit)" )
71 code, out = run(html)
72 assert code == 0 , out
73 assert "currency formatting" not in out
74
75
76 def test_multiple_bad_monthly_figures_all_reported () -> None :
77 html = GOOD .replace(
78 "Est. $112/mo" ,
79 "Est. $1,371.82/mo and Est. $80.30/mo" ,
80 )
81 code, out = run(html)
82 assert code == 1 , out
83 assert "$1,371.82" in out
84 assert "$80.30" in out
85
86
87 def test_repeated_bad_figure_reported_once () -> None :
88 html = GOOD .replace(
89 "Est. $112/mo" ,
90 "Est. $999.99/mo, repeated: Est. $999.99/mo" ,
91 )
92 code, out = run(html)
93 assert code == 1 , out
94 assert out.count( "$999.99" ) == 1
95
96
97 def test_percentages_and_versions_never_trigger_currency_check () -> None :
98 html = GOOD .replace(
99 "</footer>" ,
100 "</footer><p>Terraform 1.15.2, 82.5 % r eduction, RTO 4.5 hours.</p>" ,
101 )
102 code, out = run(html)
103 assert code == 0 , out
104 assert "currency formatting" not in out
105
106
107 def test_per_policy_rate_with_cents_passes () -> None :
108 html = GOOD .replace( "Est. $112/mo" , "Est. $5.00/mo per policy" )
109 code, out = run(html)
110 assert code == 0 , out
111 assert "currency formatting" not in out
112
113
114 def test_bad_monthly_figure_spelled_slash_month_fails () -> None :
115 # Regression: "month" alone must not exempt a figure the way "/hr" does —
116 # it's exactly the unit an ordinary monthly total is denominated in, not
117 # evidence of a per-unit rate.
118 html = GOOD .replace( "Est. $112/mo" , "Est. $25,684.89/month" )
119 code, out = run(html)
120 assert code == 1 , out
121 assert "$25,684.89" in out
122
123
124 def test_bad_monthly_figure_spelled_per_month_fails () -> None :
125 html = GOOD .replace( "Est. $112/mo" , "Est. $25,684.89 per month" )
126 code, out = run(html)
127 assert code == 1 , out
128 assert "$25,684.89" in out
129
130
131 def test_bad_monthly_figure_spelled_monthly_fails () -> None :
132 html = GOOD .replace( "Est. $112/mo" , "Est. $25,684.89 monthly" )
133 code, out = run(html)
134 assert code == 1 , out
135 assert "$25,684.89" in out
136
137
138 def test_hourly_rate_split_by_inline_tag_still_recognized () -> None :
139 # Regression: raw-HTML regex matching saw </strong> between the amount
140 # and its unit and failed to recognize the rate suffix.
141 html = GOOD .replace( "Est. $112/mo" , "Est. <strong>$23.50</strong>/hr" )
142 code, out = run(html)
143 assert code == 0 , out
144 assert "currency formatting" not in out
145
146
147 def test_hourly_rate_with_nbsp_before_unit_still_recognized () -> None :
148 # Regression: an HTML entity separator between the amount and its unit
149 # must decode before the rate-suffix match.
150 html = GOOD .replace( "Est. $112/mo" , "Est. $23.50 /hr" )
151 code, out = run(html)
152 assert code == 0 , out
153 assert "currency formatting" not in out
154
155
156 def test_commented_out_raw_figure_never_flagged () -> None :
157 # Regression: an HTML comment is never rendered by a browser, so a raw,
158 # unrounded figure left in a comment must not be flagged.
159 html = GOOD .replace(
160 "Est. $112/mo" ,
161 "Est. $112/mo<!-- raw estimate $25,684.89/mo -->" ,
162 )
163 code, out = run(html)
164 assert code == 0 , out
165 assert "currency formatting" not in out
166
167
168 def test_numeric_character_reference_dollar_sign_still_flagged () -> None :
169 # Regression: the reverse direction of the entity-decoding gap — a
170 # numeric character reference for "$" ($) is real, visible content
171 # once decoded, and must not silently pass.
172 html = GOOD .replace( "Est. $112/mo" , "Est. $25,684.89/mo" )
173 code, out = run(html)
174 assert code == 1 , out
175 assert "$25,684.89" in out
176
177
178 def test_bad_monthly_figure_not_exempted_by_next_cells_rate_word () -> None :
179 # Regression: a block-level boundary (a table cell/row end) was emitted
180 # as a single separating space, which does not itself stop a word-based
181 # regex — an unrelated word that happens to open the NEXT cell (e.g.
182 # "Hourly") was readable as the FIRST cell's own rate suffix.
183 html = GOOD .replace(
184 "Est. $112/mo" ,
185 "Est. <table><tr><td>$25,684.89</td>"
186 "<td>Hourly rates unchanged</td></tr></table>" ,
187 )
188 code, out = run(html)
189 assert code == 1 , out
190 assert "$25,684.89" in out
191
192
193 def test_bad_monthly_figure_still_flagged_with_unrelated_neighbor_cell () -> None :
194 # Control for the above: the same boundary case but the neighboring
195 # cell's text does NOT start with a rate word.
196 html = GOOD .replace(
197 "Est. $112/mo" ,
198 "Est. <table><tr><td>$25,684.89</td>"
199 "<td>Rates unchanged</td></tr></table>" ,
200 )
201 code, out = run(html)
202 assert code == 1 , out
203 assert "$25,684.89" in out