PageRenderTime 22ms CodeModel.GetById 9ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 1ms

/services/sync/tests/unit/test_async_querySpinningly.js

http://github.com/zpao/v8monkey
JavaScript | 92 lines | 74 code | 18 blank | 0 comment | 0 complexity | f92615568eb63ded53bea96e9de868f2 MD5 | raw file
 1_("Make sure querySpinningly will synchronously fetch rows for a query asyncly");
 2Cu.import("resource://services-sync/async.js");
 3
 4const SQLITE_CONSTRAINT_VIOLATION = 19;  // http://www.sqlite.org/c3ref/c_abort.html
 5
 6function querySpinningly(query, names) {
 7  let q = Svc.Form.DBConnection.createStatement(query);
 8  let r = Async.querySpinningly(q, names);
 9  q.finalize();    
10  return r;
11}
12
13function run_test() {
14  initTestLogging("Trace");
15
16  _("Make sure the call is async and allows other events to process");
17  let isAsync = false;
18  Utils.nextTick(function() { isAsync = true; });
19  do_check_false(isAsync);
20
21  _("Empty out the formhistory table");
22  let r0 = querySpinningly("DELETE FROM moz_formhistory");
23  do_check_eq(r0, null);
24
25  _("Make sure there's nothing there");
26  let r1 = querySpinningly("SELECT 1 FROM moz_formhistory");
27  do_check_eq(r1, null);
28
29  _("Insert a row");
30  let r2 = querySpinningly("INSERT INTO moz_formhistory (fieldname, value) VALUES ('foo', 'bar')");
31  do_check_eq(r2, null);
32
33  _("Request a known value for the one row");
34  let r3 = querySpinningly("SELECT 42 num FROM moz_formhistory", ["num"]);
35  do_check_eq(r3.length, 1);
36  do_check_eq(r3[0].num, 42);
37
38  _("Get multiple columns");
39  let r4 = querySpinningly("SELECT fieldname, value FROM moz_formhistory", ["fieldname", "value"]);
40  do_check_eq(r4.length, 1);
41  do_check_eq(r4[0].fieldname, "foo");
42  do_check_eq(r4[0].value, "bar");
43
44  _("Get multiple columns with a different order");
45  let r5 = querySpinningly("SELECT fieldname, value FROM moz_formhistory", ["value", "fieldname"]);
46  do_check_eq(r5.length, 1);
47  do_check_eq(r5[0].fieldname, "foo");
48  do_check_eq(r5[0].value, "bar");
49
50  _("Add multiple entries (sqlite doesn't support multiple VALUES)");
51  let r6 = querySpinningly("INSERT INTO moz_formhistory (fieldname, value) SELECT 'foo', 'baz' UNION SELECT 'more', 'values'");
52  do_check_eq(r6, null);
53
54  _("Get multiple rows");
55  let r7 = querySpinningly("SELECT fieldname, value FROM moz_formhistory WHERE fieldname = 'foo'", ["fieldname", "value"]);
56  do_check_eq(r7.length, 2);
57  do_check_eq(r7[0].fieldname, "foo");
58  do_check_eq(r7[1].fieldname, "foo");
59
60  _("Make sure updates work");
61  let r8 = querySpinningly("UPDATE moz_formhistory SET value = 'updated' WHERE fieldname = 'more'");
62  do_check_eq(r8, null);
63
64  _("Get the updated");
65  let r9 = querySpinningly("SELECT value, fieldname FROM moz_formhistory WHERE fieldname = 'more'", ["fieldname", "value"]);
66  do_check_eq(r9.length, 1);
67  do_check_eq(r9[0].fieldname, "more");
68  do_check_eq(r9[0].value, "updated");
69
70  _("Grabbing fewer fields than queried is fine");
71  let r10 = querySpinningly("SELECT value, fieldname FROM moz_formhistory", ["fieldname"]);
72  do_check_eq(r10.length, 3);
73
74  _("Generate an execution error");
75  let query = "INSERT INTO moz_formhistory (fieldname, value) VALUES ('one', NULL)";
76  let stmt = Svc.Form.DBConnection.createStatement(query);
77  let r11, except; ;
78  try {
79    r11 = Async.querySpinningly(stmt);
80  } catch(e) {
81    except = e;
82  }
83  stmt.finalize()
84  do_check_true(!!except);
85  do_check_eq(except.result, SQLITE_CONSTRAINT_VIOLATION);
86
87  _("Cleaning up");
88  querySpinningly("DELETE FROM moz_formhistory");
89
90  _("Make sure the timeout got to run before this function ends");
91  do_check_true(isAsync);
92}