storage/test/unit/test_storage_statement.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     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 mozIStorageStatement
     7 function setup()
     8 {
     9   getOpenedDatabase().createTable("test", "id INTEGER PRIMARY KEY, name TEXT");
    10 }
    12 function test_parameterCount_none()
    13 {
    14   var stmt = createStatement("SELECT * FROM test");
    15   do_check_eq(0, stmt.parameterCount);
    16   stmt.reset();
    17   stmt.finalize();
    18 }
    20 function test_parameterCount_one()
    21 {
    22   var stmt = createStatement("SELECT * FROM test WHERE id = ?1");
    23   do_check_eq(1, stmt.parameterCount);
    24   stmt.reset();
    25   stmt.finalize();
    26 }
    28 function test_getParameterName()
    29 {
    30   var stmt = createStatement("SELECT * FROM test WHERE id = :id");
    31   do_check_eq(":id", stmt.getParameterName(0));
    32   stmt.reset();
    33   stmt.finalize();
    34 }
    36 function test_getParameterIndex_different()
    37 {
    38   var stmt = createStatement("SELECT * FROM test WHERE id = :id OR name = :name");
    39   do_check_eq(0, stmt.getParameterIndex("id"));
    40   do_check_eq(1, stmt.getParameterIndex("name"));
    41   stmt.reset();
    42   stmt.finalize();
    43 }
    45 function test_getParameterIndex_same()
    46 {
    47   var stmt = createStatement("SELECT * FROM test WHERE id = :test OR name = :test");
    48   do_check_eq(0, stmt.getParameterIndex("test"));
    49   stmt.reset();
    50   stmt.finalize();
    51 }
    53 function test_columnCount()
    54 {
    55   var stmt = createStatement("SELECT * FROM test WHERE id = ?1 OR name = ?2");
    56   do_check_eq(2, stmt.columnCount);
    57   stmt.reset();
    58   stmt.finalize();
    59 }
    61 function test_getColumnName()
    62 {
    63   var stmt = createStatement("SELECT name, id FROM test");
    64   do_check_eq("id", stmt.getColumnName(1));
    65   do_check_eq("name", stmt.getColumnName(0));
    66   stmt.reset();
    67   stmt.finalize();
    68 }
    70 function test_getColumnIndex_same_case()
    71 {
    72   var stmt = createStatement("SELECT name, id FROM test");
    73   do_check_eq(0, stmt.getColumnIndex("name"));
    74   do_check_eq(1, stmt.getColumnIndex("id"));
    75   stmt.reset();
    76   stmt.finalize();
    77 }
    79 function test_getColumnIndex_different_case()
    80 {
    81   var stmt = createStatement("SELECT name, id FROM test");
    82   try {
    83     do_check_eq(0, stmt.getColumnIndex("NaMe"));
    84     do_throw("should not get here");
    85   } catch (e) {
    86     do_check_eq(Cr.NS_ERROR_INVALID_ARG, e.result);
    87   }
    88   try {
    89   do_check_eq(1, stmt.getColumnIndex("Id"));
    90     do_throw("should not get here");
    91   } catch (e) {
    92     do_check_eq(Cr.NS_ERROR_INVALID_ARG, e.result);
    93   }
    94   stmt.reset();
    95   stmt.finalize();
    96 }
    98 function test_state_ready()
    99 {
   100   var stmt = createStatement("SELECT name, id FROM test");
   101   do_check_eq(Ci.mozIStorageStatement.MOZ_STORAGE_STATEMENT_READY, stmt.state);
   102   stmt.reset();
   103   stmt.finalize();
   104 }
   106 function test_state_executing()
   107 {
   108   var stmt = createStatement("INSERT INTO test (name) VALUES ('foo')");
   109   stmt.execute();
   110   stmt.execute();
   111   stmt.finalize();
   113   stmt = createStatement("SELECT name, id FROM test");
   114   stmt.executeStep();
   115   do_check_eq(Ci.mozIStorageStatement.MOZ_STORAGE_STATEMENT_EXECUTING,
   116               stmt.state);
   117   stmt.executeStep();
   118   do_check_eq(Ci.mozIStorageStatement.MOZ_STORAGE_STATEMENT_EXECUTING,
   119               stmt.state);
   120   stmt.reset();
   121   do_check_eq(Ci.mozIStorageStatement.MOZ_STORAGE_STATEMENT_READY, stmt.state);
   122   stmt.finalize();
   123 }
   125 function test_state_after_finalize()
   126 {
   127   var stmt = createStatement("SELECT name, id FROM test");
   128   stmt.executeStep();
   129   stmt.finalize();
   130   do_check_eq(Ci.mozIStorageStatement.MOZ_STORAGE_STATEMENT_INVALID, stmt.state);
   131 }
   133 function test_getColumnDecltype()
   134 {
   135   var stmt = createStatement("SELECT name, id FROM test");
   136   do_check_eq("TEXT", stmt.getColumnDecltype(0));
   137   do_check_eq("INTEGER", stmt.getColumnDecltype(1));
   138   try {
   139     do_check_eq("GARBAGE", stmt.getColumnDecltype(2));
   140     do_throw("should not get here");
   141   } catch (e) {
   142     do_check_eq(Cr.NS_ERROR_ILLEGAL_VALUE, e.result);
   143   }
   144   stmt.finalize();
   145 }
   147 function test_failed_execute()
   148 {
   149   var stmt = createStatement("INSERT INTO test (name) VALUES ('foo')");
   150   stmt.execute();
   151   stmt.finalize();
   152   var id = getOpenedDatabase().lastInsertRowID;
   153   stmt = createStatement("INSERT INTO test(id, name) VALUES(:id, 'bar')");
   154   stmt.params.id = id;
   155   try {
   156     // Should throw a constraint error
   157     stmt.execute();
   158     do_throw("Should have seen a constraint error");
   159   }
   160   catch (e) {
   161     do_check_eq(getOpenedDatabase().lastError, Ci.mozIStorageError.CONSTRAINT);
   162   }
   163   do_check_eq(Ci.mozIStorageStatement.MOZ_STORAGE_STATEMENT_READY, stmt.state);
   164   // Should succeed without needing to reset the statement manually
   165   stmt.finalize();
   166 }
   168 var tests = [test_parameterCount_none, test_parameterCount_one,
   169              test_getParameterName, test_getParameterIndex_different,
   170              test_getParameterIndex_same, test_columnCount,
   171              test_getColumnName, test_getColumnIndex_same_case,
   172              test_getColumnIndex_different_case, test_state_ready,
   173              test_state_executing, test_state_after_finalize,
   174              test_getColumnDecltype,
   175              test_failed_execute,
   176 ];
   178 function run_test()
   179 {
   180   setup();
   182   for (var i = 0; i < tests.length; i++)
   183     tests[i]();
   185   cleanup();
   186 }

mercurial