DeleteRowTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. namespace Aliyun\OTS\Tests;
  3. use Aliyun\OTS;
  4. use Aliyun\OTS\ColumnTypeConst;
  5. use Aliyun\OTS\ComparatorTypeConst;
  6. use Aliyun\OTS\RowExistenceExpectationConst;
  7. require __DIR__ . "/TestBase.php";
  8. require __DIR__ . "/../../../vendor/autoload.php";
  9. $usedTables = array (
  10. "myTable"
  11. );
  12. SDKTestBase::cleanUp ($usedTables);
  13. SDKTestBase::createInitialTable (array (
  14. "table_meta" => array (
  15. "table_name" => $usedTables[0],
  16. "primary_key_schema" => array (
  17. "PK1" => ColumnTypeConst::CONST_INTEGER,
  18. "PK2" => ColumnTypeConst::CONST_STRING
  19. )
  20. ),
  21. "reserved_throughput" => array (
  22. "capacity_unit" => array (
  23. "read" => 0,
  24. "write" => 0
  25. )
  26. )
  27. ));
  28. SDKTestBase::waitForTableReady ();
  29. class DeleteRowTest extends SDKTestBase {
  30. /*
  31. *
  32. * TableNameOfZeroLength
  33. * 创建一个表,并删除,ListTable期望返回0个TableName。
  34. */
  35. public function testTableNameOfZeroLength() {
  36. global $usedTables;
  37. $deleterow = array (
  38. "table_name" => "",
  39. "condition" => RowExistenceExpectationConst::CONST_IGNORE,
  40. "primary_key" => array (
  41. "PK1" => 1,
  42. "PK2" => "a1"
  43. )
  44. );
  45. try {
  46. $this->otsClient->deleteRow ($deleterow);
  47. $this->fail ('An expected exception has not been raised.');
  48. } catch (\Aliyun\OTS\OTSServerException $exc) {
  49. $c = "Invalid table name: ''.";
  50. $this->assertEquals ($c, $exc->getOTSErrorMessage ());
  51. }
  52. }
  53. /*
  54. *
  55. * 5ColumnInPK
  56. * 和表主键不一致,指定5个主键
  57. */
  58. public function testColumnInPK() {
  59. global $usedTables;
  60. $deleterow = array (
  61. "table_name" => $usedTables[0],
  62. "condition" => RowExistenceExpectationConst::CONST_IGNORE,
  63. "primary_key" => array (
  64. "PK1" => "aaa",
  65. "PK2" => "cc",
  66. "PK3" => "ccd",
  67. "PK4" => "cds",
  68. "PK5" => "11s"
  69. )
  70. );
  71. try {
  72. $this->otsClient->deleteRow ($deleterow);
  73. $this->fail ('An expected exception has not been raised.');
  74. } catch (\Aliyun\OTS\OTSServerException $exc) {
  75. $c = "The number of primary key columns must be in range: [1, 4].";
  76. $this->assertEquals ($c, $exc->getOTSErrorMessage ());
  77. }
  78. }
  79. /*
  80. *
  81. * ExpectExistConditionWhenRowNotExist
  82. * 测试行不存在的条件下,写操作的Condition为EXPECT_EXIST,期望服务端返回 Invalid Condition。
  83. */
  84. public function testExpectExistConditionWhenRowNotExist() {
  85. global $usedTables;
  86. $tablename = array (
  87. "table_name" => $usedTables[0],
  88. "condition" => RowExistenceExpectationConst::CONST_IGNORE,
  89. "primary_key" => array (
  90. "PK1" => 1,
  91. "PK2" => "a1"
  92. ),
  93. "attribute_columns" => array (
  94. "att1" => "asds",
  95. "att2" => "sdsd"
  96. )
  97. );
  98. $this->otsClient->putRow ($tablename);
  99. $deleterow = array (
  100. "table_name" => $usedTables[0],
  101. "condition" => RowExistenceExpectationConst::CONST_EXPECT_EXIST,
  102. "primary_key" => array (
  103. "PK1" => 2,
  104. "PK2" => "a2"
  105. )
  106. );
  107. try {
  108. $this->otsClient->deleteRow ($deleterow);
  109. $this->fail ('An expected exception has not been raised.');
  110. } catch (\Aliyun\OTS\OTSServerException $exc) {
  111. $c = "Condition check failed.";
  112. $this->assertEquals ($c, $exc->getOTSErrorMessage ());
  113. }
  114. }
  115. /*
  116. *
  117. * ExpectExistConditionWhenRowExist
  118. * 测试行存在的条件下,写操作的Condition为EXPECT_EXIST,期望操作成功。
  119. */
  120. public function testExpectExistConditionWhenRowExist() {
  121. global $usedTables;
  122. $tablename = array (
  123. "table_name" => $usedTables[0],
  124. "condition" => RowExistenceExpectationConst::CONST_IGNORE,
  125. "primary_key" => array (
  126. "PK1" => 1,
  127. "PK2" => "a1"
  128. ),
  129. "attribute_columns" => array (
  130. "att1" => "asds",
  131. "att2" => "sdsd"
  132. )
  133. );
  134. $this->otsClient->putRow ($tablename);
  135. $deleterow = array (
  136. "table_name" => $usedTables[0],
  137. "condition" => RowExistenceExpectationConst::CONST_EXPECT_EXIST,
  138. "primary_key" => array (
  139. "PK1" => 1,
  140. "PK2" => "a1"
  141. )
  142. );
  143. $this->otsClient->deleteRow ($deleterow);
  144. $body = array (
  145. "table_name" => $usedTables[0],
  146. "primary_key" => array (
  147. "PK1" => 1,
  148. "PK2" => "a1"
  149. ),
  150. "columns_to_get" => array ()
  151. );
  152. $getrow = $this->otsClient->getRow ($body);
  153. // print_r($getrow);die;
  154. $this->assertEmpty ($getrow['row']['primary_key_columns']);
  155. $this->assertEmpty ($getrow['row']['attribute_columns']);
  156. }
  157. /*
  158. *
  159. * ExpectNotExistConditionWhenRowNotExist
  160. * 测试行不存在的条件下,写操作的Condition为EXPECT_NOT_EXIST
  161. *
  162. */
  163. public function testExpectNotExistConditionWhenRowNotExist() {
  164. global $usedTables;
  165. $deleterow = array (
  166. "table_name" => $usedTables[0],
  167. "condition" => RowExistenceExpectationConst::CONST_EXPECT_NOT_EXIST,
  168. "primary_key" => array (
  169. "PK1" => 1,
  170. "PK2" => "a1"
  171. )
  172. );
  173. try {
  174. $this->otsClient->deleteRow ($deleterow);
  175. $this->fail ('An expected exception has not been raised.');
  176. } catch (\Aliyun\OTS\OTSServerException $exc) {
  177. $c = "Invalid condition: EXPECT_NOT_EXIST while deleting row.";
  178. $this->assertEquals ($c, $exc->getOTSErrorMessage ());
  179. }
  180. }
  181. /*
  182. * ExpectNotExistConditionWhenRowExist
  183. * 测试行存在的条件下,写操作的Condition为EXPECT_NOT_EXIST
  184. */
  185. public function testExpectNotExistConditionWhenRowExist() {
  186. global $usedTables;
  187. $tablename = array (
  188. "table_name" => $usedTables[0],
  189. "condition" => RowExistenceExpectationConst::CONST_IGNORE,
  190. "primary_key" => array (
  191. "PK1" => 1,
  192. "PK2" => "a1"
  193. ),
  194. "attribute_columns" => array (
  195. "att1" => "asds",
  196. "att2" => "sdsd"
  197. )
  198. );
  199. $this->otsClient->putRow ($tablename);
  200. $deleterow = array (
  201. "table_name" => $usedTables[0],
  202. "condition" => RowExistenceExpectationConst::CONST_EXPECT_NOT_EXIST,
  203. "primary_key" => array (
  204. "PK1" => 1,
  205. "PK2" => "a1"
  206. )
  207. );
  208. try {
  209. $this->otsClient->deleteRow ($deleterow);
  210. $this->fail ('An expected exception has not been raised.');
  211. } catch (\Aliyun\OTS\OTSServerException $exc) {
  212. $c = "Invalid condition: EXPECT_NOT_EXIST while deleting row.";
  213. $this->assertEquals ($c, $exc->getOTSErrorMessage ());
  214. }
  215. }
  216. /**
  217. * 测试在使用ColumnCondition的过滤条件下,删除数据行是否成功。
  218. */
  219. public function testDeleteRowWithColumnCondition() {
  220. global $usedTables;
  221. $put_query = array (
  222. "table_name" => $usedTables[0],
  223. "condition" => RowExistenceExpectationConst::CONST_IGNORE,
  224. "primary_key" => array (
  225. "PK1" => 1,
  226. "PK2" => "a1"
  227. ),
  228. "attribute_columns" => array (
  229. "att1" => "asds",
  230. "att2" => "sdsd"
  231. )
  232. );
  233. $this->otsClient->putRow ($put_query);
  234. $delete_query = array (
  235. "table_name" => $usedTables[0],
  236. "condition" => array (
  237. "row_existence" => RowExistenceExpectationConst::CONST_EXPECT_EXIST,
  238. "column_filter" => array (
  239. "column_name" => "attr1",
  240. "value" => "asds",
  241. "comparator" => ComparatorTypeConst::CONST_EQUAL
  242. )
  243. ),
  244. "primary_key" => array (
  245. "PK1" => 1,
  246. "PK2" => "a1"
  247. )
  248. );
  249. $this->otsClient->deleteRow ($delete_query);
  250. $get_query = array (
  251. "table_name" => $usedTables[0],
  252. "primary_key" => array (
  253. "PK1" => 1,
  254. "PK2" => "a1"
  255. ),
  256. "columns_to_get" => array (
  257. "attr1",
  258. "attr2"
  259. )
  260. );
  261. $get_row_res = $this->otsClient->getRow ($get_query);
  262. $this->assertEquals (count ($get_row_res['row']['attribute_columns']), 0);
  263. $put_query2 = array (
  264. "table_name" => $usedTables[0],
  265. "condition" => RowExistenceExpectationConst::CONST_IGNORE,
  266. "primary_key" => array (
  267. "PK1" => 1,
  268. "PK2" => "a1"
  269. ),
  270. "attribute_columns" => array (
  271. "att1" => "asds",
  272. "att2" => "sdsd"
  273. )
  274. );
  275. $this->otsClient->putRow ($put_query2);
  276. $delete_query2 = array (
  277. "table_name" => $usedTables[0],
  278. "condition" => array (
  279. "row_existence" => RowExistenceExpectationConst::CONST_EXPECT_EXIST,
  280. "column_filter" => array (
  281. "column_name" => "att1",
  282. "value" => "asdsddd",
  283. "comparator" => ComparatorTypeConst::CONST_EQUAL
  284. )
  285. ),
  286. "primary_key" => array (
  287. "PK1" => 1,
  288. "PK2" => "a1"
  289. )
  290. );
  291. try {
  292. $this->otsClient->deleteRow ($delete_query2);
  293. $this->fail ('An expected exception has not been raised.');
  294. } catch (\Aliyun\OTS\OTSServerException $exc) {
  295. $a = $exc->getMessage ();
  296. $c = "Condition check failed.";
  297. $this->assertContains ( $c, $a );
  298. }
  299. }
  300. }