PageRenderTime 150ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/tags/storage-2009-04-14/QA_tests/LocalTableTest.php

#
PHP | 945 lines | 783 code | 97 blank | 65 comment | 15 complexity | 3ee88d788e14e3deb3006192c096f564 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. if (! defined ( 'PHPUnit_MAIN_METHOD' )) {
  3. define ( 'PHPUnit_MAIN_METHOD', 'Microsoft_WindowsAzure_LocalTableTest::main' );
  4. }
  5. require_once 'PHPUnit/Framework.php';
  6. //require_once 'TableTest.php';
  7. require_once 'Microsoft/WindowsAzure/Storage/Table.php';
  8. require_once 'TestTableEntity.php';
  9. class Microsoft_WindowsAzure_LocalTableTest extends PHPUnit_Framework_TestCase {
  10. protected static $tablePrefix = "phptabletest";
  11. protected static $partitionKeyPrefix = "partition";
  12. protected static $rowKeyPrefix = "row";
  13. protected static $uniqId = 0;
  14. protected $_tempTables = array ();
  15. protected $_tempEntities = array ();
  16. protected function generateTableName() {
  17. self::$uniqId ++;
  18. $name = self::$tablePrefix . self::$uniqId;
  19. $this->_tempTables [] = $name;
  20. return $name;
  21. }
  22. public function __construct() {
  23. require_once 'LocalTestConfiguration.php';
  24. self::$uniqId = mt_rand ( 0, 10000 );
  25. }
  26. /**
  27. * Test setup
  28. */
  29. protected function setUp() {
  30. }
  31. /**
  32. * Test teardown
  33. */
  34. protected function tearDown() {
  35. $tableName = "LocalTestTable";
  36. $storageClient = $this->_createStorageClient();
  37. $result = $storageClient->retrieveEntities ( $tableName);
  38. // $one = $result[0];
  39. if (is_array($result) && count($result) > 0){
  40. foreach ($result as $entity)
  41. try{
  42. $storageClient->deleteEntity ( $tableName, $entity );
  43. }catch (Exception $e){
  44. }
  45. }
  46. }
  47. public static function main() {
  48. $suite = new PHPUnit_Framework_TestSuite ( "Microsoft_WindowsAzure_LocalTableTest" );
  49. $result = PHPUnit_TextUI_TestRunner::run ( $suite );
  50. }
  51. function _createStorageClient() {
  52. return new Microsoft_WindowsAzure_Storage_Table ( TABLE_HOST, STORAGE_ACCOUNT, STORAGE_KEY, false, Microsoft_WindowsAzure_RetryPolicy::retryN ( 10, 250 ) );
  53. }
  54. protected function _generateEntities($count, $partition_increment = false, $row_increment = true) {
  55. if ($count <= 1) {
  56. $count = 1;
  57. }
  58. $eitities = array ();
  59. for($i = 1; $i <= $count; $i ++) {
  60. $eitities [] = new Test_TableEntity ( $partition_increment ? self::$partitionKeyPrefix . $i : self::$partitionKeyPrefix, $row_increment ? self::$rowKeyPrefix . $i : self::$rowKeyPrefix );
  61. }
  62. if ($count <= 1) {
  63. return $eitities [0];
  64. } else
  65. return $eitities;
  66. }
  67. function _tableExists($result, $tableName) {
  68. foreach ( $result as $table )
  69. if ($table->Name == $tableName) {
  70. return true;
  71. }
  72. return false;
  73. }
  74. function _randomString($length = 1) {
  75. $char = array ();
  76. for($i = 0; $i < $length; $i ++)
  77. $char [] = chr ( rand ( 65, 90 ) );
  78. return implode ( "", $char );
  79. }
  80. function _randomFloat($min, $max) {
  81. return ($min + lcg_value () * (abs ( $max - $min )));
  82. }
  83. /*
  84. * Test generate table in development storage
  85. */
  86. public function testGenerateTable() {
  87. $tableName = "TestTable"; //$this -> generateTableName();
  88. $storageClient = $this->_createStorageClient ();
  89. try {
  90. $storageClient->setOdbcSettings ( "local", "Administrator", "" );
  91. $result = $storageClient->generateDevelopmentTable ( "Test_TableEntity", $tableName );
  92. $result = $storageClient->listTables ();
  93. // $this->asserTrue($result->Name, $tableName);
  94. } catch ( Exception $e ) {
  95. $this->fail ( $ex->getMessage () );
  96. }
  97. $this -> assertTrue ( count($result)>0 );
  98. $this -> assertTrue ($this -> _tableExists($result, $tableName));
  99. }
  100. /*
  101. * Test generate table in development storage
  102. */
  103. public function tttestGenerateTable_Conflict() {
  104. $tableName = $this->generateTableName ();
  105. $storageClient = $this->_createStorageClient ();
  106. $exception = null;
  107. try {
  108. $storageClient->setOdbcSettings ( "local", "Administrator", "" );
  109. $result = $storageClient->generateDevelopmentTable ( "Simple_TableEntity", $tableName );
  110. $result = $storageClient->generateDevelopmentTable ( "Simple_TableEntity", $tableName );
  111. } catch ( Exception $ex ) {
  112. $exception = $ex;
  113. }
  114. $this->assertNotNull($exception, "table alread exists");
  115. $this->assertEquals("The table specified already exists.", $exception->getMessage());
  116. }
  117. /*
  118. * Test generate table in development storage with an illegal name
  119. */
  120. public function ttestGenerateTable_Illegal() {
  121. $tableName = "fs&d2_fa";
  122. $storageClient = $this->_createStorageClient ();
  123. $exception = null;
  124. try {
  125. $storageClient->setOdbcSettings ( "local", "Administrator", "" );
  126. $result = $storageClient->generateDevelopmentTable ( "Simple_TableEntity", $tableName );
  127. } catch ( Exception $e ) {
  128. $exception = $e;
  129. }
  130. $this->assertNotNull ($exception, "Illegal table name");
  131. $this->assertEqauls ( "One of the request is out of range", $exception->getMessage () );
  132. }
  133. /**
  134. * Test list tables
  135. */
  136. public function testListTables() {
  137. $storageClient = $this->_createStorageClient ();
  138. $tableName1 = $this->generateTableName ();
  139. $tableName2 = $this->generateTableName ();
  140. try {
  141. $storageClient->setOdbcSettings ( "local", "Administrator", "" );
  142. $storageClient->generateDevelopmentTable ( "Simple_TableEntity", $tableName1 );
  143. $storageClient->generateDevelopmentTable ( "Simple_TableEntity", $tableName2 );
  144. $result = $storageClient->listTables ();
  145. } catch ( Exception $ex ) {
  146. $this->fail ( $ex->getMessage () );
  147. }
  148. $this->assertTrue ( count ( $result ) > 2 );
  149. //Restart the development storage. then the new table can be found.
  150. $this->assertTrue ( $this->_tableExists ( $result, $tableName1 ) );
  151. $this->assertTrue ( $this->_tableExists ( $result, $tableName2 ) );
  152. }
  153. /**
  154. * Test delete table, The local Table service does not support dynamic deletion of tables.
  155. *
  156. */
  157. public function testDeleteTable() {
  158. //suppose that the table "deleteTable" is exist.
  159. $tableName = "deleteTable";
  160. $storageClient = $this->_createStorageClient ();
  161. try {
  162. $storageClient->deleteTable ( $tableName );
  163. $result = $storageClient->listTables ();
  164. $this->assertFalse ( $this->_tableExists ( $result, $tableName ) );
  165. } catch ( Exception $ex ) {
  166. $this->fail ( $ex->getMessage () );
  167. }
  168. }
  169. public function testDeleteTable_NotExists() {
  170. //Suppose the table "notexisttable" does not exist.
  171. $tableName = "notexisttable";
  172. $storageClient = $this->_createStorageClient ();
  173. $exception = null;
  174. try {
  175. $storageClient->deleteTable ( $tableName );
  176. } catch ( Exception $ex ) {
  177. $exception = $ex;
  178. }
  179. $this->assertNotNull($exception, "Table does not exist.");
  180. $this->assertEquals ( "The specified resource does not exist.", $exception->getMessage () );
  181. }
  182. public function testInsertEntity_DateTimeFieldInGMTFormat() {
  183. // The table LocalTestTable is exist.
  184. $tableName = "LocalTestTable";
  185. $storageClient = $this->_createStorageClient ();
  186. $entity = ($this->_generateEntities ( 1 ));
  187. $dateTime = $this->_gmtTimeFormat ();
  188. $entity->dateField = $dateTime;
  189. $exception = $null;
  190. try {
  191. $storageClient->insertEntity ( $tableName, $entity );
  192. } catch ( Exception $e ) {
  193. $exception = $e;
  194. }
  195. $this -> assertNotNull($exception, "Time format is not supported");
  196. }
  197. //Test insert operation
  198. public function testInsertEntity() {
  199. // The table LocalTestTable is exist.
  200. $tableName = "LocalTestTable";
  201. $storageClient = $this->_createStorageClient ();
  202. $entity = $this->_generateEntities ( 1 );
  203. $storageClient->insertEntity ( $tableName, $entity );
  204. $result = $storageClient->retrieveEntities ( $tableName );
  205. $this->assertEquals ( 1, sizeof ( $result ) );
  206. }
  207. public function testInsertEntity_Multiple() {
  208. // The table LocalTestTable is exist.
  209. $tableName = "LocalTestTable";
  210. $storageClient = $this->_createStorageClient ();
  211. $count = 3;
  212. $entities = $this->_generateEntities ( $count );
  213. foreach ( $entities as $entity ) {
  214. $storageClient->insertEntity ( $tableName, $entity );
  215. }
  216. $result = $storageClient->retrieveEntities ( $tableName );
  217. $this->assertEquals ( $count, sizeof ( $result ) );
  218. }
  219. public function testInsertEntity_LargeStringField() {
  220. // The table LocalTestTable is exist.
  221. $tableName = "LocalTestTable";
  222. $storageClient = $this->_createStorageClient ();
  223. $entity = ($this->_generateEntities ( 1 ));
  224. $string = str_repeat ( "hello ketty", 10 );
  225. $entity->stringField = $string;
  226. $storageClient->insertEntity ( $tableName, $entity );
  227. $result = $storageClient->retrieveEntities ( $tableName, "", "Test_TableEntity" );
  228. $one = $result [0];
  229. $this->assertTrue ( $one->stringField == $string );
  230. }
  231. public function testInsertEntity_NullField() {
  232. // The table LocalTestTable is exist.
  233. $tableName = "LocalTestTable";
  234. $storageClient = $this->_createStorageClient ();
  235. $entity = ($this->_generateEntities ( 1 ));
  236. $entity->stringField = null;
  237. $exception = null;
  238. try {
  239. $storageClient->insertEntity ( $tableName, $entity );
  240. } catch ( Exception $e ) {
  241. $exception = $e;
  242. }
  243. $this->assertTrue ( get_class ( $exception ) == Mircosoft_Azure_Exception );
  244. }
  245. public function testInsertEntity_Int32Field() {
  246. // The table LocalTestTable is exist.
  247. $tableName = "LocalTestTable";
  248. $storageClient = $this->_createStorageClient ();
  249. $entity = ($this->_generateEntities ( 1 ));
  250. $s = str_repeat ( "1", 10 );
  251. $invalue = ( int ) $s;
  252. $entity->int32Field = $invalue;
  253. $storageClient->insertEntity ( $tableName, $entity );
  254. $result = $storageClient->retrieveEntities ( $tableName, "", "Test_TableEntity" );
  255. $one = $result [0];
  256. $this->assertTrue ( $one->int32Field == $invalue );
  257. }
  258. public function testInsertEntity_Int32FieldTypeUnMatch() {
  259. // The table LocalTestTable is exist.
  260. $tableName = "LocalTestTable";
  261. $storageClient = $this->_createStorageClient ();
  262. $entity = ($this->_generateEntities ( 1 ));
  263. $invalue = 0.04;
  264. $entity->int32Field = $invalue;
  265. $exception = null;
  266. try {
  267. $storageClient->insertEntity ( $tableName, $entity );
  268. } catch ( Exception $e ) {
  269. $exception = $e;
  270. }
  271. $this->assertNotNull($exception, "Field value does not match its type");
  272. $this->_assertMircosoftAzureException ( $exception );
  273. }
  274. protected function _assertMircosoftAzureException($e) {
  275. $this->assertEquals ( 'Microsoft_WindowsAzure_Exception', get_class ( $e ) );
  276. }
  277. /**
  278. * Only ISO date format is suopport.
  279. *
  280. */
  281. public function testInsertEntity_DateTimeFieldInISOFormat() {
  282. // The table LocalTestTable is exist.
  283. $tableName = "LocalTestTable";
  284. $storageClient = $this->_createStorageClient ();
  285. $entity = ($this->_generateEntities ( 1 ));
  286. $dateTime = $this->_isoDate ();
  287. $entity->dateField = $dateTime;
  288. $storageClient->insertEntity ( $tableName, $entity );
  289. $result = $storageClient->retrieveEntities ( $tableName, "", "Test_TableEntity" );
  290. $one = $result [0];
  291. $this->assertTrue ( $one->dateField == $dateTime );
  292. $storageClient->deleteEntity ( $tableName, $entity );
  293. }
  294. public function testUpdateEntity_ValidField() {
  295. // The table LocalTestTable is exist.
  296. $tableName = "LocalTestTable";
  297. $storageClient = $this->_createStorageClient ();
  298. $entity = ($this->_generateEntities ( 1 ));
  299. $invalue = 123456;
  300. $entity->int32Field = $invalue;
  301. try {
  302. $storageClient->insertEntity ( $tableName, $entity );
  303. $result = $storageClient->retrieveEntities ( $tableName, "", "Test_TableEntity" );
  304. $one = $result [0];
  305. $this->assertEquals ( $entity->int32Field, $one->int32Field );
  306. $entity->int32Field = 555666;
  307. $storageClient->updateEntity ( $tableName, $entity );
  308. $result = $storageClient->retrieveEntities ( $tableName, "", "Test_TableEntity" );
  309. $one = $result [0];
  310. $this->assertEquals ( $entity->int32Field, $one->int32Field );
  311. } catch ( Exception $e ) {
  312. $this->fail ( $e->getMessage () );
  313. }
  314. }
  315. public function testUpdateEntity_InvalidField() {
  316. // The table LocalTestTable is exist.
  317. $tableName = "LocalTestTable";
  318. $storageClient = $this->_createStorageClient ();
  319. $entity = ($this->_generateEntities ( 1 ));
  320. $invalue = 123456;
  321. $entity->int32Field = $invalue;
  322. try {
  323. $storageClient->insertEntity ( $tableName, $entity );
  324. $result = $storageClient->retrieveEntities ( $tableName, "", "Test_TableEntity" );
  325. $one = $result [0];
  326. $this->assertEquals ( $entity->int32Field, $one->int32Field );
  327. $entity->int32Field = 0.0005;
  328. try {
  329. $storageClient->updateEntity ( $tableName, $entity );
  330. } catch ( Exception $e ) {
  331. $this->_assertMircosoftAzureException ( $e );
  332. $result = $storageClient->retrieveEntities ( $tableName, "", "Test_TableEntity" );
  333. $one = $result [0];
  334. $this->assertEquals ( $invalue, $one->int32Field );
  335. }
  336. } catch ( Exception $e ) {
  337. $this->fail ( $e->getMessage () );
  338. }
  339. }
  340. /**
  341. * Even the verify etag is not setting, you also can't change the etag by your self.
  342. *
  343. */
  344. public function testUpdateEntity_EtagChanged_NotVerifyEtag() {
  345. // The table LocalTestTable is exist.
  346. $tableName = "LocalTestTable";
  347. $storageClient = $this->_createStorageClient ();
  348. $entity = ($this->_generateEntities ( 1 ));
  349. $invalue = 123456;
  350. $entity->int32Field = $invalue;
  351. try {
  352. $storageClient->insertEntity ( $tableName, $entity );
  353. $result = $storageClient->retrieveEntities ( $tableName, "", "Test_TableEntity" );
  354. $one = $result [0];
  355. $this->assertEquals ( $entity->int32Field, $one->int32Field );
  356. $entity->int32Field = 88888;
  357. $etag = $entity->getEtag ();
  358. $newTagValue = 'newTagValue';
  359. $entity->setEtag ( $newTagValue );
  360. $storageClient->updateEntity ( $tableName, $entity, false );
  361. $result = $storageClient->retrieveEntities ( $tableName, "", "Test_TableEntity" );
  362. $one = $result [0];
  363. $this->assertEquals ( $entity->int32Field, $one->int32Field );
  364. //The eTag value is changed if update successfully
  365. $this->assertNotEquals ( $etag, $one->getEtag () );
  366. } catch ( Exception $e ) {
  367. $this->fail ( $e->getMessage () );
  368. }
  369. }
  370. public function testUpdateEntity_EtagChanged_VerifyEtag() {
  371. // The table LocalTestTable is exist.
  372. $tableName = "LocalTestTable";
  373. $storageClient = $this->_createStorageClient ();
  374. $entity = ($this->_generateEntities ( 1 ));
  375. $invalue = 123456;
  376. $entity->int32Field = $invalue;
  377. try {
  378. $storageClient->insertEntity ( $tableName, $entity );
  379. $result = $storageClient->retrieveEntities ( $tableName, "", "Test_TableEntity", true );
  380. $one = $result [0];
  381. $this->assertEquals ( $entity->int32Field, $one->int32Field );
  382. $entity->int32Field = 88888;
  383. $etag = $entity->getEtag ();
  384. $newTagValue = 'newTagValue';
  385. $entity->setEtag ( $newTagValue );
  386. try {
  387. $storageClient->updateEntity ( $tableName, $entity, true );
  388. $this->fail ( "Can't update with different etag if the verify etag is setted to true" );
  389. } catch ( Exception $e ) {
  390. $this->_assertMircosoftAzureException ( $e );
  391. $result = $storageClient->retrieveEntities ( $tableName, "", "Test_TableEntity" );
  392. $one = $result [0];
  393. //The value should not ne changed
  394. $this->assertEquals ( $invalue, $one->int32Field );
  395. $this->assertEquals ( $etag, $one->getEtag () );
  396. }
  397. } catch ( Exception $e ) {
  398. $this->fail ( $e->getMessage () );
  399. }
  400. }
  401. // public function testBatch_Rollback() {
  402. // // The table LocalTestTable is exist.
  403. // $tableName = "LocalTestTable";
  404. // $storageClient = $this->_createStorageClient ();
  405. //
  406. // $count = 2;
  407. // $entities = $this->_generateEntities ( $count );
  408. // foreach ( $entities as $entity ) {
  409. // $storageClient->insertEntity ( $tableName, $entity );
  410. // }
  411. //
  412. // foreach ( $entities as $entity ) {
  413. // $storageClient->deleteEntity ( $tableName, $entity );
  414. // }
  415. //
  416. // // Rollback
  417. // $batch->rollback ();
  418. // $result = $storageClient->retrieveEntities ( $tableName );
  419. // $this->assertEquals ( $count, sizeof ( $result ) );
  420. // }
  421. /**
  422. * Insert entity to table with some 'null' field is ok.
  423. *
  424. */
  425. public function testInsertEntity_FieldsWithNullValue() {
  426. // The table LocalTestTable is exist.
  427. $tableName = "LocalTestTable";
  428. $storageClient = $this->_createStorageClient ();
  429. $fileds = array ('date', 'string', 'binary', 'boolean', 'double', 'int32', 'int64', 'guid' );
  430. foreach ( $fileds as $field ) {
  431. $entitiy = $this->_generateEntities ( 1 );
  432. $newRowKey = $entitiy->getRowKey ();
  433. $entitiy->setRowKey ( $newRowKey . $field );
  434. $class = get_class ( $entitiy );
  435. $prop = new ReflectionProperty ( $class, $field . 'Field' );
  436. $prop->setValue ( $entitiy, null );
  437. echo "Test " . $field . " field with null value";
  438. try {
  439. $storageClient->insertEntity ( $tableName, $entitiy );
  440. } catch ( Exception $e ) {
  441. $this->fail ( $e->getMessage () . "\n" . $field + " Field with null value is not allowed when insert it to table!" );
  442. }
  443. }
  444. }
  445. /**
  446. * Insert entity to table with some 'null' field is ok, and try to retrieve them in development storage is ok.
  447. *
  448. */
  449. public function testRetrieveEntity_InsertWithNullField() {
  450. // The table LocalTestTable is exist.
  451. $tableName = "LocalTestTable";
  452. $storageClient = $this->_createStorageClient ();
  453. $fileds = array ('date', 'string', 'binary', 'boolean', 'double', 'int32', 'int64', 'guid' );
  454. foreach ( $fileds as $field ) {
  455. $entitiy = $this->_generateEntities ( 1 );
  456. $newRowKey = $entitiy->getRowKey ();
  457. $entitiy->setRowKey ( $newRowKey . $field );
  458. $class = get_class ( $entitiy );
  459. $prop = new ReflectionProperty ( $class, $field . 'Field' );
  460. $prop->setValue ( $entitiy, null );
  461. echo "Test " . $field . " field with null value";
  462. try {
  463. $storageClient->insertEntity ( $tableName, $entitiy );
  464. $storageClient->retrieveEntities ( $tableName, "", "Test_TableEntity" );
  465. } catch ( Exception $e ) {
  466. $this->fail ( $e->getMessage () . "\n" . $field + " Field with null value is not allowed when insert it to table!" );
  467. }
  468. }
  469. }
  470. public function testRetrieveEntities_Filter() {
  471. $entityClass = 'Test_TableEntity';
  472. $count = 3;
  473. $storageClient = $this->_createStorageClient ();
  474. // The table LocalTestTable is exist.
  475. $tableName = "LocalTestTable";
  476. $entities = $this->_generateEntities ( $count );
  477. foreach ( $entities as $entity ) {
  478. $storageClient->insertEntity ( $tableName, $entity );
  479. }
  480. $result = $storageClient->retrieveEntities ( $tableName, 'PartitionKey eq \'' . $entities [0]->getPartitionKey () . '\' and RowKey eq \'' . $entities [0]->getRowKey () . '\'', $entityClass );
  481. $this->assertEquals ( 1, count ( $result ) );
  482. $result = $storageClient->retrieveEntities ( $tableName, 'PartitionKey eq \'' . $entities [0]->getPartitionKey () . '\' or RowKey eq \'' . $entities [0]->getRowKey () . '\'', $entityClass );
  483. $this->assertEquals ( 3, count ( $result ) );
  484. $result = $storageClient->retrieveEntities ( $tableName, 'PartitionKey eq \'' . $entities [0]->getPartitionKey () . '\'', $entityClass );
  485. $this->assertEquals ( 3, count ( $result ) );
  486. $result = $storageClient->retrieveEntities ( $tableName, 'RowKey eq \'' . $entities [0]->getRowKey () . '\'', $entityClass );
  487. $this->assertEquals ( 1, count ( $result ) );
  488. $result = $storageClient->retrieveEntities ( $tableName, 'not (PartitionKey eq \'' . $entities [0]->getPartitionKey () . '\' and RowKey eq \'' . $entities [0]->getRowKey () . '\')', $entityClass );
  489. $this->assertEquals ( 2, count ( $result ) );
  490. $result = $storageClient->retrieveEntities ( $tableName, '(not (PartitionKey eq \'' . $entities [0]->getPartitionKey () . '\')) and RowKey eq \'' . $entities [0]->getRowKey () . '\'', $entityClass );
  491. $this->assertEquals ( 0, count ( $result ) );
  492. }
  493. public function testRetrieveEntities_Query() {
  494. $entityClass = 'Test_TableEntity';
  495. $count = 3;
  496. $storageClient = $this->_createStorageClient ();
  497. // The table LocalTestTable is exist.
  498. $tableName = "LocalTestTable";
  499. $entities = $this->_generateEntities ( $count );
  500. $entities [0]->int64Field = mt_rand ( 10, 10000 );
  501. foreach ( $entities as $entity ) {
  502. $storageClient->insertEntity ( $tableName, $entity );
  503. }
  504. $result = $storageClient->retrieveEntities ( $storageClient->select ()->from ( $tableName )->where ( 'PartitionKey eq ?', $entities [0]->getPartitionKey () )->andWhere ( 'RowKey eq ?', $entities [0]->getRowKey () ), $entityClass );
  505. $this->assertEquals ( 1, count ( $result ) );
  506. $this->assertEquals ( $entities [0]->int64Field, $result [0]->int64Field );
  507. /* in development storage, when carry on below query will thrown an exception.
  508. *
  509. * Does not support wherePartitionKey and whereRowKey
  510. */
  511. $result = $storageClient->retrieveEntities ( $storageClient->select ()->from ( $tableName )->wherePartitionKey ( $entities [0]->getPartitionKey () ), $entityClass );
  512. $result = $storageClient->retrieveEntities ( $storageClient->select ()->from ( $tableName )->where ('PartitionKey eq ?', $entities [0]->getPartitionKey () ), $entityClass );
  513. $this->assertEquals ( $count, count ( $result ) );
  514. $result = $storageClient->retrieveEntities ( $storageClient->select ()->from ( $tableName )->whereRowKey ( $entities [0]->getRowKey () ), $entityClass );
  515. $this->assertEquals ( 1, count ( $result ) );
  516. }
  517. public function testRetrieveEntities_QueryTop() {
  518. $entityClass = 'Test_TableEntity';
  519. $count = 5;
  520. $storageClient = $this->_createStorageClient ();
  521. // The table LocalTestTable is exist.
  522. $tableName = "LocalTestTable";
  523. $entities = $this->_generateEntities ( $count );
  524. $entities [0]->int64Field = mt_rand ( 10, 10000 );
  525. foreach ( $entities as $entity ) {
  526. $storageClient->insertEntity ( $tableName, $entity );
  527. }
  528. $result = $storageClient->retrieveEntities ( $storageClient->select ()->from ( $tableName )->top ( 3 ), $entityClass );
  529. $this->assertEquals ( 3, count ( $result ) );
  530. $result = $storageClient->retrieveEntities ( $storageClient->select ()->from ( $tableName )->top ( 1 ), $entityClass );
  531. $this->assertEquals ( 1, count ( $result ) );
  532. $result = $storageClient->retrieveEntities ( $storageClient->select ()->from ( $tableName )->top ( 0 ), $entityClass );
  533. $this->assertEquals ( 0, count ( $result ) );
  534. $result = $storageClient->retrieveEntities ( $storageClient->select ()->from ( $tableName )->top ( 10 ) ); // dynamic entity
  535. $this->assertEquals ( $count, count ( $result ) );
  536. }
  537. public function testRetrieveEntities_QueryOrderby() {
  538. $count = 5;
  539. $storageClient = $this->_createStorageClient ();
  540. // The table LocalTestTable is exist.
  541. $tableName = "LocalTestTable";
  542. $entities = $this->_generateEntities ( $count );
  543. foreach ( $entities as $entity ) {
  544. $entity->int64Field = mt_rand ( 10, 10000 );
  545. $storageClient->insertEntity ( $tableName, $entity );
  546. }
  547. $exception = null;
  548. try {
  549. $result = $storageClient->retrieveEntities ( $storageClient->select ()->from ( $tableName )->orderBy ( 'int64Field', 'asc' ), 'Test_TableEntity' );
  550. $this->assertEquals ( $count, count ( $result ) );
  551. } catch ( Exception $ex ) {
  552. $exception = $ex;
  553. }
  554. $this -> assertNotNull($exception);
  555. }
  556. public function testRetrieveEntityById() {
  557. // The table LocalTestTable is exist.
  558. $tableName = "LocalTestTable";
  559. $storageClient = $this->_createStorageClient ();
  560. $count = 3;
  561. $entities = $this->_generateEntities ( $count );
  562. foreach ( $entities as $entity ) {
  563. $entity->int32Field = mt_rand ( 10, 10000 );
  564. $entity->stringField = $this->_randomString ( 10 );
  565. $storageClient->insertEntity ( $tableName, $entity );
  566. }
  567. $result = $storageClient->retrieveEntityById ( $tableName, $entities [0]->getPartitionKey (), $entities [0]->getRowKey (), 'Test_TableEntity' );
  568. $this->assertEquals ( $entities [0]->int32Field, $result->int32Field );
  569. $this->assertEquals ( $entities [0]->stringField, $result->stringField );
  570. }
  571. public function testRetrieveEntityById_NotExists() {
  572. // The table LocalTestTable is exist.
  573. $tableName = "LocalTestTable";
  574. $storageClient = $this->_createStorageClient ();
  575. $count = 2;
  576. $exception = null;
  577. $entities = $this->_generateEntities ( $count );
  578. foreach ( $entities as $entity ) {
  579. $storageClient->insertEntity ( $tableName, $entity );
  580. }
  581. try {
  582. $storageClient->retrieveEntityById ( $tableName, $entities [0]->getPartitionKey () . "XXX", $entities [0]->getRowKey (), 'Test_TableEntity' );
  583. $this->fail ( "the ID does not exist." );
  584. } catch ( Exception $ex ) {
  585. $exception = $ex;
  586. }
  587. $this->assertNotNull($exception,"No entity with specified key");
  588. $this->assertEquals("The specified resource does not exist.", $exception->getMessage());
  589. try {
  590. $storageClient->retrieveEntityById ( "nosuchtableentity", $entities [0]->getPartitionKey () . "XXX", $entities [0]->getRowKey (), 'Test_TableEntity' );
  591. // $this->fail ( "the ID does not exist." );
  592. } catch ( Exception $ex ) {
  593. $exception = $ex;
  594. }
  595. $this->assertNotNull($exception,"Table does not exist");
  596. $this->assertEquals( "The table specified does not exist.", $exception->getMessage());
  597. }
  598. /**
  599. * Query entities by int field(include Edm.int32/Edm.int64)
  600. *
  601. */
  602. // public function testQueryByIntField() {
  603. // $entityClass = 'Test_TableEntity';
  604. // // The table LocalTestTable is exist.
  605. // $tableName = "LocalTestTable";
  606. // $storageClient = $this->_createStorageClient ();
  607. // try {
  608. //
  609. // $entities = $this->_generateEntities ( $count );
  610. // for($i = 1; $i <= count ( $entities ); $i ++) {
  611. // $entity = $entities [$i - 1];
  612. // $entity->int32Field = $i;
  613. // $storageClient->insertEntity ( $tableName, $entity );
  614. // }
  615. //
  616. // $result = $storageClient->retrieveEntities ( $tableName, "int32Field le 10", $entityClass );
  617. // $this->assertEquals ( 10, count ( $result ) );
  618. // } catch ( Exception $e ) {
  619. // $this->fail ( $e->getMessage () );
  620. // }
  621. //
  622. // for($i = 1; $i <= count ( $entities ); $i ++) {
  623. // $storageClient->deleteEntity ( $tableName, $entity );
  624. // }
  625. // }
  626. /**
  627. * Query entities by string field
  628. *
  629. */
  630. public function testQueryByStringField() {
  631. $entityClass = 'Test_TableEntity';
  632. //The table LocalTestTable is exist.
  633. $tableName = "LocalTestTable";
  634. $storageClient = $this->_createStorageClient ();
  635. $count = 20;
  636. $entities = $this->_generateEntities ( $count );
  637. try {
  638. for($i = 1; $i <= count ( $entities ); $i ++) {
  639. $entity = $entities [$i - 1];
  640. $entity->stringField = "A" . $i;
  641. if ($i > 10) {
  642. $entity->stringField = "B" . $i;
  643. }
  644. $storageClient->insertEntity ( $tableName, $entity );
  645. }
  646. $result = $storageClient->retrieveEntities ( $tableName, "stringField lt " . "'" . "B" . "'", $entityClass );
  647. $this->assertEquals ( 10, count ( $result ) );
  648. } catch ( Exception $e ) {
  649. $this->fail ( $e->getMessage () );
  650. }
  651. }
  652. /**
  653. * Query entities by boolean field
  654. *
  655. */
  656. public function testQueryByBooleanField() {
  657. $entityClass = 'Test_TableEntity';
  658. //The table LocalTestTable is exist.
  659. $tableName = "LocalTestTable";
  660. $storageClient = $this->_createStorageClient ();
  661. try {
  662. $count = 20;
  663. $entities = $this->_generateEntities ( $count );
  664. for($i = 1; $i <= count ( $entities ); $i ++) {
  665. $entity = $entities [$i - 1];
  666. if ($i > 10) {
  667. $entity->booleanField = true;
  668. } else {
  669. $entity->booleanField = true;
  670. }
  671. $storageClient->insertEntity ( $tableName, $entity );
  672. }
  673. $result = $storageClient->retrieveEntities ( $tableName, "booleanField eq " . 'true', $entityClass );
  674. $this->assertEquals ( 20, count ( $result ) );
  675. } catch ( Exception $e ) {
  676. $this->fail ( $e->getMessage () );
  677. }
  678. }
  679. public function testInsertEntity_BinayField() {
  680. //The table LocalTestTable is exist.
  681. $tableName = "LocalTestTable";
  682. $storageClient = $this->_createStorageClient ();
  683. $entitiy = $this->_generateEntities ( 1 );
  684. $binary = base64_encode ( "I love youlllllllllllllllllllllll" );
  685. $entitiy->binaryField = $binary;
  686. try {
  687. $storageClient->insertEntity ( $tableName, $entitiy );
  688. $one = array_pop ( $storageClient->retrieveEntities ( $tableName ) );
  689. $this->assertEquals ( $binary, $one->binaryField );
  690. } catch ( Exception $e ) {
  691. $this->fail ( $e->getMessage () );
  692. }
  693. }
  694. /**
  695. * Edm.binary support value encoded by Base64
  696. *
  697. */
  698. public function testInsertEntity_BinayFieldNotBase64() {
  699. //The table LocalTestTable is exist.
  700. $tableName = "LocalTestTable";
  701. $storageClient = $this->_createStorageClient ();
  702. $entitiy = $this->_generateEntities ( 1 );
  703. $binary = "I love youlllllllllllllllllllllll";
  704. $entitiy->binaryField = $binary;
  705. try {
  706. $storageClient->insertEntity ( $tableName, $entitiy );
  707. } catch ( Exception $e ) {
  708. $this->assertTrue ( $e != null );
  709. }
  710. }
  711. /**
  712. * The field name can't be started with a $.
  713. *
  714. */
  715. public function testInsertEntity_FieldNameWithInvalidPrefix() {
  716. //The table LocalTestTable is exist.
  717. $tableName = "LocalTestTable";
  718. $storageClient = $this->_createStorageClient ();
  719. $entitiy = new Simple_TableEntity ( 'aaa', 'bbb' );
  720. $binary = "I love youlllllllllllllllllllllll";
  721. $entitiy->binaryField = $binary;
  722. $exception = null;
  723. try {
  724. $storageClient->insertEntity ( $tableName, $entitiy );
  725. } catch ( Exception $e ) {
  726. $exception = $e;
  727. }
  728. $this->assertNotNull($exception, "Invalid entity field");
  729. }
  730. public function testMergeStaticEntity() {
  731. $entity = $this->_generateEntities ( 1 );
  732. $entity->stringField = null;
  733. $entity->binaryField = null;
  734. //The table LocalTestTable is exist.
  735. $tableName = "LocalTestTable";
  736. $storageClient = $this->_createStorageClient ();
  737. try {
  738. $storageClient->insertEntity ( $tableName, $entity );
  739. $entity->stringField = "merge string value";
  740. $entity->binaryField = base64_encode ( "I love you!" );
  741. $storageClient->mergeEntity ( $tableName, $entity );
  742. $result = $storageClient->retrieveEntities ( $tableName, "", "Test_TableEntity" );
  743. $one = $result [0];
  744. //merged field
  745. $this->assertEquals ( $entity->stringField, $one->stringField );
  746. $this->assertEquals ( $entity->binaryField, $one->binaryField );
  747. // not merged field
  748. $this->assertEquals ( $entity->int32Field, $one->int32Field );
  749. $this->assertEquals ( $entity->int64Field, $one->int64Field );
  750. } catch ( Exception $e ) {
  751. $this->fail ( $e->getMessage () );
  752. }
  753. }
  754. /* Generate ISO 8601 compliant date string in UTC time zone
  755. *
  756. * @return string
  757. */
  758. protected function _isoDate() {
  759. $tz = @date_default_timezone_get ();
  760. @date_default_timezone_set ( 'UTC' );
  761. $returnValue = str_replace ( '+00:00', 'Z', @date ( 'c' ) );
  762. @date_default_timezone_set ( $tz );
  763. return $returnValue;
  764. }
  765. protected function _gmtTime() {
  766. return new DateTime ( "now", new DateTimeZone ( 'GMT' ) );
  767. }
  768. protected function _gmtTimeFormat() {
  769. $dateTime = new DateTime ( "now", new DateTimeZone ( 'GMT' ) );
  770. return $dateTime->format ( "Y-m-d H:i:s" );
  771. }
  772. }
  773. class Simple_TableEntity extends Microsoft_WindowsAzure_Storage_TableEntity {
  774. /**
  775. * @azure Name Edm.String
  776. */
  777. public $stringField = "name";
  778. /**
  779. * @azure Address Edm.String
  780. */
  781. public $stringField1 = "address";
  782. }
  783. // Call Microsoft_WindowsAzure_LocalTableTest::main() if this source file is executed directly.
  784. if (PHPUnit_MAIN_METHOD == "Microsoft_WindowsAzure_LocalTableTest::main") {
  785. Microsoft_WindowsAzure_LocalBlobTest::main ();
  786. }
  787. ?>