public void Test_ResourceKey_MergeScenarios(string csvData, string special, string expectErrorContains, int expectSucceed, int expectFail, int expectedInstances) { EntityType type = null; ImportConfig config = null; try { // Define schema Field keyField = Resource.Name_Field.As <Field>( ); type = new EntityType { ResourceKeys = { new ResourceKey { ResourceKeyMessage = special == "custom message" ? "Custom error" : null, MergeDuplicates = special == "key merges", KeyFields = { keyField } } } }; type.Save( ); // Existing instance IEntity instance = Entity.Create(type.Id); instance.SetField(keyField.Id, "Existing"); instance.Save( ); // Define import config config = new ImportConfig { ImportFileType_Enum = ImportFileTypeEnum_Enumeration.ImportFileTypeCsv, ImportConfigMapping = new ApiResourceMapping { ImportMergeExisting = special == "config merges", ImportHeadingRow = 0, ImportDataRow = 1, MappedType = type, ResourceMemberMappings = { new ApiFieldMapping { Name = "1", MappedField = keyField }.As <ApiMemberMapping>( ) } } }; ImportRun importRun = ImportTestHelper.CsvImport(config, csvData); if (expectSucceed > 0) { Assert.That(importRun.ImportRunStatus_Enum, Is.EqualTo(WorkflowRunState_Enumeration.WorkflowRunCompleted), importRun.ImportMessages); } else { Assert.That(importRun.ImportRunStatus_Enum, Is.EqualTo(WorkflowRunState_Enumeration.WorkflowRunFailed), importRun.ImportMessages); } if (!string.IsNullOrEmpty(expectErrorContains)) { Assert.That(importRun.ImportMessages, Contains.Substring(expectErrorContains)); } Assert.That(importRun.ImportRecordsSucceeded, Is.EqualTo(expectSucceed), importRun.ImportMessages); Assert.That(importRun.ImportRecordsFailed, Is.EqualTo(expectFail), importRun.ImportMessages); int instances = Entity.GetInstancesOfType(type.Id).Count( ); Assert.That(instances, Is.EqualTo(expectedInstances), importRun.ImportMessages); } finally { type?.Delete( ); config?.Delete( ); } }
public void Test_OneToOne_Import_27716( ) { EntityType type1 = null; EntityType type2 = null; ImportConfig config = null; Relationship rel; try { // Define schema type1 = new EntityType( ); type1.Save( ); type2 = new EntityType( ); type2.Save( ); rel = new Relationship { Cardinality_Enum = CardinalityEnum_Enumeration.OneToOne, FromType = type1, ToType = type2 }; rel.Save( ); // Existing instance IEntity instance2 = Entity.Create(type2.Id); instance2.SetField(Resource.Name_Field, "Target"); instance2.Save( ); // Define import config config = new ImportConfig { ImportFileType_Enum = ImportFileTypeEnum_Enumeration.ImportFileTypeCsv, ImportConfigMapping = new ApiResourceMapping { ImportHeadingRow = 0, ImportDataRow = 1, MappedType = type1, ResourceMemberMappings = { new ApiRelationshipMapping { Name = "1", MappedRelationship = rel, MapRelationshipInReverse = false }.As <ApiMemberMapping>( ) } } }; string csvData = "Target"; // Run import a first time ImportRun importRun = ImportTestHelper.CsvImport(config, csvData); Assert.That(importRun.ImportRunStatus_Enum, Is.EqualTo(WorkflowRunState_Enumeration.WorkflowRunCompleted), importRun.ImportMessages); // Check that instance and relationship got created IEntity instance1 = Entity.GetInstancesOfType(type1).FirstOrDefault( ); Assert.That(instance1, Is.Not.Null); var relValues = instance1.GetRelationships(rel.Id, Direction.Forward).ToList( ); Assert.That(relValues, Has.Count.EqualTo(1)); Assert.That(relValues[0].Id, Is.EqualTo(instance2.Id)); // Run import a second time importRun = ImportTestHelper.CsvImport(config, csvData); Assert.That(importRun.ImportRunStatus_Enum, Is.EqualTo(WorkflowRunState_Enumeration.WorkflowRunFailed), importRun.ImportMessages); var instances = Entity.GetInstancesOfType(type1).ToList(); Assert.That(importRun.ImportMessages, Contains.Substring("Line 1: The source item cannot be associated with Target because it is already associated with another record")); Assert.That(instances, Has.Count.EqualTo(1), "Final instance count"); } finally { type1?.Delete( ); type2?.Delete( ); config?.Delete( ); } }
public void Test_ResourceKey_FieldMerge(string csvData, string expectedMergedValue, int expectedInstances) { EntityType type = null; ImportConfig config = null; try { // Define schema Field keyField = new StringField( ).As <Field>( ); Field overwriteField = new StringField( ).As <Field>( ); Field untouchedField = new StringField( ).As <Field>( ); type = new EntityType { Fields = { keyField, overwriteField, untouchedField }, ResourceKeys = { new ResourceKey { Name = "Test resource key", MergeDuplicates = true, KeyFields = { keyField } } } }; type.Save( ); // Existing instance IEntity instance = Entity.Create(type.Id); instance.SetField(keyField.Id, "Existing"); instance.SetField(overwriteField.Id, "Hammer"); instance.SetField(untouchedField.Id, "Can't touch this"); instance.Save( ); // Define import config config = new ImportConfig { ImportFileType_Enum = ImportFileTypeEnum_Enumeration.ImportFileTypeCsv, ImportConfigMapping = new ApiResourceMapping { ImportMergeExisting = true, ImportHeadingRow = 0, ImportDataRow = 1, MappedType = type, ResourceMemberMappings = { new ApiFieldMapping { Name = "1", MappedField = keyField }.As <ApiMemberMapping>( ), new ApiFieldMapping { Name = "2", MappedField = overwriteField }.As <ApiMemberMapping>( ), } } }; ImportRun importRun = ImportTestHelper.CsvImport(config, csvData); Assert.That(importRun.ImportRunStatus_Enum, Is.EqualTo(WorkflowRunState_Enumeration.WorkflowRunCompleted), importRun.ImportMessages); var instances = Entity.GetInstancesOfType(type.Id).ToList( ); Assert.That(instances.Count, Is.EqualTo(expectedInstances)); IEntity existingInstance = instances.FirstOrDefault(e => e.GetField <string>(keyField.Id) == "Existing"); Assert.That(existingInstance, Is.Not.Null, "Existing instance."); Assert.That(existingInstance.GetField(untouchedField.Id), Is.EqualTo("Can't touch this")); Assert.That(existingInstance.GetField(overwriteField.Id), Is.EqualTo(expectedMergedValue)); // we got lazy and mapped the name column to both the name field and overwrite field. } finally { config?.Delete( ); type?.Delete( ); } }