X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=tests%2Flib%2Fpq%2FGateway%2FRowTest.php;h=1d339639c27d5cda471ce5a68589750b9a3e1b23;hb=b39e14404cfeac177d41b152690b6adbb2b1e4bf;hp=fc0809468ea0a7aa2f804de834263ab8c410e5ab;hpb=0e2eb1f13ef60ce9a8709354136c42f7d87b2345;p=m6w6%2Fpq-gateway diff --git a/tests/lib/pq/Gateway/RowTest.php b/tests/lib/pq/Gateway/RowTest.php index fc08094..1d33963 100644 --- a/tests/lib/pq/Gateway/RowTest.php +++ b/tests/lib/pq/Gateway/RowTest.php @@ -18,26 +18,24 @@ class RowTest extends \PHPUnit_Framework_TestCase { protected function setUp() { $this->conn = new \pq\Connection(PQ_TEST_DSN); - $this->conn->exec(PQ_TEST_TABLE_CREATE); - $this->conn->exec(PQ_TEST_REFTABLE_CREATE); - $this->conn->exec(PQ_TEST_DATA); + $this->conn->exec(PQ_TEST_SETUP_SQL); Table::$defaultConnection = $this->conn; $this->table = new Table("test"); + $this->table->getQueryExecutor()->attach(new \QueryLogger()); } protected function tearDown() { - $this->conn->exec(PQ_TEST_REFTABLE_DROP); - $this->conn->exec(PQ_TEST_TABLE_DROP); + $this->conn->exec(PQ_TEST_TEARDOWN_SQL); } function testBasic() { $row = new Row($this->table, array("id" => 3), true); $this->assertTrue($row->isDirty()); $row->refresh(); - $this->assertSame( + $this->assertEquals( array( "id" => "3", - "created" => date("Y-m-d H:i:s", strtotime("tomorrow")), + "created" => new \pq\DateTime("tomorrow"), "counter" => "1", "number" => "1.1", "data" => "tomorrow" @@ -51,4 +49,58 @@ class RowTest extends \PHPUnit_Framework_TestCase { $row = new Row($this->table); $this->assertSame($this->table, $row->getTable()); } + + function testPessimisticLock() { + $this->table->setLock(new Table\PessimisticLock); + $txn = $this->table->getConnection()->startTransaction(); + $row = $this->table->find(null, null, 1)->current(); + $row->data = "foo"; + $row->update(); + $txn->commit(); + $this->assertSame("foo", $row->data->get()); + } + + function testPessimisticLockFail() { + $this->table->setLock(new Table\PessimisticLock); + $txn = $this->table->getConnection()->startTransaction(); + $row = $this->table->find(null, null, 1)->current(); + $row->data = "foo"; + executeInConcurrentTransaction( + $this->table->getQueryExecutor(), + "UPDATE {$this->table->getName()} SET data='bar' WHERE id=\$1", + array($row->id->get())); + $this->setExpectedException("\\UnexpectedValueException", "Row has already been modified"); + $row->update(); + $txn->commit(); + } + + function testOptimisticLock() { + $this->table->setLock(new Table\OptimisticLock("counter")); + $row = $this->table->find(null, null, 1)->current(); + $cnt = $row->counter->get(); + $row->data = "foo"; + $row->update(); + $this->assertEquals("foo", $row->data->get()); + $this->assertEquals($cnt +1, $row->counter->get()); + } + + function testOptimisticLockFail() { + $this->table->setLock(new Table\OptimisticLock("counter")); + $row = $this->table->find(null, null, 1)->current(); + $row->data = "foo"; + executeInConcurrentTransaction( + $this->table->getQueryExecutor(), + "UPDATE {$this->table->getName()} SET counter = 10 WHERE id=\$1", + array($row->id->get())); + $this->setExpectedException("\\UnexpectedValueException", "No row updated"); + $row->update(); + } + + function testRef() { + foreach ($this->table->find() as $row) { + foreach ($row->reftest() as $ref) { + $this->assertEquals($row->id->get(), $ref->test->current()->id->get()); + } + } + } }