Permalink
Browse files

Check Mq_sqlite_persistence's SQL statement validity in unit tests.

  • Loading branch information...
1 parent 592c48f commit 7f7692b13a52de548006d5a744abe58f6fbebaa9 @mfp committed Nov 12, 2010
Showing with 49 additions and 15 deletions.
  1. +7 −2 OMakefile
  2. +16 −13 mq_sqlite_persistence.ml
  3. +4 −0 mq_sqlite_persistence.mli
  4. +22 −0 test.ml
View
@@ -30,14 +30,19 @@ OBJECTS[] =
mq_server
mq_sqlite_persistence
ternary
- ocamlmq
section
OCAMLFINDFLAGS += -ppopt ocaml-sqlexpr/pa_sql.cmo
.SCANNER: scan-ocaml-%.ml: %.ml ocaml-sqlexpr/pa_sql.cmo
$(addsuffixes .cmi .cmx .cmo .o, mq_sqlite_persistence):
-OCamlProgram(ocamlmq, $(OBJECTS))
+OCamlProgram(ocamlmq, $(OBJECTS) ocamlmq)
+
+section
+ OCAMLPACKS[] += oUnit
+ TEST_FILES[] = $(removesuffix $(ls test*ml))
+ OCamlProgram(test, $(OBJECTS) $(TEST_FILES) test)
+ $(addsuffixes .cmi .cmo .cmx .o, $(TEST_FILES) test):
.DEFAULT: ocamlmq$(EXE)
View
@@ -81,23 +81,24 @@ let make ?(max_msgs_in_mem = max_int) ?(flush_period = 1.0) file =
t
let initialize t =
- execute t.db sql"ATTACH \":memory:\" AS mem";
+ execute t.db sqlinit"ATTACH \":memory:\" AS mem";
execute t.db
- sql"CREATE TABLE IF NOT EXISTS ocamlmq_msgs(
- msg_id VARCHAR(255) NOT NULL PRIMARY KEY,
- priority INT NOT NULL,
- destination VARCHAR(255) NOT NULL,
- timestamp DOUBLE NOT NULL,
- ack_timeout DOUBLE NOT NULL,
- body BLOB NOT NULL)";
+ sqlinit"CREATE TABLE IF NOT EXISTS ocamlmq_msgs(
+ msg_id VARCHAR(255) NOT NULL PRIMARY KEY,
+ priority INT NOT NULL,
+ destination VARCHAR(255) NOT NULL,
+ timestamp DOUBLE NOT NULL,
+ ack_timeout DOUBLE NOT NULL,
+ body BLOB NOT NULL
+ )";
execute t.db
- sql"CREATE INDEX IF NOT EXISTS
- ocamlmq_msgs_destination_priority_timestamp
- ON ocamlmq_msgs(destination, priority, timestamp)";
+ sqlinit"CREATE INDEX IF NOT EXISTS
+ ocamlmq_msgs_destination_priority_timestamp
+ ON ocamlmq_msgs(destination, priority, timestamp)";
execute t.db
- sql"CREATE TABLE mem.pending_acks(msg_id VARCHAR(255) NOT NULL PRIMARY KEY)";
+ sqlinit"CREATE TABLE mem.pending_acks(msg_id VARCHAR(255) NOT NULL PRIMARY KEY)";
execute t.db
- sql"CREATE TABLE mem.acked_msgs(msg_id VARCHAR(255) NOT NULL PRIMARY KEY)";
+ sqlinit"CREATE TABLE mem.acked_msgs(msg_id VARCHAR(255) NOT NULL PRIMARY KEY)";
return ()
let do_save_msg t sent msg =
@@ -249,3 +250,5 @@ let count_queue_msgs t dst =
return (Int64.add (Int64.of_int in_mem) in_db)
let crash_recovery t = return ()
+
+let init_db, check_db, auto_check_db = sql_check"sqlite"
@@ -2,3 +2,7 @@
include Mq_server.PERSISTENCE
val make : ?max_msgs_in_mem:int -> ?flush_period:float -> string -> t
+
+(* Used for testing *)
+val auto_check_db : Format.formatter -> bool
+
View
22 test.ml
@@ -0,0 +1,22 @@
+open OUnit
+
+let test_sql f () =
+ let b = Buffer.create 13 in
+ let fmt = Format.formatter_of_buffer b in
+ let contents b = Format.fprintf fmt "@?"; Buffer.contents b in
+ if not (f fmt) then
+ assert_failure (contents b)
+
+let db_tests =
+ [
+ "Mq_sqlite_persistence", Mq_sqlite_persistence.auto_check_db;
+ ]
+
+let all_tests =
+ List.map (fun (n, f) -> n >:: test_sql f) db_tests @
+ [
+ ]
+
+
+let _ =
+ run_test_tt_main ("All" >::: all_tests)

0 comments on commit 7f7692b

Please sign in to comment.