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