Skip to content

Commit

Permalink
Merge pull request #301 from SFDO-Tooling/feature/record-type-None-fix
Browse files Browse the repository at this point in the history
Fix cases where some records have record types and others don't
  • Loading branch information
prescod authored Mar 31, 2021
2 parents fc49bc2 + 9ea834e commit e77da2d
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 16 deletions.
4 changes: 3 additions & 1 deletion snowfakery/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pathlib import Path
from typing import Tuple, Union, Optional, Dict, TextIO
from typing import Tuple, Union, Optional, Dict, TextIO, Sequence
from contextlib import ExitStack

from .plugins import SnowfakeryPlugin, lazy # noqa
Expand Down Expand Up @@ -28,6 +28,7 @@ def generate_data(
generate_continuation_file: Union[TextIO, Path] = None,
generate_cci_mapping_file: Union[TextIO, Path] = None,
should_create_cci_record_type_tables: bool = False,
load_declarations: Sequence[Union[Path, str]] = None
):
from .cli import generate_cli

Expand Down Expand Up @@ -66,4 +67,5 @@ def open_if_necessary_and_close_later(file_like, mode):
generate_continuation_file=generate_continuation_file,
generate_cci_mapping_file=generate_cci_mapping_file,
should_create_cci_record_type_tables=should_create_cci_record_type_tables,
load_declarations=load_declarations,
)
2 changes: 1 addition & 1 deletion snowfakery/salesforce.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def _populate_rt_table(connection, table: Table, columnname: str, rt_table: Tabl
query_res = connection.execute(
select([column]).where(column is not None).distinct()
)
record_types = [res[0] for res in query_res]
record_types = [res[0] for res in query_res if res[0]]

if record_types:
insert_stmt = rt_table.insert()
Expand Down
14 changes: 0 additions & 14 deletions snowfakery/utils/collections.py

This file was deleted.

18 changes: 18 additions & 0 deletions tests/cci/circular_references.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
- object: Opportunity
fields:
Contact:
reference: Contact
Account:
reference: Account
- object: Contact
fields:
Account:
reference: Account
Opportunity:
reference: Opportunity
- object: Account
fields:
Contact:
reference: Contact
Opportunity:
reference: Opportunity
21 changes: 21 additions & 0 deletions tests/cci/test_declaration_parser.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from pathlib import Path
from io import StringIO

from snowfakery.cci_mapping_files.declaration_parser import (
SObjectRuleDeclaration,
SObjectRuleDeclarationFile,
unify,
)
from snowfakery.cli import generate_cli
Expand Down Expand Up @@ -138,3 +140,22 @@ def test_cli__multiple_files(self, tmpdir):
assert map_data["Insert Account"]["api"] == "rest"
assert map_data["Insert Contact"]["api"] == "rest"
assert map_data["Insert Opportunity"]["api"] == "bulk"

def test_circular_references__force_order(self, generate_in_tmpdir):
circular_test = (Path(__file__).parent / "circular_references.yml").read_text()
print(generate_in_tmpdir)
with generate_in_tmpdir(
circular_test, load_declarations=["tests/cci/mapping_mixins.load.yml"]
) as (mapping, connection):
assert tuple(mapping.keys()) == tuple(
["Insert Account", "Insert Contact", "Insert Opportunity"]
)

def test_parse_from_open_file(self):
s = StringIO(
"""
- sf_object: Foo
api: bulk
"""
)
SObjectRuleDeclarationFile.parse_from_yaml(s)
14 changes: 14 additions & 0 deletions tests/test_generate_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,20 @@ def test_Case_recordtypes(self, tmpdir, generate_in_tmpdir):
assert records == [("Bar", "Bar")], records
assert mapping["Insert Case"]["fields"]["RecordTypeId"] == "recordtype"

def test_incomplete_record_types(self, tmpdir, generate_in_tmpdir):
recipe_data = """
- object: Case
fields:
recordtype: Bar
- object: Case
fields:
blah: Blah
"""
with generate_in_tmpdir(recipe_data) as (mapping, db):
records = list(db.execute("SELECT * from Case_rt_mapping"))
assert records == [("Bar", "Bar")], records
assert mapping["Insert Case"]["fields"]["RecordTypeId"] == "recordtype"


class TestPersonAccounts:
def test_basic_person_accounts(self, generate_in_tmpdir):
Expand Down

0 comments on commit e77da2d

Please sign in to comment.