GetRowTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. <?php
  2. namespace Aliyun\OTS\Tests;
  3. use Aliyun\OTS;
  4. use Aliyun\OTS\RowExistenceExpectationConst;
  5. use Aliyun\OTS\LogicalOperatorConst;
  6. use Aliyun\OTS\ComparatorTypeConst;
  7. use Aliyun\OTS\ColumnTypeConst;
  8. require __DIR__ . "/TestBase.php";
  9. require __DIR__ . "/../../../vendor/autoload.php";
  10. $usedTables = array (
  11. "myTable"
  12. );
  13. SDKTestBase::cleanUp ($usedTables);
  14. SDKTestBase::createInitialTable (array (
  15. "table_meta" => array (
  16. "table_name" => $usedTables[0],
  17. "primary_key_schema" => array (
  18. "PK1" => ColumnTypeConst::CONST_STRING,
  19. "PK2" => ColumnTypeConst::CONST_INTEGER,
  20. "PK3" => ColumnTypeConst::CONST_STRING,
  21. "PK4" => ColumnTypeConst::CONST_INTEGER
  22. )
  23. ),
  24. "reserved_throughput" => array (
  25. "capacity_unit" => array (
  26. "read" => 0,
  27. "write" => 0
  28. )
  29. )
  30. ));
  31. SDKTestBase::waitForTableReady ();
  32. SDKTestBase::putInitialData (array (
  33. "table_name" => $usedTables[0],
  34. "condition" => RowExistenceExpectationConst::CONST_IGNORE,
  35. "primary_key" => array (
  36. "PK1" => "a1",
  37. "PK2" => 1,
  38. "PK3" => "a11",
  39. "PK4" => 11
  40. ),
  41. "attribute_columns" => array (
  42. "attr1" => 1,
  43. "attr2" => "aa",
  44. "attr3" => "tas",
  45. "attr4" => 11
  46. )
  47. ));
  48. class GetRowTest extends SDKTestBase {
  49. /*
  50. *
  51. * GetRowWithDefaultColumnsToGet
  52. * 先PutRow包含4个主键列,4个属性列,然后GetRow请求ColumnsToGet参数为4个属性列,期望读出所有4个属性列。
  53. */
  54. public function testGetRowWith4AttributeColumnsToGet() {
  55. global $usedTables;
  56. $body = array (
  57. "table_name" => $usedTables[0],
  58. "primary_key" => array (
  59. "PK1" => "a1",
  60. "PK2" => 1,
  61. "PK3" => "a11",
  62. "PK4" => 11
  63. ),
  64. "columns_to_get" => array (
  65. "attr1",
  66. "attr2",
  67. "attr3",
  68. "attr4"
  69. )
  70. );
  71. $tablename = array (
  72. "table_name" => $usedTables[0],
  73. "condition" => RowExistenceExpectationConst::CONST_IGNORE,
  74. "primary_key" => array (
  75. "PK1" => "a1",
  76. "PK2" => 1,
  77. "PK3" => "a11",
  78. "PK4" => 11
  79. ),
  80. "attribute_columns" => array (
  81. "attr1" => 1,
  82. "attr2" => "aa",
  83. "attr3" => "tas",
  84. "attr4" => 11
  85. )
  86. );
  87. $this->otsClient->putRow ($tablename);
  88. $getrow = $this->otsClient->getRow ($body);
  89. $this->assertEmpty ($getrow['row']['primary_key_columns']);
  90. $this->assertEquals ($getrow['row']['attribute_columns']['attr1'], 1);
  91. $this->assertEquals ($getrow['row']['attribute_columns']['attr2'], "aa");
  92. $this->assertEquals ($getrow['row']['attribute_columns']['attr3'], "tas");
  93. $this->assertEquals ($getrow['row']['attribute_columns']['attr4'], 11);
  94. }
  95. /*
  96. *
  97. * GetRowWithDefaultColumnsToGet
  98. * 先PutRow包含4个主键列,4个属性列,然后GetRow请求不设置ColumnsToGet,期望读出所有4个主键列和4个属性列。
  99. */
  100. public function testGetRowWithDefaultColumnsToGet() {
  101. global $usedTables;
  102. $body = array (
  103. "table_name" => $usedTables[0],
  104. "primary_key" => array (
  105. "PK1" => "a1",
  106. "PK2" => 1,
  107. "PK3" => "a11",
  108. "PK4" => 11
  109. )
  110. );
  111. $tablename = array (
  112. "table_name" => $usedTables[0],
  113. "condition" => RowExistenceExpectationConst::CONST_IGNORE,
  114. "primary_key" => array (
  115. "PK1" => "a1",
  116. "PK2" => 1,
  117. "PK3" => "a11",
  118. "PK4" => 11
  119. ),
  120. "attribute_columns" => array (
  121. "attr1" => 1,
  122. "attr2" => "aa",
  123. "attr3" => "tas",
  124. "attr4" => 11
  125. )
  126. );
  127. $this->otsClient->putRow ($tablename);
  128. $getrow = $this->otsClient->getRow ($body);
  129. $this->assertEquals ($getrow['row']['primary_key_columns']['PK1'], "a1");
  130. $this->assertEquals ($getrow['row']['primary_key_columns']['PK2'], 1);
  131. $this->assertEquals ($getrow['row']['primary_key_columns']['PK3'], "a11");
  132. $this->assertEquals ($getrow['row']['primary_key_columns']['PK4'], 11);
  133. $this->assertEquals ($getrow['row']['attribute_columns']['attr1'], 1);
  134. $this->assertEquals ($getrow['row']['attribute_columns']['attr2'], "aa");
  135. $this->assertEquals ($getrow['row']['attribute_columns']['attr3'], "tas");
  136. $this->assertEquals ($getrow['row']['attribute_columns']['attr4'], 11);
  137. }
  138. /*
  139. * GetRowWith0ColumsToGet
  140. * 先PutRow包含4个主键列,4个属性列,然后GetRow请求ColumnsToGet为空数组,期望读出所有数据。
  141. */
  142. public function testGetRowWith0ColumsToGet() {
  143. global $usedTables;
  144. $body = array (
  145. "table_name" => $usedTables[0],
  146. "primary_key" => array (
  147. "PK1" => "a1",
  148. "PK2" => 1,
  149. "PK3" => "a11",
  150. "PK4" => 11
  151. ),
  152. "columns_to_get" => array ()
  153. );
  154. $tablename = array (
  155. "table_name" => $usedTables[0],
  156. "condition" => RowExistenceExpectationConst::CONST_IGNORE,
  157. "primary_key" => array (
  158. "PK1" => "a1",
  159. "PK2" => 1,
  160. "PK3" => "a11",
  161. "PK4" => 11
  162. ),
  163. "attribute_columns" => array (
  164. "attr1" => 1,
  165. "attr2" => "aa",
  166. "attr3" => "tas",
  167. "attr4" => 11
  168. )
  169. );
  170. $this->otsClient->putRow ($tablename);
  171. $getrow = $this->otsClient->getRow ($body);
  172. $this->assertEquals ($getrow['row']['primary_key_columns'], $tablename['primary_key']);
  173. $this->assertEquals ($getrow['row']['attribute_columns'], $tablename['attribute_columns']);
  174. }
  175. /*
  176. * GetRowWith4ColumnsToGet
  177. * 先PutRow包含4个主键列,4个属性列,然后GetRow请求ColumnsToGet包含其中2个主键列,2个属性列,期望返回数据包含参数中指定的列。
  178. */
  179. public function testGetRowWith4ColumnsToGet() {
  180. global $usedTables;
  181. $body = array (
  182. "table_name" => $usedTables[0],
  183. "primary_key" => array (
  184. "PK1" => "a1",
  185. "PK2" => 1,
  186. "PK3" => "a11",
  187. "PK4" => 11
  188. ),
  189. "columns_to_get" => array (
  190. "PK1",
  191. "PK2",
  192. "attr1",
  193. "attr2"
  194. )
  195. );
  196. $tablename = array (
  197. "table_name" => $usedTables[0],
  198. "condition" => RowExistenceExpectationConst::CONST_IGNORE,
  199. "primary_key" => array (
  200. "PK1" => "a1",
  201. "PK2" => 1,
  202. "PK3" => "a11",
  203. "PK4" => 11
  204. ),
  205. "attribute_columns" => array (
  206. "attr1" => 1,
  207. "attr2" => "aa",
  208. "attr3" => "tas",
  209. "attr4" => 11
  210. )
  211. );
  212. $this->otsClient->putRow ($tablename);
  213. $getrow = $this->otsClient->getRow ($body);
  214. $this->assertArrayHasKey ("PK1", $getrow['row']['primary_key_columns']);
  215. $this->assertArrayHasKey ("PK2", $getrow['row']['primary_key_columns']);
  216. $this->assertArrayHasKey ("attr1", $getrow['row']['attribute_columns']);
  217. $this->assertArrayHasKey ("attr2", $getrow['row']['attribute_columns']);
  218. }
  219. /*
  220. * GetRowWith1000ColumnsToGet
  221. * GetRow请求ColumnsToGet包含1000个不重复的列名,期望返回服务端错误
  222. */
  223. public function testGetRowWith1000ColumnsToGet() {
  224. global $usedTables;
  225. for($a = 1; $a < 1000; $a ++) {
  226. $b[] = 'a' . $a;
  227. }
  228. // echo $b;
  229. $body = array (
  230. "table_name" => $usedTables[0],
  231. "primary_key" => array (
  232. "PK1" => "a1",
  233. "PK2" => 1,
  234. "PK3" => "a11",
  235. "PK4" => 11
  236. ),
  237. "columns_to_get" => $b
  238. );
  239. $this->otsClient->getRow ($body);
  240. }
  241. /*
  242. * GetRowWithDuplicateColumnsToGet
  243. * GetRow请求ColumnsToGet包含2个重复的列名,成功返回这一列的值
  244. */
  245. public function testGetRowWithDuplicateColumnsToGet() {
  246. global $usedTables;
  247. $body = array (
  248. "table_name" => $usedTables[0],
  249. "primary_key" => array (
  250. "PK1" => "a1",
  251. "PK2" => 1,
  252. "PK3" => "a11",
  253. "PK4" => 11
  254. ),
  255. "columns_to_get" => array (
  256. "PK1",
  257. "PK1"
  258. )
  259. );
  260. $tablename = array (
  261. "table_name" => $usedTables[0],
  262. "condition" => RowExistenceExpectationConst::CONST_IGNORE,
  263. "primary_key" => array (
  264. "PK1" => "a1",
  265. "PK2" => 1,
  266. "PK3" => "a11",
  267. "PK4" => 11
  268. ),
  269. "attribute_columns" => array (
  270. "attr1" => 1,
  271. "attr2" => "aa",
  272. "attr3" => "tas",
  273. "attr4" => 11
  274. )
  275. );
  276. $this->otsClient->putRow ($tablename);
  277. $getrow = $this->otsClient->getRow ($body);
  278. // if (is_array($getrow)) {
  279. // print_r($getrow);die;
  280. $this->assertEquals ($getrow['row']['primary_key_columns']["PK1"], $body['primary_key']['PK1']);
  281. // }
  282. }
  283. /**
  284. * 测试在使用ColumnCondition的过滤条件的情况下,获取数据行的操作是否成功。
  285. */
  286. public function testGetRowWithColumnFilterToGet() {
  287. global $usedTables;
  288. $putdata1 = array (
  289. "table_name" => $usedTables[0],
  290. "condition" => RowExistenceExpectationConst::CONST_IGNORE,
  291. "primary_key" => array (
  292. "PK1" => "a1",
  293. "PK2" => 1,
  294. "PK3" => "a11",
  295. "PK4" => 11
  296. ),
  297. "attribute_columns" => array (
  298. "attr1" => 1,
  299. "attr2" => "aa",
  300. "attr3" => "tas",
  301. "attr4" => 11
  302. )
  303. );
  304. $putdata2 = array (
  305. "table_name" => $usedTables[0],
  306. "condition" => RowExistenceExpectationConst::CONST_IGNORE,
  307. "primary_key" => array (
  308. "PK1" => "a2",
  309. "PK2" => 2,
  310. "PK3" => "a22",
  311. "PK4" => 22
  312. ),
  313. "attribute_columns" => array (
  314. "attr1" => 2,
  315. "attr2" => "aaa",
  316. "attr3" => "tass",
  317. "attr4" => 22
  318. )
  319. );
  320. $this->otsClient->putRow ($putdata1);
  321. $this->otsClient->putRow ($putdata2);
  322. $querybody = array (
  323. "table_name" => $usedTables[0],
  324. "primary_key" => array (
  325. "PK1" => "a2",
  326. "PK2" => 2,
  327. "PK3" => "a22",
  328. "PK4" => 22
  329. ),
  330. "columns_to_get" => array (
  331. "PK1",
  332. "PK2",
  333. "PK3",
  334. "PK4"
  335. ),
  336. "column_filter" => array (
  337. "logical_operator" => LogicalOperatorConst::CONST_AND,
  338. "sub_conditions" => array (
  339. array (
  340. "column_name" => "attr1",
  341. "value" => 1,
  342. "comparator" => ComparatorTypeConst::CONST_GREATER_THAN
  343. ),
  344. array (
  345. "column_name" => "attr4",
  346. "value" => 30,
  347. "comparator" => ComparatorTypeConst::CONST_LESS_THAN
  348. )
  349. )
  350. )
  351. );
  352. $getrowres = $this->otsClient->getRow ($querybody);
  353. $this->assertEquals ($getrowres['row']['primary_key_columns']["PK1"], $putdata2['primary_key']['PK1']);
  354. $this->assertEquals ($getrowres['row']['primary_key_columns']["PK2"], $putdata2['primary_key']['PK2']);
  355. $this->assertEquals ($getrowres['row']['primary_key_columns']["PK3"], $putdata2['primary_key']['PK3']);
  356. $this->assertEquals ($getrowres['row']['primary_key_columns']["PK4"], $putdata2['primary_key']['PK4']);
  357. }
  358. /**
  359. * 查询在使用ColumnCondition的过滤条件的情况下,获取数据行的操作是否成功。
  360. */
  361. public function testGetRowWithColumnFilterToGet2() {
  362. global $usedTables;
  363. $putdata1 = array (
  364. "table_name" => $usedTables[0],
  365. "condition" => RowExistenceExpectationConst::CONST_IGNORE,
  366. "primary_key" => array (
  367. "PK1" => "a1",
  368. "PK2" => 1,
  369. "PK3" => "a11",
  370. "PK4" => 11
  371. ),
  372. "attribute_columns" => array (
  373. "attr1" => 1,
  374. "attr2" => "aa",
  375. "attr3" => "tas",
  376. "attr4" => 11
  377. )
  378. );
  379. $putdata2 = array (
  380. "table_name" => $usedTables[0],
  381. "condition" => RowExistenceExpectationConst::CONST_IGNORE,
  382. "primary_key" => array (
  383. "PK1" => "aa2",
  384. "PK2" => 2,
  385. "PK3" => "aa22",
  386. "PK4" => 22
  387. ),
  388. "attribute_columns" => array (
  389. "attr1" => 2,
  390. "attr2" => "aaa",
  391. "attr3" => "tass",
  392. "attr4" => 22
  393. )
  394. );
  395. $this->otsClient->putRow ($putdata1);
  396. $this->otsClient->putRow ($putdata2);
  397. $querybody = array (
  398. "table_name" => $usedTables[0],
  399. "primary_key" => array (
  400. "PK1" => "aa2",
  401. "PK2" => 2,
  402. "PK3" => "aa22",
  403. "PK4" => 22
  404. ),
  405. "columns_to_get" => array (
  406. "attr1",
  407. "attr2",
  408. "attr3",
  409. "attr4"
  410. ),
  411. "column_filter" => array (
  412. "logical_operator" => LogicalOperatorConst::CONST_NOT,
  413. "sub_conditions" => array (
  414. array (
  415. "column_name" => "attr4",
  416. "value" => 22,
  417. "comparator" => ComparatorTypeConst::CONST_NOT_EQUAL
  418. )
  419. )
  420. )
  421. );
  422. $getrowres = $this->otsClient->getRow ($querybody);
  423. $this->assertEquals ($getrowres['row']['attribute_columns']["attr1"], $putdata2['attribute_columns']['attr1']);
  424. $this->assertEquals ($getrowres['row']['attribute_columns']["attr2"], $putdata2['attribute_columns']['attr2']);
  425. $this->assertEquals ($getrowres['row']['attribute_columns']["attr3"], $putdata2['attribute_columns']['attr3']);
  426. $this->assertEquals ($getrowres['row']['attribute_columns']["attr4"], $putdata2['attribute_columns']['attr4']);
  427. }
  428. /**
  429. * 测试在使用ColumnCondition的过滤条件和过滤的某项数据列缺失的情况下,获取查询数据航是否成功。
  430. */
  431. public function testGetRowWithColumnFilterAndMissingField() {
  432. global $usedTables;
  433. $putdata1 = array (
  434. "table_name" => $usedTables[0],
  435. "condition" => RowExistenceExpectationConst::CONST_IGNORE,
  436. "primary_key" => array (
  437. "PK1" => "a1",
  438. "PK2" => 1,
  439. "PK3" => "a11",
  440. "PK4" => 11
  441. ),
  442. "attribute_columns" => array (
  443. "attr1" => 1,
  444. "attr2" => "aa",
  445. "attr3" => "tas",
  446. "attr4" => 11
  447. )
  448. );
  449. $putdata2 = array (
  450. "table_name" => $usedTables[0],
  451. "condition" => RowExistenceExpectationConst::CONST_IGNORE,
  452. "primary_key" => array (
  453. "PK1" => "a2",
  454. "PK2" => 2,
  455. "PK3" => "a22",
  456. "PK4" => 22
  457. ),
  458. "attribute_columns" => array (
  459. "attr1" => 2,
  460. "attr2" => "aaa",
  461. "attr3" => "tass",
  462. "attr4" => 22
  463. )
  464. );
  465. $this->otsClient->putRow ($putdata1);
  466. $this->otsClient->putRow ($putdata2);
  467. $querybody = array (
  468. "table_name" => $usedTables[0],
  469. "primary_key" => array (
  470. "PK1" => "a2",
  471. "PK2" => 2,
  472. "PK3" => "a22",
  473. "PK4" => 22
  474. ),
  475. "columns_to_get" => array (
  476. "PK1",
  477. "PK2",
  478. "PK3",
  479. "PK4"
  480. ),
  481. "column_filter" => array (
  482. "logical_operator" => LogicalOperatorConst::CONST_AND,
  483. "sub_conditions" => array (
  484. array (
  485. "column_name" => "attr55",
  486. "value" => 1,
  487. "comparator" => ComparatorTypeConst::CONST_GREATER_THAN,
  488. "pass_if_missing" => false
  489. ),
  490. array (
  491. "column_name" => "attr4",
  492. "value" => 30,
  493. "comparator" => ComparatorTypeConst::CONST_LESS_THAN
  494. )
  495. )
  496. )
  497. );
  498. $getrowres = $this->otsClient->getRow ($querybody);
  499. $this->assertEquals (count ($getrowres['row']['primary_key_columns']), 0);
  500. $this->assertEquals (count ($getrowres['row']['attribute_columns']), 0);
  501. }
  502. /**
  503. * 测试在使用ColumnCondition的过滤条件和多重逻辑运算符的情况下,获取查询的数据行是否成功。
  504. */
  505. public function testGetRowWithColumnFilterAndMultipleLogicalOperatorsToGet() {
  506. global $usedTables;
  507. $putdata1 = array (
  508. "table_name" => $usedTables[0],
  509. "condition" => RowExistenceExpectationConst::CONST_IGNORE,
  510. "primary_key" => array (
  511. "PK1" => "a1",
  512. "PK2" => 1,
  513. "PK3" => "a11",
  514. "PK4" => 11
  515. ),
  516. "attribute_columns" => array (
  517. "attr1" => 1,
  518. "attr2" => "aa",
  519. "attr3" => "tas",
  520. "attr4" => 11
  521. )
  522. );
  523. $putdata2 = array (
  524. "table_name" => $usedTables[0],
  525. "condition" => RowExistenceExpectationConst::CONST_IGNORE,
  526. "primary_key" => array (
  527. "PK1" => "a2",
  528. "PK2" => 2,
  529. "PK3" => "a22",
  530. "PK4" => 22
  531. ),
  532. "attribute_columns" => array (
  533. "attr1" => 2,
  534. "attr2" => "aaa",
  535. "attr3" => "tass",
  536. "attr4" => 22
  537. )
  538. );
  539. $this->otsClient->putRow ($putdata1);
  540. $this->otsClient->putRow ($putdata2);
  541. $querybody = array (
  542. "table_name" => $usedTables[0],
  543. "primary_key" => array (
  544. "PK1" => "a2",
  545. "PK2" => 2,
  546. "PK3" => "a22",
  547. "PK4" => 22
  548. ),
  549. "columns_to_get" => array (
  550. "PK1",
  551. "PK2",
  552. "PK3",
  553. "PK4"
  554. ),
  555. "column_filter" => array (
  556. "logical_operator" => LogicalOperatorConst::CONST_AND,
  557. "sub_conditions" => array (
  558. array (
  559. "column_name" => "attr1",
  560. "value" => 1,
  561. "comparator" => ComparatorTypeConst::CONST_GREATER_THAN
  562. ),
  563. array (
  564. "column_name" => "attr4",
  565. "value" => 30,
  566. "comparator" => ComparatorTypeConst::CONST_LESS_THAN
  567. ),
  568. array (
  569. "logical_operator" => LogicalOperatorConst::CONST_OR,
  570. "sub_conditions" => array (
  571. array (
  572. "column_name" => "attr2",
  573. "value" => "aaaaa",
  574. "comparator" => ComparatorTypeConst::CONST_EQUAL
  575. ),
  576. array (
  577. "column_name" => "attr3",
  578. "value" => "tass",
  579. "comparator" => ComparatorTypeConst::CONST_EQUAL
  580. )
  581. )
  582. )
  583. )
  584. )
  585. );
  586. $getrowres = $this->otsClient->getRow ($querybody);
  587. $this->assertEquals ($getrowres['row']['primary_key_columns']["PK1"], $putdata2['primary_key']['PK1']);
  588. $this->assertEquals ($getrowres['row']['primary_key_columns']["PK2"], $putdata2['primary_key']['PK2']);
  589. $this->assertEquals ($getrowres['row']['primary_key_columns']["PK3"], $putdata2['primary_key']['PK3']);
  590. $this->assertEquals ($getrowres['row']['primary_key_columns'] ["PK4"], $putdata2 ['primary_key'] ['PK4'] );
  591. }
  592. }