Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental support for multi-select picklists #371

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cumulusci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ project:
name: Snowfakery
dependencies:
- github: https://github.com/SalesforceFoundation/NPSP
source_format: sfdx

sources:
npsp:
Expand Down
18 changes: 18 additions & 0 deletions examples/multiselect.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
- plugin: snowfakery.standard_plugins.experimental.multiselect.Multiselect
- object: DNA
fields:
with_replacement:
Multiselect.random_choices:
min: 0
max: 10
with_replacement: True
choices: A,C,G,T
no_replacement:
Multiselect.random_choices:
min: 0
max: 10
with_replacement: False
choices: A,C,G,T
defaults: # no replacement, min=1, max=len(choices)
Multiselect.random_choices:
choices: A,C,G,T
12 changes: 12 additions & 0 deletions examples/salesforce/custom_field_multiselect.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# use `cci task run dx_push` to push the appropriate metadata

- plugin: snowfakery.standard_plugins.experimental.multiselect.Multiselect
- object: Contact
fields:
FirstName:
fake: FirstName
LastName:
fake: LastName
Types__c:
Multiselect.random_choices:
choices: Donor;Volunteer;Staff Member
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>Types__c</fullName>
<externalId>false</externalId>
<label>All Types</label>
<required>false</required>
<trackFeedHistory>false</trackFeedHistory>
<type>MultiselectPicklist</type>
<valueSet>
<restricted>true</restricted>
<valueSetDefinition>
<sorted>false</sorted>
<value>
<fullName>Donor</fullName>
<default>false</default>
<label>Donor</label>
</value>
<value>
<fullName>Staff Member</fullName>
<default>false</default>
<label>Staff Member</label>
</value>
<value>
<fullName>Volunteer</fullName>
<default>false</default>
<label>Volunteer</label>
</value>
</valueSetDefinition>
</valueSet>
<visibleLines>3</visibleLines>
</CustomField>
9 changes: 9 additions & 0 deletions force-app/main/default/profiles/Admin.profile-meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<Profile xmlns="http://soap.sforce.com/2006/04/metadata">
<custom>false</custom>
<fieldPermissions>
<editable>true</editable>
<field>Contact.Types__c</field>
<readable>true</readable>
</fieldPermissions>
</Profile>
12 changes: 12 additions & 0 deletions sfdx-project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"packageDirectories": [
{
"path": "force-app",
"default": true
}
],
"name": "snowfakery-demo",
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "51.0"
}
29 changes: 29 additions & 0 deletions snowfakery/standard_plugins/experimental/multiselect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import random
import builtins

from snowfakery import SnowfakeryPlugin


# This is experimental primarily because it hard-codes the separator.
# It would be superior for the output stream to select the separator.

# Also not thrilled that it cannot accept a list as input.


class Multiselect(SnowfakeryPlugin):
class Functions:
def random_choices(
self,
choices: str,
min: int = 1,
max: int = None,
with_replacement: bool = False,
separator=";",
):
choices = choices.split(",")
max = max or len(choices)
num_choices = random.randint(min, builtins.min(max, len(choices)))
if with_replacement:
return ";".join(random.choices(choices, k=num_choices))
else:
return ";".join(random.sample(choices, k=num_choices))
7 changes: 7 additions & 0 deletions tests/test_custom_plugins_and_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import math
import operator
from base64 import b64decode
from pathlib import Path

from snowfakery import SnowfakeryPlugin, lazy
from snowfakery.plugins import PluginResult
Expand Down Expand Up @@ -307,3 +308,9 @@ def test_null_attributes(self):
with pytest.raises(DataGenError) as e:
generate_data(StringIO(yaml))
assert 6 > e.value.line_num >= 3

def test_experimental_multiselect(self, generated_rows):
example = Path(__file__).parent.parent / "examples/multiselect.yml"
with open(example) as f:
generate_data(f)
assert all(ch in "ACGT;" for ch in generated_rows.row_values(0, "defaults"))