From 325fc2818277e2417e2c1ad74a003447ba0e6692 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Sun, 24 Nov 2013 22:46:13 +0100 Subject: [PATCH] several fixes and cleanup --- framework/yii/elasticsearch/ActiveRecord.php | 7 +- framework/yii/elasticsearch/Command.php | 15 +- tests/unit/data/ar/elasticsearch/Order.php | 2 +- tests/unit/framework/ar/ActiveRecordTestTrait.php | 18 +- .../framework/elasticsearch/ActiveRecordTest.php | 202 ++++++++++----------- .../elasticsearch/ElasticSearchConnectionTest.php | 3 + .../elasticsearch/ElasticSearchTestCase.php | 2 +- tests/unit/framework/elasticsearch/QueryTest.php | 3 +- 8 files changed, 137 insertions(+), 115 deletions(-) diff --git a/framework/yii/elasticsearch/ActiveRecord.php b/framework/yii/elasticsearch/ActiveRecord.php index 7ed0f59..5f58a04 100644 --- a/framework/yii/elasticsearch/ActiveRecord.php +++ b/framework/yii/elasticsearch/ActiveRecord.php @@ -51,7 +51,12 @@ class ActiveRecord extends \yii\db\ActiveRecord $query = static::createQuery(); if (is_array($q)) { if (count($q) == 1 && (array_key_exists(ActiveRecord::PRIMARY_KEY_NAME, $q))) { - return static::get($q[ActiveRecord::PRIMARY_KEY_NAME]); + $pk = $q[ActiveRecord::PRIMARY_KEY_NAME]; + if (is_array($pk)) { + return static::mget($pk); + } else { + return static::get($pk); + } } return $query->where($q)->one(); } elseif ($q !== null) { diff --git a/framework/yii/elasticsearch/Command.php b/framework/yii/elasticsearch/Command.php index 9ce4fb5..35334f4 100644 --- a/framework/yii/elasticsearch/Command.php +++ b/framework/yii/elasticsearch/Command.php @@ -87,10 +87,17 @@ class Command extends Component public function insert($index, $type, $data, $id = null, $options = []) { $body = is_array($data) ? Json::encode($data) : $data; - if ($id !== null) { - $response = $this->db->http()->put($this->createUrl([$index, $type, $id], $options), null, $body)->send(); - } else { - $response = $this->db->http()->post($this->createUrl([$index, $type], $options), null, $body)->send(); + + try { + if ($id !== null) { + $response = $this->db->http()->put($this->createUrl([$index, $type, $id], $options), null, $body)->send(); + } else { + $response = $this->db->http()->post($this->createUrl([$index, $type], $options), null, $body)->send(); + } + } catch(ClientErrorResponseException $e) { + throw new Exception("elasticsearch error:\n\n" + . $body . "\n\n" . $e->getMessage() + . print_r(Json::decode($e->getResponse()->getBody(true)), true), [], 0, $e); } return Json::decode($response->getBody(true)); } diff --git a/tests/unit/data/ar/elasticsearch/Order.php b/tests/unit/data/ar/elasticsearch/Order.php index 8c794a2..2efeb07 100644 --- a/tests/unit/data/ar/elasticsearch/Order.php +++ b/tests/unit/data/ar/elasticsearch/Order.php @@ -30,7 +30,7 @@ class Order extends ActiveRecord public function getItems() { return $this->hasMany(Item::className(), [ActiveRecord::PRIMARY_KEY_NAME => 'item_id']) - ->via('orderItems')->orderBy('name'); + ->via('orderItems')->orderBy('id'); } public function getItemsInOrder1() diff --git a/tests/unit/framework/ar/ActiveRecordTestTrait.php b/tests/unit/framework/ar/ActiveRecordTestTrait.php index ea4e961..0a1f7f1 100644 --- a/tests/unit/framework/ar/ActiveRecordTestTrait.php +++ b/tests/unit/framework/ar/ActiveRecordTestTrait.php @@ -134,6 +134,8 @@ trait ActiveRecordTestTrait $this->assertNull($customer); $customer = $this->callCustomerFind(['id' => 5]); $this->assertNull($customer); + $customer = $this->callCustomerFind(['name' => 'user5']); + $this->assertNull($customer); // find by attributes $customer = $this->callCustomerFind()->where(['name' => 'user2'])->one(); @@ -141,6 +143,7 @@ trait ActiveRecordTestTrait $this->assertEquals(2, $customer->id); // scope + $this->assertEquals(2, count($this->callCustomerFind()->active()->all())); $this->assertEquals(2, $this->callCustomerFind()->active()->count()); // asArray @@ -158,8 +161,14 @@ trait ActiveRecordTestTrait { /** @var TestCase|ActiveRecordTestTrait $this */ // query scalar - $customerName = $this->callCustomerFind()->where(array('id' => 2))->scalar('name'); + $customerName = $this->callCustomerFind()->where(['id' => 2])->scalar('name'); $this->assertEquals('user2', $customerName); + $customerName = $this->callCustomerFind()->where(['status' => 2])->scalar('name'); + $this->assertEquals('user3', $customerName); + $customerName = $this->callCustomerFind()->where(['status' => 2])->scalar('noname'); + $this->assertNull($customerName); + $customerId = $this->callCustomerFind()->where(['status' => 2])->scalar('id'); + $this->assertEquals(3, $customerId); } public function testFindColumn() @@ -365,7 +374,12 @@ trait ActiveRecordTestTrait $this->assertEquals(2, count($order->items)); $this->assertEquals(1, $order->items[0]->id); $this->assertEquals(2, $order->items[1]->id); + } + public function testFindLazyVia2() + { + /** @var TestCase|ActiveRecordTestTrait $this */ + /** @var Order $order */ $order = $this->callOrderFind(1); $order->id = 100; $this->assertEquals([], $order->items); @@ -551,7 +565,7 @@ trait ActiveRecordTestTrait $customer->save(); $this->afterSave(); - $this->assertEquals(4, $customer->id); + $this->assertNotNull($customer->id); $this->assertFalse(static::$afterSaveNewRecord); $this->assertTrue(static::$afterSaveInsert); $this->assertFalse($customer->isNewRecord); diff --git a/tests/unit/framework/elasticsearch/ActiveRecordTest.php b/tests/unit/framework/elasticsearch/ActiveRecordTest.php index c1d38c2..2264de3 100644 --- a/tests/unit/framework/elasticsearch/ActiveRecordTest.php +++ b/tests/unit/framework/elasticsearch/ActiveRecordTest.php @@ -4,6 +4,7 @@ namespace yiiunit\framework\elasticsearch; use yii\elasticsearch\Connection; use yii\elasticsearch\ActiveQuery; +use yii\helpers\Json; use yiiunit\framework\ar\ActiveRecordTestTrait; use yiiunit\data\ar\elasticsearch\ActiveRecord; use yiiunit\data\ar\elasticsearch\Customer; @@ -11,6 +12,9 @@ use yiiunit\data\ar\elasticsearch\OrderItem; use yiiunit\data\ar\elasticsearch\Order; use yiiunit\data\ar\elasticsearch\Item; +/** + * @group elasticsearch + */ class ActiveRecordTest extends ElasticSearchTestCase { use ActiveRecordTestTrait; @@ -43,6 +47,30 @@ class ActiveRecordTest extends ElasticSearchTestCase // delete all indexes $db->http()->delete('_all')->send(); + $db->http()->post('items', null, Json::encode([ + 'mappings' => [ + "item" => [ + "_source" => [ "enabled" => true ], + "properties" => [ + // allow proper sorting by name + "name" => ["type" => "string", "index" => "not_analyzed"], + ] + ] + ], + ]))->send(); + + $db->http()->post('customers', null, Json::encode([ + 'mappings' => [ + "item" => [ + "_source" => [ "enabled" => true ], + "properties" => [ + // this is for the boolean test + "status" => ["type" => "boolean"], + ] + ] + ], + ]))->send(); + $customer = new Customer(); $customer->id = 1; $customer->setAttributes(['email' => 'user1@example.com', 'name' => 'user1', 'address' => 'address1', 'status' => 1], false); @@ -115,110 +143,6 @@ class ActiveRecordTest extends ElasticSearchTestCase Customer::getDb()->createCommand()->flushIndex(); } - public function testFind() - { - // find one - $result = Customer::find(); - $this->assertTrue($result instanceof ActiveQuery); - $customer = $result->one(); - $this->assertTrue($customer instanceof Customer); - - // find all - $customers = Customer::find()->all(); - $this->assertEquals(3, count($customers)); - $this->assertTrue($customers[0] instanceof Customer); - $this->assertTrue($customers[1] instanceof Customer); - $this->assertTrue($customers[2] instanceof Customer); - - // find all asArray - $customers = Customer::find()->asArray()->all(); - $this->assertEquals(3, count($customers)); - $this->assertArrayHasKey(ActiveRecord::PRIMARY_KEY_NAME, $customers[0]); - $this->assertArrayHasKey('name', $customers[0]); - $this->assertArrayHasKey('email', $customers[0]); - $this->assertArrayHasKey('address', $customers[0]); - $this->assertArrayHasKey('status', $customers[0]); - $this->assertArrayHasKey(ActiveRecord::PRIMARY_KEY_NAME, $customers[1]); - $this->assertArrayHasKey('name', $customers[1]); - $this->assertArrayHasKey('email', $customers[1]); - $this->assertArrayHasKey('address', $customers[1]); - $this->assertArrayHasKey('status', $customers[1]); - $this->assertArrayHasKey(ActiveRecord::PRIMARY_KEY_NAME, $customers[2]); - $this->assertArrayHasKey('name', $customers[2]); - $this->assertArrayHasKey('email', $customers[2]); - $this->assertArrayHasKey('address', $customers[2]); - $this->assertArrayHasKey('status', $customers[2]); - - // find by a single primary key - $customer = Customer::find(2); - $this->assertTrue($customer instanceof Customer); - $this->assertEquals('user2', $customer->name); - $customer = Customer::find(5); - $this->assertNull($customer); - - // query scalar - $customerName = Customer::find()->where(['status' => 2])->scalar('name'); - $this->assertEquals('user3', $customerName); - $customerName = Customer::find()->where(['status' => 2])->scalar('noname'); - $this->assertNull($customerName); - $customerId = Customer::find()->where(['status' => 2])->scalar(ActiveRecord::PRIMARY_KEY_NAME); - $this->assertEquals(3, $customerId); - - // find by column values - $customer = Customer::find(['name' => 'user2']); - $this->assertTrue($customer instanceof Customer); - $this->assertEquals('user2', $customer->name); - $customer = Customer::find(['name' => 'user1', ActiveRecord::PRIMARY_KEY_NAME => 2]); - $this->assertNull($customer); - $customer = Customer::find([ActiveRecord::PRIMARY_KEY_NAME => 5]); - $this->assertNull($customer); - $customer = Customer::find(['name' => 'user5']); - $this->assertNull($customer); - - // find by attributes - $customer = Customer::find()->where(['name' => 'user2'])->one(); - $this->assertTrue($customer instanceof Customer); - $this->assertEquals('user2', $customer->name); - - // find count - $this->assertEquals(3, Customer::find()->count()); - $this->assertEquals(2, Customer::find()->where(['or', [ActiveRecord::PRIMARY_KEY_NAME => 1], [ActiveRecord::PRIMARY_KEY_NAME => 2]])->count()); -// $this->assertEquals(6, Customer::find()->sum('id')); -// $this->assertEquals(2, Customer::find()->average('id')); -// $this->assertEquals(1, Customer::find()->min('id')); -// $this->assertEquals(3, Customer::find()->max('id')); - - // scope - $this->assertEquals(2, count(Customer::find()->active()->all())); -// $this->assertEquals(2, Customer::find()->active()->count()); - - // asArray - $customer = Customer::find()->where(['name' => 'user2'])->asArray()->one(); - $this->assertEquals(array( - 'email' => 'user2@example.com', - 'name' => 'user2', - 'address' => 'address2', - 'status' => '1', - ActiveRecord::PRIMARY_KEY_NAME => 2, - ), $customer); - - // indexBy - $customers = Customer::find()->indexBy('name')->all(); - $this->assertEquals(3, count($customers)); - $this->assertTrue($customers['user1'] instanceof Customer); - $this->assertTrue($customers['user2'] instanceof Customer); - $this->assertTrue($customers['user3'] instanceof Customer); - - // indexBy callable - $customers = Customer::find()->indexBy(function ($customer) { - return $customer->status . '-' . $customer->name; - })->orderBy('name')->all(); - $this->assertEquals(3, count($customers)); - $this->assertTrue($customers['1-user1'] instanceof Customer); - $this->assertTrue($customers['1-user2'] instanceof Customer); - $this->assertTrue($customers['2-user3'] instanceof Customer); - } - public function testGetDb() { $this->mockApplication(['components' => ['elasticsearch' => Connection::className()]]); @@ -331,4 +255,74 @@ class ActiveRecordTest extends ElasticSearchTestCase $orderItem->$pkName = 13; $orderItem->save(); } + + public function testFindLazyVia2() + { + /** @var TestCase|ActiveRecordTestTrait $this */ + /** @var Order $order */ + $orderClass = $this->getOrderClass(); + $pkName = ActiveRecord::PRIMARY_KEY_NAME; + + $order = new $orderClass(); + $order->$pkName = 100; + $this->assertEquals([], $order->items); + } + + public function testUpdateCounters() + { + // Update Counters is not supported by elasticsearch +// $this->setExpectedException('yii\base\NotSupportedException'); +// ActiveRecordTestTrait::testUpdateCounters(); + } + + /** + * Some PDO implementations(e.g. cubrid) do not support boolean values. + * Make sure this does not affect AR layer. + */ + public function testBooleanAttribute() + { + $db = $this->getConnection(); + $db->createCommand()->deleteIndex('customers'); + $db->http()->post('customers', null, Json::encode([ + 'mappings' => [ + "item" => [ + "_source" => [ "enabled" => true ], + "properties" => [ + // this is for the boolean test + "status" => ["type" => "boolean"], + ] + ] + ], + ]))->send(); + + $customerClass = $this->getCustomerClass(); + $customer = new $customerClass(); + $customer->name = 'boolean customer'; + $customer->email = 'mail@example.com'; + $customer->status = true; + $customer->save(false); + + $customer->refresh(); + $this->assertEquals(true, $customer->status); + + $customer->status = false; + $customer->save(false); + + $customer->refresh(); + $this->assertEquals(false, $customer->status); + + $customer = new Customer(); + $customer->setAttributes(['email' => 'user2b@example.com', 'name' => 'user2b', 'status' => true], false); + $customer->save(false); + $customer = new Customer(); + $customer->setAttributes(['email' => 'user3b@example.com', 'name' => 'user3b', 'status' => false], false); + $customer->save(false); + $this->afterSave(); + + $customers = $this->callCustomerFind()->where(['status' => true])->all(); + $this->assertEquals(1, count($customers)); + + $customers = $this->callCustomerFind()->where(['status' => false])->all(); + $this->assertEquals(2, count($customers)); + } } \ No newline at end of file diff --git a/tests/unit/framework/elasticsearch/ElasticSearchConnectionTest.php b/tests/unit/framework/elasticsearch/ElasticSearchConnectionTest.php index 70b39b1..af8b9ff 100644 --- a/tests/unit/framework/elasticsearch/ElasticSearchConnectionTest.php +++ b/tests/unit/framework/elasticsearch/ElasticSearchConnectionTest.php @@ -4,6 +4,9 @@ namespace yiiunit\framework\elasticsearch; use yii\redis\Connection; +/** + * @group elasticsearch + */ class ElasticSearchConnectionTest extends ElasticSearchTestCase { /** diff --git a/tests/unit/framework/elasticsearch/ElasticSearchTestCase.php b/tests/unit/framework/elasticsearch/ElasticSearchTestCase.php index a19f851..88e24b5 100644 --- a/tests/unit/framework/elasticsearch/ElasticSearchTestCase.php +++ b/tests/unit/framework/elasticsearch/ElasticSearchTestCase.php @@ -6,7 +6,7 @@ use yii\elasticsearch\Connection; use yiiunit\TestCase; /** - * RedisTestCase is the base class for all redis related test cases + * ElasticSearchTestCase is the base class for all elasticsearch related test cases */ class ElasticSearchTestCase extends TestCase { diff --git a/tests/unit/framework/elasticsearch/QueryTest.php b/tests/unit/framework/elasticsearch/QueryTest.php index 41e842f..44d91ea 100644 --- a/tests/unit/framework/elasticsearch/QueryTest.php +++ b/tests/unit/framework/elasticsearch/QueryTest.php @@ -5,8 +5,7 @@ namespace yiiunit\framework\elasticsearch; use yii\elasticsearch\Query; /** - * @group db - * @group mysql + * @group elasticsearch */ class QueryTest extends ElasticSearchTestCase {