|
1 | 1 | from decimal import Decimal |
| 2 | +from datetime import date, datetime |
2 | 3 |
|
| 4 | +import pytest |
3 | 5 | import sqlalchemy as sa |
4 | 6 | from sqlalchemy import Table, Column, Integer, Unicode |
5 | | - |
6 | 7 | from sqlalchemy.testing.fixtures import TestBase, TablesTest |
7 | 8 |
|
8 | | -from datetime import date, datetime |
| 9 | +import ydb |
| 10 | +from ydb._grpc.v4.protos import ydb_common_pb2 |
| 11 | + |
| 12 | +from ydb_sqlalchemy.sqlalchemy import types |
9 | 13 |
|
10 | 14 |
|
11 | 15 | def clear_sql(stm): |
@@ -200,3 +204,166 @@ def test_select_types(self, connection): |
200 | 204 |
|
201 | 205 | row = connection.execute(sa.select(tb)).fetchone() |
202 | 206 | assert row == (1, "Hello World!", 3.5, True, now, today) |
| 207 | + |
| 208 | + |
| 209 | +class TestWithClause(TablesTest): |
| 210 | + run_create_tables = "each" |
| 211 | + |
| 212 | + @staticmethod |
| 213 | + def _create_table_and_get_desc(connection, metadata, **kwargs): |
| 214 | + table = Table( |
| 215 | + "clause_with_test", |
| 216 | + metadata, |
| 217 | + Column("id", types.UInt32, primary_key=True), |
| 218 | + **kwargs, |
| 219 | + ) |
| 220 | + table.create(connection) |
| 221 | + |
| 222 | + session: ydb.Session = connection.connection.driver_connection.pool.acquire() |
| 223 | + table_description = session.describe_table("/local/" + table.name) |
| 224 | + session.delete() |
| 225 | + return table_description |
| 226 | + |
| 227 | + @pytest.mark.parametrize( |
| 228 | + "auto_partitioning_by_size,res", |
| 229 | + [ |
| 230 | + (None, ydb_common_pb2.FeatureFlag.Status.ENABLED), |
| 231 | + (True, ydb_common_pb2.FeatureFlag.Status.ENABLED), |
| 232 | + (False, ydb_common_pb2.FeatureFlag.Status.DISABLED), |
| 233 | + ], |
| 234 | + ) |
| 235 | + def test_auto_partitioning_by_size(self, connection, auto_partitioning_by_size, res, metadata): |
| 236 | + desc = self._create_table_and_get_desc( |
| 237 | + connection, metadata, ydb_auto_partitioning_by_size=auto_partitioning_by_size |
| 238 | + ) |
| 239 | + assert desc.partitioning_settings.partitioning_by_size == res |
| 240 | + |
| 241 | + @pytest.mark.parametrize( |
| 242 | + "auto_partitioning_by_load,res", |
| 243 | + [ |
| 244 | + (None, ydb_common_pb2.FeatureFlag.Status.DISABLED), |
| 245 | + (True, ydb_common_pb2.FeatureFlag.Status.ENABLED), |
| 246 | + (False, ydb_common_pb2.FeatureFlag.Status.DISABLED), |
| 247 | + ], |
| 248 | + ) |
| 249 | + def test_auto_partitioning_by_load(self, connection, auto_partitioning_by_load, res, metadata): |
| 250 | + desc = self._create_table_and_get_desc( |
| 251 | + connection, |
| 252 | + metadata, |
| 253 | + ydb_auto_partitioning_by_load=auto_partitioning_by_load, |
| 254 | + ) |
| 255 | + assert desc.partitioning_settings.partitioning_by_load == res |
| 256 | + |
| 257 | + @pytest.mark.parametrize( |
| 258 | + "auto_partitioning_partition_size_mb,res", |
| 259 | + [ |
| 260 | + (None, 2048), |
| 261 | + (2000, 2000), |
| 262 | + ], |
| 263 | + ) |
| 264 | + def test_auto_partitioning_partition_size_mb(self, connection, auto_partitioning_partition_size_mb, res, metadata): |
| 265 | + desc = self._create_table_and_get_desc( |
| 266 | + connection, |
| 267 | + metadata, |
| 268 | + ydb_auto_partitioning_partition_size_mb=auto_partitioning_partition_size_mb, |
| 269 | + ) |
| 270 | + assert desc.partitioning_settings.partition_size_mb == res |
| 271 | + |
| 272 | + @pytest.mark.parametrize( |
| 273 | + "auto_partitioning_min_partitions_count,res", |
| 274 | + [ |
| 275 | + (None, 1), |
| 276 | + (10, 10), |
| 277 | + ], |
| 278 | + ) |
| 279 | + def test_auto_partitioning_min_partitions_count( |
| 280 | + self, |
| 281 | + connection, |
| 282 | + auto_partitioning_min_partitions_count, |
| 283 | + res, |
| 284 | + metadata, |
| 285 | + ): |
| 286 | + desc = self._create_table_and_get_desc( |
| 287 | + connection, |
| 288 | + metadata, |
| 289 | + ydb_auto_partitioning_min_partitions_count=auto_partitioning_min_partitions_count, |
| 290 | + ) |
| 291 | + assert desc.partitioning_settings.min_partitions_count == res |
| 292 | + |
| 293 | + @pytest.mark.parametrize( |
| 294 | + "auto_partitioning_max_partitions_count,res", |
| 295 | + [ |
| 296 | + (None, 0), |
| 297 | + (10, 10), |
| 298 | + ], |
| 299 | + ) |
| 300 | + def test_auto_partitioning_max_partitions_count( |
| 301 | + self, |
| 302 | + connection, |
| 303 | + auto_partitioning_max_partitions_count, |
| 304 | + res, |
| 305 | + metadata, |
| 306 | + ): |
| 307 | + desc = self._create_table_and_get_desc( |
| 308 | + connection, |
| 309 | + metadata, |
| 310 | + ydb_auto_partitioning_max_partitions_count=auto_partitioning_max_partitions_count, |
| 311 | + ) |
| 312 | + assert desc.partitioning_settings.max_partitions_count == res |
| 313 | + |
| 314 | + @pytest.mark.parametrize( |
| 315 | + "uniform_partitions,res", |
| 316 | + [ |
| 317 | + (None, 1), |
| 318 | + (10, 10), |
| 319 | + ], |
| 320 | + ) |
| 321 | + def test_uniform_partitions( |
| 322 | + self, |
| 323 | + connection, |
| 324 | + uniform_partitions, |
| 325 | + res, |
| 326 | + metadata, |
| 327 | + ): |
| 328 | + desc = self._create_table_and_get_desc( |
| 329 | + connection, |
| 330 | + metadata, |
| 331 | + ydb_uniform_partitions=uniform_partitions, |
| 332 | + ) |
| 333 | + # it not only do the initiation partition but also set up the minimum partition count |
| 334 | + assert desc.partitioning_settings.min_partitions_count == res |
| 335 | + |
| 336 | + @pytest.mark.parametrize( |
| 337 | + "partition_at_keys,res", |
| 338 | + [ |
| 339 | + (None, 1), |
| 340 | + ((100, 1000), 3), |
| 341 | + ], |
| 342 | + ) |
| 343 | + def test_partition_at_keys( |
| 344 | + self, |
| 345 | + connection, |
| 346 | + partition_at_keys, |
| 347 | + res, |
| 348 | + metadata, |
| 349 | + ): |
| 350 | + desc = self._create_table_and_get_desc( |
| 351 | + connection, |
| 352 | + metadata, |
| 353 | + ydb_partition_at_keys=partition_at_keys, |
| 354 | + ) |
| 355 | + assert desc.partitioning_settings.min_partitions_count == res |
| 356 | + |
| 357 | + def test_several_keys(self, connection, metadata): |
| 358 | + desc = self._create_table_and_get_desc( |
| 359 | + connection, |
| 360 | + metadata, |
| 361 | + ydb_auto_partitioning_by_size=True, |
| 362 | + ydb_auto_partitioning_by_load=True, |
| 363 | + ydb_auto_partitioning_min_partitions_count=3, |
| 364 | + ydb_auto_partitioning_max_partitions_count=5, |
| 365 | + ) |
| 366 | + assert desc.partitioning_settings.partitioning_by_size == 1 |
| 367 | + assert desc.partitioning_settings.partitioning_by_load == 1 |
| 368 | + assert desc.partitioning_settings.min_partitions_count == 3 |
| 369 | + assert desc.partitioning_settings.max_partitions_count == 5 |
0 commit comments