emqx_utils_sql_tests.erl 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 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_utils_sql_tests).
  17. -compile(nowarn_export_all).
  18. -compile(export_all).
  19. -include_lib("proper/include/proper.hrl").
  20. -include_lib("eunit/include/eunit.hrl").
  21. %%------------------------------------------------------------------------------
  22. %% Properties
  23. %%------------------------------------------------------------------------------
  24. snowflake_escape_test_() ->
  25. Props = [prop_snowflake_escape()],
  26. Opts = [{numtests, 1_000}, {to_file, user}, {max_size, 100}],
  27. {timeout, 300, [?_assert(proper:quickcheck(Prop, Opts)) || Prop <- Props]}.
  28. prop_snowflake_escape() ->
  29. ?FORALL(
  30. Input,
  31. binary(),
  32. begin
  33. Escaped = iolist_to_binary(emqx_utils_sql:escape_snowflake(Input)),
  34. Content = binary_part(Escaped, 1, iolist_size(Escaped) - 2),
  35. %% Should not have any single double quotes after escaping, except for the
  36. %% leading and trailing ones.
  37. ?WHENFAIL(
  38. io:format(
  39. user,
  40. "Input:\n ~s\n\nEscaped:\n ~s\n",
  41. [Input, Escaped]
  42. ),
  43. nomatch =:= re:run(Content, <<"[^\"]\"[^\"]">>, [{capture, none}])
  44. )
  45. end
  46. ).