emqx_utils_maps_tests.erl 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2022-2023 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_utils_maps_tests).
  17. -include_lib("eunit/include/eunit.hrl").
  18. best_effort_recursive_sum_test_() ->
  19. DummyLogger = fun(_) -> ok end,
  20. [
  21. ?_assertEqual(
  22. #{foo => 3},
  23. emqx_utils_maps:best_effort_recursive_sum(#{foo => 1}, #{foo => 2}, DummyLogger)
  24. ),
  25. ?_assertEqual(
  26. #{foo => 3, bar => 6.0},
  27. emqx_utils_maps:best_effort_recursive_sum(
  28. #{foo => 1, bar => 2.0}, #{foo => 2, bar => 4.0}, DummyLogger
  29. )
  30. ),
  31. ?_assertEqual(
  32. #{foo => 1, bar => 2},
  33. emqx_utils_maps:best_effort_recursive_sum(#{foo => 1}, #{bar => 2}, DummyLogger)
  34. ),
  35. ?_assertEqual(
  36. #{foo => #{bar => 42}},
  37. emqx_utils_maps:best_effort_recursive_sum(
  38. #{foo => #{bar => 2}}, #{foo => #{bar => 40}}, DummyLogger
  39. )
  40. ),
  41. fun() ->
  42. Self = self(),
  43. Logger = fun(What) -> Self ! {log, What} end,
  44. ?assertEqual(
  45. #{foo => 1, bar => 2},
  46. emqx_utils_maps:best_effort_recursive_sum(
  47. #{foo => 1, bar => 2}, #{bar => bar}, Logger
  48. )
  49. ),
  50. receive
  51. {log, Log} ->
  52. ?assertEqual(#{failed_to_merge => bar, bad_value => bar}, Log)
  53. after 1000 -> error(timeout)
  54. end
  55. end,
  56. ?_assertEqual(
  57. #{},
  58. emqx_utils_maps:best_effort_recursive_sum(
  59. #{foo => foo}, #{foo => bar}, DummyLogger
  60. )
  61. ),
  62. ?_assertEqual(
  63. #{foo => 1},
  64. emqx_utils_maps:best_effort_recursive_sum(
  65. #{foo => 1}, #{foo => bar}, DummyLogger
  66. )
  67. ),
  68. ?_assertEqual(
  69. #{foo => 1},
  70. emqx_utils_maps:best_effort_recursive_sum(
  71. #{foo => bar}, #{foo => 1}, DummyLogger
  72. )
  73. ),
  74. ?_assertEqual(
  75. #{foo => #{bar => 1}},
  76. emqx_utils_maps:best_effort_recursive_sum(
  77. #{foo => #{bar => 1}}, #{foo => 1}, DummyLogger
  78. )
  79. ),
  80. ?_assertEqual(
  81. #{foo => #{bar => 1}},
  82. emqx_utils_maps:best_effort_recursive_sum(
  83. #{foo => 1}, #{foo => #{bar => 1}}, DummyLogger
  84. )
  85. ),
  86. ?_assertEqual(
  87. #{foo => #{bar => 1}},
  88. emqx_utils_maps:best_effort_recursive_sum(
  89. #{foo => 1, bar => ignored}, #{foo => #{bar => 1}}, DummyLogger
  90. )
  91. ),
  92. ?_assertEqual(
  93. #{foo => #{bar => 2}, bar => #{foo => 1}},
  94. emqx_utils_maps:best_effort_recursive_sum(
  95. #{foo => 1, bar => #{foo => 1}}, #{foo => #{bar => 2}, bar => 2}, DummyLogger
  96. )
  97. ),
  98. ?_assertEqual(
  99. #{foo => #{bar => 2}, bar => #{foo => 1}},
  100. emqx_utils_maps:best_effort_recursive_sum(
  101. #{foo => #{bar => 2}, bar => 2}, #{foo => 1, bar => #{foo => 1}}, DummyLogger
  102. )
  103. ),
  104. ?_assertEqual(
  105. #{foo => #{bar => #{}}},
  106. emqx_utils_maps:best_effort_recursive_sum(
  107. #{foo => #{bar => #{foo => []}}}, #{foo => 1}, DummyLogger
  108. )
  109. )
  110. ].