Beispiel #1
0
 protected function createCustomersRangeTable()
 {
     $command = $this->createCommand();
     /* @var $command \UrbanIndo\Yii2\DynamoDb\Command */
     $table = \test\data\CustomerRange::tableName();
     if ($command->tableExists($table)) {
         $command->deleteTable($table)->execute();
     }
     $index = \test\data\CustomerRange::secondaryIndex()[0];
     $command->createTable($table, ['AttributeDefinitions' => [['AttributeName' => \test\data\CustomerRange::primaryKey()[0], 'AttributeType' => 'N'], ['AttributeName' => \test\data\CustomerRange::primaryKey()[1], 'AttributeType' => 'S'], ['AttributeName' => \test\data\CustomerRange::keySecondayIndex()[$index][1], 'AttributeType' => 'S']], 'KeySchema' => [['AttributeName' => \test\data\CustomerRange::primaryKey()[0], 'KeyType' => 'HASH'], ['AttributeName' => \test\data\CustomerRange::primaryKey()[1], 'KeyType' => 'RANGE']], 'LocalSecondaryIndexes' => [['IndexName' => $index, 'KeySchema' => [['AttributeName' => \test\data\CustomerRange::keySecondayIndex()[$index][0], 'KeyType' => 'HASH'], ['AttributeName' => \test\data\CustomerRange::keySecondayIndex()[$index][1], 'KeyType' => 'RANGE']], 'Projection' => ['ProjectionType' => 'ALL']]], 'ProvisionedThroughput' => ['ReadCapacityUnits' => 10, 'WriteCapacityUnits' => 10]])->execute();
 }
 public function testCondition2()
 {
     $objectToInsert = new \test\data\CustomerRange();
     $id1 = (int) \Faker\Provider\Base::randomNumber(5);
     $faker = \Faker\Factory::create();
     $objectToInsert->id = $id1;
     $objectToInsert->name = $faker->name;
     $objectToInsert->phone = $faker->phoneNumber;
     $objectToInsert->contacts = ['telephone1' => 123456, 'telephone2' => 345678, 'telephone3' => 345678];
     $objectToInsert->prices = [1000000, 1000000, 1000000, 1000000];
     $objectToInsert->kids = ['Alice', 'Billy', 'Charlie'];
     $this->assertTrue($objectToInsert->save(false));
     $objectToInsert2 = new \test\data\CustomerRange();
     $id2 = $id1 + 1;
     $faker = \Faker\Factory::create();
     $objectToInsert2->id = $id2;
     $objectToInsert2->name = $faker->name;
     $objectToInsert2->phone = $faker->phoneNumber;
     $objectToInsert2->contacts = ['telephone1' => 123456, 'telephone2' => 345678, 'telephone3' => 345678];
     $objectToInsert2->prices = [1000000, 1000000, 1000000, 1000000];
     $objectToInsert2->kids = ['Alice', 'Ari', 'Charlie'];
     $this->assertTrue($objectToInsert2->save(false));
     $objectToInsert3 = new \test\data\CustomerRange();
     $id3 = $id2 + 1;
     $faker = \Faker\Factory::create();
     $objectToInsert3->id = $id3;
     $objectToInsert3->name = $faker->name;
     $objectToInsert3->phone = $faker->phoneNumber;
     $objectToInsert3->contacts = ['telephone1' => 123456, 'telephone2' => 345678, 'telephone3' => 345678];
     $objectToInsert3->prices = [1000000, 1000000, 1000000, 1000000];
     $objectToInsert3->kids = ['Alice', 'Ari', 'Angle'];
     $this->assertTrue($objectToInsert3->save(false));
     $objectsFromFind = \test\data\CustomerRange::find()->where(['id' => [$id1]])->all();
     $this->assertEquals(1, count($objectsFromFind));
     $objectsFromFind = \test\data\CustomerRange::find()->where(['id' => $id1])->all();
     $this->assertEquals(1, count($objectsFromFind));
     $objectsFromFind = \test\data\CustomerRange::find()->where(['id' => $objectToInsert3->id])->andWhere(['phone' => $objectToInsert3->phone])->indexBy(\test\data\CustomerRange::secondaryIndex()[0])->all();
     $this->assertEquals(1, count($objectsFromFind));
     $objectsFromFind = \test\data\CustomerRange::find()->where(['id' => [$id1, $id2]])->all();
     $this->assertEquals(2, count($objectsFromFind));
     $objectsFromFind = \test\data\CustomerRange::find()->where(['IN', 'id', [$id1, $id2]])->all();
     $this->assertEquals(2, count($objectsFromFind));
     $objectsFromFind = \test\data\CustomerRange::find()->where(['id' => $id1])->orWhere(['id' => $id2])->all();
     $this->assertEquals(2, count($objectsFromFind));
     $objectsFromFind = \test\data\CustomerRange::find()->where(['>=', 'id', $id1])->all();
     $this->assertEquals(3, count($objectsFromFind));
     $objectsFromFind = \test\data\CustomerRange::find()->limit(2)->all();
     $this->assertEquals(2, count($objectsFromFind));
     $objectsFromFind = \test\data\CustomerRange::find()->where(['>', 'id', $id1])->all();
     $objectsFromFind = \test\data\CustomerRange::find()->where(['CONTAINS', 'kids', 'Angle'])->all();
     $this->assertEquals(1, count($objectsFromFind));
 }
 /**
  * Test build Query method with complicated condition
  * @return void
  */
 public function testBuildQueryWithComplicatedCondition()
 {
     $qb = $this->createQueryBuilder();
     $faker = \Faker\Factory::create();
     $name = $faker->firstNameFemale;
     $id1 = $faker->firstNameFemale;
     $id2 = $faker->firstNameFemale;
     $query1 = $this->createQuery()->select(['id', 'name', 'contacts'])->where(['name' => $name])->andWhere(['id' => $id1]);
     $query2 = $this->createQuery()->select(['id', 'name', 'contacts'])->where(['name' => $name])->andWhere(['id' => [$id1, $id2]]);
     $query3 = $this->createQuery()->select(['id', 'name', 'contacts'])->where(['name' => $name])->andWhere(['attribute_exists', 'id']);
     $query4 = $this->createQuery()->select(['id', 'name', 'contacts'])->where(['name' => $name])->andWhere(['begins_with', 'id', $id1]);
     $expected = ['TableName' => Customer::tableName(), 'KeyConditionExpression' => '(name=:dqp0) AND (id=:dqp1)', 'ProjectionExpression' => 'id, name, contacts', 'ExpressionAttributeValues' => [':dqp0' => ['S' => $name], ':dqp1' => ['S' => $id1]]];
     $this->assertEquals($expected, $qb->build($query1)[1]);
     $expected['KeyConditionExpression'] = '(name=:dqp0) AND (id IN (:dqp1, :dqp2))';
     $expected['ExpressionAttributeValues'][':dqp2'] = ['S' => $id2];
     $this->assertEquals($expected, $qb->build($query2)[1]);
     $expected['KeyConditionExpression'] = '(name=:dqp0) AND (attribute_exists (:dqp1))';
     $expected['ExpressionAttributeValues'][':dqp1'] = ['S' => 'id'];
     unset($expected['ExpressionAttributeValues'][':dqp2']);
     $this->assertEquals($expected, $qb->build($query3)[1]);
     $expected['KeyConditionExpression'] = '(name=:dqp0) AND (begins_with (id, :dqp1))';
     $expected['ExpressionAttributeValues'][':dqp1'] = ['S' => $id1];
     $this->assertEquals($expected, $qb->build($query4)[1]);
 }