Bug 383619 - Get xpcshell tests for mozStorage. r=vladimir

This commit is contained in:
sdwilsh@shawnwilsher.com 2007-06-11 20:59:26 -07:00
parent 3c3e037df3
commit 23d6cb6f49
6 changed files with 694 additions and 0 deletions

View File

@ -21,6 +21,7 @@
#
# Contributor(s):
# Vladimir Vukicevic <vladimir.vukicevic@oracle.com>
# Shawn Wilsher <me@shawnwilsher.com>
#
# Alternatively, the contents of this file may be used under the terms of
# either of the GNU General Public License Version 2 or later (the "GPL"),
@ -43,6 +44,10 @@ VPATH = @srcdir@
include $(DEPTH)/config/autoconf.mk
MODULE = test_storage
XPCSHELL_TESTS = unit
PROGRAM = teststorage1$(BIN_SUFFIX)
CPPSRCS = storage1.cpp

View File

@ -0,0 +1,75 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Storage Test Code.
*
* The Initial Developer of the Original Code is
* Mozilla Corporation.
* Portions created by the Initial Developer are Copyright (C) 2007
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Shawn Wilsher <me@shawnwilsher.com> (Original Author)
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
const Ci = Components.interfaces;
const Cc = Components.classes;
const Cr = Components.results;
var dirSvc = Cc["@mozilla.org/file/directory_service;1"].
getService(Ci.nsIProperties);
function getTestDB()
{
var db = dirSvc.get("CurProcD", Ci.nsIFile);
db.append("test_storage.sqlite");
return db;
}
function cleanup()
{
// removing test db
var dbFile = getTestDB();
if (dbFile.exists()) dbFile.remove(false);
}
function getService()
{
return Cc["@mozilla.org/storage/service;1"].getService(Ci.mozIStorageService);
}
function getOpenedDatabase()
{
return getService().openDatabase(getTestDB());
}
function createStatement(aSQL)
{
return getOpenedDatabase().createStatement(aSQL);
}
cleanup();

View File

@ -0,0 +1,160 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Storage Test Code.
*
* The Initial Developer of the Original Code is
* Mozilla Corporation.
* Portions created by the Initial Developer are Copyright (C) 2007
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Shawn Wilsher <me@shawnwilsher.com> (Original Author)
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
// This file tests the functions of mozIStorageConnection
function test_connectionReady()
{
// there doesn't seem to be a way for the connection to not be ready
// It can only fail if GetPath fails on the database file, or if we run out
// of memory trying to use an in-memory database
var msc = getOpenedDatabase();
do_check_true(msc.connectionReady);
}
function test_databaseFile()
{
var msc = getOpenedDatabase();
do_check_true(getTestDB().equals(msc.databaseFile));
}
function test_tableExists_not_created()
{
var msc = getOpenedDatabase();
do_check_false(msc.tableExists("foo"));
}
function test_indexExists_not_created()
{
var msc = getOpenedDatabase();
do_check_false(msc.indexExists("foo"));
}
function test_createTable_not_created()
{
var msc = getOpenedDatabase();
msc.createTable("test", "id INTEGER PRIMARY KEY, name TEXT");
do_check_true(msc.tableExists("test"));
}
function test_indexExists_created()
{
var msc = getOpenedDatabase();
msc.executeSimpleSQL("CREATE INDEX name_ind ON test (name)");
do_check_true(msc.indexExists("name_ind"));
}
function test_createTable_already_created()
{
var msc = getOpenedDatabase();
do_check_true(msc.tableExists("test"));
try {
msc.createTable("test", "id INTEGER PRIMARY KEY, name TEXT");
do_throw("We shouldn't get here!");
} catch (e) {
do_check_eq(Cr.NS_ERROR_FAILURE, e.result);
}
}
function test_lastInsertRowID()
{
var msc = getOpenedDatabase();
msc.executeSimpleSQL("INSERT INTO test (name) VALUES ('foo')");
do_check_eq(1, msc.lastInsertRowID);
}
function test_transactionInProgress_no()
{
var msc = getOpenedDatabase();
do_check_false(msc.transactionInProgress);
}
function test_transactionInProgress_yes()
{
var msc = getOpenedDatabase();
msc.beginTransaction();
do_check_true(msc.transactionInProgress);
msc.commitTransaction();
do_check_false(msc.transactionInProgress);
msc.beginTransaction();
do_check_true(msc.transactionInProgress);
msc.rollbackTransaction();
do_check_false(msc.transactionInProgress);
}
function test_commitTransaction_no_transaction()
{
var msc = getOpenedDatabase();
do_check_false(msc.transactionInProgress);
try {
msc.commitTransaction();
do_throw("We should not get here!");
} catch (e) {
do_check_eq(Cr.NS_ERROR_FAILURE, e.result);
}
}
function test_rollbackTransaction_no_transaction()
{
var msc = getOpenedDatabase();
do_check_false(msc.transactionInProgress);
try {
msc.rollbackTransaction();
do_throw("We should not get here!");
} catch (e) {
do_check_eq(Cr.NS_ERROR_FAILURE, e.result);
}
}
var tests = [test_connectionReady, test_databaseFile,
test_tableExists_not_created, test_indexExists_not_created,
test_createTable_not_created, test_indexExists_created,
test_createTable_already_created, test_lastInsertRowID,
test_transactionInProgress_no, test_transactionInProgress_yes,
test_commitTransaction_no_transaction,
test_rollbackTransaction_no_transaction];
function run_test()
{
for (var i = 0; i < tests.length; i++)
tests[i]();
cleanup();
}

View File

@ -0,0 +1,80 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Storage Test Code.
*
* The Initial Developer of the Original Code is
* Mozilla Corporation.
* Portions created by the Initial Developer are Copyright (C) 2007
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Shawn Wilsher <me@shawnwilsher.com> (Original Author)
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
// This file tests the functions of mozIStorageService
function test_openSpecialDatabase_invalid_arg()
{
try {
getService().openSpecialDatabase("abcd");
do_throw("We should not get here!");
} catch (e) {
print(e);
print("e.result is " + e.result);
do_check_eq(Cr.NS_ERROR_INVALID_ARG, e.result);
}
}
function test_openDatabase_file_DNE()
{
// the file should be created after calling
var db = getTestDB();
do_check_false(db.exists());
getService().openDatabase(db);
do_check_true(db.exists());
}
function test_openDatabase_file_exists()
{
// it should already exist from our last test
var db = getTestDB();
do_check_true(db.exists());
getService().openDatabase(db);
do_check_true(db.exists());
}
var tests = [test_openSpecialDatabase_invalid_arg, test_openDatabase_file_DNE,
test_openDatabase_file_exists];
function run_test()
{
for (var i = 0; i < tests.length; i++)
tests[i]();
cleanup();
}

View File

@ -0,0 +1,146 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Storage Test Code.
*
* The Initial Developer of the Original Code is
* Mozilla Corporation.
* Portions created by the Initial Developer are Copyright (C) 2007
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Shawn Wilsher <me@shawnwilsher.com> (Original Author)
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
// 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);
}
function test_parameterCount_one()
{
var stmt = createStatement("SELECT * FROM test WHERE id = ?1");
do_check_eq(1, stmt.parameterCount);
}
function test_getParameterName()
{
var stmt = createStatement("SELECT * FROM test WHERE id = :id");
do_check_eq(":id", stmt.getParameterName(0));
}
function test_getParameterIndexes_different()
{
var stmt = createStatement("SELECT * FROM test WHERE id = :id OR name = :name");
var count = { value: 0 };
var result = stmt.getParameterIndexes(":id", count);
do_check_eq(1, count.value);
do_check_eq(1, result.length);
do_check_eq(1, result[0]); // XXX is this a bug?
// SQLite index is one, but how we treat them, it should be zero
result = stmt.getParameterIndexes(":name", count);
do_check_eq(1, count.value);
do_check_eq(1, result.length);
do_check_eq(2, result[0]); // XXX is this a bug?
// SQLite index is two, but how we treat them, it should be one
}
function test_getParameterIndexes_same()
{
// XXX SQLite is buggy with this case :(
return;
var stmt = createStatement("SELECT * FROM test WHERE id = :test OR name = :test");
print("param count = " + stmt.parameterCount);
var count = { value: 0 };
var result = stmt.getParameterIndexes(":test", count);
print("count.value = " + count.value);
do_check_eq(2, count.value);
print("result.length = " + result.length);
print("result[0] = " + result[0]);
do_check_eq(2, result.length);
}
function test_columnCount()
{
var stmt = createStatement("SELECT * FROM test WHERE id = ?1 OR name = ?2");
do_check_eq(2, stmt.columnCount);
}
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));
}
function test_state_ready()
{
var stmt = createStatement("SELECT name, id FROM test");
do_check_eq(Ci.mozIStorageStatement.MOZ_STORAGE_STATEMENT_READY, stmt.state);
}
function test_state_executing()
{
var stmt = createStatement("INSERT INTO test (name) VALUES ('foo')");
stmt.execute();
stmt.execute();
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);
}
var tests = [test_parameterCount_none, test_parameterCount_one,
test_getParameterName, test_getParameterIndexes_different,
test_getParameterIndexes_same, test_columnCount,
test_getColumnName, test_state_ready, test_state_executing];
function run_test()
{
setup();
for (var i = 0; i < tests.length; i++)
tests[i]();
cleanup();
}

View File

@ -0,0 +1,228 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Storage Test Code.
*
* The Initial Developer of the Original Code is
* Mozilla Corporation.
* Portions created by the Initial Developer are Copyright (C) 2007
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Shawn Wilsher <me@shawnwilsher.com> (Original Author)
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
// This file tests the functions of mozIStorageValueArray
function setup()
{
getOpenedDatabase().createTable("test", "id INTEGER PRIMARY KEY, name TEXT," +
"number REAL, nuller NULL, blobber BLOB");
var stmt = createStatement("INSERT INTO test (name, number, blobber) " +
"VALUES (?1, ?2, ?3)");
stmt.bindUTF8StringParameter(0, "foo");
stmt.bindDoubleParameter(1, 2.34);
stmt.bindBlobParameter(2, [], 0);
stmt.execute();
stmt.bindStringParameter(0, "");
stmt.bindDoubleParameter(1, 1.23);
stmt.bindBlobParameter(2, [1, 2], 2);
stmt.execute();
stmt.reset();
}
function test_getIsNull_for_null()
{
var stmt = createStatement("SELECT nuller, blobber FROM test WHERE id = ?1");
stmt.bindInt32Parameter(0, 1);
do_check_true(stmt.executeStep());
do_check_true(stmt.getIsNull(0)); // null field
do_check_true(stmt.getIsNull(1)); // data is null if size is 0
stmt.reset();
}
function test_getIsNull_for_non_null()
{
var stmt = createStatement("SELECT name, blobber FROM test WHERE id = ?1");
stmt.bindInt32Parameter(0, 2);
do_check_true(stmt.executeStep());
do_check_false(stmt.getIsNull(0));
do_check_false(stmt.getIsNull(1));
stmt.reset();
}
function test_value_type_null()
{
var stmt = createStatement("SELECT nuller FROM test WHERE id = ?1");
stmt.bindInt32Parameter(0, 1);
do_check_true(stmt.executeStep());
do_check_eq(Ci.mozIStorageValueArray.VALUE_TYPE_NULL,
stmt.getTypeOfIndex(0));
stmt.reset();
}
function test_value_type_integer()
{
var stmt = createStatement("SELECT id FROM test WHERE id = ?1");
stmt.bindInt32Parameter(0, 1);
do_check_true(stmt.executeStep());
do_check_eq(Ci.mozIStorageValueArray.VALUE_TYPE_INTEGER,
stmt.getTypeOfIndex(0));
stmt.reset();
}
function test_value_type_float()
{
var stmt = createStatement("SELECT number FROM test WHERE id = ?1");
stmt.bindInt32Parameter(0, 1);
do_check_true(stmt.executeStep());
do_check_eq(Ci.mozIStorageValueArray.VALUE_TYPE_FLOAT,
stmt.getTypeOfIndex(0));
stmt.reset();
}
function test_value_type_text()
{
var stmt = createStatement("SELECT name FROM test WHERE id = ?1");
stmt.bindInt32Parameter(0, 1);
do_check_true(stmt.executeStep());
do_check_eq(Ci.mozIStorageValueArray.VALUE_TYPE_TEXT,
stmt.getTypeOfIndex(0));
stmt.reset();
}
function test_value_type_blob()
{
var stmt = createStatement("SELECT blobber FROM test WHERE id = ?1");
stmt.bindInt32Parameter(0, 2);
do_check_true(stmt.executeStep());
do_check_eq(Ci.mozIStorageValueArray.VALUE_TYPE_BLOB,
stmt.getTypeOfIndex(0));
stmt.reset();
}
function test_numEntries_one()
{
var stmt = createStatement("SELECT blobber FROM test WHERE id = ?1");
stmt.bindInt32Parameter(0, 2);
do_check_true(stmt.executeStep());
do_check_eq(1, stmt.numEntries);
stmt.reset();
}
function test_numEntries_all()
{
var stmt = createStatement("SELECT * FROM test WHERE id = ?1");
stmt.bindInt32Parameter(0, 2);
do_check_true(stmt.executeStep());
do_check_eq(5, stmt.numEntries);
stmt.reset();
}
function test_getInt()
{
var stmt = createStatement("SELECT id FROM test WHERE id = ?1");
stmt.bindInt32Parameter(0, 2);
do_check_true(stmt.executeStep());
do_check_eq(2, stmt.getInt32(0));
do_check_eq(2, stmt.getInt64(0));
stmt.reset();
}
function test_getDouble()
{
var stmt = createStatement("SELECT number FROM test WHERE id = ?1");
stmt.bindInt32Parameter(0, 2);
do_check_true(stmt.executeStep());
do_check_eq(1.23, stmt.getDouble(0));
stmt.reset();
}
function test_getUTF8String()
{
var stmt = createStatement("SELECT name FROM test WHERE id = ?1");
stmt.bindInt32Parameter(0, 1);
do_check_true(stmt.executeStep());
do_check_eq("foo", stmt.getUTF8String(0));
stmt.reset();
}
function test_getString()
{
var stmt = createStatement("SELECT name FROM test WHERE id = ?1");
stmt.bindInt32Parameter(0, 2);
do_check_true(stmt.executeStep());
do_check_eq("", stmt.getString(0));
stmt.reset();
}
function test_getBlob()
{
var stmt = createStatement("SELECT blobber FROM test WHERE id = ?1");
stmt.bindInt32Parameter(0, 2);
do_check_true(stmt.executeStep());
var count = { value: 0 };
var arr = { value: null };
stmt.getBlob(0, count, arr);
do_check_eq(2, count.value);
do_check_eq(1, arr.value[0]);
do_check_eq(2, arr.value[1]);
stmt.reset();
}
var tests = [test_getIsNull_for_null, test_getIsNull_for_non_null,
test_value_type_null, test_value_type_integer,
test_value_type_float, test_value_type_text, test_value_type_blob,
test_numEntries_one, test_numEntries_all, test_getInt,
test_getDouble, test_getUTF8String, test_getString, test_getBlob];
function run_test()
{
setup();
for (var i = 0; i < tests.length; i++)
tests[i]();
cleanup();
}