Skip to content

Commit 95a14d9

Browse files
committed
Refactor tests
1 parent 98da67c commit 95a14d9

File tree

1 file changed

+84
-89
lines changed

1 file changed

+84
-89
lines changed

test/seshat_test.erl

Lines changed: 84 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,12 @@ format_group() ->
9999
seshat:new(Group, widget1, Counters, #{component => widget1}),
100100
seshat:new(Group, widget2, Counters, #{component => widget2}),
101101
seshat:new(Group, screw, Counters), % no labels, will be omitted
102-
PrometheusFormat = seshat:format(Group),
103-
ExpectedPrometheusFormat = #{reads => #{type => counter,
104-
help => "Total reads",
105-
values => #{#{component => widget1} => 0,
106-
#{component => widget2} => 0}}},
107-
?assertEqual(ExpectedPrometheusFormat, PrometheusFormat),
102+
Result = seshat:format(Group),
103+
ExpectedMap = #{reads => #{type => counter,
104+
help => "Total reads",
105+
values => #{#{component => widget1} => 0,
106+
#{component => widget2} => 0}}},
107+
?assertEqual(ExpectedMap, Result),
108108
ok.
109109

110110
format_one() ->
@@ -113,11 +113,11 @@ format_one() ->
113113
seshat:new_group(Group),
114114
seshat:new(Group, widget1, Counters, #{component => widget1}),
115115
seshat:new(Group, widget2, Counters, #{component => widget2}),
116-
PrometheusFormat = seshat:format_one(Group, widget2),
117-
ExpectedPrometheusFormat = #{reads => #{type => counter,
118-
help => "Total reads",
119-
values => #{#{component => widget2} => 0}}},
120-
?assertEqual(ExpectedPrometheusFormat, PrometheusFormat),
116+
Result = seshat:format_one(Group, widget2),
117+
ExpectedMap = #{reads => #{type => counter,
118+
help => "Total reads",
119+
values => #{#{component => widget2} => 0}}},
120+
?assertEqual(ExpectedMap, Result),
121121
ok.
122122

123123
format_with_many_labels() ->
@@ -128,12 +128,12 @@ format_with_many_labels() ->
128128
seshat:new(Group, widget2, Counters, #{component => "widget2", status => down}),
129129
set_value(Group, widget1, reads, 1),
130130
set_value(Group, widget2, reads, 2),
131-
PrometheusFormat = seshat:format(Group),
132-
ExpectedPrometheusFormat = #{reads => #{type => counter,
133-
help => "Total reads",
134-
values => #{#{component => "widget1", status => up} => 1,
135-
#{component => "widget2", status => down} => 2}}},
136-
?assertEqual(ExpectedPrometheusFormat, PrometheusFormat),
131+
Result = seshat:format(Group),
132+
ExpectedMap = #{reads => #{type => counter,
133+
help => "Total reads",
134+
values => #{#{component => "widget1", status => up} => 1,
135+
#{component => "widget2", status => down} => 2}}},
136+
?assertEqual(ExpectedMap, Result),
137137
ok.
138138

139139
format_selected_metrics() ->
@@ -146,16 +146,16 @@ format_selected_metrics() ->
146146
seshat:new_group(Group),
147147
seshat:new(Group, thing1, Counters, #{component => "thing1"}),
148148
seshat:new(Group, thing2, Counters, #{component => "thing2"}),
149-
PrometheusFormat = seshat:format(Group, [reads, writes]),
150-
ExpectedPrometheusFormat = #{reads => #{type => counter,
151-
help => "Total reads",
152-
values => #{#{component => "thing1"} => 0,
153-
#{component => "thing2"} => 0}},
154-
writes => #{type => counter,
155-
help => "Total writes",
156-
values => #{#{component => "thing1"} => 0,
157-
#{component => "thing2"} => 0}}},
158-
?assertEqual(ExpectedPrometheusFormat, PrometheusFormat),
149+
Result = seshat:format(Group, [reads, writes]),
150+
ExpectedMap = #{reads => #{type => counter,
151+
help => "Total reads",
152+
values => #{#{component => "thing1"} => 0,
153+
#{component => "thing2"} => 0}},
154+
writes => #{type => counter,
155+
help => "Total writes",
156+
values => #{#{component => "thing1"} => 0,
157+
#{component => "thing2"} => 0}}},
158+
?assertEqual(ExpectedMap, Result),
159159
ok.
160160

161161
invalid_fields() ->
@@ -181,21 +181,20 @@ format_ratio() ->
181181
set_value(Group, test_component, pangs, 33),
182182
set_value(Group, test_component, rings, 100),
183183

184-
PrometheusFormat = seshat:format(Group),
185-
ExpectedPrometheusFormat = #{pings => #{type => gauge,
186-
help => "Some ratio that happens to be 0%",
187-
values => #{#{component => test} => 0.0}},
188-
pongs => #{type => gauge,
189-
help => "Some ratio that happens to be 17%",
190-
values => #{#{component => test} => 0.17}},
191-
pangs => #{type => gauge,
192-
help => "Some ratio that happens to be 33%",
193-
values => #{#{component => test} => 0.33}},
194-
rings => #{type => gauge,
195-
help => "Some ratio that happens to be 100%",
196-
values => #{#{component => test} => 1.0}}},
197-
198-
?assertEqual(ExpectedPrometheusFormat, PrometheusFormat),
184+
Result = seshat:format(Group),
185+
ExpectedMap = #{pings => #{type => gauge,
186+
help => "Some ratio that happens to be 0%",
187+
values => #{#{component => test} => 0.0}},
188+
pongs => #{type => gauge,
189+
help => "Some ratio that happens to be 17%",
190+
values => #{#{component => test} => 0.17}},
191+
pangs => #{type => gauge,
192+
help => "Some ratio that happens to be 33%",
193+
values => #{#{component => test} => 0.33}},
194+
rings => #{type => gauge,
195+
help => "Some ratio that happens to be 100%",
196+
values => #{#{component => test} => 1.0}}},
197+
?assertEqual(ExpectedMap, Result),
199198
ok.
200199

201200
format_time_metrics() ->
@@ -214,25 +213,24 @@ format_ratio() ->
214213
set_value(Group, test_component, short_latency, 5),
215214
set_value(Group, test_component, long_latency, 1500),
216215

217-
MapFormat = seshat:format(Group),
218-
ExpectedMapFormat = #{
216+
MapResult = seshat:format(Group),
217+
ExpectedMap = #{
219218
job_duration => #{type => gauge,
220-
help => "Job duration",
221-
values => #{Labels => 30.0}},
219+
help => "Job duration",
220+
values => #{Labels => 30.0}},
222221
short_latency => #{type => gauge,
223-
help => "Short latency",
224-
values => #{Labels => 0.005}},
222+
help => "Short latency",
223+
values => #{Labels => 0.005}},
225224
long_latency => #{type => gauge,
226-
help => "Request latency",
227-
values => #{Labels => 1.5}}
225+
help => "Request latency",
226+
values => #{Labels => 1.5}}
228227
},
229-
?assertEqual(ExpectedMapFormat, MapFormat),
228+
?assertEqual(ExpectedMap, MapResult),
230229

231230
Prefix = "myapp",
232231
MetricNames = [job_duration, short_latency, long_latency], % Added new metric name
233-
ResultAsList = binary_to_list(seshat:text_format(Group, Prefix, MetricNames)),
232+
TextResult = seshat:text_format(Group, Prefix, MetricNames),
234233

235-
% Expected format needs sorting because order isn't guaranteed
236234
ExpectedLines = [
237235
"# HELP myapp_job_duration_seconds Job duration",
238236
"# TYPE myapp_job_duration_seconds gauge",
@@ -244,15 +242,9 @@ format_ratio() ->
244242
"# TYPE myapp_long_latency_seconds gauge",
245243
"myapp_long_latency_seconds{component=\"test\"} 1.5"
246244
],
247-
ExpectedSortedText = lists:sort(ExpectedLines),
248-
249-
% Split and sort the actual result for comparison
250-
ResultLines = string:split(ResultAsList, "\n", all),
251-
FilteredResultLines = [Line || Line <- ResultLines, Line /= ""],
252-
SortedResultText = lists:sort(FilteredResultLines),
253-
254-
?assertEqual(ExpectedSortedText, SortedResultText),
245+
ExpectedResult = list_to_binary(string:join(ExpectedLines, "\n") ++ "\n"),
255246

247+
?assertEqual(ExpectedResult, TextResult),
256248
ok.
257249

258250
text_format_selected_metrics() ->
@@ -288,34 +280,37 @@ text_format_selected_metrics() ->
288280
set_value(Group, thing3, duration, 345),
289281
set_value(Group, thing3, npc, 1), % to be ignored
290282

291-
ResultAsList = binary_to_list(seshat:text_format(Group, "acme", [reads, writes, cached, latency, duration])),
292-
ExpectedPrometheusFormat = "# HELP acme_reads Total reads\n"
293-
"# TYPE acme_reads counter\n"
294-
"acme_reads{version=\"1.2.3\",component=\"thing1\"} 1\n"
295-
"acme_reads{component=\"thing2\",some_atom=\"atom_value\"} 3\n"
296-
"acme_reads{component=\"thing3\",some_binary=\"binary_value\"} 1234\n"
297-
"# HELP acme_writes Total writes\n"
298-
"# TYPE acme_writes counter\n"
299-
"acme_writes{version=\"1.2.3\",component=\"thing1\"} 2\n"
300-
"acme_writes{component=\"thing2\",some_atom=\"atom_value\"} 4\n"
301-
"acme_writes{component=\"thing3\",some_binary=\"binary_value\"} 4321\n"
302-
"# HELP acme_cached_ratio Ratio of things served from cache\n"
303-
"# TYPE acme_cached_ratio gauge\n"
304-
"acme_cached_ratio{version=\"1.2.3\",component=\"thing1\"} 0.1\n"
305-
"acme_cached_ratio{component=\"thing2\",some_atom=\"atom_value\"} 1.0\n"
306-
"acme_cached_ratio{component=\"thing3\",some_binary=\"binary_value\"} 0.17\n"
307-
"# HELP acme_latency_seconds Latency\n"
308-
"# TYPE acme_latency_seconds gauge\n"
309-
"acme_latency_seconds{version=\"1.2.3\",component=\"thing1\"} 0.005\n"
310-
"acme_latency_seconds{component=\"thing2\",some_atom=\"atom_value\"} 0.006\n"
311-
"acme_latency_seconds{component=\"thing3\",some_binary=\"binary_value\"} 0.007\n"
312-
"# HELP acme_duration_seconds Duration\n"
313-
"# TYPE acme_duration_seconds gauge\n"
314-
"acme_duration_seconds{version=\"1.2.3\",component=\"thing1\"} 123.0\n"
315-
"acme_duration_seconds{component=\"thing2\",some_atom=\"atom_value\"} 234.0\n"
316-
"acme_duration_seconds{component=\"thing3\",some_binary=\"binary_value\"} 345.0\n",
317-
318-
?assertEqual(ExpectedPrometheusFormat, ResultAsList),
283+
Result = seshat:text_format(Group, "acme", [reads, writes, cached, latency, duration]),
284+
ExpectedLines = [
285+
"# HELP acme_reads Total reads",
286+
"# TYPE acme_reads counter",
287+
"acme_reads{version=\"1.2.3\",component=\"thing1\"} 1",
288+
"acme_reads{component=\"thing2\",some_atom=\"atom_value\"} 3",
289+
"acme_reads{component=\"thing3\",some_binary=\"binary_value\"} 1234",
290+
"# HELP acme_writes Total writes",
291+
"# TYPE acme_writes counter",
292+
"acme_writes{version=\"1.2.3\",component=\"thing1\"} 2",
293+
"acme_writes{component=\"thing2\",some_atom=\"atom_value\"} 4",
294+
"acme_writes{component=\"thing3\",some_binary=\"binary_value\"} 4321",
295+
"# HELP acme_cached_ratio Ratio of things served from cache",
296+
"# TYPE acme_cached_ratio gauge",
297+
"acme_cached_ratio{version=\"1.2.3\",component=\"thing1\"} 0.1",
298+
"acme_cached_ratio{component=\"thing2\",some_atom=\"atom_value\"} 1.0",
299+
"acme_cached_ratio{component=\"thing3\",some_binary=\"binary_value\"} 0.17",
300+
"# HELP acme_latency_seconds Latency",
301+
"# TYPE acme_latency_seconds gauge",
302+
"acme_latency_seconds{version=\"1.2.3\",component=\"thing1\"} 0.005",
303+
"acme_latency_seconds{component=\"thing2\",some_atom=\"atom_value\"} 0.006",
304+
"acme_latency_seconds{component=\"thing3\",some_binary=\"binary_value\"} 0.007",
305+
"# HELP acme_duration_seconds Duration",
306+
"# TYPE acme_duration_seconds gauge",
307+
"acme_duration_seconds{version=\"1.2.3\",component=\"thing1\"} 123.0",
308+
"acme_duration_seconds{component=\"thing2\",some_atom=\"atom_value\"} 234.0",
309+
"acme_duration_seconds{component=\"thing3\",some_binary=\"binary_value\"} 345.0"
310+
],
311+
ExpectedResult = list_to_binary(string:join(ExpectedLines, "\n") ++ "\n"),
312+
313+
?assertEqual(ExpectedResult, Result),
319314
ok.
320315

321316
%% test helpers

0 commit comments

Comments
 (0)