Skip to content

Replace live Salesforce instance with mock in tests - #9

Open
chrisarridge wants to merge 8 commits into
livefrom
ca/mock-sf
Open

Replace live Salesforce instance with mock in tests#9
chrisarridge wants to merge 8 commits into
livefrom
ca/mock-sf

Conversation

@chrisarridge

Copy link
Copy Markdown
Contributor

This PR refactors tests/test_views.py so that it uses an offline mock of the 360G Salesforce instead of requiring credentials to access the live Salesforce backend. This closes #8.

A mock SimpleSalesforce class (MockSimpleSalesforce) is constructed that provides the functionality used by the Registry. This is achieved using an in-memory SQL database and simple conversion of the Salesforce SOQL queries into SQL. An inherited mock object MockSimpleSalesforce360Giving is built on that to generate a fake corpus of data that is returned to the Registry from the SimpleSalesforce mock by monkey patching registry.salesforce.salesforce.get_salesforce_access(). The tests are updated.

As part of this PR I've done a couple of chores, updating requirements, a bit of linting, and finally re-enabling the tests to run in GH Actions.

This commit adds an SOQL query converter to transform SOQL queries
into SQL for SQLite so that we can mock query methods in
SimpleSalesforce.  Some tests of the parser are also included.  To
achieve this some dependencies were added, and as part of this black
was updated which required some linting.
This adds a mock 360G Salesforce object with fake organisations
and datasets, and then modifies the Registry tests to use the mock
rather than a live server.
# Parse the query and check we can convert it.
parsed_query = python_soql_parser.parse(soql_query)
if parsed_query[0] != "select":
raise Exception()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could raise a value error instead to explain only select queries are valid

"""

if "Id" in data:
raise Exception

@jwilson232 jwilson232 Aug 13, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could add some details to the exception to explain caller should not pass an id


Returns
-------
str

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return type is ConvertedQuery

Comment on lines +174 to +185
def query_all(self, soql_query: str) -> OrderedDict:
"""Run a query on the in-memory database and return the results.

Parameters
----------
soql_query : str
Salesforce Object Query Language query string.

Returns
-------
OrderedDict
"""

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the return type just dict not OrderedDict?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, but it should be an OrderedDict for the mock to work as it does with SimpleSalesforce. I've fixed it further down where the return object is constructed.

+ [rel + "__r" for rel in related]
)
sql += ");"
cur = self.con.cursor()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need this if we already have a cursor from line 85?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch :)

@jwilson232

Copy link
Copy Markdown

@chrisarridge look good! just a couple minor comments

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.
@chrisarridge

Copy link
Copy Markdown
Contributor Author

@chrisarridge look good! just a couple minor comments

Thanks @jwilson232 - fixing commit added.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Update tests to use mocks and fixtures rather than connecting to a SF instance

2 participants