11#!/usr/bin/env python
22import io
33import json
4+ import os
45
56import pytest
67import requests
@@ -63,6 +64,7 @@ def test_set_user_agent(self, tool, url, email, expect):
6364 {
6465 "database" : "enwiki_p" ,
6566 "host" : "enwiki.web.db.svc.wikimedia.cloud" ,
67+ "read_default_file" : os .path .expanduser ("~/replica.my.cnf" ),
6668 },
6769 ),
6870 (
@@ -71,6 +73,7 @@ def test_set_user_agent(self, tool, url, email, expect):
7173 {
7274 "database" : "enwiki_p" ,
7375 "host" : "enwiki.web.db.svc.wikimedia.cloud" ,
76+ "read_default_file" : os .path .expanduser ("~/replica.my.cnf" ),
7477 },
7578 ),
7679 (
@@ -79,6 +82,7 @@ def test_set_user_agent(self, tool, url, email, expect):
7982 {
8083 "database" : "enwiki_p" ,
8184 "host" : "enwiki.analytics.db.svc.wikimedia.cloud" ,
85+ "read_default_file" : os .path .expanduser ("~/replica.my.cnf" ),
8286 },
8387 ),
8488 (
@@ -87,6 +91,7 @@ def test_set_user_agent(self, tool, url, email, expect):
8791 {
8892 "database" : "meta_p" ,
8993 "host" : "s7.analytics.db.svc.wikimedia.cloud" ,
94+ "read_default_file" : os .path .expanduser ("~/replica.my.cnf" ),
9095 },
9196 ),
9297 (
@@ -95,13 +100,77 @@ def test_set_user_agent(self, tool, url, email, expect):
95100 {
96101 "database" : "wikidatawiki_p" ,
97102 "host" : "termstore.wikidatawiki.web.db.svc.wikimedia.cloud" ,
103+ "read_default_file" : os .path .expanduser ("~/replica.my.cnf" ),
98104 },
99105 ),
100106 ],
101107 )
102108 def test_connect (self , mocker , args , kwargs , expects ):
103109 self ._assert_connect (mocker , toolforge .connect , args , kwargs , expects )
104110
111+ @pytest .mark .parametrize (
112+ ("env" , "expects" ),
113+ [
114+ (
115+ {
116+ "TOOL_REPLICA_USER" : "user name" ,
117+ "TOOL_REPLICA_PASSWORD" : "password" ,
118+ },
119+ {
120+ "user" : "user name" ,
121+ "password" : "password" ,
122+ },
123+ ),
124+ (
125+ {
126+ "TOOL_REPLICA_USER" : None ,
127+ "TOOL_REPLICA_PASSWORD" : None ,
128+ },
129+ {
130+ "read_default_file" : os .path .expanduser ("~/replica.my.cnf" ),
131+ },
132+ ),
133+ (
134+ {
135+ "TOOL_REPLICA_USER" : "unused user name" ,
136+ "TOOL_REPLICA_PASSWORD" : None ,
137+ },
138+ {
139+ "read_default_file" : os .path .expanduser ("~/replica.my.cnf" ),
140+ },
141+ ),
142+ (
143+ {
144+ "TOOL_REPLICA_USER" : None ,
145+ "TOOL_REPLICA_PASSWORD" : "unused password" ,
146+ },
147+ {
148+ "read_default_file" : os .path .expanduser ("~/replica.my.cnf" ),
149+ },
150+ ),
151+ (
152+ {
153+ "TOOL_TOOLSDB_USER" : "unused" ,
154+ "TOOL_TOOLSDB_PASSWORD" : "(toolsdb != replica)" ,
155+ },
156+ {
157+ "read_default_file" : os .path .expanduser ("~/replica.my.cnf" ),
158+ },
159+ ),
160+ ],
161+ )
162+ def test_connect_env (self , mocker , monkeypatch , env , expects ):
163+ for name , value in env .items ():
164+ if value is None :
165+ monkeypatch .delenv (name , raising = False )
166+ else :
167+ monkeypatch .setenv (name , value )
168+ args = ["enwiki_p" ]
169+ kwargs = {}
170+ expects ["database" ] = "enwiki_p"
171+ expects ["host" ] = "enwiki.web.db.svc.wikimedia.cloud"
172+ self ._assert_connect (mocker , toolforge .connect , args , kwargs , expects )
173+
105174 def _assert_connect (self , mocker , func , args , kwargs , expect ):
106175 """Mock toolforge._connect and assert it is called as expected.
107176
@@ -134,13 +203,15 @@ def test_connect_rejects_unknown_cluster(self, mocker):
134203 {
135204 "database" : "s12345__foo" ,
136205 "host" : "tools.db.svc.wikimedia.cloud" ,
206+ "read_default_file" : os .path .expanduser ("~/replica.my.cnf" ),
137207 },
138208 ),
139209 (
140210 ["s12345__foo_p" ],
141211 {
142212 "database" : "s12345__foo_p" ,
143213 "host" : "tools.db.svc.wikimedia.cloud" ,
214+ "read_default_file" : os .path .expanduser ("~/replica.my.cnf" ),
144215 },
145216 ),
146217 ],
@@ -149,6 +220,69 @@ def test_toolsdb(self, mocker, args, expects):
149220 kwargs = {}
150221 self ._assert_connect (mocker , toolforge .toolsdb , args , kwargs , expects )
151222
223+ @pytest .mark .parametrize (
224+ ("env" , "expects" ),
225+ [
226+ (
227+ {
228+ "TOOL_TOOLSDB_USER" : "user name" ,
229+ "TOOL_TOOLSDB_PASSWORD" : "password" ,
230+ },
231+ {
232+ "user" : "user name" ,
233+ "password" : "password" ,
234+ },
235+ ),
236+ (
237+ {
238+ "TOOL_TOOLSDB_USER" : None ,
239+ "TOOL_TOOLSDB_PASSWORD" : None ,
240+ },
241+ {
242+ "read_default_file" : os .path .expanduser ("~/replica.my.cnf" ),
243+ },
244+ ),
245+ (
246+ {
247+ "TOOL_TOOLSDB_USER" : "unused user name" ,
248+ "TOOL_TOOLSDB_PASSWORD" : None ,
249+ },
250+ {
251+ "read_default_file" : os .path .expanduser ("~/replica.my.cnf" ),
252+ },
253+ ),
254+ (
255+ {
256+ "TOOL_TOOLSDB_USER" : None ,
257+ "TOOL_TOOLSDB_PASSWORD" : "unused password" ,
258+ },
259+ {
260+ "read_default_file" : os .path .expanduser ("~/replica.my.cnf" ),
261+ },
262+ ),
263+ (
264+ {
265+ "TOOL_REPLICA_USER" : "unused" ,
266+ "TOOL_REPLICA_PASSWORD" : "(toolsdb != replica)" ,
267+ },
268+ {
269+ "read_default_file" : os .path .expanduser ("~/replica.my.cnf" ),
270+ },
271+ ),
272+ ],
273+ )
274+ def test_toolsdb_env (self , mocker , monkeypatch , env , expects ):
275+ for name , value in env .items ():
276+ if value is None :
277+ monkeypatch .delenv (name , raising = False )
278+ else :
279+ monkeypatch .setenv (name , value )
280+ args = ["s12345__foo" ]
281+ kwargs = {}
282+ expects ["database" ] = "s12345__foo"
283+ expects ["host" ] = "tools.db.svc.wikimedia.cloud"
284+ self ._assert_connect (mocker , toolforge .toolsdb , args , kwargs , expects )
285+
152286 def test_assert_private_file_no_args (self , mocker ):
153287 load = mocker .Mock (
154288 return_value = "data" ,
0 commit comments