Skip to content

Commit 8a61ecc

Browse files
author
Atin65536
committed
+ introduce MachinePool "connection_uri" unique index.
Closes #245
1 parent 9fc7fbc commit 8a61ecc

5 files changed

Lines changed: 522 additions & 3 deletions

File tree

genesis_core/tests/functional/conftest.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,3 +915,72 @@ def setup_db_for_worker(worker_id):
915915
engine.close_connection(conn)
916916
del engine
917917
engines.engine_factory.destroy_engine()
918+
919+
920+
921+
@pytest.fixture(scope="session")
922+
def main_db_url() -> str:
923+
return consts.get_database_uri()
924+
925+
926+
@pytest.fixture(scope="session")
927+
def test_db_name(worker_id: tp.Any) -> str:
928+
return "test" if not worker_id else f"test_{worker_id}"
929+
930+
931+
@pytest.fixture(scope="session")
932+
def test_db_url(main_db_url: str, test_db_name: str) -> str:
933+
parsed_db_url = urlparse(main_db_url)
934+
return parsed_db_url._replace(path=test_db_name).geturl()
935+
936+
937+
@pytest.fixture(scope="session")
938+
def test_db(
939+
main_db_url: str,
940+
test_db_url: str,
941+
test_db_name: str,
942+
) -> tp.Iterable[
943+
test_utils.TestDBManager
944+
]:
945+
class MainDBManager(test_utils.TestDBManager):
946+
manager_config = test_utils.TestDBManagerConfig(
947+
database_url=main_db_url,
948+
create_db=test_db_name,
949+
engine_alias="test_main",
950+
)
951+
952+
class TestDBManager(test_utils.TestDBManager):
953+
manager_config = test_utils.TestDBManagerConfig(
954+
database_url=test_db_url,
955+
engine_alias="test",
956+
)
957+
958+
with MainDBManager() as main_db_manager:
959+
with main_db_manager.db():
960+
with TestDBManager() as test_db_manager:
961+
yield test_db_manager
962+
963+
964+
@pytest.fixture(scope="session")
965+
def test_migrations_manager(
966+
test_db: test_utils.TestDBManager,
967+
) -> tp.Iterable[None]:
968+
class TestMigrationManager(test_utils.TestMigrationManager):
969+
migration_config = test_utils.TestMigrationManagerConfig(
970+
first_migration=FIRST_MIGRATION,
971+
)
972+
973+
with TestMigrationManager(db_manager=test_db) as migration_manager:
974+
with migration_manager.migrations():
975+
yield
976+
977+
978+
@pytest.fixture()
979+
def test_session(
980+
test_db: test_utils.TestDBManager,
981+
test_migrations_manager: None,
982+
) -> tp.Iterable[
983+
test_utils.AbstractSession
984+
]:
985+
with test_db.session() as session:
986+
yield session

genesis_core/tests/functional/dm/__init__.py

Whitespace-only changes.
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# Copyright 2026 Genesis Corporation.
2+
#
3+
# All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License. You may obtain
7+
# a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
# License for the specific language governing permissions and limitations
15+
# under the License.
16+
17+
import typing as tp
18+
19+
import pytest
20+
21+
from restalchemy.storage import exceptions
22+
23+
from genesis_core.compute.dm.models import MachinePool
24+
from genesis_core.tests.functional import utils as test_utils
25+
26+
27+
DictStrAny = tp.Dict[str, tp.Any]
28+
29+
30+
class DriverSpecWithException(tp.TypedDict):
31+
driver_spec: tp.Optional[
32+
DictStrAny
33+
]
34+
35+
exception: tp.Optional[
36+
tp.Type[Exception]
37+
]
38+
39+
40+
DEFAULT_MACHINE_POOL_CONNECTION_URI = "qemu://system"
41+
DEFAULT_DRIVER_SPEC_WITH_EXCEPTION = DriverSpecWithException(
42+
driver_spec={"connection_uri": DEFAULT_MACHINE_POOL_CONNECTION_URI},
43+
exception=None,
44+
)
45+
46+
47+
class PoolFactory(tp.Protocol):
48+
def __call__(self, *, driver_spec: DictStrAny) -> DictStrAny:
49+
...
50+
51+
52+
@pytest.mark.parametrize(
53+
"driver_specs_with_exceptions",
54+
[
55+
pytest.param(
56+
[
57+
DEFAULT_DRIVER_SPEC_WITH_EXCEPTION,
58+
],
59+
id="single-insert"
60+
),
61+
pytest.param(
62+
[
63+
DriverSpecWithException(driver_spec=None, exception=None),
64+
],
65+
id="none-driver-spec",
66+
),
67+
pytest.param(
68+
[
69+
DriverSpecWithException(driver_spec={}, exception=None),
70+
],
71+
id="empty-dict-driver-spec",
72+
),
73+
pytest.param(
74+
[
75+
DriverSpecWithException(
76+
driver_spec={"connection_uri": None},
77+
exception=None,
78+
),
79+
],
80+
id="empty-connection-uri-driver-spec",
81+
),
82+
pytest.param(
83+
[
84+
DriverSpecWithException(driver_spec=None, exception=None),
85+
DriverSpecWithException(driver_spec=None, exception=None),
86+
],
87+
id="allow-connection-uri-null-duplicate",
88+
),
89+
pytest.param(
90+
[
91+
DEFAULT_DRIVER_SPEC_WITH_EXCEPTION,
92+
DriverSpecWithException(
93+
driver_spec={
94+
"connection_uri": DEFAULT_MACHINE_POOL_CONNECTION_URI,
95+
},
96+
exception=exceptions.ConflictRecords,
97+
)
98+
],
99+
id="disallow-duplicate-connection-uri",
100+
),
101+
],
102+
)
103+
def test_connection_uri_idx(
104+
driver_specs_with_exceptions: tp.List[DriverSpecWithException],
105+
test_session: test_utils.AbstractSession,
106+
pool_factory: PoolFactory,
107+
):
108+
for param in driver_specs_with_exceptions:
109+
machine_pool = MachinePool.restore_from_simple_view(
110+
**pool_factory(
111+
driver_spec=param["driver_spec"],
112+
)
113+
)
114+
115+
if param["exception"] is None:
116+
machine_pool.insert(session=test_session)
117+
else:
118+
with pytest.raises(param["exception"]):
119+
machine_pool.insert(session=test_session)

0 commit comments

Comments
 (0)