123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- <?php
- namespace Aliyun\OTS\Tests;
- use Aliyun\OTS;
- use Aliyun\OTS\ColumnTypeConst;
- use Aliyun\OTS\ComparatorTypeConst;
- use Aliyun\OTS\RowExistenceExpectationConst;
- require __DIR__ . "/TestBase.php";
- require __DIR__ . "/../../../vendor/autoload.php";
- $usedTables = array (
- "myTable"
- );
- SDKTestBase::cleanUp ($usedTables);
- SDKTestBase::createInitialTable (array (
- "table_meta" => array (
- "table_name" => $usedTables[0],
- "primary_key_schema" => array (
- "PK1" => ColumnTypeConst::CONST_INTEGER,
- "PK2" => ColumnTypeConst::CONST_STRING
- )
- ),
- "reserved_throughput" => array (
- "capacity_unit" => array (
- "read" => 0,
- "write" => 0
- )
- )
- ));
- SDKTestBase::waitForTableReady ();
- class DeleteRowTest extends SDKTestBase {
-
- /*
- *
- * TableNameOfZeroLength
- * 创建一个表,并删除,ListTable期望返回0个TableName。
- */
- public function testTableNameOfZeroLength() {
- global $usedTables;
- $deleterow = array (
- "table_name" => "",
- "condition" => RowExistenceExpectationConst::CONST_IGNORE,
- "primary_key" => array (
- "PK1" => 1,
- "PK2" => "a1"
- )
- );
- try {
- $this->otsClient->deleteRow ($deleterow);
- $this->fail ('An expected exception has not been raised.');
- } catch (\Aliyun\OTS\OTSServerException $exc) {
- $c = "Invalid table name: ''.";
- $this->assertEquals ($c, $exc->getOTSErrorMessage ());
- }
- }
-
- /*
- *
- * 5ColumnInPK
- * 和表主键不一致,指定5个主键
- */
- public function testColumnInPK() {
- global $usedTables;
- $deleterow = array (
- "table_name" => $usedTables[0],
- "condition" => RowExistenceExpectationConst::CONST_IGNORE,
- "primary_key" => array (
- "PK1" => "aaa",
- "PK2" => "cc",
- "PK3" => "ccd",
- "PK4" => "cds",
- "PK5" => "11s"
- )
- );
- try {
- $this->otsClient->deleteRow ($deleterow);
- $this->fail ('An expected exception has not been raised.');
- } catch (\Aliyun\OTS\OTSServerException $exc) {
- $c = "The number of primary key columns must be in range: [1, 4].";
- $this->assertEquals ($c, $exc->getOTSErrorMessage ());
- }
- }
-
- /*
- *
- * ExpectExistConditionWhenRowNotExist
- * 测试行不存在的条件下,写操作的Condition为EXPECT_EXIST,期望服务端返回 Invalid Condition。
- */
- public function testExpectExistConditionWhenRowNotExist() {
- global $usedTables;
- $tablename = array (
- "table_name" => $usedTables[0],
- "condition" => RowExistenceExpectationConst::CONST_IGNORE,
- "primary_key" => array (
- "PK1" => 1,
- "PK2" => "a1"
- ),
- "attribute_columns" => array (
- "att1" => "asds",
- "att2" => "sdsd"
- )
- );
- $this->otsClient->putRow ($tablename);
- $deleterow = array (
- "table_name" => $usedTables[0],
- "condition" => RowExistenceExpectationConst::CONST_EXPECT_EXIST,
- "primary_key" => array (
- "PK1" => 2,
- "PK2" => "a2"
- )
- );
- try {
- $this->otsClient->deleteRow ($deleterow);
- $this->fail ('An expected exception has not been raised.');
- } catch (\Aliyun\OTS\OTSServerException $exc) {
- $c = "Condition check failed.";
- $this->assertEquals ($c, $exc->getOTSErrorMessage ());
- }
- }
-
- /*
- *
- * ExpectExistConditionWhenRowExist
- * 测试行存在的条件下,写操作的Condition为EXPECT_EXIST,期望操作成功。
- */
- public function testExpectExistConditionWhenRowExist() {
- global $usedTables;
- $tablename = array (
- "table_name" => $usedTables[0],
- "condition" => RowExistenceExpectationConst::CONST_IGNORE,
- "primary_key" => array (
- "PK1" => 1,
- "PK2" => "a1"
- ),
- "attribute_columns" => array (
- "att1" => "asds",
- "att2" => "sdsd"
- )
- );
- $this->otsClient->putRow ($tablename);
- $deleterow = array (
- "table_name" => $usedTables[0],
- "condition" => RowExistenceExpectationConst::CONST_EXPECT_EXIST,
- "primary_key" => array (
- "PK1" => 1,
- "PK2" => "a1"
- )
- );
- $this->otsClient->deleteRow ($deleterow);
- $body = array (
- "table_name" => $usedTables[0],
- "primary_key" => array (
- "PK1" => 1,
- "PK2" => "a1"
- ),
- "columns_to_get" => array ()
- );
- $getrow = $this->otsClient->getRow ($body);
- // print_r($getrow);die;
- $this->assertEmpty ($getrow['row']['primary_key_columns']);
- $this->assertEmpty ($getrow['row']['attribute_columns']);
- }
-
- /*
- *
- * ExpectNotExistConditionWhenRowNotExist
- * 测试行不存在的条件下,写操作的Condition为EXPECT_NOT_EXIST
- *
- */
- public function testExpectNotExistConditionWhenRowNotExist() {
- global $usedTables;
- $deleterow = array (
- "table_name" => $usedTables[0],
- "condition" => RowExistenceExpectationConst::CONST_EXPECT_NOT_EXIST,
- "primary_key" => array (
- "PK1" => 1,
- "PK2" => "a1"
- )
- );
- try {
- $this->otsClient->deleteRow ($deleterow);
- $this->fail ('An expected exception has not been raised.');
- } catch (\Aliyun\OTS\OTSServerException $exc) {
- $c = "Invalid condition: EXPECT_NOT_EXIST while deleting row.";
- $this->assertEquals ($c, $exc->getOTSErrorMessage ());
- }
- }
- /*
- * ExpectNotExistConditionWhenRowExist
- * 测试行存在的条件下,写操作的Condition为EXPECT_NOT_EXIST
- */
- public function testExpectNotExistConditionWhenRowExist() {
- global $usedTables;
- $tablename = array (
- "table_name" => $usedTables[0],
- "condition" => RowExistenceExpectationConst::CONST_IGNORE,
- "primary_key" => array (
- "PK1" => 1,
- "PK2" => "a1"
- ),
- "attribute_columns" => array (
- "att1" => "asds",
- "att2" => "sdsd"
- )
- );
- $this->otsClient->putRow ($tablename);
- $deleterow = array (
- "table_name" => $usedTables[0],
- "condition" => RowExistenceExpectationConst::CONST_EXPECT_NOT_EXIST,
- "primary_key" => array (
- "PK1" => 1,
- "PK2" => "a1"
- )
- );
- try {
- $this->otsClient->deleteRow ($deleterow);
- $this->fail ('An expected exception has not been raised.');
- } catch (\Aliyun\OTS\OTSServerException $exc) {
- $c = "Invalid condition: EXPECT_NOT_EXIST while deleting row.";
- $this->assertEquals ($c, $exc->getOTSErrorMessage ());
- }
- }
-
- /**
- * 测试在使用ColumnCondition的过滤条件下,删除数据行是否成功。
- */
- public function testDeleteRowWithColumnCondition() {
- global $usedTables;
- $put_query = array (
- "table_name" => $usedTables[0],
- "condition" => RowExistenceExpectationConst::CONST_IGNORE,
- "primary_key" => array (
- "PK1" => 1,
- "PK2" => "a1"
- ),
- "attribute_columns" => array (
- "att1" => "asds",
- "att2" => "sdsd"
- )
- );
- $this->otsClient->putRow ($put_query);
-
- $delete_query = array (
- "table_name" => $usedTables[0],
- "condition" => array (
- "row_existence" => RowExistenceExpectationConst::CONST_EXPECT_EXIST,
- "column_filter" => array (
- "column_name" => "attr1",
- "value" => "asds",
- "comparator" => ComparatorTypeConst::CONST_EQUAL
- )
- ),
- "primary_key" => array (
- "PK1" => 1,
- "PK2" => "a1"
- )
- );
- $this->otsClient->deleteRow ($delete_query);
-
- $get_query = array (
- "table_name" => $usedTables[0],
- "primary_key" => array (
- "PK1" => 1,
- "PK2" => "a1"
- ),
- "columns_to_get" => array (
- "attr1",
- "attr2"
- )
- );
- $get_row_res = $this->otsClient->getRow ($get_query);
- $this->assertEquals (count ($get_row_res['row']['attribute_columns']), 0);
-
- $put_query2 = array (
- "table_name" => $usedTables[0],
- "condition" => RowExistenceExpectationConst::CONST_IGNORE,
- "primary_key" => array (
- "PK1" => 1,
- "PK2" => "a1"
- ),
- "attribute_columns" => array (
- "att1" => "asds",
- "att2" => "sdsd"
- )
- );
- $this->otsClient->putRow ($put_query2);
-
- $delete_query2 = array (
- "table_name" => $usedTables[0],
- "condition" => array (
- "row_existence" => RowExistenceExpectationConst::CONST_EXPECT_EXIST,
- "column_filter" => array (
- "column_name" => "att1",
- "value" => "asdsddd",
- "comparator" => ComparatorTypeConst::CONST_EQUAL
- )
- ),
- "primary_key" => array (
- "PK1" => 1,
- "PK2" => "a1"
- )
- );
- try {
- $this->otsClient->deleteRow ($delete_query2);
- $this->fail ('An expected exception has not been raised.');
- } catch (\Aliyun\OTS\OTSServerException $exc) {
- $a = $exc->getMessage ();
- $c = "Condition check failed.";
- $this->assertContains ( $c, $a );
- }
- }
- }
|