Replace live Salesforce instance with mock in tests - #9
Conversation
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.
Use of runtime.txt is deprecated in favour of .python-version ( https://devcenter.heroku.com/changelog-items/3141).
| # Parse the query and check we can convert it. | ||
| parsed_query = python_soql_parser.parse(soql_query) | ||
| if parsed_query[0] != "select": | ||
| raise Exception() |
There was a problem hiding this comment.
this could raise a value error instead to explain only select queries are valid
| """ | ||
|
|
||
| if "Id" in data: | ||
| raise Exception |
There was a problem hiding this comment.
could add some details to the exception to explain caller should not pass an id
|
|
||
| Returns | ||
| ------- | ||
| str |
| 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 | ||
| """ |
There was a problem hiding this comment.
is the return type just dict not OrderedDict?
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
do we need this if we already have a cursor from line 85?
|
@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.
707c9d4 to
2d4dc2c
Compare
Thanks @jwilson232 - fixing commit added. |
This PR refactors
tests/test_views.pyso 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 objectMockSimpleSalesforce360Givingis built on that to generate a fake corpus of data that is returned to the Registry from the SimpleSalesforce mock by monkey patchingregistry.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.