Skip to content

Commit

Permalink
Cache records for performance (#726)
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Prescod committed Sep 15, 2022
1 parent 6d79f0a commit e888862
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
7 changes: 6 additions & 1 deletion docs/salesforce.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,14 @@ Perhaps you do not care which Campaign you connect to:
```

As you can see, `find_record` looks for a particular record, and returns the first
one that Salesforce finds. `random_record` looks for an random record out of the
one that Salesforce finds. The output of `find_record` is cached to reduce
unnecessary calls to Salesforce, but the precise rules for how long the
cache lasts are undocumented and may change based on performance testing.

`random_record` looks for an random record out of the
first 2000 Salesforce finds. The 2000-record scope limit is based on a Salesforce
limitation and future versions of Snowfakery may incorporate a workaround.
`random_record` values are never cached.

NOTE: The features we are discussing in this section are for linking to records
that are in the Salesforce org _before_ the recipe iteration started. These features
Expand Down
4 changes: 3 additions & 1 deletion snowfakery/standard_plugins/Salesforce.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from tempfile import TemporaryDirectory
from pathlib import Path

from snowfakery.plugins import ParserMacroPlugin
from snowfakery.plugins import ParserMacroPlugin, memorable
from snowfakery.data_generator_runtime_object_model import (
ObjectTemplate,
FieldFactory,
Expand Down Expand Up @@ -321,6 +321,7 @@ def _generate_psa(self, context, line_info, name):
return new_template

class Functions:
@memorable
def ProfileId(self, name):
query = f"select Id from Profile where Name='{name}'"
return self.context.plugin.sf_connection.query_single_record(query)
Expand Down Expand Up @@ -451,6 +452,7 @@ def random_record(self, *args, fields="Id", where=None, **kwargs):
# todo: use CompositeParallelSalesforce to cache 200 at a time
return self._sf_connection.query_single_record(query)

@memorable
def find_record(self, *args, fields="Id", where=None, **kwargs):
"""Find a particular record"""
query_from = self._parse_from_from_args(args, kwargs)
Expand Down
15 changes: 15 additions & 0 deletions tests/test_with_cci.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,21 @@ def test_soql_plugin_record(self, fake_sf_client, generated_rows):
assert fake_sf_client.mock_calls
assert generated_rows.row_values(0, "AccountId") == "FAKEID0"

def test_soql_plugin_cache(self, fake_sf_client, generated_rows):
yaml = """
- plugin: snowfakery.standard_plugins.Salesforce.SalesforceQuery
- object: Contact
count: 5
fields:
FirstName: Suzy
LastName: Salesforce
AccountId:
SalesforceQuery.find_record: Account
"""
generate(StringIO(yaml), plugin_options={"org_name": "blah"})
assert fake_sf_client.mock_calls
assert len(fake_sf_client.mock_calls) == 1, fake_sf_client.mock_calls

def test_soql_plugin_random__orgname_long(self, fake_sf_client, generated_rows):
yaml = """
- plugin: snowfakery.standard_plugins.Salesforce.SalesforceQuery
Expand Down

0 comments on commit e888862

Please sign in to comment.