Skip to content

Commit

Permalink
Additional unit tests for error handling with executeModify()
Browse files Browse the repository at this point in the history
  • Loading branch information
probertson committed Aug 17, 2010
1 parent 83c131e commit 378b451
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tests/sql/InsertError.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
INSERT INTO main.testTable
(
foo, -- doesn't exist, so throws an error
colInt
)
VALUES
(
:colString,
:colInt
)
179 changes: 179 additions & 0 deletions tests/tests/com/probertson/data/SQLRunnerExecuteModifyErrors.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
package tests.com.probertson.data
{
import com.probertson.data.QueuedStatement;
import com.probertson.data.SQLRunner;

import events.ExecuteModifyErrorEvent;
import events.ExecuteResultEvent;

import flash.data.SQLResult;
import flash.errors.SQLError;
import flash.events.EventDispatcher;
import flash.filesystem.File;

import flexunit.framework.Assert;

import org.flexunit.async.Async;

import utils.CreateDatabase;

public class SQLRunnerExecuteModifyErrors extends EventDispatcher
{
// Reference declaration for class to test
private var _sqlRunner:SQLRunner;


// ------- Instance vars -------

private var _dbFile:File;
private var _errorCount:int = 0;
private var _expectedErrors:int = 0;


// ------- Setup/cleanup -------

[Before]
public function setUp():void
{
_dbFile = File.createTempDirectory().resolvePath("test.db");
var createDB:CreateDatabase = new CreateDatabase(_dbFile);
createDB.createDatabase();

_errorCount = 0;
_expectedErrors = 0;
}


[After(async, timeout="250")]
public function tearDown():void
{
_sqlRunner.close(sqlRunner_close);
}

private function sqlRunner_close():void
{
_sqlRunner = null;
var tempDir:File = _dbFile.parent;
tempDir.deleteDirectory(true);
}


// ------- Tests -------

// ----- One statment in a batch -----

[Ignore("This test doesn't really work, since it will automatically succeed when the first error hits so it doesn't really test if multiple errors are thrown.")]
[Test(async, timeout="500")]
public function testOneStatementErrorThrowing():void
{
addEventListener(ExecuteModifyErrorEvent.ERROR, Async.asyncHandler(this, testOneStatementErrorThrowing_result2, 500));

_sqlRunner = new SQLRunner(_dbFile);
var stmt:QueuedStatement = new QueuedStatement(INSERT_ERROR_SQL, {colString:"Hello", colInt:7});
_sqlRunner.executeModify(Vector.<QueuedStatement>([stmt]), testOneStatementErrorThrowing_result, testOneStatementErrorThrowing_error);
}

// --- handlers ---

private function testOneStatementErrorThrowing_error(error:SQLError):void
{
dispatchEvent(new ExecuteModifyErrorEvent(ExecuteModifyErrorEvent.ERROR, error));
}

private function testOneStatementErrorThrowing_result(results:Vector.<SQLResult>):void
{
Assert.fail("Expected an error but none occurred");
}

private function testOneStatementErrorThrowing_result2(event:ExecuteModifyErrorEvent, passThroughData:Object):void
{
Assert.assertTrue(true);
}


// ----- Multiple statements in a batch -----

[Ignore("This test doesn't really work, since it will automatically succeed when the first error hits so it doesn't really test if multiple errors are thrown.")]
[Test(async, timeout="500")]
public function testMultipleStatementsErrorThrowing():void
{
addEventListener(ExecuteModifyErrorEvent.ERROR, Async.asyncHandler(this, testMultipleStatementsErrorThrowing_result2, 500));

_sqlRunner = new SQLRunner(_dbFile);
var stmt:QueuedStatement = new QueuedStatement(INSERT_ERROR_SQL, {colString:"Hello", colInt:7});
var stmt2:QueuedStatement = new QueuedStatement(INSERT_ERROR_SQL, {colString:"Hello", colInt:7});
_sqlRunner.executeModify(Vector.<QueuedStatement>([stmt, stmt2]), testMultipleStatementsErrorThrowing_result, testMultipleStatementsErrorThrowing_error);
_expectedErrors = 1;
}

// --- handlers ---

private function testMultipleStatementsErrorThrowing_error(error:SQLError):void
{
_errorCount++;

if (_errorCount == _expectedErrors)
dispatchEvent(new ExecuteModifyErrorEvent(ExecuteModifyErrorEvent.ERROR, error));
}

private function testMultipleStatementsErrorThrowing_result(results:Vector.<SQLResult>):void
{
Assert.fail("Expected an error but none occurred");
}

private function testMultipleStatementsErrorThrowing_result2(event:ExecuteModifyErrorEvent, passThroughData:Object):void
{
Assert.assertTrue(true);
}


// ----- Multiple statements in a batch, followed by a SELECT -----

[Test(async, timeout="500")]
public function testMultipleStatementsPlusSelectErrorHandling():void
{
addEventListener(ExecuteResultEvent.RESULT, Async.asyncHandler(this, testMultipleStatementsPlusSelectErrorHandling_result2, 500));

_sqlRunner = new SQLRunner(_dbFile);
var stmt:QueuedStatement = new QueuedStatement(INSERT_ERROR_SQL, {colString:"Hello", colInt:7});
var stmt2:QueuedStatement = new QueuedStatement(INSERT_ERROR_SQL, {colString:"Hello", colInt:7});
_sqlRunner.executeModify(Vector.<QueuedStatement>([stmt, stmt2]), testMultipleStatementsPlusSelectErrorHandling_executeModifyResult, testMultipleStatementsPlusSelectErrorHandling_error);
_expectedErrors = 1;
_sqlRunner.execute(LOAD_ROWS_LIMIT_SQL, null, testMultipleStatementsPlusSelectErrorHandling_executeResult);
}

// --- handlers ---

private function testMultipleStatementsPlusSelectErrorHandling_error(error:SQLError):void
{
_errorCount++;
}

private function testMultipleStatementsPlusSelectErrorHandling_executeModifyResult(results:Vector.<SQLResult>):void
{
Assert.fail("Expected an error but none occurred");
}

private function testMultipleStatementsPlusSelectErrorHandling_executeResult(result:SQLResult):void
{
dispatchEvent(new ExecuteResultEvent(ExecuteResultEvent.RESULT, result));
}

private function testMultipleStatementsPlusSelectErrorHandling_result2(event:ExecuteResultEvent, passThroughData:Object):void
{
Assert.assertEquals(_expectedErrors, _errorCount);
}


// ------- SQL statements -------

[Embed(source="sql/InsertError.sql", mimeType="application/octet-stream")]
private static const InsertErrorStatementText:Class;
private static const INSERT_ERROR_SQL:String = new InsertErrorStatementText();

[Embed(source="sql/LoadRowsLimit.sql", mimeType="application/octet-stream")]
private static const LoadRowsLimitStatementText:Class;
private static const LOAD_ROWS_LIMIT_SQL:String = new LoadRowsLimitStatementText();

}
}

0 comments on commit 378b451

Please sign in to comment.