storage/test/unit/test_storage_value_array.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 // This file tests the functions of mozIStorageValueArray
     7 function setup()
     8 {
     9   getOpenedDatabase().createTable("test", "id INTEGER PRIMARY KEY, name TEXT," +
    10                                           "number REAL, nuller NULL, blobber BLOB");
    12   var stmt = createStatement("INSERT INTO test (name, number, blobber) " +
    13                              "VALUES (?1, ?2, ?3)");
    14   stmt.bindByIndex(0, "foo");
    15   stmt.bindByIndex(1, 2.34);
    16   stmt.bindBlobByIndex(2, [], 0);
    17   stmt.execute();
    19   stmt.bindByIndex(0, "");
    20   stmt.bindByIndex(1, 1.23);
    21   stmt.bindBlobByIndex(2, [1, 2], 2);
    22   stmt.execute();
    24   stmt.reset();
    25   stmt.finalize();
    26 }
    28 function test_getIsNull_for_null()
    29 {
    30   var stmt = createStatement("SELECT nuller, blobber FROM test WHERE id = ?1");
    31   stmt.bindByIndex(0, 1);
    32   do_check_true(stmt.executeStep());
    34   do_check_true(stmt.getIsNull(0)); // null field
    35   do_check_true(stmt.getIsNull(1)); // data is null if size is 0
    36   stmt.reset();
    37   stmt.finalize();
    38 }
    40 function test_getIsNull_for_non_null()
    41 {
    42   var stmt = createStatement("SELECT name, blobber FROM test WHERE id = ?1");
    43   stmt.bindByIndex(0, 2);
    44   do_check_true(stmt.executeStep());
    46   do_check_false(stmt.getIsNull(0));
    47   do_check_false(stmt.getIsNull(1));
    48   stmt.reset();
    49   stmt.finalize();
    50 }
    52 function test_value_type_null()
    53 {
    54   var stmt = createStatement("SELECT nuller FROM test WHERE id = ?1");
    55   stmt.bindByIndex(0, 1);
    56   do_check_true(stmt.executeStep());
    58   do_check_eq(Ci.mozIStorageValueArray.VALUE_TYPE_NULL,
    59               stmt.getTypeOfIndex(0));
    60   stmt.reset();
    61   stmt.finalize();
    62 }
    64 function test_value_type_integer()
    65 {
    66   var stmt = createStatement("SELECT id FROM test WHERE id = ?1");
    67   stmt.bindByIndex(0, 1);
    68   do_check_true(stmt.executeStep());
    70   do_check_eq(Ci.mozIStorageValueArray.VALUE_TYPE_INTEGER,
    71               stmt.getTypeOfIndex(0));
    72   stmt.reset();
    73   stmt.finalize();
    74 }
    76 function test_value_type_float()
    77 {
    78   var stmt = createStatement("SELECT number FROM test WHERE id = ?1");
    79   stmt.bindByIndex(0, 1);
    80   do_check_true(stmt.executeStep());
    82   do_check_eq(Ci.mozIStorageValueArray.VALUE_TYPE_FLOAT,
    83               stmt.getTypeOfIndex(0));
    84   stmt.reset();
    85   stmt.finalize();
    86 }
    88 function test_value_type_text()
    89 {
    90   var stmt = createStatement("SELECT name FROM test WHERE id = ?1");
    91   stmt.bindByIndex(0, 1);
    92   do_check_true(stmt.executeStep());
    94   do_check_eq(Ci.mozIStorageValueArray.VALUE_TYPE_TEXT,
    95               stmt.getTypeOfIndex(0));
    96   stmt.reset();
    97   stmt.finalize();
    98 }
   100 function test_value_type_blob()
   101 {
   102   var stmt = createStatement("SELECT blobber FROM test WHERE id = ?1");
   103   stmt.bindByIndex(0, 2);
   104   do_check_true(stmt.executeStep());
   106   do_check_eq(Ci.mozIStorageValueArray.VALUE_TYPE_BLOB,
   107               stmt.getTypeOfIndex(0));
   108   stmt.reset();
   109   stmt.finalize();
   110 }
   112 function test_numEntries_one()
   113 {
   114   var stmt = createStatement("SELECT blobber FROM test WHERE id = ?1");
   115   stmt.bindByIndex(0, 2);
   116   do_check_true(stmt.executeStep());
   118   do_check_eq(1, stmt.numEntries);
   119   stmt.reset();
   120   stmt.finalize();
   121 }
   123 function test_numEntries_all()
   124 {
   125   var stmt = createStatement("SELECT * FROM test WHERE id = ?1");
   126   stmt.bindByIndex(0, 2);
   127   do_check_true(stmt.executeStep());
   129   do_check_eq(5, stmt.numEntries);
   130   stmt.reset();
   131   stmt.finalize();
   132 }
   134 function test_getInt()
   135 {
   136   var stmt = createStatement("SELECT id FROM test WHERE id = ?1");
   137   stmt.bindByIndex(0, 2);
   138   do_check_true(stmt.executeStep());
   140   do_check_eq(2, stmt.getInt32(0));
   141   do_check_eq(2, stmt.getInt64(0));
   142   stmt.reset();
   143   stmt.finalize();
   144 }
   146 function test_getDouble()
   147 {
   148   var stmt = createStatement("SELECT number FROM test WHERE id = ?1");
   149   stmt.bindByIndex(0, 2);
   150   do_check_true(stmt.executeStep());
   152   do_check_eq(1.23, stmt.getDouble(0));
   153   stmt.reset();
   154   stmt.finalize();
   155 }
   157 function test_getUTF8String()
   158 {
   159   var stmt = createStatement("SELECT name FROM test WHERE id = ?1");
   160   stmt.bindByIndex(0, 1);
   161   do_check_true(stmt.executeStep());
   163   do_check_eq("foo", stmt.getUTF8String(0));
   164   stmt.reset();
   165   stmt.finalize();
   166 }
   168 function test_getString()
   169 {
   170   var stmt = createStatement("SELECT name FROM test WHERE id = ?1");
   171   stmt.bindByIndex(0, 2);
   172   do_check_true(stmt.executeStep());
   174   do_check_eq("", stmt.getString(0));
   175   stmt.reset();
   176   stmt.finalize();
   177 }
   179 function test_getBlob()
   180 {
   181   var stmt = createStatement("SELECT blobber FROM test WHERE id = ?1");
   182   stmt.bindByIndex(0, 2);
   183   do_check_true(stmt.executeStep());
   185   var count = { value: 0 };
   186   var arr = { value: null };
   187   stmt.getBlob(0, count, arr);
   188   do_check_eq(2, count.value);
   189   do_check_eq(1, arr.value[0]);
   190   do_check_eq(2, arr.value[1]);
   191   stmt.reset();
   192   stmt.finalize();
   193 }
   195 var tests = [test_getIsNull_for_null, test_getIsNull_for_non_null,
   196              test_value_type_null, test_value_type_integer,
   197              test_value_type_float, test_value_type_text, test_value_type_blob,
   198              test_numEntries_one, test_numEntries_all, test_getInt,
   199              test_getDouble, test_getUTF8String, test_getString, test_getBlob];
   201 function run_test()
   202 {
   203   setup();
   205   for (var i = 0; i < tests.length; i++)
   206     tests[i]();
   208   cleanup();
   209 }

mercurial