Пример #1
0
    def test_extracts_polymorphic_lookups(self):
        oc = amaxa.ExtractOperation(Connection(self.connection))
        oc.file_store = MockFileStore()

        rec = self.connection.query(
            "SELECT Id FROM Account WHERE Name = 'Caprica Cosmetics'"
        )
        oc.add_dependency("Account", rec.get("records")[0]["Id"])

        oc.add_step(
            amaxa.ExtractionStep(
                "Account",
                amaxa.ExtractionScope.SELECTED_RECORDS,
                ["Id", "Name", "OwnerId"],
            )
        )
        oc.add_step(
            amaxa.ExtractionStep(
                "User", amaxa.ExtractionScope.DESCENDENTS, ["Id", "Username"]
            )
        )

        oc.initialize()
        oc.execute()

        self.assertEqual(1, len(oc.get_extracted_ids("Account")))
        self.assertEqual(1, len(oc.get_extracted_ids("User")))
Пример #2
0
    def test_descendents_extracts_object_network(self):
        expected_names = {"Elosha", "Gaius"}
        oc = amaxa.ExtractOperation(Connection(self.connection, "48.0"))
        oc.file_store = MockFileStore()

        rec = self.connection.query(
            "SELECT Id FROM Account WHERE Name = 'Caprica Cosmetics'")
        oc.add_dependency("Account", rec.get("records")[0]["Id"])

        oc.add_step(
            amaxa.ExtractionStep(
                "Account",
                amaxa.ExtractionScope.SELECTED_RECORDS,
                ["Id", "Name", "ParentId"],
            ))
        oc.add_step(
            amaxa.ExtractionStep(
                "Contact",
                amaxa.ExtractionScope.DESCENDENTS,
                ["Id", "FirstName", "LastName", "AccountId"],
            ))

        oc.initialize()
        oc.execute()

        self.assertEqual(3, len(oc.get_extracted_ids("Account")))
        self.assertEqual(2, len(oc.get_extracted_ids("Contact")))
        for c in oc.file_store.get_csv(
                "Contact", amaxa.FileType.OUTPUT).writerow.call_args_list:
            self.assertIn(c[0][0]["FirstName"], expected_names)
            expected_names.remove(c[0][0]["FirstName"])

        self.assertEqual(0, len(expected_names))
Пример #3
0
    def test_query_extracts_self_lookup_hierarchy(self):
        expected_names = {
            "Caprica Cosmetics",
            "Gemenon Gastronomy",
            "Aerilon Agrinomics",
        }
        oc = amaxa.ExtractOperation(Connection(self.connection))
        oc.file_store = MockFileStore()

        rec = self.connection.query(
            "SELECT Id FROM Account WHERE Name = 'Caprica Cosmetics'"
        )
        oc.add_dependency("Account", rec.get("records")[0]["Id"])

        extraction = amaxa.ExtractionStep(
            "Account",
            amaxa.ExtractionScope.SELECTED_RECORDS,
            ["Id", "Name", "ParentId"],
        )
        oc.add_step(extraction)

        extraction.initialize()
        extraction.execute()

        self.assertEqual(3, len(oc.get_extracted_ids("Account")))
        for c in oc.file_store.get_csv(
            "Account", amaxa.FileType.OUTPUT
        ).writerow.call_args_list:
            self.assertIn(c[0][0]["Name"], expected_names)
            expected_names.remove(c[0][0]["Name"])

        self.assertEqual(0, len(expected_names))
Пример #4
0
    def test_store_result_registers_self_lookup_dependencies(self):
        connection = Mock()

        oc = amaxa.ExtractOperation(connection)

        oc.store_result = Mock()
        oc.add_dependency = Mock()
        oc.get_field_map = Mock(
            return_value={
                "Lookup__c": {
                    "name": "Lookup__c",
                    "type": "reference",
                    "referenceTo": ["Account"],
                }
            })
        oc.get_sobject_list = Mock(return_value=["Account"])

        step = amaxa.ExtractionStep("Account",
                                    amaxa.ExtractionScope.ALL_RECORDS,
                                    ["Lookup__c"])
        oc.add_step(step)
        step.initialize()

        step.store_result({
            "Id": "001000000000000",
            "Lookup__c": "001000000000001",
            "Name": "Picon Fleet Headquarters",
        })
        oc.add_dependency.assert_called_once_with(
            "Account", amaxa.SalesforceId("001000000000001"))
    def test_loads_and_extracts_high_data_volume(self):
        # This is a single unit test rather than multiple to save on execution time.
        records = []

        for i in range(100000):
            records.append({
                "Id": "00Q000000{:06d}".format(i),
                "Company": "[not provided]",
                "LastName": "Lead {:06d}".format(i),
            })

        op = amaxa.LoadOperation(Connection(self.connection, "52.0"))
        op.file_store = MockFileStore()
        op.file_store.records["Lead"] = records
        op.add_step(amaxa.LoadStep("Lead", set(["LastName", "Company"])))

        op.initialize()
        op.execute()

        self.assertEqual(
            100000,
            self.connection.query("SELECT count() FROM Lead").get("totalSize"))

        oc = amaxa.ExtractOperation(Connection(self.connection, "52.0"))
        oc.file_store = MockFileStore()

        extraction = amaxa.ExtractionStep("Lead",
                                          amaxa.ExtractionScope.ALL_RECORDS,
                                          ["Id", "LastName"])
        oc.add_step(extraction)

        extraction.initialize()
        extraction.execute()

        self.assertEqual(100000, len(oc.get_extracted_ids("Lead")))
Пример #6
0
    def test_store_result_respects_self_lookup_options(self):
        connection = Mock()

        oc = amaxa.ExtractOperation(connection)

        oc.store_result = Mock()
        oc.add_dependency = Mock()
        oc.get_field_map = Mock(
            return_value={
                "Lookup__c": {
                    "name": "Lookup__c",
                    "type": "reference",
                    "referenceTo": ["Account"],
                }
            })
        oc.get_sobject_list = Mock(return_value=["Account"])

        step = amaxa.ExtractionStep(
            "Account",
            amaxa.ExtractionScope.ALL_RECORDS,
            ["Lookup__c"],
            None,
            amaxa.SelfLookupBehavior.TRACE_NONE,
        )
        oc.add_step(step)
        step.initialize()

        step.store_result({
            "Id": "001000000000000",
            "Lookup__c": "001000000000001",
            "Name": "Picon Fleet Headquarters",
        })
        oc.add_dependency.assert_not_called()
Пример #7
0
    def test_get_sobject_ids_for_reference_returns_correct_ids(self):
        connection = Mock()

        oc = amaxa.ExtractOperation(connection)
        oc.file_store = MockFileStore()
        oc.get_field_map = Mock(
            return_value={"Lookup__c": {"referenceTo": ["Account", "Contact"]}}
        )

        oc.store_result(
            "Account", {"Id": "001000000000000", "Name": "University of Caprica"}
        )
        oc.store_result("Contact", {"Id": "003000000000000", "Name": "Gaius Baltar"})
        oc.store_result(
            "Opportunity", {"Id": "006000000000000", "Name": "Defense Mainframe"}
        )

        self.assertEqual(
            set(
                [
                    amaxa.SalesforceId("001000000000000"),
                    amaxa.SalesforceId("003000000000000"),
                ]
            ),
            oc.get_sobject_ids_for_reference("Account", "Lookup__c"),
        )
Пример #8
0
    def test_initialize_identifies_all_lookups_within_extraction(self):
        connection = Mock()

        oc = amaxa.ExtractOperation(connection)

        oc.file_store.set_csv("Account", amaxa.FileType.OUTPUT, Mock())
        oc.file_store.set_csv("Contact", amaxa.FileType.OUTPUT, Mock())
        oc.get_field_map = Mock(
            return_value={
                "Lookup__c": {
                    "name": "Lookup__c",
                    "type": "reference",
                    "referenceTo": ["Account"],
                },
                "Other__c": {
                    "name": "Other__c",
                    "type": "reference",
                    "referenceTo": ["Contact"],
                },
                "Outside__c": {
                    "name": "Outside__c",
                    "type": "reference",
                    "referenceTo": ["Opportunity"],
                },
            })
        oc.get_sobject_list = Mock(return_value=["Account", "Contact"])

        step = ConcreteStep("Account", ["Lookup__c", "Other__c", "Outside__c"])
        oc.add_step(step)

        step.initialize()

        self.assertEqual(set(["Other__c", "Lookup__c"]), step.all_lookups)
Пример #9
0
    def test_initialize_handles_mixed_polymorphic_lookups(self):
        connection = Mock()

        oc = amaxa.ExtractOperation(connection)

        oc.file_store.set_csv("Account", amaxa.FileType.OUTPUT, Mock())
        oc.file_store.set_csv("Contact", amaxa.FileType.OUTPUT, Mock())
        oc.file_store.set_csv("Opportunity", amaxa.FileType.OUTPUT, Mock())
        oc.get_field_map = Mock(
            return_value={
                "Poly_Lookup__c": {
                    "name": "Lookup__c",
                    "type": "reference",
                    "referenceTo": ["Account", "Opportunity"],
                },
                "Other__c": {
                    "name": "Other__c",
                    "type": "reference",
                    "referenceTo": ["Contact"],
                },
            })
        oc.get_sobject_list = Mock(
            return_value=["Account", "Contact", "Opportunity"])

        step = ConcreteStep("Contact", ["Poly_Lookup__c", "Other__c"])
        oc.add_step(step)

        step.initialize()

        self.assertEqual(set(["Poly_Lookup__c"]), step.dependent_lookups)
        self.assertEqual(set(["Poly_Lookup__c"]), step.descendent_lookups)
Пример #10
0
    def test_store_result_clears_dependencies(self):
        connection = Mock()

        oc = amaxa.ExtractOperation(connection)
        oc.file_store = MockFileStore()
        oc.add_dependency("Account", amaxa.SalesforceId("001000000000000"))

        oc.store_result("Account", {"Id": "001000000000000", "Name": "Caprica Steel"})
        self.assertEqual(set(), oc.get_dependencies("Account"))
Пример #11
0
    def test_store_result_retains_ids(self):
        connection = Mock()

        oc = amaxa.ExtractOperation(connection)
        oc.file_store = MockFileStore()

        oc.store_result("Account", {"Id": "001000000000000", "Name": "Caprica Steel"})
        self.assertEqual(
            set([amaxa.SalesforceId("001000000000000")]), oc.extracted_ids["Account"]
        )
Пример #12
0
    def test_add_dependency_tracks_dependencies(self):
        connection = Mock()

        oc = amaxa.ExtractOperation(connection)

        self.assertEqual(set(), oc.get_dependencies("Account"))
        oc.add_dependency("Account", amaxa.SalesforceId("001000000000000"))
        self.assertEqual(
            set([amaxa.SalesforceId("001000000000000")]), oc.get_dependencies("Account")
        )
Пример #13
0
    def test_store_result_writes_records(self):
        connection = Mock()

        oc = amaxa.ExtractOperation(connection)
        oc.file_store = MockFileStore()

        oc.store_result("Account", {"Id": "001000000000000", "Name": "Caprica Steel"})
        oc.file_store.get_csv(
            "Account", amaxa.FileType.OUTPUT
        ).writerow.assert_called_once_with(
            {"Id": "001000000000000", "Name": "Caprica Steel"}
        )
Пример #14
0
    def test_execute_runs_all_steps(self):
        connection = Mock()
        oc = amaxa.ExtractOperation(connection)

        # pylint: disable=W0612
        for i in range(3):
            oc.add_step(Mock(sobjectname=str(i), errors=[]))

        oc.execute()

        for s in oc.steps:
            s.execute.assert_called_once_with()
            self.assertEqual(oc, s.context)
Пример #15
0
    def test_all_records_extracts_accounts(self):
        oc = amaxa.ExtractOperation(Connection(self.connection))
        oc.file_store = MockFileStore()

        extraction = amaxa.ExtractionStep(
            "Account", amaxa.ExtractionScope.ALL_RECORDS, ["Id", "Name"]
        )
        oc.add_step(extraction)

        extraction.initialize()
        extraction.execute()

        self.assertEqual(5, len(oc.get_extracted_ids("Account")))
Пример #16
0
    def test_store_result_handles_empty_lookup_values(self):
        connection = Mock()

        oc = amaxa.ExtractOperation(connection)

        oc.store_result = Mock()
        oc.add_dependency = Mock()
        oc.get_extracted_ids = Mock()
        oc.get_field_map = Mock(
            return_value={
                "Lookup__c": {
                    "name": "Lookup__c",
                    "type": "reference",
                    "referenceTo": ["Opportunity", "Account", "Task"],
                }
            })
        oc.get_sobject_list = Mock(
            return_value=["Account", "Contact", "Opportunity"])
        oc.get_sobject_name_for_id = Mock()

        step = amaxa.ExtractionStep("Contact",
                                    amaxa.ExtractionScope.ALL_RECORDS,
                                    ["Lookup__c"])
        oc.add_step(step)
        step.initialize()

        # Validate that the polymorphic lookup is treated properly when the content is a dependent reference
        step.dependent_lookups = ["Lookup__c"]
        step.descendent_lookups = []
        step.self_lookups = []
        step.store_result({
            "Id": "001000000000000",
            "Lookup__c": None,
            "Name": "Kara Thrace"
        })
        oc.add_dependency.assert_not_called()
        oc.get_sobject_name_for_id.assert_not_called()
        oc.get_extracted_ids.assert_not_called()

        # Validate that the polymorphic lookup is treated properly when the content is a descendent reference
        step.dependent_lookups = []
        step.descendent_lookups = ["Lookup__c"]
        step.self_lookups = []
        step.store_result({
            "Id": "001000000000000",
            "Lookup__c": None,
            "Name": "Kara Thrace"
        })
        oc.add_dependency.assert_not_called()
        oc.get_sobject_name_for_id.assert_not_called()
        oc.get_extracted_ids.assert_not_called()
Пример #17
0
    def test_execute_returns_on_error(self):
        connection = Mock()
        oc = amaxa.ExtractOperation(connection)

        # pylint: disable=W0612
        for i in range(3):
            oc.add_step(Mock(sobjectname=str(i), errors=[]))
        oc.steps[1].errors = ["Bad things happened"]

        assert oc.execute() == -1

        oc.steps[0].execute.assert_called_once_with()
        oc.steps[1].execute.assert_called_once_with()
        oc.steps[2].execute.assert_not_called()
Пример #18
0
    def test_perform_bulk_api_pass_stores_results(self):
        retval = [{"Id": "001000000000001"}, {"Id": "001000000000002"}]
        connection = MockConnection(bulk_query_results=retval)

        oc = amaxa.ExtractOperation(connection)

        step = amaxa.ExtractionStep("Account",
                                    amaxa.ExtractionScope.ALL_RECORDS,
                                    ["Name"])
        step.store_result = Mock()
        oc.add_step(step)
        step.initialize()

        step.perform_bulk_api_pass("SELECT Id FROM Account")
        step.store_result.assert_any_call(retval[0])
        step.store_result.assert_any_call(retval[1])
Пример #19
0
    def test_execute_loads_all_descendents(self):
        connection = Mock()
        connection.retrieve_records_by_id.return_value = []

        oc = amaxa.ExtractOperation(connection)
        oc.get_field_map = Mock(
            return_value={
                "Name": {
                    "name": "Name",
                    "type": "text"
                },
                "AccountId": {
                    "name": "AccountId",
                    "type": "reference",
                    "referenceTo": ["Account"],
                },
                "Household__c": {
                    "name": "Household__c",
                    "type": "reference",
                    "referenceTo": ["Account"],
                },
                "Event__c": {
                    "name": "Event__c",
                    "type": "reference",
                    "referenceTo": ["Event__c"],
                },
            })
        oc.get_sobject_list = Mock(return_value=["Account", "Contact"])

        step = amaxa.ExtractionStep(
            "Contact",
            amaxa.ExtractionScope.DESCENDENTS,
            ["Name", "AccountId", "Household__c"],
        )
        step.perform_lookup_pass = Mock()
        oc.add_step(step)

        step.initialize()
        step.execute()

        step.perform_lookup_pass.assert_has_calls(
            [
                unittest.mock.call("AccountId"),
                unittest.mock.call("Household__c")
            ],
            any_order=True,
        )
Пример #20
0
    def test_extracts_dependencies(self):
        expected_account_names = {
            "Caprica Cosmetics",
            "Gemenon Gastronomy",
            "Aerilon Agrinomics",
        }
        expected_contact_names = {"Gaius"}

        oc = amaxa.ExtractOperation(Connection(self.connection))
        oc.file_store = MockFileStore()

        rec = self.connection.query("SELECT Id FROM Contact WHERE LastName = 'Baltar'")
        oc.add_dependency("Contact", rec.get("records")[0]["Id"])

        oc.add_step(
            amaxa.ExtractionStep(
                "Contact",
                amaxa.ExtractionScope.SELECTED_RECORDS,
                ["Id", "FirstName", "LastName", "AccountId"],
            )
        )
        oc.add_step(
            amaxa.ExtractionStep(
                "Account", amaxa.ExtractionScope.DESCENDENTS, ["Id", "Name", "ParentId"]
            )
        )

        oc.initialize()
        oc.execute()

        self.assertEqual(3, len(oc.get_extracted_ids("Account")))
        self.assertEqual(1, len(oc.get_extracted_ids("Contact")))

        for c in oc.file_store.get_csv(
            "Contact", amaxa.FileType.OUTPUT
        ).writerow.call_args_list:
            self.assertIn(c[0][0]["FirstName"], expected_contact_names)
            expected_contact_names.remove(c[0][0]["FirstName"])
        self.assertEqual(0, len(expected_contact_names))

        for c in oc.file_store.get_csv(
            "Account", amaxa.FileType.OUTPUT
        ).writerow.call_args_list:
            self.assertIn(c[0][0]["Name"], expected_account_names)
            expected_account_names.remove(c[0][0]["Name"])
        self.assertEqual(0, len(expected_account_names))
Пример #21
0
    def test_store_result_respects_outside_lookup_behavior_error(self):
        connection = Mock()

        oc = amaxa.ExtractOperation(connection)

        oc.store_result = Mock()
        oc.add_dependency = Mock()
        oc.get_field_map = Mock(
            return_value={
                "AccountId": {
                    "name": "AccountId",
                    "type": "reference",
                    "referenceTo": ["Account"],
                },
                "LastName": {
                    "name": "Name",
                    "type": "string"
                },
            })
        oc.get_sobject_list = Mock(return_value=["Account", "Contact"])
        oc.get_extracted_ids = Mock(return_value=set())

        step = amaxa.ExtractionStep(
            "Contact",
            amaxa.ExtractionScope.DESCENDENTS,
            ["AccountId"],
            outside_lookup_behavior=amaxa.OutsideLookupBehavior.ERROR,
        )

        oc.add_step(step)
        step.initialize()

        step.store_result({
            "Id": "003000000000001",
            "AccountId": "001000000000001"
        })
        self.assertEqual(
            [
                "{} {} has an outside reference in field {} ({}), which is not allowed by the extraction configuration."
                .format("Contact", "003000000000001", "AccountId",
                        "001000000000001")
            ],
            step.errors,
        )
Пример #22
0
    def test_resolve_registered_dependencies_registers_error_for_missing_ids(
            self):
        connection = Mock()

        oc = amaxa.ExtractOperation(connection)
        oc.get_field_map = Mock(
            return_value={
                "Lookup__c": {
                    "name": "Lookup__c",
                    "type": "reference",
                    "referenceTo": ["Account"],
                }
            })
        oc.get_dependencies = Mock(side_effect=[
            set([
                amaxa.SalesforceId("001000000000001"),
                amaxa.SalesforceId("001000000000002"),
            ]),
            set([amaxa.SalesforceId("001000000000002")]),
        ])

        step = amaxa.ExtractionStep("Account",
                                    amaxa.ExtractionScope.ALL_RECORDS,
                                    ["Lookup__c"])
        connection.retrieve_records_by_id = Mock(return_value=[])
        oc.add_step(step)
        step.initialize()

        step.resolve_registered_dependencies()
        self.assertEqual(
            [
                "Unable to resolve dependencies for sObject {}. The following Ids could not be found: {}"
                .format(
                    step.sobjectname,
                    ", ".join([
                        str(i)
                        for i in [amaxa.SalesforceId("001000000000002")]
                    ]),
                )
            ],
            step.errors,
        )
Пример #23
0
    def test_execute_with_all_records_performs_bulk_api_pass(self):
        connection = Mock()

        oc = amaxa.ExtractOperation(connection)
        oc.get_field_map = Mock(
            return_value={"Name": {
                "name": "Name",
                "type": "text"
            }})

        step = amaxa.ExtractionStep("Account",
                                    amaxa.ExtractionScope.ALL_RECORDS,
                                    ["Name"])
        step.perform_bulk_api_pass = Mock()
        oc.add_step(step)

        step.initialize()
        step.execute()

        step.perform_bulk_api_pass.assert_called_once_with(
            "SELECT Name FROM Account")
Пример #24
0
    def test_store_result_transforms_output(self):
        connection = Mock()

        oc = amaxa.ExtractOperation(connection)
        oc.file_store = MockFileStore()
        mapper_mock = Mock()
        mapper_mock.transform_record = Mock(
            return_value={"Id": "001000000000000", "Name": "Caprica City Steel"}
        )

        oc.mappers["Account"] = mapper_mock

        oc.store_result("Account", {"Id": "001000000000000", "Name": "Caprica Steel"})
        mapper_mock.transform_record.assert_called_once_with(
            {"Id": "001000000000000", "Name": "Caprica Steel"}
        )
        oc.file_store.get_csv(
            "Account", amaxa.FileType.OUTPUT
        ).writerow.assert_called_once_with(
            {"Id": "001000000000000", "Name": "Caprica City Steel"}
        )
Пример #25
0
    def test_store_result_respects_outside_lookup_behavior_include(self):
        connection = Mock()

        oc = amaxa.ExtractOperation(connection)

        oc.store_result = Mock()
        oc.add_dependency = Mock()
        oc.get_field_map = Mock(
            return_value={
                "AccountId": {
                    "name": "AccountId",
                    "type": "reference",
                    "referenceTo": ["Account"],
                },
                "LastName": {
                    "name": "Name",
                    "type": "string"
                },
            })
        oc.get_sobject_list = Mock(return_value=["Account", "Contact"])
        oc.get_extracted_ids = Mock(return_value=set())

        step = amaxa.ExtractionStep(
            "Contact",
            amaxa.ExtractionScope.DESCENDENTS,
            ["AccountId"],
            outside_lookup_behavior=amaxa.OutsideLookupBehavior.INCLUDE,
        )

        oc.add_step(step)
        step.initialize()

        step.store_result({
            "Id": "003000000000001",
            "AccountId": "001000000000001"
        })
        oc.store_result.assert_called_once_with("Contact", {
            "Id": "003000000000001",
            "AccountId": "001000000000001"
        })
Пример #26
0
    def test_execute_with_query_performs_bulk_api_pass(self):
        connection = Mock()
        connection.bulk_api_query.return_value = []
        connection.retrieve_records_by_id.return_value = []

        oc = amaxa.ExtractOperation(connection)
        oc.get_field_map = Mock(
            return_value={"Name": {
                "name": "Name",
                "type": "text"
            }})

        step = amaxa.ExtractionStep("Account", amaxa.ExtractionScope.QUERY,
                                    ["Name"], "Name != null")
        step.perform_bulk_api_pass = Mock()
        oc.add_step(step)

        step.initialize()
        step.execute()

        step.perform_bulk_api_pass.assert_called_once_with(
            "SELECT Name FROM Account WHERE Name != null")
Пример #27
0
    def test_execute_does_not_trace_self_lookups_without_trace_all(self):
        connection = Mock()

        oc = amaxa.ExtractOperation(connection)
        oc.get_field_map = Mock(
            return_value={
                "Name": {
                    "name": "Name",
                    "type": "text"
                },
                "ParentId": {
                    "name": "ParentId",
                    "type": "reference",
                    "referenceTo": ["Account"],
                },
            })
        oc.get_extracted_ids = Mock()

        step = amaxa.ExtractionStep(
            "Account",
            amaxa.ExtractionScope.QUERY,
            ["Name", "ParentId"],
            "Name = 'ACME'",
            amaxa.SelfLookupBehavior.TRACE_NONE,
        )

        step.perform_bulk_api_pass = Mock()
        step.perform_lookup_pass = Mock()
        step.resolve_registered_dependencies = Mock()

        oc.add_step(step)

        step.initialize()
        step.execute()

        self.assertEqual(set(["ParentId"]), step.self_lookups)
        step.resolve_registered_dependencies.assert_called_once_with()
        oc.get_extracted_ids.assert_not_called()
Пример #28
0
    def test_execute_resolves_self_lookups(self):
        connection = Mock()

        oc = amaxa.ExtractOperation(connection)
        oc.get_field_map = Mock(
            return_value={
                "Name": {
                    "name": "Name",
                    "type": "text"
                },
                "ParentId": {
                    "name": "ParentId",
                    "type": "reference",
                    "referenceTo": ["Account"],
                },
            })
        oc.get_extracted_ids = Mock(side_effect=[
            set([amaxa.SalesforceId("001000000000001")]),
            set([
                amaxa.SalesforceId("001000000000001"),
                amaxa.SalesforceId("001000000000002"),
            ]),
            set([
                amaxa.SalesforceId("001000000000001"),
                amaxa.SalesforceId("001000000000002"),
            ]),
            set([
                amaxa.SalesforceId("001000000000001"),
                amaxa.SalesforceId("001000000000002"),
            ]),
        ])

        step = amaxa.ExtractionStep(
            "Account",
            amaxa.ExtractionScope.QUERY,
            ["Name", "ParentId"],
            "Name = 'ACME'",
        )
        step.perform_bulk_api_pass = Mock()
        step.perform_lookup_pass = Mock()
        step.resolve_registered_dependencies = Mock()
        oc.add_step(step)

        step.initialize()
        self.assertEqual(set(["ParentId"]), step.self_lookups)

        step.execute()

        step.perform_bulk_api_pass.assert_called_once_with(
            "SELECT Name, ParentId FROM Account WHERE Name = 'ACME'")
        oc.get_extracted_ids.assert_has_calls([
            unittest.mock.call("Account"),
            unittest.mock.call("Account"),
            unittest.mock.call("Account"),
            unittest.mock.call("Account"),
        ])
        step.perform_lookup_pass.assert_has_calls(
            [unittest.mock.call("ParentId"),
             unittest.mock.call("ParentId")])
        step.resolve_registered_dependencies.assert_has_calls(
            [unittest.mock.call(), unittest.mock.call()])
Пример #29
0
    def test_store_result_handles_polymorphic_lookups(self):
        connection = Mock()

        oc = amaxa.ExtractOperation(connection)

        oc.store_result = Mock()
        oc.add_dependency = Mock()
        oc.get_field_map = Mock(
            return_value={
                "Lookup__c": {
                    "name": "Lookup__c",
                    "type": "reference",
                    "referenceTo": ["Opportunity", "Account", "Task"],
                }
            })
        oc.get_sobject_list = Mock(
            return_value=["Account", "Contact", "Opportunity"])
        oc.get_extracted_ids = Mock(return_value=["001000000000001"])
        oc.get_sobject_name_for_id = Mock(side_effect=lambda id: {
            "001": "Account",
            "006": "Opportunity",
            "00T": "Task",
        }[id[:3]])

        step = amaxa.ExtractionStep("Contact",
                                    amaxa.ExtractionScope.ALL_RECORDS,
                                    ["Lookup__c"])
        oc.add_step(step)
        step.initialize()

        # Validate that the polymorphic lookup is treated properly when the content is a dependent reference
        step.store_result({
            "Id": "001000000000000",
            "Lookup__c": "006000000000001",
            "Name": "Kara Thrace",
        })
        oc.add_dependency.assert_called_once_with(
            "Opportunity", amaxa.SalesforceId("006000000000001"))
        oc.store_result.assert_called_once_with(
            "Contact",
            {
                "Id": "001000000000000",
                "Lookup__c": "006000000000001",
                "Name": "Kara Thrace",
            },
        )
        oc.add_dependency.reset_mock()
        oc.store_result.reset_mock()

        # Validate that the polymorphic lookup is treated properly when the content is a descendent reference
        step.store_result({
            "Id": "001000000000000",
            "Lookup__c": "001000000000001",
            "Name": "Kara Thrace",
        })
        oc.add_dependency.assert_not_called()
        oc.store_result.assert_called_once_with(
            "Contact",
            {
                "Id": "001000000000000",
                "Lookup__c": "001000000000001",
                "Name": "Kara Thrace",
            },
        )
        oc.add_dependency.reset_mock()
        oc.store_result.reset_mock()

        # Validate that the polymorphic lookup is treated properly when the content is a off-extraction reference
        step.store_result({
            "Id": "001000000000000",
            "Lookup__c": "00T000000000001",
            "Name": "Kara Thrace",
        })
        oc.add_dependency.assert_not_called()
        oc.store_result.assert_called_once_with(
            "Contact",
            {
                "Id": "001000000000000",
                "Lookup__c": "00T000000000001",
                "Name": "Kara Thrace",
            },
        )