Skip to content

Commit b00b684

Browse files
committed
fix: slight refactor
1 parent 6f60df7 commit b00b684

File tree

2 files changed

+36
-29
lines changed

2 files changed

+36
-29
lines changed

src/url.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -373,15 +373,27 @@ impl PyMultiHostUrl {
373373
password: Option<&str>,
374374
port: Option<&str>,
375375
) -> PyResult<&'a PyAny> {
376-
let mut url = if hosts.is_some() && host.is_some() {
376+
// todo better error message
377+
let mut url = if hosts.is_some() && (host.is_some() || user.is_some() || password.is_some() || port.is_some()) {
377378
return Err(PyValueError::new_err("Only one of `host` or `hosts` may be set"));
378-
}
379-
else if hosts.is_some() {
379+
} else if hosts.is_some() {
380380
// check all of host / user / password / port empty
381381
// build multi-host url
382+
for single_host in hosts.as_deref().unwrap_or_default() {
383+
let mut multi_url = format!("{scheme}://");
384+
if single_host.username.is_some()
385+
&& single_host.host.is_some()
386+
&& single_host.password.is_some()
387+
&& single_host.port.is_some()
388+
{
389+
todo!()
390+
}
391+
else {
392+
return Err(PyValueError::new_err("Incomplete object."));
393+
};
394+
}
382395
todo!()
383-
}
384-
else if let Some(host) = host {
396+
} else if let Some(host) = host {
385397
let user_password = match (user, password) {
386398
(Some(user), None) => format!("{user}@"),
387399
(None, Some(password)) => format!(":{password}@"),

tests/validators/test_url.py

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,33 +1233,27 @@ def test_multi_url_build() -> None:
12331233
assert str(url) == 'postgresql://testuser:[email protected]:5432/database?sslmode=require#test'
12341234

12351235

1236-
def test_multi_url_build_host_and_hosts_set() -> None:
1236+
@pytest.mark.parametrize('field', ['host', 'password', 'user', 'port'])
1237+
def test_multi_url_build_hosts_set_with_single_value(field) -> None:
1238+
"""Hosts can't be provided with any single url values."""
12371239
hosts = [
1238-
{
1239-
'host': '127.0.0.1:5432',
1240-
'password': 'testpassword',
1241-
'username': 'testuser',
1242-
'port': '5432'
1243-
},
1244-
{
1245-
'host': '127.0.0.1:5432',
1246-
'password': 'testpassword',
1247-
'username': 'testuser',
1248-
'port': '5432'
1249-
},
1240+
{'host': '127.0.0.1:5432', 'password': 'testpassword', 'username': 'testuser', 'port': '5432'},
1241+
{'host': '127.0.0.1:5432', 'password': 'testpassword', 'username': 'testuser', 'port': '5432'},
12501242
]
1243+
kwargs = dict(scheme='postgresql', hosts=hosts, path='database', query='sslmode=require', fragment='test')
1244+
kwargs[field] = 'test'
12511245
with pytest.raises(ValueError):
1252-
MultiHostUrl.build(
1253-
scheme='postgresql',
1254-
user='testuser',
1255-
password='testpassword',
1256-
host='127.0.0.1',
1257-
hosts=hosts,
1258-
port='5432',
1259-
path='database',
1260-
query='sslmode=require',
1261-
fragment='test',
1262-
)
1246+
MultiHostUrl.build(**kwargs)
1247+
1248+
1249+
@pytest.mark.parametrize('field', ['host', 'password', 'username', 'port'])
1250+
def test_multi_url_build_hosts_invalid_host(field) -> None:
1251+
"""Hosts can't be provided with any single url values."""
1252+
host = {'host': '127.0.0.1:5432', 'password': 'testpassword', 'username': 'testuser', 'port': '5432'}
1253+
del host[field]
1254+
with pytest.raises(ValueError):
1255+
MultiHostUrl.build(scheme='postgresql', hosts=[host], path='database', query='sslmode=require', fragment='test')
1256+
12631257

12641258
def test_multi_url_build_neither_host_and_hosts_set() -> None:
12651259
with pytest.raises(ValueError):
@@ -1273,6 +1267,7 @@ def test_multi_url_build_neither_host_and_hosts_set() -> None:
12731267
fragment='test',
12741268
)
12751269

1270+
12761271
def test_url_build() -> None:
12771272
url = Url.build(
12781273
scheme='postgresql',

0 commit comments

Comments
 (0)