services/common/tests/unit/test_async_querySpinningly.js

Wed, 31 Dec 2014 07:53:36 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:53:36 +0100
branch
TOR_BUG_3246
changeset 5
4ab42b5ab56c
permissions
-rw-r--r--

Correct small whitespace inconsistency, lost while renaming variables.

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

mercurial