@@ -30,9 +30,12 @@ setup() ->
30
30
create_db (Db1Url ),
31
31
Db2Url = lists :concat ([Url , " db2" ]),
32
32
create_db (Db2Url ),
33
+ mock (fabric_util ),
34
+ mock (chttpd_util ),
33
35
Url .
34
36
35
37
teardown (Url ) ->
38
+ meck :unload (),
36
39
Db1Url = lists :concat ([Url , " db1" ]),
37
40
Db2Url = lists :concat ([Url , " db2" ]),
38
41
delete_db (Db1Url ),
@@ -46,6 +49,16 @@ create_db(Url) ->
46
49
delete_db (Url ) ->
47
50
{ok , 200 , _ , _ } = test_request :delete (Url , [? AUTH ]).
48
51
52
+ mock (Module ) ->
53
+ meck :new (Module , [passthrough ]).
54
+
55
+ mock_timeout () ->
56
+ meck :expect (fabric_util , request_timeout , fun () -> 0 end ).
57
+
58
+ mock_db_not_exist () ->
59
+ meck :expect (chttpd_util , get_db_info ,
60
+ fun (_ ) -> {error , database_does_not_exist } end ).
61
+
49
62
dbs_info_test_ () ->
50
63
{
51
64
" chttpd dbs info tests" ,
@@ -58,7 +71,18 @@ dbs_info_test_() ->
58
71
fun setup /0 ,
59
72
fun teardown /1 ,
60
73
[
61
- fun should_return_error_for_get_db_info /1 ,
74
+ fun get_db_info_should_return_db_info /1 ,
75
+ fun get_db_info_should_return_error_when_db_not_exist /1 ,
76
+ fun get_db_info_should_return_error_when_time_out /1 ,
77
+ fun should_return_error_for_put_dbs_info /1 ,
78
+ fun should_return_dbs_info_for_get_dbs_info /1 ,
79
+ fun should_return_nothing_when_db_not_exist_for_get_dbs_info /1 ,
80
+ fun should_return_500_time_out_when_time_is_not_enough_for_get_dbs_info /1 ,
81
+ fun should_return_db2_for_get_dbs_info_with_descending /1 ,
82
+ fun should_return_db1_for_get_dbs_info_with_limit_1 /1 ,
83
+ fun should_return_db2_for_get_dbs_info_with_skip_1 /1 ,
84
+ fun should_return_dbs_info_with_correct_start_end_key /1 ,
85
+ fun should_return_empty_list_with_wrong_start_end_key /1 ,
62
86
fun should_return_dbs_info_for_single_db /1 ,
63
87
fun should_return_dbs_info_for_multiple_dbs /1 ,
64
88
fun should_return_error_for_exceeded_keys /1 ,
@@ -69,22 +93,132 @@ dbs_info_test_() ->
69
93
}
70
94
}.
71
95
72
- should_return_error_for_get_db_info (Url ) ->
73
- ? _test (begin
74
- {ok , Code , _ , ResultBody } = test_request :get (
75
- Url ++ " /_dbs_info?" ++
76
- " keys=[\" db1\" ]" ,
77
- [? CONTENT_JSON , ? AUTH ]
78
- ),
79
- {Body } = jiffy :decode (ResultBody ),
80
- [
81
- ? assertEqual (
82
- <<" method_not_allowed" >>,
83
- couch_util :get_value (<<" error" >>, Body )
84
- ),
85
- ? assertEqual (405 , Code )
86
- ]
87
- end ).
96
+
97
+ get_db_info_should_return_db_info (_ ) ->
98
+ DbInfo = fabric :get_db_info (" db1" ),
99
+ ? _assertEqual (DbInfo , chttpd_util :get_db_info (" db1" )).
100
+
101
+
102
+ get_db_info_should_return_error_when_db_not_exist (_ ) ->
103
+ ? _assertEqual ({error , database_does_not_exist },
104
+ chttpd_util :get_db_info (" db_not_exist" )).
105
+
106
+
107
+ get_db_info_should_return_error_when_time_out (_ ) ->
108
+ ? _test (
109
+ begin
110
+ mock_timeout (),
111
+ ? assertEqual ({error , timeout }, chttpd_util :get_db_info (" db1" ))
112
+ end ).
113
+
114
+
115
+ should_return_error_for_put_dbs_info (Url ) ->
116
+ ? _test (
117
+ begin
118
+ {ok , Code , _ , ResultBody } = test_request :put (Url
119
+ ++ " _dbs_info" , [? CONTENT_JSON , ? AUTH ], " " ),
120
+ {Body } = jiffy :decode (ResultBody ),
121
+ ? assertEqual (405 , Code ),
122
+ ? assertEqual (<<" method_not_allowed" >>,
123
+ couch_util :get_value (<<" error" >>, Body ))
124
+ end ).
125
+
126
+
127
+ should_return_dbs_info_for_get_dbs_info (Url ) ->
128
+ ? _test (
129
+ begin
130
+ {ok , _ , _ , ResultBody } = test_request :get (Url
131
+ ++ " _dbs_info" , [? CONTENT_JSON , ? AUTH ]),
132
+ BodyJson = jiffy :decode (ResultBody ),
133
+ {Db1Data } = lists :nth (1 , BodyJson ),
134
+ {Db2Data } = lists :nth (2 , BodyJson ),
135
+ ? assertEqual (2 , length (BodyJson )),
136
+ ? assertEqual (<<" db1" >>, couch_util :get_value (<<" key" >>, Db1Data )),
137
+ ? assertEqual (<<" db2" >>, couch_util :get_value (<<" key" >>, Db2Data ))
138
+ end ).
139
+
140
+
141
+ should_return_nothing_when_db_not_exist_for_get_dbs_info (Url ) ->
142
+ ? _test (
143
+ begin
144
+ mock_db_not_exist (),
145
+ {ok , Code , _ , ResultBody } = test_request :get (Url
146
+ ++ " _dbs_info" , [? CONTENT_JSON , ? AUTH ]),
147
+ BodyJson = jiffy :decode (ResultBody ),
148
+ ? assertEqual (200 , Code ),
149
+ ? assertEqual ([], BodyJson )
150
+ end ).
151
+
152
+
153
+ should_return_500_time_out_when_time_is_not_enough_for_get_dbs_info (Url ) ->
154
+ ? _test (
155
+ begin
156
+ mock_timeout (),
157
+ {ok , Code , _ , ResultBody } = test_request :get (Url ++ " _dbs_info"
158
+ ++ " ?buffer_response=true" , [? CONTENT_JSON , ? AUTH ]),
159
+ {Body } = jiffy :decode (ResultBody ),
160
+ ? assertEqual (500 , Code ),
161
+ ? assertEqual (<<" timeout" >>, couch_util :get_value (<<" error" >>, Body ))
162
+ end ).
163
+
164
+
165
+ should_return_db2_for_get_dbs_info_with_descending (Url ) ->
166
+ ? _test (
167
+ begin
168
+ {ok , _ , _ , ResultBody } = test_request :get (Url ++ " _dbs_info"
169
+ ++ " ?descending=true" , [? CONTENT_JSON , ? AUTH ]),
170
+ BodyJson = jiffy :decode (ResultBody ),
171
+ {Db1Data } = lists :nth (1 , BodyJson ),
172
+ {Db2Data } = lists :nth (2 , BodyJson ),
173
+ ? assertEqual (2 , length (BodyJson )),
174
+ ? assertEqual (<<" db2" >>, couch_util :get_value (<<" key" >>, Db1Data )),
175
+ ? assertEqual (<<" db1" >>, couch_util :get_value (<<" key" >>, Db2Data ))
176
+ end ).
177
+
178
+
179
+ should_return_db1_for_get_dbs_info_with_limit_1 (Url ) ->
180
+ ? _test (
181
+ begin
182
+ {ok , _ , _ , ResultBody } = test_request :get (Url ++ " _dbs_info"
183
+ ++ " ?limit=1" , [? CONTENT_JSON , ? AUTH ]),
184
+ BodyJson = jiffy :decode (ResultBody ),
185
+ {DbData } = lists :nth (1 , BodyJson ),
186
+ ? assertEqual (1 , length (BodyJson )),
187
+ ? assertEqual (<<" db1" >>, couch_util :get_value (<<" key" >>, DbData ))
188
+ end ).
189
+
190
+
191
+ should_return_db2_for_get_dbs_info_with_skip_1 (Url ) ->
192
+ ? _test (
193
+ begin
194
+ {ok , _ , _ , ResultBody } = test_request :get (Url ++ " _dbs_info"
195
+ ++ " ?skip=1" , [? CONTENT_JSON , ? AUTH ]),
196
+ BodyJson = jiffy :decode (ResultBody ),
197
+ {DbData } = lists :nth (1 , BodyJson ),
198
+ ? assertEqual (1 , length (BodyJson )),
199
+ ? assertEqual (<<" db2" >>, couch_util :get_value (<<" key" >>, DbData ))
200
+ end ).
201
+
202
+
203
+ should_return_dbs_info_with_correct_start_end_key (Url ) ->
204
+ ? _test (
205
+ begin
206
+ {ok , _ , _ , ResultBody } = test_request :get (Url ++ " _dbs_info"
207
+ ++ " ?startkey=\" db1\" &endkey=\" db2\" " , [? CONTENT_JSON , ? AUTH ]),
208
+ BodyJson = jiffy :decode (ResultBody ),
209
+ {DbData } = lists :nth (1 , BodyJson ),
210
+ ? assertEqual (2 , length (BodyJson )),
211
+ ? assertEqual (<<" db1" >>, couch_util :get_value (<<" key" >>, DbData ))
212
+ end ).
213
+
214
+
215
+ should_return_empty_list_with_wrong_start_end_key (Url ) ->
216
+ ? _test (
217
+ begin
218
+ {ok , _ , _ , ResultBody } = test_request :get (Url ++ " _dbs_info"
219
+ ++ " ?startkey=\" db3\" &endkey=\" db4\" " , [? CONTENT_JSON , ? AUTH ]),
220
+ ? assertEqual ([], jiffy :decode (ResultBody ))
221
+ end ).
88
222
89
223
should_return_dbs_info_for_single_db (Url ) ->
90
224
? _test (begin
0 commit comments