CheckTypeTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace YdbPlatform\Ydb\Test;
  3. use PHPUnit\Framework\TestCase;
  4. use YdbPlatform\Ydb\Auth\Implement\AnonymousAuthentication;
  5. use YdbPlatform\Ydb\Types\BoolType;
  6. use YdbPlatform\Ydb\Types\DatetimeType;
  7. use YdbPlatform\Ydb\Types\DateType;
  8. use YdbPlatform\Ydb\Types\DoubleType;
  9. use YdbPlatform\Ydb\Types\FloatType;
  10. use YdbPlatform\Ydb\Types\Int16Type;
  11. use YdbPlatform\Ydb\Types\Int32Type;
  12. use YdbPlatform\Ydb\Types\Int64Type;
  13. use YdbPlatform\Ydb\Types\Int8Type;
  14. use YdbPlatform\Ydb\Types\JsonType;
  15. use YdbPlatform\Ydb\Types\StringType;
  16. use YdbPlatform\Ydb\Types\TimestampType;
  17. use YdbPlatform\Ydb\Types\Uint16Type;
  18. use YdbPlatform\Ydb\Types\Uint32Type;
  19. use YdbPlatform\Ydb\Types\Uint64Type;
  20. use YdbPlatform\Ydb\Types\Uint8Type;
  21. use YdbPlatform\Ydb\Types\Utf8Type;
  22. use YdbPlatform\Ydb\Ydb;
  23. class CheckTypeTest extends TestCase{
  24. public function testCheckTypesInDeclare(){
  25. $config = [
  26. // Database path
  27. 'database' => '/local',
  28. // Database endpoint
  29. 'endpoint' => 'localhost:2136',
  30. // Auto discovery (dedicated server only)
  31. 'discovery' => false,
  32. // IAM config
  33. 'iam_config' => [
  34. 'insecure' => true,
  35. ],
  36. 'credentials' => new AnonymousAuthentication()
  37. ];
  38. $checkTypes = [
  39. "Bool" => [
  40. "class" => BoolType::class,
  41. "values" => [
  42. true, false
  43. ]
  44. ],
  45. "Int8" => [
  46. "class" => Int8Type::class,
  47. "values" => [
  48. -1*pow(2,7), 0, pow(2,7)-1
  49. ]
  50. ],
  51. "Uint8" => [
  52. "class" => Uint8Type::class,
  53. "values" => [
  54. 0, pow(2,8)-1
  55. ]
  56. ],
  57. "Int16" => [
  58. "class" => Int16Type::class,
  59. "values" => [
  60. -1*pow(2,15), 0, pow(2,15)-1
  61. ]
  62. ],
  63. "Uint16" => [
  64. "class" => Uint16Type::class,
  65. "values" => [
  66. 0, pow(2,16)-1
  67. ]
  68. ],
  69. "Int32" => [
  70. "class" => Int32Type::class,
  71. "values" => [
  72. -1*pow(2,31), 0, pow(2,31)-1
  73. ]
  74. ],
  75. "Uint32" => [
  76. "class" => Uint32Type::class,
  77. "values" => [
  78. 0, pow(2,32)-1
  79. ]
  80. ],
  81. "Int64" => [
  82. "class" => Int64Type::class,
  83. "values" => [
  84. -1*pow(2,63), 0, PHP_INT_MIN, 0x7FFFFFFFFFFFFFFF // 2^63 -1
  85. ]
  86. ],
  87. "Uint64" => [
  88. "class" => Uint64Type::class,
  89. "values" => [
  90. 0, 1<<64 -1 // 2^64 - 1
  91. ]
  92. ],
  93. "Float" => [
  94. "class" => FloatType::class,
  95. "values" => [
  96. 345.34534
  97. ]
  98. ],
  99. "Double" => [
  100. "class" => DoubleType::class,
  101. "values" => [
  102. -345.3453453745
  103. ]
  104. ],
  105. "String" => [
  106. "class" => StringType::class,
  107. "values" => [
  108. random_bytes(5)
  109. ]
  110. ],
  111. "Utf8" => [
  112. "class" => Utf8Type::class,
  113. "values" => [
  114. "", "YDB"
  115. ]
  116. ],
  117. "Json" => [
  118. "class" => JsonType::class,
  119. "values" => [
  120. [], (object)[
  121. "num" => 1
  122. ]
  123. ]
  124. ],
  125. "Date" => [
  126. "class" => DateType::class,
  127. "values" => [
  128. "2023-06-14"
  129. ]
  130. ],
  131. "Datetime" => [
  132. "class" => DatetimeType::class,
  133. "values" => [
  134. "2023-06-14 17:12:15"
  135. ]
  136. ],
  137. "Timestamp" => [
  138. "class" => TimestampType::class,
  139. "values" => [
  140. "2023-06-14 17:12:15.023476",
  141. "2023-06-14 17:12:15.000000"
  142. ]
  143. ]
  144. ];
  145. $ydb = new Ydb($config);
  146. $table = $ydb->table();
  147. $session = $table->createSession();
  148. $query = "DECLARE \$v as Optional<Int32>; SELECT \$v as val;";
  149. $prepared = $session->prepare($query);
  150. $result = $prepared->execute([
  151. 'v' => null,
  152. ]);
  153. $query = "DECLARE \$v as Optional<Int32>; SELECT \$v as val;";
  154. $prepared = $session->prepare($query);
  155. $result = $prepared->execute([
  156. 'v' => 4,
  157. ]);
  158. $query = "DECLARE \$v as Struct<x:Int32>; SELECT \$v as val;";
  159. $prepared = $session->prepare($query);
  160. $result = $prepared->execute([
  161. 'v' => ["x"=>2],
  162. ]);
  163. $query = "DECLARE \$v as List<Int32>; SELECT \$v as val;";
  164. $prepared = $session->prepare($query);
  165. $result = $prepared->execute([
  166. 'v' => [2],
  167. ]);
  168. $query = "DECLARE \$v as List<Int32>; SELECT \$v as val;";
  169. $prepared = $session->prepare($query);
  170. $result = $prepared->execute([
  171. 'v' => [],
  172. ]);
  173. foreach ($checkTypes as $type=>$data) {
  174. $query = "DECLARE \$v as $type; SELECT \$v as val;";
  175. $prepared = $session->prepare($query);
  176. foreach ($data["values"] as $value) {
  177. $result = $prepared->execute([
  178. 'v' => $value,
  179. ]);
  180. self::assertEquals(strtoupper($type),strtoupper($result->columns()[0]["type"]));
  181. self::assertEquals($value,$result->rows()[0]["val"]);
  182. }
  183. }
  184. }
  185. }