CheckTypeTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. "Yson" => [
  40. "class" => YsonType::class,
  41. "values" => [
  42. "<a=b>c"
  43. ]
  44. ],
  45. "Bool" => [
  46. "class" => BoolType::class,
  47. "values" => [
  48. true, false
  49. ]
  50. ],
  51. "Int8" => [
  52. "class" => Int8Type::class,
  53. "values" => [
  54. -1*pow(2,7), 0, pow(2,7)-1
  55. ]
  56. ],
  57. "Uint8" => [
  58. "class" => Uint8Type::class,
  59. "values" => [
  60. 0, pow(2,8)-1
  61. ]
  62. ],
  63. "Int16" => [
  64. "class" => Int16Type::class,
  65. "values" => [
  66. -1*pow(2,15), 0, pow(2,15)-1
  67. ]
  68. ],
  69. "Uint16" => [
  70. "class" => Uint16Type::class,
  71. "values" => [
  72. 0, pow(2,16)-1
  73. ]
  74. ],
  75. "Int32" => [
  76. "class" => Int32Type::class,
  77. "values" => [
  78. -1*pow(2,31), 0, pow(2,31)-1
  79. ]
  80. ],
  81. "Uint32" => [
  82. "class" => Uint32Type::class,
  83. "values" => [
  84. 0, pow(2,32)-1
  85. ]
  86. ],
  87. "Int64" => [
  88. "class" => Int64Type::class,
  89. "values" => [
  90. -1*pow(2,63), 0, PHP_INT_MIN, 0x7FFFFFFFFFFFFFFF // 2^63 -1
  91. ]
  92. ],
  93. "Uint64" => [
  94. "class" => Uint64Type::class,
  95. "values" => [
  96. 0, 1<<64 -1 // 2^64 - 1
  97. ]
  98. ],
  99. "Float" => [
  100. "class" => FloatType::class,
  101. "values" => [
  102. 345.34534
  103. ]
  104. ],
  105. "Double" => [
  106. "class" => DoubleType::class,
  107. "values" => [
  108. -345.3453453745
  109. ]
  110. ],
  111. "String" => [
  112. "class" => StringType::class,
  113. "values" => [
  114. random_bytes(5)
  115. ]
  116. ],
  117. "Utf8" => [
  118. "class" => Utf8Type::class,
  119. "values" => [
  120. "", "YDB"
  121. ]
  122. ],
  123. "Json" => [
  124. "class" => JsonType::class,
  125. "values" => [
  126. [], (object)[
  127. "num" => 1
  128. ]
  129. ]
  130. ],
  131. "Date" => [
  132. "class" => DateType::class,
  133. "values" => [
  134. "2023-06-14"
  135. ]
  136. ],
  137. "Datetime" => [
  138. "class" => DatetimeType::class,
  139. "values" => [
  140. "2023-06-14 17:12:15"
  141. ]
  142. ],
  143. "Timestamp" => [
  144. "class" => TimestampType::class,
  145. "values" => [
  146. "2023-06-14 17:12:15.023476",
  147. "2023-06-14 17:12:15.000000"
  148. ]
  149. ]
  150. ];
  151. $ydb = new Ydb($config);
  152. $table = $ydb->table();
  153. $session = $table->createSession();
  154. $query = "DECLARE \$v as Optional<Int32>; SELECT \$v as val;";
  155. $prepared = $session->prepare($query);
  156. $result = $prepared->execute([
  157. 'v' => null,
  158. ]);
  159. $query = "DECLARE \$v as Optional<Int32>; SELECT \$v as val;";
  160. $prepared = $session->prepare($query);
  161. $result = $prepared->execute([
  162. 'v' => 4,
  163. ]);
  164. $query = "DECLARE \$v as Struct<x:Int32>; SELECT \$v as val;";
  165. $prepared = $session->prepare($query);
  166. $result = $prepared->execute([
  167. 'v' => ["x"=>2],
  168. ]);
  169. $query = "DECLARE \$v as List<Int32>; SELECT \$v as val;";
  170. $prepared = $session->prepare($query);
  171. $result = $prepared->execute([
  172. 'v' => [2],
  173. ]);
  174. $query = "DECLARE \$v as List<Int32>; SELECT \$v as val;";
  175. $prepared = $session->prepare($query);
  176. $result = $prepared->execute([
  177. 'v' => [],
  178. ]);
  179. foreach ($checkTypes as $type=>$data) {
  180. $query = "DECLARE \$v as $type; SELECT \$v as val;";
  181. $prepared = $session->prepare($query);
  182. foreach ($data["values"] as $value) {
  183. $result = $prepared->execute([
  184. 'v' => $value,
  185. ]);
  186. self::assertEquals(strtoupper($type),strtoupper($result->columns()[0]["type"]));
  187. self::assertEquals($value,$result->rows()[0]["val"]);
  188. }
  189. }
  190. }
  191. }