Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tests and fixes for issue #1 (Calling SQLRunner.executeModify() with …
…same statement multiple times throws error)
- Loading branch information
1 parent
9b81069
commit 09f2e5c
Showing
8 changed files
with
324 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package events | ||
{ | ||
import flash.errors.SQLError; | ||
import flash.events.Event; | ||
|
||
public class ExecuteModifyErrorEvent extends Event | ||
{ | ||
// ------- Event type constants ------- | ||
|
||
public static const ERROR:String = "executeModifyError"; | ||
|
||
|
||
// ------- Constructor ------- | ||
|
||
public function ExecuteModifyErrorEvent(type:String, error:SQLError, bubbles:Boolean=false, cancelable:Boolean=false) | ||
{ | ||
super(type, bubbles, cancelable); | ||
this.error = error; | ||
} | ||
|
||
|
||
// ------- Public properties ------- | ||
|
||
public var error:SQLError; | ||
|
||
|
||
// ------- Event overrides ------- | ||
|
||
public override function clone():Event | ||
{ | ||
return new ExecuteModifyErrorEvent(type, error, bubbles, cancelable); | ||
} | ||
|
||
|
||
public override function toString():String | ||
{ | ||
return formatToString("ExecuteModifyErrorEvent", "type", "bubbles", "cancelable", "eventPhase"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package events | ||
{ | ||
import flash.data.SQLResult; | ||
import flash.events.Event; | ||
|
||
public class ExecuteModifyResultEvent extends Event | ||
{ | ||
// ------- Event type constants ------- | ||
|
||
public static const RESULT:String = "executeModifyResult"; | ||
|
||
|
||
// ------- Constructor ------- | ||
|
||
public function ExecuteModifyResultEvent(type:String, results:Vector.<SQLResult>, bubbles:Boolean=false, cancelable:Boolean=false) | ||
{ | ||
super(type, bubbles, cancelable); | ||
this.results = results; | ||
} | ||
|
||
|
||
// ------- Public properties ------- | ||
|
||
public var results:Vector.<SQLResult>; | ||
|
||
|
||
// ------- Event overrides ------- | ||
|
||
public override function clone():Event | ||
{ | ||
return new ExecuteModifyResultEvent(type, results, bubbles, cancelable); | ||
} | ||
|
||
|
||
public override function toString():String | ||
{ | ||
return formatToString("ExecuteModifyResultEvent", "type", "bubbles", "cancelable", "eventPhase"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
INSERT INTO main.testTable | ||
( | ||
colString, | ||
colInt | ||
) | ||
VALUES | ||
( | ||
:colString, | ||
:colInt | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
CREATE TABLE main.testTable | ||
( | ||
colIntPK int PRIMARY KEY AUTOINCREMENT, | ||
colString String NOT NULL, | ||
colInt int | ||
) |
154 changes: 154 additions & 0 deletions
154
tests/tests/com/probertson/data/SQLRunnerExecuteModifyTest.as
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package tests.com.probertson.data | ||
{ | ||
import com.probertson.data.QueuedStatement; | ||
import com.probertson.data.SQLRunner; | ||
import events.ExecuteModifyErrorEvent; | ||
import events.ExecuteModifyResultEvent; | ||
import flash.data.SQLConnection; | ||
import flash.data.SQLResult; | ||
import flash.data.SQLStatement; | ||
import flash.errors.SQLError; | ||
import flash.events.Event; | ||
import flash.events.EventDispatcher; | ||
import flash.filesystem.File; | ||
import flexunit.framework.Assert; | ||
import org.flexunit.async.Async; | ||
import utils.CreateDatabase; | ||
|
||
public class SQLRunnerExecuteModifyTest extends EventDispatcher | ||
{ | ||
// Reference declaration for class to test | ||
private var _sqlRunner:com.probertson.data.SQLRunner; | ||
|
||
|
||
public function SQLRunnerExecuteModifyTest() | ||
{ | ||
} | ||
|
||
|
||
// ------- Instance vars ------- | ||
|
||
private var _dbFile:File; | ||
|
||
|
||
// ------- Setup/cleanup ------- | ||
|
||
[Before] | ||
public function setup():void | ||
{ | ||
_dbFile = File.createTempDirectory().resolvePath("test.db"); | ||
var createDB:CreateDatabase = new CreateDatabase(_dbFile); | ||
createDB.createDatabase(); | ||
} | ||
|
||
|
||
[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 ------- | ||
|
||
[Test(async, timeout="500")] | ||
public function testOneStatement():void | ||
{ | ||
addEventListener(ExecuteModifyResultEvent.RESULT, Async.asyncHandler(this, testOneStatement_result2, 500)); | ||
|
||
_sqlRunner = new SQLRunner(_dbFile); | ||
var stmt:QueuedStatement = new QueuedStatement(ADD_ROW_SQL, {colString:"Hello", colInt:7}); | ||
_sqlRunner.executeModify(Vector.<QueuedStatement>([stmt]), testOneStatement_result, testOneStatement_error); | ||
} | ||
|
||
// --- handlers --- | ||
|
||
private function testOneStatement_result(results:Vector.<SQLResult>):void | ||
{ | ||
dispatchEvent(new ExecuteModifyResultEvent(ExecuteModifyResultEvent.RESULT, results)); | ||
} | ||
|
||
private function testOneStatement_result2(event:ExecuteModifyResultEvent, passThroughData:Object):void | ||
{ | ||
Assert.assertEquals(1, event.results.length); | ||
Assert.assertEquals(1, event.results[0].rowsAffected); | ||
} | ||
|
||
private function testOneStatement_error(error:SQLError):void | ||
{ | ||
Assert.fail(error.message); | ||
} | ||
|
||
|
||
[Test(async, timeout="500")] | ||
public function testReuseStatement():void | ||
{ | ||
addEventListener(ExecuteModifyResultEvent.RESULT, Async.asyncHandler(this, testReuseStatement_result2, 500)); | ||
|
||
_sqlRunner = new SQLRunner(_dbFile); | ||
var stmt1:QueuedStatement = new QueuedStatement(ADD_ROW_SQL, {colString:"Hello", colInt:7}); | ||
var stmt2:QueuedStatement = new QueuedStatement(ADD_ROW_SQL, {colString:"World", colInt:17}); | ||
_sqlRunner.executeModify(Vector.<QueuedStatement>([stmt1, stmt2]), testReuseStatement_result, testReuseStatement_error); | ||
} | ||
|
||
// --- handlers --- | ||
|
||
private function testReuseStatement_result(results:Vector.<SQLResult>):void | ||
{ | ||
dispatchEvent(new ExecuteModifyResultEvent(ExecuteModifyResultEvent.RESULT, results)); | ||
} | ||
|
||
private function testReuseStatement_result2(event:ExecuteModifyResultEvent, passThroughData:Object):void | ||
{ | ||
// verify that the inserts happened | ||
Assert.assertEquals(2, event.results.length); | ||
Assert.assertEquals(1, event.results[0].rowsAffected); | ||
Assert.assertEquals(1, event.results[1].rowsAffected); | ||
|
||
// verify that the inserted data matches | ||
var id1:int = event.results[0].lastInsertRowID; | ||
var id2:int = event.results[1].lastInsertRowID; | ||
|
||
var conn:SQLConnection = new SQLConnection(); | ||
conn.open(_dbFile); | ||
|
||
var stmt:SQLStatement = new SQLStatement(); | ||
stmt.sqlConnection = conn; | ||
stmt.text = "SELECT colString, colInt FROM main.testTable WHERE colIntPK = :colIntPK"; | ||
var result:SQLResult; | ||
|
||
stmt.parameters[":colIntPK"] = id1; | ||
stmt.execute(); | ||
result = stmt.getResult(); | ||
Assert.assertEquals("Hello", result.data[0].colString); | ||
Assert.assertEquals(7, result.data[0].colInt); | ||
|
||
stmt.parameters[":colIntPK"] = id2; | ||
stmt.execute(); | ||
result = stmt.getResult(); | ||
Assert.assertEquals("World", result.data[0].colString); | ||
Assert.assertEquals(17, result.data[0].colInt); | ||
|
||
conn.close(); | ||
} | ||
|
||
private function testReuseStatement_error(error:SQLError):void | ||
{ | ||
Assert.fail(error.message); | ||
} | ||
|
||
|
||
// ------- SQL statements ------- | ||
|
||
[Embed(source="sql/AddRow.sql", mimeType="application/octet-stream")] | ||
private static const AddRowStatementText:Class; | ||
private static const ADD_ROW_SQL:String = new AddRowStatementText(); | ||
} | ||
} |
Oops, something went wrong.