Skip to content

Commit

Permalink
Merge branch 'main' into feature/scheduled-event
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Prescod committed Oct 6, 2022
2 parents ea01bf0 + 2f589a4 commit 32b5648
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 78 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Coverage Status](https://coveralls.io/repos/github/SFDO-Tooling/Snowfakery/badge.svg?branch=master)](https://coveralls.io/github/SFDO-Tooling/Snowfakery?branch=master)
[![Coverage Status](https://coveralls.io/repos/github/SFDO-Tooling/Snowfakery/badge.svg?branch=main)](https://coveralls.io/github/SFDO-Tooling/Snowfakery?branch=main)

# Snowfakery Documentation

Expand Down
1 change: 0 additions & 1 deletion docs/extending.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ represents the values that would be available to a formula running in the same c
and `self.context.current_filename` which is the filename of the YAML file being
processed.


### Plugin Function Return Values

Plugins can return normal Python primitive types, `datetime.date`, `ObjectRow` or `PluginResult` objects. `ObjectRow` objects represent new output records/objects. `PluginResult` objects
Expand Down
13 changes: 5 additions & 8 deletions docs/salesforce.md
Original file line number Diff line number Diff line change
Expand Up @@ -352,24 +352,21 @@ There is also an alternate syntax which allows nicknaming:
Files can be used as Salesforce ContentVersions like this:

```yaml
- plugin: snowfakery.standard_plugins.base64.Base64
- plugin: snowfakery.standard_plugins.file.File
# examples/salesforce/ContentVersion.recipe.yml
- plugin: snowfakery.standard_plugins.Salesforce
- object: Account
nickname: FileOwner
fields:
Name:
fake: company
- object: ContentVersion
nickname: FileAttachment
fields:
Title: Attachment for ${{Account.Name}}
PathOnClient: example.pdf
Description: example.pdf
Description: The example.pdf file
VersionData:
Base64.encode:
- File.file_data:
encoding: binary
file: ${{PathOnClient}}
Salesforce.ContentFile:
file: example.pdf
FirstPublishLocationId:
reference: Account
```
Expand Down
32 changes: 32 additions & 0 deletions examples/base64_file.recipe.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
- plugin: snowfakery.standard_plugins.base64.Base64
- plugin: snowfakery.standard_plugins.file.File
- plugin: snowfakery.standard_plugins.Salesforce
- object: Account
nickname: FileOwner
fields:
Name:
fake: company
- object: ContentVersion
nickname: FileAttachment
fields:
Title: Attachment for ${{Account.Name}}
PathOnClient: salesforce/example.pdf
Description: example.pdf
VersionData:
Base64.encode:
- File.file_data:
encoding: binary
file: ${{PathOnClient}}
FirstPublishLocationId:
reference: Account
- object: ContentVersion
nickname: FileAttachment2
fields:
Title: Attachment for ${{Account.Name}}
PathOnClient: salesforce/example.pdf
Description: example.pdf
VersionData:
Salesforce.ContentFile:
file: ${{PathOnClient}}
FirstPublishLocationId:
reference: Account
12 changes: 4 additions & 8 deletions examples/salesforce/ContentVersion.recipe.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
- plugin: snowfakery.standard_plugins.base64.Base64
- plugin: snowfakery.standard_plugins.file.File
- plugin: snowfakery.standard_plugins.Salesforce
- object: Account
nickname: FileOwner
fields:
Name:
fake: company
- object: ContentVersion
nickname: FileAttachment
fields:
Title: Attachment for ${{Account.Name}}
PathOnClient: example.pdf
Description: example.pdf
Description: The example.pdf file
VersionData:
Base64.encode:
- File.file_data:
encoding: binary
file: ${{PathOnClient}}
Salesforce.ContentFile:
file: example.pdf
FirstPublishLocationId:
reference: Account
4 changes: 4 additions & 0 deletions snowfakery/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ def evaluate(self, field_definition):
def current_filename(self):
return self.interpreter.current_context.current_template.filename

@property
def current_filename(self):
return self.interpreter.current_context.current_template.filename


def lazy(func: Any) -> Callable:
"""A lazy function is one that expects its arguments to be unparsed"""
Expand Down
61 changes: 7 additions & 54 deletions snowfakery/standard_plugins/Salesforce.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from logging import getLogger
from tempfile import TemporaryDirectory
from pathlib import Path
from base64 import b64encode

from snowfakery.plugins import ParserMacroPlugin, memorable
from snowfakery.data_generator_runtime_object_model import (
Expand Down Expand Up @@ -266,60 +267,6 @@ def _parse_special_args(self, args):

return sobj, nickname

# FIXME: This code is not documented or tested
def ContentFile(self, context, args) -> ObjectTemplate:
return {
"Base64.encode": [
{"File.file_data": {"encoding": "binary", "file": args.get("path")}}
]
}

def PermissionSetAssignments(self, context, args) -> ObjectTemplate:
names = args.get("names")
if not isinstance(names, str):
raise DataGenValueError(
f"string `names` not specified for PermissionSetAssignments: {names}"
)
names = names.split(",")
line_info = context.line_num()
decls = [self._generate_psa(context, line_info, name) for name in names]

return ObjectTemplate(
"__wrapper_for_permission_sets",
filename=line_info["filename"],
line_num=line_info["line_num"],
friends=decls,
)

def _generate_psa(self, context, line_info, name):
fields = {"AssigneeId": ("Use")}

query = f"PermissionSet where Name = '{name}'"

fields = [
FieldFactory(
"PermissionSetId",
StructuredValue(
"SalesforceQuery.find_record", {"from": query}, **line_info
),
**line_info,
),
FieldFactory(
"AssigneeId",
StructuredValue("reference", ["User"], **line_info),
**line_info,
),
]

new_template = ObjectTemplate(
"PermissionSetAssignment",
filename=line_info["filename"],
line_num=line_info["line_num"],
fields=fields,
)
context.register_template(new_template)
return new_template

class Functions:
@memorable
def ProfileId(self, name):
Expand All @@ -328,6 +275,12 @@ def ProfileId(self, name):

Profile = ProfileId

def ContentFile(self, file: str):
template_path = Path(self.context.current_filename).parent

with open(template_path / file, "rb") as data:
return b64encode(data.read()).decode("ascii")


class SOQLDatasetImpl(DatasetBase):
iterator = None
Expand Down
12 changes: 6 additions & 6 deletions tests/test_salesforce_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@

class TestSalesforceGen:
def test_content_version(self, generated_rows):
content_version = "examples/salesforce/ContentVersion.recipe.yml"
content_version = "examples/base64_file.recipe.yml"
generate_data(content_version)
b64data = generated_rows.table_values("ContentVersion", 0)["VersionData"]
rawdata = b64decode(b64data)
assert rawdata.startswith(b"%PDF-1.3")
assert b"Helvetica" in rawdata

for i in range(0, 2):
b64data = generated_rows.table_values("ContentVersion", i)["VersionData"]
rawdata = b64decode(b64data)
assert rawdata.startswith(b"%PDF-1.3")
assert b"Helvetica" in rawdata

class TestSalesforcePlugin:
@skip_if_cumulusci_missing
Expand Down

0 comments on commit 32b5648

Please sign in to comment.