Skip to content

Commit 49d8a3c

Browse files
committed
test: sf-mock: apply a few fixes, corrections and improvements
This commit applies some fixes in response to PR review. It fixes a few type hints, removes a piece of redundant code, corrects the query_all return type, and particularly clarifies the exceptions returned to the user.
1 parent 3d6482c commit 49d8a3c

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

registry/tests/mock_simple_salesforce/core.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ def add_custom_table(
9090
+ [rel + "__r" for rel in related]
9191
)
9292
sql += ");"
93-
cur = self.con.cursor()
9493
cur.execute(sql)
9594
self.con.commit()
9695

@@ -111,12 +110,14 @@ def create_record(self, sobject: str, data: dict[str, Any]) -> str:
111110
112111
Raises
113112
------
114-
Exception
113+
ValueError
115114
If the caller provides an Id, rather than letting this method create a random id.
116115
"""
117116

118117
if "Id" in data:
119-
raise Exception
118+
raise ValueError(
119+
"Record ids should be created automatically rather than set by the caller"
120+
)
120121

121122
field_names = ["Id"] + list(data.keys())
122123

@@ -237,4 +238,6 @@ def query_all(self, soql_query: str) -> OrderedDict:
237238

238239
records.append(record)
239240

240-
return {"records": records, "totalSize": len(records), "done": True}
241+
return OrderedDict(
242+
{"records": records, "totalSize": len(records), "done": True}
243+
)

registry/tests/mock_simple_salesforce/soql.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def parse_field_name(field_name_from_query: str, sobject: str) -> ParsedFieldNam
8888
ConvertedQuery = namedtuple("ConvertedQuery", ["sql", "sobject", "joins", "fields"])
8989

9090

91-
def convert_soql_to_sqlite(soql_query: str) -> str:
91+
def convert_soql_to_sqlite(soql_query: str) -> ConvertedQuery:
9292
"""Parse Salesforce Object Query Language query and convert into an SQLite query
9393
9494
Only select queries are supported.
@@ -100,17 +100,20 @@ def convert_soql_to_sqlite(soql_query: str) -> str:
100100
101101
Returns
102102
-------
103-
str
103+
ConvertedQuery
104104
105105
Raises
106106
------
107-
Exception
107+
ValueError
108+
If a non-SELECT query is passed.
108109
"""
109110

110111
# Parse the query and check we can convert it.
111112
parsed_query = python_soql_parser.parse(soql_query)
112113
if parsed_query[0] != "select":
113-
raise Exception()
114+
raise ValueError(
115+
"Only SELECT queries can be parsed by the SimpleSalesforce mock"
116+
)
114117

115118
# We need to do three things: find all the tables we need to JOIN, sort out
116119
# join ids, correct the field names by adding/changing table prefixes.

0 commit comments

Comments
 (0)