iilyak commented on code in PR #4483:
URL: https://github.com/apache/couchdb/pull/4483#discussion_r1176982587
##########
src/couch_log/test/eunit/couch_log_formatter_test.erl:
##########
@@ -25,6 +25,22 @@ truncate_test() ->
Entry = couch_log_formatter:format(info, self(), Msg),
?assert(length(Entry#log_entry.msg) =< 16000).
+format_report_etoolong_test() ->
+ Payload = lists:flatten(["a" || _ <- lists:seq(1, 1048576)]),
+ Resp = couch_log_formatter:format_report(self(), report123, #{
+ msg => Payload
+ }),
+ ?assertEqual({error, emsgtoolong}, Resp).
+
+format_report_test() ->
+ {ok, Entry} = couch_log_formatter:format_report(self(), report123, #{
+ foo => 123,
+ bar => "barStr",
+ baz => baz
+ }),
+ Formatted = "[foo=\"123\" baz=\"baz\" bar=\"barStr\"]",
Review Comment:
> The "fix" that jumps out to me would be to dissect the resulting output
string and assert the expected keys. For instance, we could first regex pattern
match on ^[ and ]$ to assert the formatted output is in brackets. Then we could
split the inner string on spaces, sort the output, and then assert each of the
key/value pairs matches up.
Something like
```erlang
parse_data(String) ->
maps:from_list(
lists:map(fun(X) ->
[K, V] = string:split(X, "="),
{list_to_atom(K), V}
end, string:split(
string:trim(String, both, [$[, $]]),
" ",
all))).
```
Another approach is based on the fact that we don't need to fully parse the
data. For us it is enough to get to the level of
```
[
"bar=\"barStr\"",
"baz=\"baz\"",
"foo=\"123\""
]
```
It could be done as following
```erlang
Expected = split_and_order("[foo=\"123\" baz=\"baz\" bar=\"barStr\"]"),
?assertEqual(Expected, lists:flatten(split_and_order(Entry#log_entry.msg))),
?assertEqual($[, lists:first(Entry#log_entry.msg)).
?assertEqual($], lists:last(Entry#log_entry.msg)).
def split_and_order(String) ->
lists:sort(
string:split(
string:trim(String, both, [$[, $]]),
" ",
all
)
).
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]