Commit 325fc281 by Carsten Brandt

several fixes and cleanup

parent 47705f0b
...@@ -51,7 +51,12 @@ class ActiveRecord extends \yii\db\ActiveRecord ...@@ -51,7 +51,12 @@ class ActiveRecord extends \yii\db\ActiveRecord
$query = static::createQuery(); $query = static::createQuery();
if (is_array($q)) { if (is_array($q)) {
if (count($q) == 1 && (array_key_exists(ActiveRecord::PRIMARY_KEY_NAME, $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(); return $query->where($q)->one();
} elseif ($q !== null) { } elseif ($q !== null) {
......
...@@ -87,10 +87,17 @@ class Command extends Component ...@@ -87,10 +87,17 @@ class Command extends Component
public function insert($index, $type, $data, $id = null, $options = []) public function insert($index, $type, $data, $id = null, $options = [])
{ {
$body = is_array($data) ? Json::encode($data) : $data; $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(); try {
} else { if ($id !== null) {
$response = $this->db->http()->post($this->createUrl([$index, $type], $options), null, $body)->send(); $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)); return Json::decode($response->getBody(true));
} }
......
...@@ -30,7 +30,7 @@ class Order extends ActiveRecord ...@@ -30,7 +30,7 @@ class Order extends ActiveRecord
public function getItems() public function getItems()
{ {
return $this->hasMany(Item::className(), [ActiveRecord::PRIMARY_KEY_NAME => 'item_id']) return $this->hasMany(Item::className(), [ActiveRecord::PRIMARY_KEY_NAME => 'item_id'])
->via('orderItems')->orderBy('name'); ->via('orderItems')->orderBy('id');
} }
public function getItemsInOrder1() public function getItemsInOrder1()
......
...@@ -134,6 +134,8 @@ trait ActiveRecordTestTrait ...@@ -134,6 +134,8 @@ trait ActiveRecordTestTrait
$this->assertNull($customer); $this->assertNull($customer);
$customer = $this->callCustomerFind(['id' => 5]); $customer = $this->callCustomerFind(['id' => 5]);
$this->assertNull($customer); $this->assertNull($customer);
$customer = $this->callCustomerFind(['name' => 'user5']);
$this->assertNull($customer);
// find by attributes // find by attributes
$customer = $this->callCustomerFind()->where(['name' => 'user2'])->one(); $customer = $this->callCustomerFind()->where(['name' => 'user2'])->one();
...@@ -141,6 +143,7 @@ trait ActiveRecordTestTrait ...@@ -141,6 +143,7 @@ trait ActiveRecordTestTrait
$this->assertEquals(2, $customer->id); $this->assertEquals(2, $customer->id);
// scope // scope
$this->assertEquals(2, count($this->callCustomerFind()->active()->all()));
$this->assertEquals(2, $this->callCustomerFind()->active()->count()); $this->assertEquals(2, $this->callCustomerFind()->active()->count());
// asArray // asArray
...@@ -158,8 +161,14 @@ trait ActiveRecordTestTrait ...@@ -158,8 +161,14 @@ trait ActiveRecordTestTrait
{ {
/** @var TestCase|ActiveRecordTestTrait $this */ /** @var TestCase|ActiveRecordTestTrait $this */
// query scalar // query scalar
$customerName = $this->callCustomerFind()->where(array('id' => 2))->scalar('name'); $customerName = $this->callCustomerFind()->where(['id' => 2])->scalar('name');
$this->assertEquals('user2', $customerName); $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() public function testFindColumn()
...@@ -365,7 +374,12 @@ trait ActiveRecordTestTrait ...@@ -365,7 +374,12 @@ trait ActiveRecordTestTrait
$this->assertEquals(2, count($order->items)); $this->assertEquals(2, count($order->items));
$this->assertEquals(1, $order->items[0]->id); $this->assertEquals(1, $order->items[0]->id);
$this->assertEquals(2, $order->items[1]->id); $this->assertEquals(2, $order->items[1]->id);
}
public function testFindLazyVia2()
{
/** @var TestCase|ActiveRecordTestTrait $this */
/** @var Order $order */
$order = $this->callOrderFind(1); $order = $this->callOrderFind(1);
$order->id = 100; $order->id = 100;
$this->assertEquals([], $order->items); $this->assertEquals([], $order->items);
...@@ -551,7 +565,7 @@ trait ActiveRecordTestTrait ...@@ -551,7 +565,7 @@ trait ActiveRecordTestTrait
$customer->save(); $customer->save();
$this->afterSave(); $this->afterSave();
$this->assertEquals(4, $customer->id); $this->assertNotNull($customer->id);
$this->assertFalse(static::$afterSaveNewRecord); $this->assertFalse(static::$afterSaveNewRecord);
$this->assertTrue(static::$afterSaveInsert); $this->assertTrue(static::$afterSaveInsert);
$this->assertFalse($customer->isNewRecord); $this->assertFalse($customer->isNewRecord);
......
...@@ -4,6 +4,7 @@ namespace yiiunit\framework\elasticsearch; ...@@ -4,6 +4,7 @@ namespace yiiunit\framework\elasticsearch;
use yii\elasticsearch\Connection; use yii\elasticsearch\Connection;
use yii\elasticsearch\ActiveQuery; use yii\elasticsearch\ActiveQuery;
use yii\helpers\Json;
use yiiunit\framework\ar\ActiveRecordTestTrait; use yiiunit\framework\ar\ActiveRecordTestTrait;
use yiiunit\data\ar\elasticsearch\ActiveRecord; use yiiunit\data\ar\elasticsearch\ActiveRecord;
use yiiunit\data\ar\elasticsearch\Customer; use yiiunit\data\ar\elasticsearch\Customer;
...@@ -11,6 +12,9 @@ use yiiunit\data\ar\elasticsearch\OrderItem; ...@@ -11,6 +12,9 @@ use yiiunit\data\ar\elasticsearch\OrderItem;
use yiiunit\data\ar\elasticsearch\Order; use yiiunit\data\ar\elasticsearch\Order;
use yiiunit\data\ar\elasticsearch\Item; use yiiunit\data\ar\elasticsearch\Item;
/**
* @group elasticsearch
*/
class ActiveRecordTest extends ElasticSearchTestCase class ActiveRecordTest extends ElasticSearchTestCase
{ {
use ActiveRecordTestTrait; use ActiveRecordTestTrait;
...@@ -43,6 +47,30 @@ class ActiveRecordTest extends ElasticSearchTestCase ...@@ -43,6 +47,30 @@ class ActiveRecordTest extends ElasticSearchTestCase
// delete all indexes // delete all indexes
$db->http()->delete('_all')->send(); $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 = new Customer();
$customer->id = 1; $customer->id = 1;
$customer->setAttributes(['email' => 'user1@example.com', 'name' => 'user1', 'address' => 'address1', 'status' => 1], false); $customer->setAttributes(['email' => 'user1@example.com', 'name' => 'user1', 'address' => 'address1', 'status' => 1], false);
...@@ -115,110 +143,6 @@ class ActiveRecordTest extends ElasticSearchTestCase ...@@ -115,110 +143,6 @@ class ActiveRecordTest extends ElasticSearchTestCase
Customer::getDb()->createCommand()->flushIndex(); 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() public function testGetDb()
{ {
$this->mockApplication(['components' => ['elasticsearch' => Connection::className()]]); $this->mockApplication(['components' => ['elasticsearch' => Connection::className()]]);
...@@ -331,4 +255,74 @@ class ActiveRecordTest extends ElasticSearchTestCase ...@@ -331,4 +255,74 @@ class ActiveRecordTest extends ElasticSearchTestCase
$orderItem->$pkName = 13; $orderItem->$pkName = 13;
$orderItem->save(); $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
...@@ -4,6 +4,9 @@ namespace yiiunit\framework\elasticsearch; ...@@ -4,6 +4,9 @@ namespace yiiunit\framework\elasticsearch;
use yii\redis\Connection; use yii\redis\Connection;
/**
* @group elasticsearch
*/
class ElasticSearchConnectionTest extends ElasticSearchTestCase class ElasticSearchConnectionTest extends ElasticSearchTestCase
{ {
/** /**
......
...@@ -6,7 +6,7 @@ use yii\elasticsearch\Connection; ...@@ -6,7 +6,7 @@ use yii\elasticsearch\Connection;
use yiiunit\TestCase; 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 class ElasticSearchTestCase extends TestCase
{ {
......
...@@ -5,8 +5,7 @@ namespace yiiunit\framework\elasticsearch; ...@@ -5,8 +5,7 @@ namespace yiiunit\framework\elasticsearch;
use yii\elasticsearch\Query; use yii\elasticsearch\Query;
/** /**
* @group db * @group elasticsearch
* @group mysql
*/ */
class QueryTest extends ElasticSearchTestCase class QueryTest extends ElasticSearchTestCase
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment