summaryrefslogtreecommitdiff
path: root/apps/couch/test/etap/080-config-get-set.t
blob: c457c7f0d3e3eb6edc23e6484dcde099c5586d3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env escript
%% -*- erlang -*-

% Licensed under the Apache License, Version 2.0 (the "License"); you may not
% use this file except in compliance with the License. You may obtain a copy of
% the License at
%
%   http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
% License for the specific language governing permissions and limitations under
% the License.

default_config() ->
    filename:join([code:lib_dir(couch, test), "etap/080-config-get-set.ini"]).

main(_) ->
    test_util:init_code_path(),
    etap:plan(12),
    case (catch test()) of
        ok ->
            etap:end_tests();
        Other ->
            etap:diag(io_lib:format("Test died abnormally: ~p", [Other])),
            etap:bail(Other)
    end,
    ok.

test() ->
    % start couch_config with default
    couch_config_event:start_link(),
    couch_config:start_link([default_config()]),


    % Check that we can get values


    etap:fun_is(
        fun(List) -> length(List) > 0 end,
        couch_config:all(),
        "Data was loaded from the INI file."
    ),

    etap:fun_is(
        fun(List) -> length(List) > 0 end,
        couch_config:get("daemons"),
        "There are settings in the [daemons] section of the INI file."
    ),

    etap:is(
        couch_config:get("httpd_design_handlers", "_view"),
        "{couch_httpd_view, handle_view_req}",
        "The {httpd_design_handlers, view} is the expected default."
    ),

    etap:is(
        couch_config:get("httpd", "foo", "bar"),
        "bar",
        "Returns the default when key doesn't exist in config."
    ),

    etap:is(
        couch_config:get("httpd", "foo"),
        undefined,
        "The default default is the atom 'undefined'."
    ),

    etap:is(
        couch_config:get("httpd", "port", "bar"),
        "5984",
        "Only returns the default when the config setting does not exist."
    ),


    % Check that setting values works.


    ok = couch_config:set("log", "level", "severe", false),

    etap:is(
        couch_config:get("log", "level"),
        "severe",
        "Non persisted changes take effect."
    ),

    etap:is(
        couch_config:get("new_section", "bizzle"),
        undefined,
        "Section 'new_section' does not exist."
    ),

    ok = couch_config:set("new_section", "bizzle", "bang", false),

    etap:is(
        couch_config:get("new_section", "bizzle"),
        "bang",
        "New section 'new_section' was created for a new key/value pair."
    ),


    % Check that deleting works


    ok = couch_config:delete("new_section", "bizzle", false),
    etap:is(
        couch_config:get("new_section", "bizzle"),
        undefined,
        "Deleting sets the value to \"\""
    ),


    % Check ge/set/delete binary strings

    ok = couch_config:set(<<"foo">>, <<"bar">>, <<"baz">>, false),
    etap:is(
        couch_config:get(<<"foo">>, <<"bar">>),
        <<"baz">>,
        "Can get and set with binary section and key values."
    ),
    ok = couch_config:delete(<<"foo">>, <<"bar">>, false),
    etap:is(
        couch_config:get(<<"foo">>, <<"bar">>),
        undefined,
        "Deleting with binary section/key pairs sets the value to \"\""
    ),

    ok.