From 9478fba18018c1f072f5f6dd4335c5828f23bc80 Mon Sep 17 00:00:00 2001 From: xue <> Date: Fri, 24 Nov 2006 04:21:06 +0000 Subject: Added DB layer. --- tests/unit/Data/TDbCommandTest.php | 131 +++++++++++++++++++++++++++++++++ tests/unit/Data/TDbConnectionTest.php | 114 ++++++++++++++++++++++++++++ tests/unit/Data/TDbDataReaderTest.php | 114 ++++++++++++++++++++++++++++ tests/unit/Data/TDbTransactionTest.php | 73 ++++++++++++++++++ 4 files changed, 432 insertions(+) create mode 100644 tests/unit/Data/TDbCommandTest.php create mode 100644 tests/unit/Data/TDbConnectionTest.php create mode 100644 tests/unit/Data/TDbDataReaderTest.php create mode 100644 tests/unit/Data/TDbTransactionTest.php (limited to 'tests') diff --git a/tests/unit/Data/TDbCommandTest.php b/tests/unit/Data/TDbCommandTest.php new file mode 100644 index 00000000..f0c02f75 --- /dev/null +++ b/tests/unit/Data/TDbCommandTest.php @@ -0,0 +1,131 @@ +_connection=new TDbConnection('sqlite:'.TEST_DB_FILE); + $this->_connection->Active=true; + $this->_connection->createCommand('CREATE TABLE foo (id INTEGER NOT NULL PRIMARY KEY, name VARCHAR(8))')->execute(); + $this->_connection->createCommand('INSERT TABLE foo (name) VALUES (\'my name\')')->execute; + } + + public function tearDown() + { + $this->_connection=null; + } + + public function testGetText() + { + $sql='SELECT * FROM foo'; + $command=$this->_connection->createCommand($sql); + $this->assertEquals($command->Text,$sql); + } + + public function testSetText() + { + $sql='SELECT * FROM foo'; + $newSql='SELECT id FROM foo'; + $command=$this->_connection->createCommand($sql); + $command->query()->read(); + $command->Text=$newSql; + $this->assertEquals($command->Text,$newSql); + $row=$command->query()->read(); + $this->assertEquals($row['id'],1); + } + +/* + public function testActive() + { + $this->assertFalse($this->_connection2->Active); + + $this->_connection2->Active=true; + $this->assertTrue($this->_connection2->Active); + $pdo=$this->_connection2->PdoInstance; + // test setting Active repeatedly doesn't re-connect DB + $this->_connection2->Active=true; + $this->assertTrue($pdo===$this->_connection2->PdoInstance); + + $this->_connection2->Active=false; + $this->assertFalse($this->_connection2->Active); + } + + public function testCreateCommand() + { + $sql='CREATE TABLE foo (id INTEGER NOT NULL PRIMARY KEY, name VARCHAR(8))'; + try + { + $this->_connection2->createCommand($sql); + $this->fail('Expected exception is not raised'); + } + catch(TDbException $e) + { + } + + $command=$this->_connection->createCommand($sql); + $this->assertTrue($command instanceof TDbCommand); + } + + public function testBeginTransaction() + { + $sql='INSERT INTO foo(id,name) VALUES (1,\'my name\')'; + $transaction=$this->_connection->beginTransaction(); + try + { + $this->_connection->createCommand($sql)->execute(); + $this->_connection->createCommand($sql)->execute(); + $this->fail('Expected exception not raised'); + $transaction->commit(); + } + catch(Exception $e) + { + $transaction->rollBack(); + $reader=$this->_connection->createCommand('SELECT * FROM foo')->query(); + $this->assertFalse($reader->read()); + } + } + + public function testLastInsertID() + { + $sql='INSERT INTO foo(name) VALUES (\'my name\')'; + $this->_connection->createCommand($sql)->execute(); + $value=$this->_connection->LastInsertID; + $this->assertEquals($this->_connection->LastInsertID,'1'); + } + + public function testQuoteString() + { + $str="this is 'my' name"; + $expectedStr="'this is ''my'' name'"; + $this->assertEquals($expectedStr,$this->_connection->quoteString($str)); + } + + public function testColumnNameCase() + { + $this->assertEquals(TDbColumnCaseMode::Preserved,$this->_connection->ColumnCase); + $this->_connection->ColumnCase=TDbColumnCaseMode::LowerCase; + $this->assertEquals(TDbColumnCaseMode::LowerCase,$this->_connection->ColumnCase); + } + + public function testNullConversion() + { + $this->assertEquals(TDbNullConversionMode::Preserved,$this->_connection->NullConversion); + $this->_connection->NullConversion=TDbNullConversionMode::NullToEmptyString; + $this->assertEquals(TDbNullConversionMode::NullToEmptyString,$this->_connection->NullConversion); + } + */ +} + +?> \ No newline at end of file diff --git a/tests/unit/Data/TDbConnectionTest.php b/tests/unit/Data/TDbConnectionTest.php new file mode 100644 index 00000000..21d6730b --- /dev/null +++ b/tests/unit/Data/TDbConnectionTest.php @@ -0,0 +1,114 @@ +_connection1=new TDbConnection('sqlite:'.TEST_DB_FILE); + $this->_connection1->Active=true; + $this->_connection1->createCommand('CREATE TABLE foo (id INTEGER NOT NULL PRIMARY KEY, name VARCHAR(8))')->execute(); + $this->_connection2=new TDbConnection('sqlite:'.TEST_DB_FILE2); + } + + public function tearDown() + { + $this->_connection1=null; + $this->_connection2=null; + } + + public function testActive() + { + $this->assertFalse($this->_connection2->Active); + + $this->_connection2->Active=true; + $this->assertTrue($this->_connection2->Active); + $pdo=$this->_connection2->PdoInstance; + // test setting Active repeatedly doesn't re-connect DB + $this->_connection2->Active=true; + $this->assertTrue($pdo===$this->_connection2->PdoInstance); + + $this->_connection2->Active=false; + $this->assertFalse($this->_connection2->Active); + } + + public function testCreateCommand() + { + $sql='CREATE TABLE foo (id INTEGER NOT NULL PRIMARY KEY, name VARCHAR(8))'; + try + { + $this->_connection2->createCommand($sql); + $this->fail('Expected exception is not raised'); + } + catch(TDbException $e) + { + } + + $command=$this->_connection1->createCommand($sql); + $this->assertTrue($command instanceof TDbCommand); + } + + public function testBeginTransaction() + { + $sql='INSERT INTO foo(id,name) VALUES (1,\'my name\')'; + $transaction=$this->_connection1->beginTransaction(); + try + { + $this->_connection1->createCommand($sql)->execute(); + $this->_connection1->createCommand($sql)->execute(); + $this->fail('Expected exception not raised'); + $transaction->commit(); + } + catch(Exception $e) + { + $transaction->rollBack(); + $reader=$this->_connection1->createCommand('SELECT * FROM foo')->query(); + $this->assertFalse($reader->read()); + } + } + + public function testLastInsertID() + { + $sql='INSERT INTO foo(name) VALUES (\'my name\')'; + $this->_connection1->createCommand($sql)->execute(); + $value=$this->_connection1->LastInsertID; + $this->assertEquals($this->_connection1->LastInsertID,'1'); + } + + public function testQuoteString() + { + $str="this is 'my' name"; + $expectedStr="'this is ''my'' name'"; + $this->assertEquals($expectedStr,$this->_connection1->quoteString($str)); + } + + public function testColumnNameCase() + { + $this->assertEquals(TDbColumnCaseMode::Preserved,$this->_connection1->ColumnCase); + $this->_connection1->ColumnCase=TDbColumnCaseMode::LowerCase; + $this->assertEquals(TDbColumnCaseMode::LowerCase,$this->_connection1->ColumnCase); + } + + public function testNullConversion() + { + $this->assertEquals(TDbNullConversionMode::Preserved,$this->_connection1->NullConversion); + $this->_connection1->NullConversion=TDbNullConversionMode::NullToEmptyString; + $this->assertEquals(TDbNullConversionMode::NullToEmptyString,$this->_connection1->NullConversion); + } +} + +?> \ No newline at end of file diff --git a/tests/unit/Data/TDbDataReaderTest.php b/tests/unit/Data/TDbDataReaderTest.php new file mode 100644 index 00000000..21d6730b --- /dev/null +++ b/tests/unit/Data/TDbDataReaderTest.php @@ -0,0 +1,114 @@ +_connection1=new TDbConnection('sqlite:'.TEST_DB_FILE); + $this->_connection1->Active=true; + $this->_connection1->createCommand('CREATE TABLE foo (id INTEGER NOT NULL PRIMARY KEY, name VARCHAR(8))')->execute(); + $this->_connection2=new TDbConnection('sqlite:'.TEST_DB_FILE2); + } + + public function tearDown() + { + $this->_connection1=null; + $this->_connection2=null; + } + + public function testActive() + { + $this->assertFalse($this->_connection2->Active); + + $this->_connection2->Active=true; + $this->assertTrue($this->_connection2->Active); + $pdo=$this->_connection2->PdoInstance; + // test setting Active repeatedly doesn't re-connect DB + $this->_connection2->Active=true; + $this->assertTrue($pdo===$this->_connection2->PdoInstance); + + $this->_connection2->Active=false; + $this->assertFalse($this->_connection2->Active); + } + + public function testCreateCommand() + { + $sql='CREATE TABLE foo (id INTEGER NOT NULL PRIMARY KEY, name VARCHAR(8))'; + try + { + $this->_connection2->createCommand($sql); + $this->fail('Expected exception is not raised'); + } + catch(TDbException $e) + { + } + + $command=$this->_connection1->createCommand($sql); + $this->assertTrue($command instanceof TDbCommand); + } + + public function testBeginTransaction() + { + $sql='INSERT INTO foo(id,name) VALUES (1,\'my name\')'; + $transaction=$this->_connection1->beginTransaction(); + try + { + $this->_connection1->createCommand($sql)->execute(); + $this->_connection1->createCommand($sql)->execute(); + $this->fail('Expected exception not raised'); + $transaction->commit(); + } + catch(Exception $e) + { + $transaction->rollBack(); + $reader=$this->_connection1->createCommand('SELECT * FROM foo')->query(); + $this->assertFalse($reader->read()); + } + } + + public function testLastInsertID() + { + $sql='INSERT INTO foo(name) VALUES (\'my name\')'; + $this->_connection1->createCommand($sql)->execute(); + $value=$this->_connection1->LastInsertID; + $this->assertEquals($this->_connection1->LastInsertID,'1'); + } + + public function testQuoteString() + { + $str="this is 'my' name"; + $expectedStr="'this is ''my'' name'"; + $this->assertEquals($expectedStr,$this->_connection1->quoteString($str)); + } + + public function testColumnNameCase() + { + $this->assertEquals(TDbColumnCaseMode::Preserved,$this->_connection1->ColumnCase); + $this->_connection1->ColumnCase=TDbColumnCaseMode::LowerCase; + $this->assertEquals(TDbColumnCaseMode::LowerCase,$this->_connection1->ColumnCase); + } + + public function testNullConversion() + { + $this->assertEquals(TDbNullConversionMode::Preserved,$this->_connection1->NullConversion); + $this->_connection1->NullConversion=TDbNullConversionMode::NullToEmptyString; + $this->assertEquals(TDbNullConversionMode::NullToEmptyString,$this->_connection1->NullConversion); + } +} + +?> \ No newline at end of file diff --git a/tests/unit/Data/TDbTransactionTest.php b/tests/unit/Data/TDbTransactionTest.php new file mode 100644 index 00000000..9d34857d --- /dev/null +++ b/tests/unit/Data/TDbTransactionTest.php @@ -0,0 +1,73 @@ +_connection=new TDbConnection('sqlite:'.TEST_DB_FILE); + $this->_connection->Active=true; + $this->_connection->createCommand('CREATE TABLE foo (id INTEGER NOT NULL PRIMARY KEY, name VARCHAR(8))')->execute(); + } + + public function tearDown() + { + $this->_connection=null; + } + + public function testRollBack() + { + $sql='INSERT INTO foo(id,name) VALUES (1,\'my name\')'; + $transaction=$this->_connection->beginTransaction(); + try + { + $this->_connection->createCommand($sql)->execute(); + $this->_connection->createCommand($sql)->execute(); + $this->fail('Expected exception not raised'); + $transaction->commit(); + } + catch(Exception $e) + { + $this->assertTrue($transaction->Active); + $transaction->rollBack(); + $this->assertFalse($transaction->Active); + $reader=$this->_connection->createCommand('SELECT * FROM foo')->query(); + $this->assertFalse($reader->read()); + } + } + + public function testCommit() + { + $sql1='INSERT INTO foo(id,name) VALUES (1,\'my name\')'; + $sql2='INSERT INTO foo(id,name) VALUES (2,\'my name\')'; + $transaction=$this->_connection->beginTransaction(); + try + { + $this->_connection->createCommand($sql1)->execute(); + $this->_connection->createCommand($sql2)->execute(); + $this->assertTrue($transaction->Active); + $transaction->commit(); + $this->assertFalse($transaction->Active); + } + catch(Exception $e) + { + $transaction->rollBack(); + $this->fail('Unexpected exception'); + } + $result=$this->_connection->createCommand('SELECT * FROM foo')->query()->readAll(); + $this->assertEquals(count($result),2); + } +} + +?> \ No newline at end of file -- cgit v1.2.3