Skip to content

Commit

Permalink
Merge pull request #74 from SFDO-Tooling/feature/add-child-count
Browse files Browse the repository at this point in the history
Add child_count feature
  • Loading branch information
prescod committed Jun 12, 2020
2 parents e4db339 + c483899 commit 55af3c8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
6 changes: 3 additions & 3 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -610,12 +610,12 @@ The `when` clause can be a Python formula and it will be interpreted as a boolea

The functions below are designed to be used inside of formulas:

The `child_count` variable returns a counter of how many objects from this template were generated
The `child_index` variable returns a counter of how many objects from this template were generated
during the execution of the nearest parent template. It resets each time the parent template is
executed again.

```
child_num: Child number ${{child_count}}
child_num: Child number ${{child_index}}
```

The `date` function can either coerce a string into a date object for calculations OR generate
Expand All @@ -630,7 +630,7 @@ The `relativedelta` [function](https://dateutil.readthedocs.io/en/stable/relativ
from `dateutil` is available for use in calculations like this:

```
${{ date(Date_Established__c) + relativedelta(months=child_count) }}
${{ date(Date_Established__c) + relativedelta(months=child_index) }}
```

## Macros
Expand Down
6 changes: 4 additions & 2 deletions snowfakery/data_generator_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ def simple_field_vars(self):
return {
"id": obj.id if obj else None,
"count": obj.id if obj else None,
"child_index": obj._child_index if obj else None,
"this": obj,
"today": interpreter.globals.today,
"fake": interpreter.faker_template_library,
Expand Down Expand Up @@ -390,11 +391,12 @@ class ObjectRow(yaml.YAMLObject):
yaml_dumper = yaml.SafeDumper
yaml_tag = "!snowfakery_objectrow"

__slots__ = ["_tablename", "_values"]
__slots__ = ["_tablename", "_values", "_child_index"]

def __init__(self, tablename, values=()):
def __init__(self, tablename, values=(), index=0):
self._tablename = tablename
self._values = values
self._child_index = index

def __getattr__(self, name):
try:
Expand Down
6 changes: 3 additions & 3 deletions snowfakery/data_generator_runtime_object_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def generate_rows(
count = self._evaluate_count(context)
with self.exception_handling(f"Cannot generate {self.name}"):
for i in range(count):
rc = self._generate_row(storage, context)
rc = self._generate_row(storage, context, i)

return rc # return last row

Expand Down Expand Up @@ -126,10 +126,10 @@ def name(self) -> str:
name += " (self.nickname)"
return name

def _generate_row(self, storage, context: RuntimeContext) -> ObjectRow:
def _generate_row(self, storage, context: RuntimeContext, index: int) -> ObjectRow:
"""Generate an individual row"""
row = {"id": context.generate_id()}
sobj = ObjectRow(self.tablename, row)
sobj = ObjectRow(self.tablename, row, index)

context.register_object(sobj, self.nickname)

Expand Down
13 changes: 13 additions & 0 deletions tests/test_template_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,16 @@ def test_functions_inline(self, write_row):
generate(StringIO(yaml), {}, None)
assert "2012" in write_row.mock_calls[0][1][1]["wedding"]
assert "1" in write_row.mock_calls[0][1][1]["number"]

@mock.patch(write_row_path)
def test_child_index(self, write_row):
yaml = """
- object: A
friends:
- object: B
count: 3
fields:
num: <<child_index>>
"""
generate(StringIO(yaml), {}, None)
assert write_row.mock_calls[3][1][1]["num"] == 2

0 comments on commit 55af3c8

Please sign in to comment.