Skip to content

Commit c69aec9

Browse files
committed
Style: Refactor using failwith_fmt
Signed-off-by: Vincent Liu <[email protected]>
1 parent eacc53b commit c69aec9

File tree

1 file changed

+34
-59
lines changed

1 file changed

+34
-59
lines changed

ocaml/database/database_test.ml

Lines changed: 34 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ let name_label = "name__label"
1616

1717
let name_description = "name__description"
1818

19+
let failwith_fmt fmt = Printf.ksprintf failwith fmt
20+
1921
module Tests =
2022
functor
2123
(Client : Db_interface.DB_ACCESS)
@@ -111,7 +113,7 @@ functor
111113
; where_value= ""
112114
}
113115
in
114-
failwith (Printf.sprintf "%s <invalid table>" fn_name)
116+
failwith_fmt "%s <invalid table>" fn_name
115117
) ;
116118
Printf.printf
117119
"%s <valid table> <invalid return> <valid field> <valid value>\n"
@@ -126,11 +128,9 @@ functor
126128
; where_value= name
127129
}
128130
in
129-
failwith
130-
(Printf.sprintf
131-
"%s <valid table> <invalid return> <valid field> <valid value>"
132-
fn_name
133-
)
131+
failwith_fmt
132+
"%s <valid table> <invalid return> <valid field> <valid value>"
133+
fn_name
134134
) ;
135135
Printf.printf
136136
"%s <valid table> <valid return> <invalid field> <valid value>\n"
@@ -145,11 +145,9 @@ functor
145145
; where_value= ""
146146
}
147147
in
148-
failwith
149-
(Printf.sprintf
150-
"%s <valid table> <valid return> <invalid field> <valid value>"
151-
fn_name
152-
)
148+
failwith_fmt
149+
"%s <valid table> <valid return> <invalid field> <valid value>"
150+
fn_name
153151
)
154152

155153
(* Verify the ref_index contents are correct for a given [tblname] and [key] (uuid/ref) *)
@@ -168,10 +166,9 @@ functor
168166
| Some {Ref_index.name_label= name_label'; uuid; _ref} ->
169167
(* key should be either uuid or _ref *)
170168
if key <> uuid && key <> _ref then
171-
failwith
172-
(Printf.sprintf "check_ref_index %s key %s: got ref %s uuid %s"
173-
tblname key _ref uuid
174-
) ;
169+
failwith_fmt "check_ref_index %s key %s: got ref %s uuid %s" tblname
170+
key _ref uuid ;
171+
175172
let real_ref =
176173
if Client.is_valid_ref t key then
177174
key
@@ -183,14 +180,11 @@ functor
183180
with _ -> None
184181
in
185182
if name_label' <> real_name_label then
186-
failwith
187-
(Printf.sprintf
188-
"check_ref_index %s key %s: ref_index name_label = %s; db has \
189-
%s"
190-
tblname key
191-
(Option.value ~default:"None" name_label')
192-
(Option.value ~default:"None" real_name_label)
193-
)
183+
failwith_fmt
184+
"check_ref_index %s key %s: ref_index name_label = %s; db has %s"
185+
tblname key
186+
(Option.value ~default:"None" name_label')
187+
(Option.value ~default:"None" real_name_label)
194188

195189
open Db_cache_types
196190

@@ -226,11 +220,9 @@ functor
226220
in
227221
let bar_foos = Row.find "foos" bar_1 in
228222
if bar_foos <> Set ["foo:1"] then
229-
failwith
230-
(Printf.sprintf
231-
"check_many_to_many: bar(bar:1).foos expected ('foo:1') got %s"
232-
(Schema.Value.marshal bar_foos)
233-
) ;
223+
failwith_fmt
224+
"check_many_to_many: bar(bar:1).foos expected ('foo:1') got %s"
225+
(Schema.Value.marshal bar_foos) ;
234226
(* set foo.bars to [] *)
235227
(* let foo_1 = Table.find "foo:1" (TableSet.find "foo" (Database.tableset db)) in*)
236228
let db = set_field "foo" "foo:1" "bars" (Set []) db in
@@ -240,11 +232,8 @@ functor
240232
in
241233
let bar_foos = Row.find "foos" bar_1 in
242234
if bar_foos <> Set [] then
243-
failwith
244-
(Printf.sprintf
245-
"check_many_to_many: bar(bar:1).foos expected () got %s"
246-
(Schema.Value.marshal bar_foos)
247-
) ;
235+
failwith_fmt "check_many_to_many: bar(bar:1).foos expected () got %s"
236+
(Schema.Value.marshal bar_foos) ;
248237
(* add 'bar' to foo.bars *)
249238
let db = set_field "foo" "foo:1" "bars" (Set ["bar:1"]) db in
250239
(* check that 'bar.foos' includes 'foo' *)
@@ -253,11 +242,9 @@ functor
253242
in
254243
let bar_foos = Row.find "foos" bar_1 in
255244
if bar_foos <> Set ["foo:1"] then
256-
failwith
257-
(Printf.sprintf
258-
"check_many_to_many: bar(bar:1).foos expected ('foo:1') got %s - 2"
259-
(Schema.Value.marshal bar_foos)
260-
) ;
245+
failwith_fmt
246+
"check_many_to_many: bar(bar:1).foos expected ('foo:1') got %s - 2"
247+
(Schema.Value.marshal bar_foos) ;
261248
(* delete 'bar' *)
262249
let db = remove_row "bar" "bar:1" db in
263250
(* check that 'foo.bars' is empty *)
@@ -266,11 +253,8 @@ functor
266253
in
267254
let foo_bars = Row.find "bars" foo_1 in
268255
if foo_bars <> Set [] then
269-
failwith
270-
(Printf.sprintf
271-
"check_many_to_many: foo(foo:1).foos expected () got %s"
272-
(Schema.Value.marshal foo_bars)
273-
) ;
256+
failwith_fmt "check_many_to_many: foo(foo:1).foos expected () got %s"
257+
(Schema.Value.marshal foo_bars) ;
274258
()
275259

276260
let check_events t =
@@ -503,8 +487,7 @@ functor
503487
| None ->
504488
Printf.printf "Reference '%s' has no associated table\n" invalid_ref
505489
| Some t ->
506-
failwith
507-
(Printf.sprintf "Reference '%s' exists in table '%s'" invalid_ref t)
490+
failwith_fmt "Reference '%s' exists in table '%s'" invalid_ref t
508491
) ;
509492
Printf.printf "is_valid_ref <invalid_ref>\n" ;
510493
if Client.is_valid_ref t invalid_ref then
@@ -571,10 +554,8 @@ functor
571554
Printf.printf "db_get_by_uuid <valid uuid>\n" ;
572555
let r = Client.db_get_by_uuid t "VM" valid_uuid in
573556
if r <> valid_ref then
574-
failwith
575-
(Printf.sprintf "db_get_by_uuid <valid uuid>: got %s; expected %s" r
576-
valid_ref
577-
) ;
557+
failwith_fmt "db_get_by_uuid <valid uuid>: got %s; expected %s" r
558+
valid_ref ;
578559
Printf.printf "db_get_by_uuid <invalid uuid>\n" ;
579560
expect_missing_uuid "VM" invalid_uuid (fun () ->
580561
let (_ : string) = Client.db_get_by_uuid t "VM" invalid_uuid in
@@ -584,20 +565,14 @@ functor
584565
let r = Client.db_get_by_uuid_opt t "VM" valid_uuid in
585566
( if r <> Some valid_ref then
586567
let rs = Option.value ~default:"None" r in
587-
failwith
588-
(Printf.sprintf
589-
"db_get_by_uuid_opt <valid uuid>: got %s; expected %s" rs
590-
valid_ref
591-
)
568+
failwith_fmt "db_get_by_uuid_opt <valid uuid>: got %s; expected %s" rs
569+
valid_ref
592570
) ;
593571
Printf.printf "db_get_by_uuid_opt <invalid uuid>\n" ;
594572
let r = Client.db_get_by_uuid_opt t "VM" invalid_uuid in
595573
if not (Option.is_none r) then
596-
failwith
597-
(Printf.sprintf
598-
"db_get_by_uuid_opt <invalid uuid>: got %s; expected None"
599-
valid_ref
600-
) ;
574+
failwith_fmt "db_get_by_uuid_opt <invalid uuid>: got %s; expected None"
575+
valid_ref ;
601576
Printf.printf "get_by_name_label <invalid name label>\n" ;
602577
if Client.db_get_by_name_label t "VM" invalid_name <> [] then
603578
failwith "db_get_by_name_label <invalid name label>" ;

0 commit comments

Comments
 (0)