Skip to content

Commit 5edef21

Browse files
prescodPaul Prescodjstvz
authored
Cleanup some warnings (#3491)
Cleanup a few warnings. I'll use comments to explain. --------- Co-authored-by: Paul Prescod <pprescod@salesforce.com> Co-authored-by: James Estevez <jestevez@salesforce.com>
1 parent c731219 commit 5edef21

15 files changed

Lines changed: 111 additions & 66 deletions

File tree

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Test*.yaml

cumulusci/core/config/org_config.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from collections import defaultdict, namedtuple
44
from contextlib import contextmanager
55
from datetime import date, datetime
6+
from typing import Optional
67
from urllib.parse import urlparse
78

89
import requests
@@ -47,14 +48,12 @@ class OrgConfig(BaseConfig):
4748
is_sandbox: bool
4849
namespace: str
4950
namespaced: bool
50-
org_id: str
5151
org_type: str
5252
password: str
5353
scratch: bool
5454
scratch_org_type: str
5555
set_password: bool
5656
sfdx_alias: str
57-
username: str
5857
userinfo: str
5958
id: str
6059
active: bool
@@ -63,8 +62,9 @@ class OrgConfig(BaseConfig):
6362
refresh_token: str
6463
client_secret: str
6564
connected_app: str
65+
serialization_format: str
6666

67-
createable: bool = None
67+
createable: Optional[bool] = None
6868

6969
# make sure it can be mocked for tests
7070
OAuth2Client = OAuth2Client
@@ -204,7 +204,15 @@ def user_id(self):
204204

205205
@property
206206
def org_id(self):
207-
return self.id.split("/")[-2]
207+
try:
208+
if org_id := self.config.get("org_id"):
209+
return org_id
210+
elif hasattr(self, "id") and self.id:
211+
return self.id.split("/")[-2]
212+
else:
213+
return None
214+
except Exception as e: # pragma: no cover
215+
assert e is None, e
208216

209217
@property
210218
def username(self):
@@ -254,7 +262,7 @@ def populate_expiration_date(self):
254262
@property
255263
def organization_sobject(self):
256264
"""Cached copy of Organization sObject. Does not perform API call."""
257-
return self._org_sobject
265+
return getattr(self, "_org_sobject", None)
258266

259267
def _fetch_community_info(self):
260268
"""Use the API to re-fetch information about communities"""
@@ -317,7 +325,8 @@ def installed_packages(self):
317325
To check if a required package is present, call `has_minimum_package_version()` with either the
318326
namespace or 033 Id of the desired package and its version, in 1.2.3 format.
319327
320-
Beta version of a package are represented as "1.2.3b5", where 5 is the build number."""
328+
Beta version of a package are represented as "1.2.3b5", where 5 is the build number.
329+
"""
321330
if self._installed_packages is None:
322331
isp_result = self.salesforce_client.restful(
323332
"tooling/query/?q=SELECT SubscriberPackage.Id, SubscriberPackage.NamespacePrefix, "

cumulusci/core/config/tests/test_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def test_getattr_toplevel_key_missing(self):
6666
assert config.foo is None
6767
with mock.patch(
6868
"cumulusci.core.config.base_config.STRICT_GETATTR", True
69-
), pytest.raises(AssertionError):
69+
), pytest.deprecated_call(), pytest.raises(AssertionError):
7070
assert config.foo is None
7171

7272
def test_getattr_child_key(self):

cumulusci/core/dependencies/tests/test_dependencies.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,7 @@ def test_install(self, api_deploy_mock, zip_builder_mock, download_mock):
645645
assert mock_task.project_config == context
646646

647647
api_deploy_mock.return_value.assert_called_once()
648+
zf.close()
648649

649650
def test_get_unmanaged(self):
650651
org = mock.Mock()
@@ -733,6 +734,7 @@ def test_install(self, api_deploy_mock, zip_builder_mock, download_mock):
733734
assert mock_task.project_config == context
734735

735736
api_deploy_mock.return_value.assert_called_once()
737+
zf.close()
736738

737739
def test_get_unmanaged(self):
738740
org = mock.Mock()
@@ -793,6 +795,7 @@ def test_get_metadata_package_zip_builder__mdapi_root(
793795
},
794796
context=mock.ANY,
795797
)
798+
zf.close()
796799

797800
@mock.patch("cumulusci.core.dependencies.dependencies.MetadataPackageZipBuilder")
798801
@mock.patch("cumulusci.core.dependencies.dependencies.download_extract_zip")
@@ -827,6 +830,7 @@ def test_get_metadata_package_zip_builder__mdapi_subfolder(
827830
},
828831
context=mock.ANY,
829832
)
833+
zf.close()
830834

831835
@mock.patch("cumulusci.core.dependencies.dependencies.MetadataPackageZipBuilder")
832836
@mock.patch("cumulusci.core.dependencies.dependencies.download_extract_zip")
@@ -866,6 +870,7 @@ def test_get_metadata_package_zip_builder__sfdx(
866870
capture_output=True,
867871
check_return=True,
868872
)
873+
zf.close()
869874

870875

871876
class TestParseDependency:

cumulusci/oauth/tests/test_client.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
import responses
1414
from requests.models import Response
1515

16-
from cumulusci.core.exceptions import SalesforceCredentialsException
16+
from cumulusci.core.exceptions import (
17+
CumulusCIUsageError,
18+
SalesforceCredentialsException,
19+
)
1720
from cumulusci.core.keychain.base_project_keychain import DEFAULT_CONNECTED_APP_PORT
1821
from cumulusci.oauth.client import (
1922
PORT_IN_USE_ERR,
@@ -72,9 +75,17 @@ def http_client(client_config):
7275

7376
@contextmanager
7477
@mock.patch("time.sleep", time.sleep) # undo mock from conftest
75-
def httpd_thread(oauth_client):
78+
def httpd_thread(oauth_client, expected_error=None):
7679
# call OAuth object on another thread - this spawns local httpd
77-
thread = threading.Thread(target=oauth_client.auth_code_flow)
80+
81+
def run_code_and_check_exception():
82+
if expected_error:
83+
with pytest.raises(expected_error):
84+
oauth_client.auth_code_flow()
85+
else:
86+
oauth_client.auth_code_flow()
87+
88+
thread = threading.Thread(target=run_code_and_check_exception)
7889
thread.start()
7990
while thread.is_alive():
8091
if oauth_client.httpd:
@@ -192,7 +203,7 @@ def test_oauth_flow_error_from_auth(self, client):
192203
)
193204

194205
# call OAuth object on another thread - this spawns local httpd
195-
with httpd_thread(client):
206+
with httpd_thread(client, OAuth2Error):
196207
# simulate callback from browser
197208
with pytest.raises(urllib.error.HTTPError):
198209
urllib.request.urlopen(
@@ -204,7 +215,7 @@ def test_oauth_flow_error_from_auth(self, client):
204215
sys.platform.startswith("win"), reason="setup differs from windows"
205216
)
206217
def test_create_httpd__port_already_in_use(self, client):
207-
with httpd_thread(client):
218+
with httpd_thread(client, CumulusCIUsageError):
208219
with pytest.raises(
209220
OAuth2Error, match=PORT_IN_USE_ERR.format(DEFAULT_CONNECTED_APP_PORT)
210221
):
@@ -227,7 +238,7 @@ def test_oauth_flow_error_from_token(self, client):
227238
)
228239

229240
# call OAuth object on another thread - this spawns local httpd
230-
with httpd_thread(client):
241+
with httpd_thread(client, OAuth2Error):
231242
# simulate callback from browser
232243
with pytest.raises(urllib.error.HTTPError):
233244
urllib.request.urlopen(client.client_config.redirect_uri + "?code=123")

cumulusci/tasks/apex/tests/test_apex_tasks.py

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import pytest
1010
import responses
11+
from responses.matchers import query_string_matcher
1112
from simple_salesforce import SalesforceGeneralError
1213

1314
from cumulusci.core import exceptions as exc
@@ -73,9 +74,9 @@ def setup_method(self):
7374

7475
def _mock_apex_class_query(self, name="TestClass_TEST", namespace=None):
7576
namespace_param = "null" if namespace is None else f"%27{namespace}%27"
76-
url = (
77-
self.base_tooling_url
78-
+ "query/?q=SELECT+Id%2C+Name+"
77+
url = self.base_tooling_url + "query/"
78+
query_string = (
79+
"q=SELECT+Id%2C+Name+"
7980
+ f"FROM+ApexClass+WHERE+NamespacePrefix+%3D+{namespace_param}"
8081
+ "+AND+%28Name+LIKE+%27%25_TEST%27%29"
8182
)
@@ -85,7 +86,10 @@ def _mock_apex_class_query(self, name="TestClass_TEST", namespace=None):
8586
"totalSize": 1,
8687
}
8788
responses.add(
88-
responses.GET, url, match_querystring=True, json=expected_response
89+
responses.GET,
90+
url,
91+
match=[query_string_matcher(query_string)],
92+
json=expected_response,
8993
)
9094

9195
def _get_mock_test_query_results(self, methodnames, outcomes, messages):
@@ -163,16 +167,14 @@ def _get_mock_test_query_results(self, methodnames, outcomes, messages):
163167

164168
def _get_mock_test_query_url(self, job_id):
165169
return (
166-
self.base_tooling_url
167-
+ "query/?q=%0ASELECT+Id%2CApexClassId%2CTestTimestamp%2C%0A+++++++Message%2CMethodName%2COutcome%2C%0A+++++++RunTime%2CStackTrace%2C%0A+++++++%28SELECT%0A++++++++++Id%2CCallouts%2CAsyncCalls%2CDmlRows%2CEmail%2C%0A++++++++++LimitContext%2CLimitExceptions%2CMobilePush%2C%0A++++++++++QueryRows%2CSosl%2CCpu%2CDml%2CSoql%0A++++++++FROM+ApexTestResults%29%0AFROM+ApexTestResult%0AWHERE+AsyncApexJobId%3D%27{}%27%0A".format(
168-
job_id
169-
)
170+
self.base_tooling_url + "query/",
171+
f"q=%0ASELECT+Id%2CApexClassId%2CTestTimestamp%2C%0A+++++++Message%2CMethodName%2COutcome%2C%0A+++++++RunTime%2CStackTrace%2C%0A+++++++%28SELECT%0A++++++++++Id%2CCallouts%2CAsyncCalls%2CDmlRows%2CEmail%2C%0A++++++++++LimitContext%2CLimitExceptions%2CMobilePush%2C%0A++++++++++QueryRows%2CSosl%2CCpu%2CDml%2CSoql%0A++++++++FROM+ApexTestResults%29%0AFROM+ApexTestResult%0AWHERE+AsyncApexJobId%3D%27{job_id}%27%0A",
170172
)
171173

172174
def _get_mock_testqueueitem_status_query_url(self, job_id):
173175
return (
174-
self.base_tooling_url
175-
+ f"query/?q=SELECT+Id%2C+Status%2C+ExtendedStatus%2C+ApexClassId+FROM+ApexTestQueueItem+WHERE+ParentJobId+%3D+%27{job_id}%27+AND+Status+%3D+%27Failed%27"
176+
(self.base_tooling_url + "query/"),
177+
f"q=SELECT+Id%2C+Status%2C+ExtendedStatus%2C+ApexClassId+FROM+ApexTestQueueItem+WHERE+ParentJobId+%3D+%27{job_id}%27+AND+Status+%3D+%27Failed%27",
176178
)
177179

178180
def _mock_get_test_results(
@@ -182,44 +184,50 @@ def _mock_get_test_results(
182184
job_id="JOB_ID1234567",
183185
methodname=["TestMethod"],
184186
):
185-
url = self._get_mock_test_query_url(job_id)
187+
url, query_string = self._get_mock_test_query_url(job_id)
186188

187189
expected_response = self._get_mock_test_query_results(
188190
methodname, [outcome], [message]
189191
)
190192
responses.add(
191-
responses.GET, url, match_querystring=True, json=expected_response
193+
responses.GET,
194+
url,
195+
match=[query_string_matcher(query_string)],
196+
json=expected_response,
192197
)
193198

194199
def _mock_get_test_results_multiple(
195200
self, method_names, outcomes, messages, job_id="JOB_ID1234567"
196201
):
197-
url = self._get_mock_test_query_url(job_id)
202+
url, query_string = self._get_mock_test_query_url(job_id)
198203

199204
expected_response = self._get_mock_test_query_results(
200205
method_names, outcomes, messages
201206
)
202207
responses.add(
203-
responses.GET, url, match_querystring=True, json=expected_response
208+
responses.GET,
209+
url,
210+
match=[query_string_matcher(query_string)],
211+
json=expected_response,
204212
)
205213

206214
def _mock_get_failed_test_classes(self, job_id="JOB_ID1234567"):
207-
url = self._get_mock_testqueueitem_status_query_url(job_id)
215+
url, query_string = self._get_mock_testqueueitem_status_query_url(job_id)
208216

209217
responses.add(
210218
responses.GET,
211219
url,
212-
match_querystring=True,
220+
match=[query_string_matcher(query_string)],
213221
json={"totalSize": 0, "records": [], "done": True},
214222
)
215223

216224
def _mock_get_failed_test_classes_failure(self, job_id="JOB_ID1234567"):
217-
url = self._get_mock_testqueueitem_status_query_url(job_id)
225+
url, query_string = self._get_mock_testqueueitem_status_query_url(job_id)
218226

219227
responses.add(
220228
responses.GET,
221229
url,
222-
match_querystring=True,
230+
match=[query_string_matcher(query_string)],
223231
json={
224232
"totalSize": 1,
225233
"records": [
@@ -235,14 +243,15 @@ def _mock_get_failed_test_classes_failure(self, job_id="JOB_ID1234567"):
235243
)
236244

237245
def _mock_get_symboltable(self):
238-
url = (
239-
self.base_tooling_url
240-
+ "query/?q=SELECT+SymbolTable+FROM+ApexClass+WHERE+Name%3D%27TestClass_TEST%27"
246+
url = self.base_tooling_url + "query/"
247+
query_string = (
248+
"q=SELECT+SymbolTable+FROM+ApexClass+WHERE+Name%3D%27TestClass_TEST%27"
241249
)
242250

243251
responses.add(
244252
responses.GET,
245253
url,
254+
match=[query_string_matcher(query_string)],
246255
json={
247256
"records": [
248257
{
@@ -265,9 +274,9 @@ def _mock_get_symboltable_failure(self):
265274
responses.add(responses.GET, url, json={"records": []})
266275

267276
def _mock_tests_complete(self, job_id="JOB_ID1234567"):
268-
url = (
269-
self.base_tooling_url
270-
+ "query/?q=SELECT+Id%2C+Status%2C+"
277+
url = self.base_tooling_url + "query/"
278+
query_string = (
279+
"q=SELECT+Id%2C+Status%2C+"
271280
+ "ApexClassId+FROM+ApexTestQueueItem+WHERE+ParentJobId+%3D+%27"
272281
+ "{}%27".format(job_id)
273282
)
@@ -277,23 +286,29 @@ def _mock_tests_complete(self, job_id="JOB_ID1234567"):
277286
"records": [{"Status": "Completed"}],
278287
}
279288
responses.add(
280-
responses.GET, url, match_querystring=True, json=expected_response
289+
responses.GET,
290+
url,
291+
match=[query_string_matcher(query_string)],
292+
json=expected_response,
281293
)
282294

283295
def _mock_tests_processing(self, job_id="JOB_ID1234567"):
284-
url = (
285-
self.base_tooling_url
286-
+ "query/?q=SELECT+Id%2C+Status%2C+"
296+
url = self.base_tooling_url + "query/"
297+
query_string = (
298+
"q=SELECT+Id%2C+Status%2C+"
287299
+ "ApexClassId+FROM+ApexTestQueueItem+WHERE+ParentJobId+%3D+%27"
288-
+ "{}%27".format(job_id)
300+
+ f"{job_id}%27"
289301
)
290302
expected_response = {
291303
"done": True,
292304
"totalSize": 1,
293305
"records": [{"Status": "Processing", "ApexClassId": 1}],
294306
}
295307
responses.add(
296-
responses.GET, url, match_querystring=True, json=expected_response
308+
responses.GET,
309+
url,
310+
match=[query_string_matcher(query_string)],
311+
json=expected_response,
297312
)
298313

299314
def _mock_run_tests(self, success=True, body="JOB_ID1234567"):

cumulusci/tasks/bulkdata/tests/test_snowfakery.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -783,12 +783,11 @@ def test_explicit_channel_declarations(self, mock_load_data, create_task):
783783
"recipe": Path(__file__).parent
784784
/ "snowfakery/simple_snowfakery.recipe.yml",
785785
"run_until_recipe_repeated": 15,
786-
"recipe_options": {"xyzzy": "Nothing happens", "some_number": 42},
787786
"loading_rules": Path(__file__).parent
788787
/ "snowfakery/simple_snowfakery_channels.load.yml",
789788
},
790789
)
791-
with mock.patch.object(
790+
with pytest.warns(UserWarning), mock.patch.object(
792791
task.project_config, "keychain", DummyKeychain()
793792
) as keychain:
794793

@@ -833,7 +832,6 @@ def test_serial_mode(self, mock_load_data, create_task):
833832
"recipe": Path(__file__).parent
834833
/ "snowfakery/simple_snowfakery.recipe.yml",
835834
"run_until_recipe_repeated": 15,
836-
"recipe_options": {"xyzzy": "Nothing happens", "some_number": 42},
837835
"bulk_mode": "Serial",
838836
},
839837
)

0 commit comments

Comments
 (0)