X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fpq-gateway;a=blobdiff_plain;f=tests%2Flib%2Fpq%2FGateway%2FRowTest.php;h=6a738d3b4e54dae0f58be6d3f8141c57809e101b;hp=28d2728c006817ad09e59470d492ed8abf93d666;hb=3c8b32baaac62855e2c9f5bfdb5ede9685ce2b76;hpb=4879955d1b86d606dc24401f26ebde9be7612fbf diff --git a/tests/lib/pq/Gateway/RowTest.php b/tests/lib/pq/Gateway/RowTest.php index 28d2728..6a738d3 100644 --- a/tests/lib/pq/Gateway/RowTest.php +++ b/tests/lib/pq/Gateway/RowTest.php @@ -17,16 +17,18 @@ class RowTest extends \PHPUnit_Framework_TestCase { protected $table; protected function setUp() { - $this->conn = new \pq\Connection(PQ_DSN); - $this->conn->exec(PQ_TEST_DROP_TABLE); - $this->conn->exec(PQ_TEST_CREATE_TABLE); - $this->conn->exec(PQ_TEST_CREATE_DATA); - - $this->table = new Table(PQ_TEST_TABLE_NAME, $this->conn); + $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); + Table::$defaultConnection = $this->conn; + $this->table = new Table("test"); + $this->table->getQueryExecutor()->attach(new \QueryLogger()); } protected function tearDown() { - $this->conn->exec(PQ_TEST_DROP_TABLE); + $this->conn->exec(PQ_TEST_REFTABLE_DROP); + $this->conn->exec(PQ_TEST_TABLE_DROP); } function testBasic() { @@ -50,4 +52,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()); + } + } + } }