summaryrefslogtreecommitdiffstats
path: root/storage/test/unit/test_storage_statement.js
blob: 026e271ac5f0454a0a144746f2560b1cc2659b0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// This file tests the functions of mozIStorageStatement

function setup()
{
  getOpenedDatabase().createTable("test", "id INTEGER PRIMARY KEY, name TEXT");
}

function test_parameterCount_none()
{
  var stmt = createStatement("SELECT * FROM test");
  do_check_eq(0, stmt.parameterCount);
  stmt.reset();
  stmt.finalize();
}

function test_parameterCount_one()
{
  var stmt = createStatement("SELECT * FROM test WHERE id = ?1");
  do_check_eq(1, stmt.parameterCount);
  stmt.reset();
  stmt.finalize();
}

function test_getParameterName()
{
  var stmt = createStatement("SELECT * FROM test WHERE id = :id");
  do_check_eq(":id", stmt.getParameterName(0));
  stmt.reset();
  stmt.finalize();
}

function test_getParameterIndex_different()
{
  var stmt = createStatement("SELECT * FROM test WHERE id = :id OR name = :name");
  do_check_eq(0, stmt.getParameterIndex("id"));
  do_check_eq(1, stmt.getParameterIndex("name"));
  stmt.reset();
  stmt.finalize();
}

function test_getParameterIndex_same()
{
  var stmt = createStatement("SELECT * FROM test WHERE id = :test OR name = :test");
  do_check_eq(0, stmt.getParameterIndex("test"));
  stmt.reset();
  stmt.finalize();
}

function test_columnCount()
{
  var stmt = createStatement("SELECT * FROM test WHERE id = ?1 OR name = ?2");
  do_check_eq(2, stmt.columnCount);
  stmt.reset();
  stmt.finalize();
}

function test_getColumnName()
{
  var stmt = createStatement("SELECT name, id FROM test");
  do_check_eq("id", stmt.getColumnName(1));
  do_check_eq("name", stmt.getColumnName(0));
  stmt.reset();
  stmt.finalize();
}

function test_getColumnIndex_same_case()
{
  var stmt = createStatement("SELECT name, id FROM test");
  do_check_eq(0, stmt.getColumnIndex("name"));
  do_check_eq(1, stmt.getColumnIndex("id"));
  stmt.reset();
  stmt.finalize();
}

function test_getColumnIndex_different_case()
{
  var stmt = createStatement("SELECT name, id FROM test");
  try {
    do_check_eq(0, stmt.getColumnIndex("NaMe"));
    do_throw("should not get here");
  } catch (e) {
    do_check_eq(Cr.NS_ERROR_INVALID_ARG, e.result);
  }
  try {
    do_check_eq(1, stmt.getColumnIndex("Id"));
    do_throw("should not get here");
  } catch (e) {
    do_check_eq(Cr.NS_ERROR_INVALID_ARG, e.result);
  }
  stmt.reset();
  stmt.finalize();
}

function test_state_ready()
{
  var stmt = createStatement("SELECT name, id FROM test");
  do_check_eq(Ci.mozIStorageStatement.MOZ_STORAGE_STATEMENT_READY, stmt.state);
  stmt.reset();
  stmt.finalize();
}

function test_state_executing()
{
  var stmt = createStatement("INSERT INTO test (name) VALUES ('foo')");
  stmt.execute();
  stmt.execute();
  stmt.finalize();

  stmt = createStatement("SELECT name, id FROM test");
  stmt.executeStep();
  do_check_eq(Ci.mozIStorageStatement.MOZ_STORAGE_STATEMENT_EXECUTING,
              stmt.state);
  stmt.executeStep();
  do_check_eq(Ci.mozIStorageStatement.MOZ_STORAGE_STATEMENT_EXECUTING,
              stmt.state);
  stmt.reset();
  do_check_eq(Ci.mozIStorageStatement.MOZ_STORAGE_STATEMENT_READY, stmt.state);
  stmt.finalize();
}

function test_state_after_finalize()
{
  var stmt = createStatement("SELECT name, id FROM test");
  stmt.executeStep();
  stmt.finalize();
  do_check_eq(Ci.mozIStorageStatement.MOZ_STORAGE_STATEMENT_INVALID, stmt.state);
}

function test_failed_execute()
{
  var stmt = createStatement("INSERT INTO test (name) VALUES ('foo')");
  stmt.execute();
  stmt.finalize();
  var id = getOpenedDatabase().lastInsertRowID;
  stmt = createStatement("INSERT INTO test(id, name) VALUES(:id, 'bar')");
  stmt.params.id = id;
  try {
    // Should throw a constraint error
    stmt.execute();
    do_throw("Should have seen a constraint error");
  }
  catch (e) {
    do_check_eq(getOpenedDatabase().lastError, Ci.mozIStorageError.CONSTRAINT);
  }
  do_check_eq(Ci.mozIStorageStatement.MOZ_STORAGE_STATEMENT_READY, stmt.state);
  // Should succeed without needing to reset the statement manually
  stmt.finalize();
}

function test_bind_undefined()
{
  var stmt = createStatement("INSERT INTO test (name) VALUES ('foo')");

  expectError(Cr.NS_ERROR_ILLEGAL_VALUE,
              () => stmt.bindParameters(undefined));

  stmt.finalize();
}

var tests = [test_parameterCount_none, test_parameterCount_one,
             test_getParameterName, test_getParameterIndex_different,
             test_getParameterIndex_same, test_columnCount,
             test_getColumnName, test_getColumnIndex_same_case,
             test_getColumnIndex_different_case, test_state_ready,
             test_state_executing, test_state_after_finalize,
             test_failed_execute,
             test_bind_undefined,
];

function run_test()
{
  setup();

  for (var i = 0; i < tests.length; i++) {
    tests[i]();
  }

  cleanup();
}