Skip to content

Commit 0d764c9

Browse files
lucaswerkmeisterbd808
authored andcommitted
Add support for extension databases
Via a new keyword-only extension parameter to the connect function. Bug: T396115
1 parent 8ad8474 commit 0d764c9

2 files changed

Lines changed: 32 additions & 8 deletions

File tree

src/toolforge/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from typing import Callable
2626
from typing import IO
2727
from typing import NewType
28+
from typing import Optional
2829
from typing import cast
2930

3031
import decorator
@@ -41,7 +42,13 @@
4142
)
4243

4344

44-
def connect(dbname: str, cluster: str = "web", **kwargs: str) -> _Connection:
45+
def connect(
46+
dbname: str,
47+
cluster: str = "web",
48+
*,
49+
extension: Optional[str] = None,
50+
**kwargs: str,
51+
) -> _Connection:
4552
"""
4653
Get a database connection for the specified wiki.
4754
@@ -63,6 +70,8 @@ def connect(dbname: str, cluster: str = "web", **kwargs: str) -> _Connection:
6370
host = f"s7.{domain}"
6471
else:
6572
host = f"{dbname}.{domain}"
73+
if extension is not None:
74+
host = f"{extension}.{host}"
6675
host = kwargs.pop("host", host)
6776

6877
return _connect(

tests/main_test.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,51 +55,64 @@ def test_set_user_agent(self, tool, url, email, expect):
5555
}
5656

5757
@pytest.mark.parametrize(
58-
("args", "expects"),
58+
("args", "kwargs", "expects"),
5959
[
6060
(
6161
["enwiki_p"],
62+
{},
6263
{
6364
"database": "enwiki_p",
6465
"host": "enwiki.web.db.svc.wikimedia.cloud",
6566
},
6667
),
6768
(
6869
["enwiki"],
70+
{},
6971
{
7072
"database": "enwiki_p",
7173
"host": "enwiki.web.db.svc.wikimedia.cloud",
7274
},
7375
),
7476
(
7577
["enwiki", "analytics"],
78+
{},
7679
{
7780
"database": "enwiki_p",
7881
"host": "enwiki.analytics.db.svc.wikimedia.cloud",
7982
},
8083
),
8184
(
8285
["meta", "analytics"],
86+
{},
8387
{
8488
"database": "meta_p",
8589
"host": "s7.analytics.db.svc.wikimedia.cloud",
8690
},
8791
),
92+
(
93+
["wikidatawiki"],
94+
{"extension": "termstore"},
95+
{
96+
"database": "wikidatawiki_p",
97+
"host": "termstore.wikidatawiki.web.db.svc.wikimedia.cloud",
98+
},
99+
),
88100
],
89101
)
90-
def test_connect(self, mocker, args, expects):
91-
self._assert_connect(mocker, toolforge.connect, args, expects)
102+
def test_connect(self, mocker, args, kwargs, expects):
103+
self._assert_connect(mocker, toolforge.connect, args, kwargs, expects)
92104

93-
def _assert_connect(self, mocker, func, args, expect):
105+
def _assert_connect(self, mocker, func, args, kwargs, expect):
94106
"""Mock toolforge._connect and assert it is called as expected.
95107
96108
:param func: Function to call after mocking toolforge._connect
97-
:param args: Arguments for calling func
109+
:param args: Positional arguments for calling func
110+
:param kwargs: Keyword arguments for calling func
98111
:param expect: Dict of expected arguments to toolforge._connect
99112
"""
100113
mm = mocker.patch("toolforge._connect")
101114
mm.return_value = None
102-
conn = func(*args)
115+
conn = func(*args, **kwargs)
103116
assert conn is None
104117
mm.assert_called_once_with(**expect)
105118

@@ -109,6 +122,7 @@ def test_connect_rejects_unknown_cluster(self, mocker):
109122
mocker,
110123
toolforge.connect,
111124
["ignored", "not web or analytics"],
125+
{},
112126
{"ignored": "ignored"},
113127
)
114128

@@ -132,7 +146,8 @@ def test_connect_rejects_unknown_cluster(self, mocker):
132146
],
133147
)
134148
def test_toolsdb(self, mocker, args, expects):
135-
self._assert_connect(mocker, toolforge.toolsdb, args, expects)
149+
kwargs = {}
150+
self._assert_connect(mocker, toolforge.toolsdb, args, kwargs, expects)
136151

137152
def test_assert_private_file_no_args(self, mocker):
138153
load = mocker.Mock(

0 commit comments

Comments
 (0)