emqx_jsonish_tests.erl 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2024 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -module(emqx_jsonish_tests).
  17. -include_lib("eunit/include/eunit.hrl").
  18. prop_prio_test_() ->
  19. [
  20. ?_assertEqual(
  21. {ok, 42},
  22. emqx_jsonish:lookup([<<"foo">>], #{<<"foo">> => 42, foo => 1337})
  23. ),
  24. ?_assertEqual(
  25. {ok, 1337},
  26. emqx_jsonish:lookup([<<"foo">>], #{foo => 1337})
  27. )
  28. ].
  29. undefined_test() ->
  30. ?assertEqual(
  31. {error, undefined},
  32. emqx_jsonish:lookup([<<"foo">>], #{})
  33. ).
  34. undefined_deep_test() ->
  35. ?assertEqual(
  36. {error, undefined},
  37. emqx_jsonish:lookup([<<"foo">>, <<"bar">>], #{})
  38. ).
  39. undefined_deep_json_test() ->
  40. ?assertEqual(
  41. {error, undefined},
  42. emqx_jsonish:lookup(
  43. [<<"foo">>, <<"bar">>, <<"baz">>],
  44. <<"{\"foo\":{\"bar\":{\"no\":{}}}}">>
  45. )
  46. ).
  47. invalid_type_test() ->
  48. ?assertEqual(
  49. {error, {0, number}},
  50. emqx_jsonish:lookup([<<"foo">>], <<"42">>)
  51. ).
  52. invalid_type_deep_test() ->
  53. ?assertEqual(
  54. {error, {2, atom}},
  55. emqx_jsonish:lookup([<<"foo">>, <<"bar">>, <<"tuple">>], #{foo => #{bar => baz}})
  56. ).
  57. decode_json_test() ->
  58. ?assertEqual(
  59. {ok, 42},
  60. emqx_jsonish:lookup([<<"foo">>, <<"bar">>], <<"{\"foo\":{\"bar\":42}}">>)
  61. ).
  62. decode_json_deep_test() ->
  63. ?assertEqual(
  64. {ok, 42},
  65. emqx_jsonish:lookup([<<"foo">>, <<"bar">>], #{<<"foo">> => <<"{\"bar\": 42}">>})
  66. ).
  67. decode_json_invalid_type_test() ->
  68. ?assertEqual(
  69. {error, {1, list}},
  70. emqx_jsonish:lookup([<<"foo">>, <<"bar">>], #{<<"foo">> => <<"[1,2,3]">>})
  71. ).
  72. decode_no_json_test() ->
  73. ?assertEqual(
  74. {error, {1, binary}},
  75. emqx_jsonish:lookup([<<"foo">>, <<"bar">>], #{<<"foo">> => <<0, 1, 2, 3>>})
  76. ).
  77. decode_json_no_nested_test() ->
  78. ?assertEqual(
  79. {error, {2, binary}},
  80. emqx_jsonish:lookup(
  81. [<<"foo">>, <<"bar">>, <<"baz">>],
  82. #{<<"foo">> => <<"{\"bar\":\"{\\\"baz\\\":42}\"}">>}
  83. )
  84. ).