gmile
Asserting on the JSON response from a controller action in OTP26
After upgrading to OTP26, we’re seeing a lot of failures related to the shape of JSON response. For example, given the view is constructed using atoms (which is how we do it), the following assertion easily fails:
output = Jason.encode!(%{one: 1, two: 2})
assert ~s|{"one":1,"two":2}| = output
This seems in line with one of the highlights of the OTP26 release, e.g. order of atom keys in map.
Simple assertions like above are possible to re-write so that order doesn’t matter. However, in our case, we have assertions fail on significantly bigger JSONs which are extremely hard to re-write.
The above makes me question if we aren’t doing anything wrong here.
Maybe it doesn’t make sense to use atoms when constructing the view?
The reason we always did it that way was to not to pollute process memory with strings that are common - a problem which (supposedly?) doesn’t exist for the same atoms.
Maybe testing shape of the output is not a good idea in the first place? If so, then how do you test your views?
How are you people dealing with a situation similar to this?
Most Liked
benwilson512
Unless I’m missing something, I don’t see how atoms vs strings are relevant here.
Testing on the string order of json keys is in general unreliable. The JSON spec makes no promise of key order, nor do Elixir maps. If you want to test the semantic equivalence of something you need to decode the JSON, not assert on a JSON string.
Ideally you simply don’t encode to a string at all and instead test:
assert %{"one" => 1, "two" => 2} == output
If it is unavoidably already encoded then just decode it:
assert %{"one" => 1, "two" => 2} == Jason.decode!(output)
hst337
Yes, you’re trying to compare JSON as strings, while standard clearly defines that order of keys in json maps may be different for the same structure.
You can try doing something like
assert %{one: 1, two: 2} == result
hst337
If those strings are larger than 64 bytes, than they’re most likely shared. Smaller strings are not shared. But still, with atoms you’re saving just a few kilobytes for huge maps








