diff options
author | ILYA Khlopotov <iilyak@apache.org> | 2018-12-03 18:34:51 +0000 |
---|---|---|
committer | ILYA Khlopotov <iilyak@apache.org> | 2018-12-03 18:34:51 +0000 |
commit | a2c7f3186a1d576b7b920a41a4aed45031060fd4 (patch) | |
tree | c6a193698adb61d39142c53ed70cc0d017cdb3a7 | |
parent | 9f7724e06e3f4c7acc6e44156a1d8f121efe97a9 (diff) | |
download | couchdb-a2c7f3186a1d576b7b920a41a4aed45031060fd4.tar.gz |
Move couch_flags_config tests into its own module
-rw-r--r-- | src/couch/src/couch_flags_config.erl | 96 | ||||
-rw-r--r-- | src/couch/test/couch_flags_config_tests.erl | 95 |
2 files changed, 100 insertions, 91 deletions
diff --git a/src/couch/src/couch_flags_config.erl b/src/couch/src/couch_flags_config.erl index 513a8154f..104a48257 100644 --- a/src/couch/src/couch_flags_config.erl +++ b/src/couch/src/couch_flags_config.erl @@ -20,6 +20,11 @@ data_provider/0 ]). +%% for test suite only +-export([ + parse_flags_term/1 +]). + -define(DATA_INTERVAL, 1000). -define(MAX_FLAG_NAME_LENGTH, 256). @@ -281,94 +286,3 @@ get_config_section(Section) -> catch error:badarg -> [] end. - -%% ------------------------------------------------------------------ -%% Tests -%% ------------------------------------------------------------------ - --ifdef(TEST). --include_lib("eunit/include/eunit.hrl"). - -all_combinations_return_same_result_test_() -> - Config = [ - {"foo, bar||*", "true"}, - {"baz, qux||*", "false"}, - {"baz||shards/test*", "true"}, - {"baz||shards/blacklist*", "false"}, - {"bar||shards/test*", "false"}, - {"bar||shards/test/blacklist*", "true"} - ], - Expected = [ - {{<<"shards/test/blacklist*">>},{<<"shards/test/blacklist*">>,22,[bar, foo]}}, - {{<<"shards/test*">>},{<<"shards/test*">>, 12, [baz, foo]}}, - {{<<"shards/blacklist*">>},{<<"shards/blacklist*">>, 17, [bar, foo]}}, - {{<<"*">>},{<<"*">>, 1, [bar, foo]}} - ], - Combinations = couch_tests_combinatorics:permutations(Config), - [{test_id(Items), ?_assertEqual(Expected, data(Items))} - || Items <- Combinations]. - -rules_are_sorted_test() -> - Expected = [ - {{<<"shards/test/exact">>},{<<"shards/test/exact">>, 17, [baz,flag_bar,flag_foo]}}, - {{<<"shards/test/blacklist*">>},{<<"shards/test/blacklist*">>,22,[flag_foo]}}, - {{<<"shards/test*">>},{<<"shards/test*">>, 12, [baz,flag_bar,flag_foo]}}, - {{<<"shards/exact">>},{<<"shards/exact">>, 12, [flag_bar,flag_foo]}}, - {{<<"shards/blacklist*">>},{<<"shards/blacklist*">>, 17, []}}, - {{<<"*">>},{<<"*">>, 1, [flag_foo]}} - ], - ?assertEqual(Expected, data(test_config())). - -latest_overide_wins_test_() -> - Cases = [ - {[ - {"flag||*", "false"}, {"flag||a*", "true"}, - {"flag||ab*", "true"}, {"flag||abc*", "true"} - ], true}, - {[ - {"flag||*", "true"}, {"flag||a*", "false"}, - {"flag||ab*", "true"}, {"flag||abc*", "false"} - ], false} - ], - [{test_id(Rules, Expected), - ?_assertEqual(Expected, lists:member(flag, flags(hd(data(Rules)))))} - || {Rules, Expected} <- Cases]. - -flags({{_Pattern}, {_Pattern, _Size, Flags}}) -> - Flags. - -test_id(Items, ExpectedResult) -> - lists:flatten(io_lib:format("~p -> ~p", [[P || {P, _} <- Items], ExpectedResult])). - - -test_id(Items) -> - lists:flatten(io_lib:format("~p", [[P || {P, _} <- Items]])). - -test_config() -> - [ - {"flag_foo||*", "true"}, - {"flag_bar||*", "false"}, - {"flag_bar||shards/test*", "true"}, - {"flag_foo||shards/blacklist*", "false"}, - {"baz||shards/test*", "true"}, - {"baz||shards/test/blacklist*", "false"}, - {"flag_bar||shards/exact", "true"}, - {"flag_bar||shards/test/exact", "true"} - ]. - -parse_flags_term_test_() -> - LongBinary = binary:copy(<<"a">>, ?MAX_FLAG_NAME_LENGTH + 1), - ExpectedError = {error, {"Cannot parse list of tags: ~n~p", - [{too_long, LongBinary}]}}, - ExpectedUnknownError = {error,{"Cannot parse list of tags: ~n~p", - [{invalid_flag,<<"dddddddd">>}]}}, - [ - {"empty binary", ?_assertEqual([], parse_flags_term(<<>>))}, - {"single flag", ?_assertEqual([fff], parse_flags_term(<<"fff">>))}, - {"sorted", ?_assertEqual([aaa,bbb,fff], parse_flags_term(<<"fff,aaa,bbb">>))}, - {"whitespace", ?_assertEqual([aaa,bbb,fff], parse_flags_term(<<"fff , aaa, bbb ">>))}, - {"error", ?_assertEqual(ExpectedError, parse_flags_term(LongBinary))}, - {"unknown_flag", ?_assertEqual(ExpectedUnknownError, parse_flags_term(<<"dddddddd">>))} - ]. - --endif. diff --git a/src/couch/test/couch_flags_config_tests.erl b/src/couch/test/couch_flags_config_tests.erl new file mode 100644 index 000000000..3ba71a490 --- /dev/null +++ b/src/couch/test/couch_flags_config_tests.erl @@ -0,0 +1,95 @@ +-module(couch_flags_config_tests). +-include_lib("eunit/include/eunit.hrl"). + +%% value copied from couch_flags_config +-define(MAX_FLAG_NAME_LENGTH, 256). + +all_combinations_return_same_result_test_() -> + Config = [ + {"foo, bar||*", "true"}, + {"baz, qux||*", "false"}, + {"baz||shards/test*", "true"}, + {"baz||shards/blacklist*", "false"}, + {"bar||shards/test*", "false"}, + {"bar||shards/test/blacklist*", "true"} + ], + Expected = [ + {{<<"shards/test/blacklist*">>},{<<"shards/test/blacklist*">>,22,[bar, foo]}}, + {{<<"shards/test*">>},{<<"shards/test*">>, 12, [baz, foo]}}, + {{<<"shards/blacklist*">>},{<<"shards/blacklist*">>, 17, [bar, foo]}}, + {{<<"*">>},{<<"*">>, 1, [bar, foo]}} + ], + Combinations = couch_tests_combinatorics:permutations(Config), + [{test_id(Items), ?_assertEqual(Expected, couch_flags_config:data(Items))} + || Items <- Combinations]. + +rules_are_sorted_test() -> + Expected = [ + {{<<"shards/test/exact">>},{<<"shards/test/exact">>, 17, [baz,flag_bar,flag_foo]}}, + {{<<"shards/test/blacklist*">>},{<<"shards/test/blacklist*">>,22,[flag_foo]}}, + {{<<"shards/test*">>},{<<"shards/test*">>, 12, [baz,flag_bar,flag_foo]}}, + {{<<"shards/exact">>},{<<"shards/exact">>, 12, [flag_bar,flag_foo]}}, + {{<<"shards/blacklist*">>},{<<"shards/blacklist*">>, 17, []}}, + {{<<"*">>},{<<"*">>, 1, [flag_foo]}} + ], + ?assertEqual(Expected, couch_flags_config:data(test_config())). + +latest_overide_wins_test_() -> + Cases = [ + {[ + {"flag||*", "false"}, {"flag||a*", "true"}, + {"flag||ab*", "true"}, {"flag||abc*", "true"} + ], true}, + {[ + {"flag||*", "true"}, {"flag||a*", "false"}, + {"flag||ab*", "true"}, {"flag||abc*", "false"} + ], false} + ], + [{test_id(Rules, Expected), + ?_assertEqual(Expected, lists:member(flag, + flags(hd(couch_flags_config:data(Rules)))))} + || {Rules, Expected} <- Cases]. + +flags({{_Pattern}, {_Pattern, _Size, Flags}}) -> + Flags. + +test_id(Items, ExpectedResult) -> + lists:flatten(io_lib:format("~p -> ~p", [[P || {P, _} <- Items], ExpectedResult])). + + +test_id(Items) -> + lists:flatten(io_lib:format("~p", [[P || {P, _} <- Items]])). + +test_config() -> + [ + {"flag_foo||*", "true"}, + {"flag_bar||*", "false"}, + {"flag_bar||shards/test*", "true"}, + {"flag_foo||shards/blacklist*", "false"}, + {"baz||shards/test*", "true"}, + {"baz||shards/test/blacklist*", "false"}, + {"flag_bar||shards/exact", "true"}, + {"flag_bar||shards/test/exact", "true"} + ]. + +parse_flags_term_test_() -> + LongBinary = binary:copy(<<"a">>, ?MAX_FLAG_NAME_LENGTH + 1), + ExpectedError = {error, {"Cannot parse list of tags: ~n~p", + [{too_long, LongBinary}]}}, + ExpectedUnknownError = {error,{"Cannot parse list of tags: ~n~p", + [{invalid_flag,<<"dddddddd">>}]}}, + [ + {"empty binary", ?_assertEqual( + [], couch_flags_config:parse_flags_term(<<>>))}, + {"single flag", ?_assertEqual( + [fff], couch_flags_config:parse_flags_term(<<"fff">>))}, + {"sorted", ?_assertEqual( + [aaa,bbb,fff], couch_flags_config:parse_flags_term(<<"fff,aaa,bbb">>))}, + {"whitespace", ?_assertEqual( + [aaa,bbb,fff], couch_flags_config:parse_flags_term(<<"fff , aaa, bbb ">>))}, + {"error", ?_assertEqual( + ExpectedError, couch_flags_config:parse_flags_term(LongBinary))}, + {"unknown_flag", ?_assertEqual( + ExpectedUnknownError, couch_flags_config:parse_flags_term(<<"dddddddd">>))} + ]. + |