public void collection_insert_with_pks_generates_insert_for_all_with_update_contingencies() { var dtos = new List<ParentDto>() {new ParentDto {ParentKey = 1}, new ParentDto {ParentKey = 2}}; var logger = new MockSimpleSaveLogger(); SimpleSaveExtensions.Logger = logger; try { using (IDbConnection connection = new SqlConnection()) // TODO: localdb connection { connection.CreateAll((IEnumerable<ParentDto>) dtos); } } catch (InvalidOperationException) { // Because we haven't opened the connection (deliberately - we don't care; we just want to inspect the scripts) } var scripts = logger.Scripts; Assert.AreEqual(2, scripts.Count, "Unexpected number of scripts."); foreach (var script in scripts) { var sql = script.Buffer.ToString(); Assert.IsTrue(sql.Contains("INSERT INTO dbo.[Parent]"), "No insert on parent."); Assert.AreEqual(1, Regex.Matches(sql, "INSERT").Count, "Unexpected number of INSERTs."); Assert.AreEqual(1, Regex.Matches(sql, "UPDATE").Count, "Should be one UPDATE."); Assert.AreEqual(0, Regex.Matches(sql, "DELETE").Count, "Should be no DELETEs."); Assert.AreEqual(3, Regex.Matches(sql, "SELECT").Count, "Should be three SELECTs - one to check for existence of row, and two for returning PK value of INSERTED/UPDATED row."); } }
public void insert_application_with_nested_children_inserts_children() { var application = CreateTestApplication(); var logger = new MockSimpleSaveLogger(); SimpleSaveExtensions.Logger = logger; try { using (IDbConnection connection = new SqlConnection()) { connection.Create(application); } } catch (InvalidOperationException) { // Don't care } var scripts = logger.Scripts; Assert.AreEqual(5, scripts.Count, "Unexpected number of scripts."); Assert.IsTrue(scripts[0].Buffer.ToString().Contains("INSERT INTO [app].[APPLICATION_MST]")); Assert.IsTrue(scripts[1].Buffer.ToString().Contains("INSERT INTO [app].[LEGAL_INFO_MST]")); // Address has to go in first or it can't be referenced, which is where the problem lies Assert.IsTrue(scripts[2].Buffer.ToString().Contains("INSERT INTO [gen].[ADDRESS_MST]")); Assert.IsTrue(scripts[3].Buffer.ToString().Contains("INSERT INTO [app].[LOCATION_MST]")); Assert.IsTrue(scripts[4].Buffer.ToString().Contains("INSERT INTO [opp].[OPPORTUNITY_MST]")); }
private void check_changes_only_appear_once(DateTimePropertiesDto dto1, DateTimePropertiesDto dto2) { var logger = new MockSimpleSaveLogger(); SimpleSaveExtensions.Logger = logger; SimpleSaveExtensions.DifferenceProcessed += SimpleSaveExtensions_DifferenceProcessed; try { using (IDbConnection connection = new SqlConnection()) { connection.Update(dto1, dto2); } } catch (InvalidOperationException) { // Don't care } var scripts = logger.Scripts; Assert.AreEqual(1, scripts.Count, "Unexpected number of scripts."); var sql = scripts[0].Buffer.ToString(); Assert.AreEqual(sql.IndexOf("[DateTimeProperty]"), sql.LastIndexOf("[DateTimeProperty]"), "Should only be one occurence of [DateTimeProperty]."); Assert.AreEqual(sql.IndexOf("[DateTimeOffsetProperty]"), sql.LastIndexOf("[DateTimeOffsetProperty]"), "Should only be one occurence of [DateTimeOffsetProperty]."); }
public void insert_contact_referencing_phone_numbers_sets_guid_values_not_ints_on_fk_columns() { var logger = new MockSimpleSaveLogger(); SimpleSaveExtensions.Logger = logger; try { using (var connection = new SqlConnection()) { connection.Create(SamplePopulatedDtos.Contact); } } catch (InvalidOperationException) { // Not a real DB connection. } var scripts = logger.Scripts; Assert.AreEqual(1, scripts.Count, "Unexpected number of scripts."); var script = scripts[0]; var guid = ExtractGuid(script, "p9"); Assert.AreEqual( SamplePopulatedDtos.Contact.MainPhone.PhoneGuid, guid, string.Format("Invalid main phone GUID: {0}", guid)); guid = ExtractGuid(script, "p9"); Assert.AreEqual( SamplePopulatedDtos.Contact.MobilePhone.PhoneGuid, guid, string.Format("Invalid main phone GUID: {0}", guid)); }
public void collection_update_generates_update_for_all() { var updates = new List <Tuple <ParentDto, ParentDto> >() { Tuple.Create(new ParentDto { ParentKey = 1, IsActive = false }, new ParentDto { ParentKey = 1, IsActive = true }), Tuple.Create(new ParentDto { ParentKey = 2, IsActive = true }, new ParentDto { ParentKey = 2, IsActive = false }) }; var logger = new MockSimpleSaveLogger(); SimpleSaveExtensions.Logger = logger; try { using (IDbConnection connection = new SqlConnection()) { connection.UpdateAll(updates); } } catch (InvalidOperationException) { // Because we haven't opened the connection (deliberately - we don't care; we just want to inspect the scripts) } var scripts = logger.Scripts; Assert.AreEqual(2, scripts.Count, "Unexpected number of scripts."); foreach (var script in scripts) { var sql = script.Buffer.ToString(); Assert.IsTrue(sql.Contains("UPDATE dbo.[Parent]"), "No update on parent."); Assert.AreEqual(1, Regex.Matches(sql, "UPDATE").Count, "Unexpected number of UPDATEs."); Assert.AreEqual(0, Regex.Matches(sql, "INSERT").Count, "Should be no INSERTs."); Assert.AreEqual(0, Regex.Matches(sql, "DELETE").Count, "Should be no DELETEs."); Assert.AreEqual(0, Regex.Matches(sql, "SELECT").Count, "Should be no SELECTs."); } }
public void insert_offer_with_gateway_offer_should_only_insert_offer() { var offer = new OfferDto { GatewayOffer = new GatewayOfferDto { IsBilledYearly = true, PeriodicCharge = 100 }, OfferReference = "Test offer", OpportunityGuid = Guid.NewGuid() }; var logger = new MockSimpleSaveLogger(); SimpleSaveExtensions.Logger = logger; try { using (IDbConnection connection = new SqlConnection()) { connection.Create(offer); } } catch (InvalidOperationException) { // Don't care } var scripts = logger.Scripts; Assert.AreEqual(1, scripts.Count, "Unexpected number of scripts."); var sql = scripts[0].Buffer.ToString(); Assert.IsTrue( sql.Contains("INSERT INTO [opp].[OFFER_TRN]"), "Should insert into OFFER_TRN."); Assert.IsFalse( sql.Contains("[opp].[GATEWAY_OFFER_TRN]"), "Should not insert into GATEWAY_OFFER_TRN"); }
public void collection_insert_with_pks_generates_insert_for_all_with_update_contingencies() { var dtos = new List <ParentDto>() { new ParentDto { ParentKey = 1 }, new ParentDto { ParentKey = 2 } }; var logger = new MockSimpleSaveLogger(); SimpleSaveExtensions.Logger = logger; try { using (IDbConnection connection = new SqlConnection()) // TODO: localdb connection { connection.CreateAll((IEnumerable <ParentDto>)dtos); } } catch (InvalidOperationException) { // Because we haven't opened the connection (deliberately - we don't care; we just want to inspect the scripts) } var scripts = logger.Scripts; Assert.AreEqual(2, scripts.Count, "Unexpected number of scripts."); foreach (var script in scripts) { var sql = script.Buffer.ToString(); Assert.IsTrue(sql.Contains("INSERT INTO dbo.[Parent]"), "No insert on parent."); Assert.AreEqual(1, Regex.Matches(sql, "INSERT").Count, "Unexpected number of INSERTs."); Assert.AreEqual(1, Regex.Matches(sql, "UPDATE").Count, "Should be one UPDATE."); Assert.AreEqual(0, Regex.Matches(sql, "DELETE").Count, "Should be no DELETEs."); Assert.AreEqual(3, Regex.Matches(sql, "SELECT").Count, "Should be three SELECTs - one to check for existence of row, and two for returning PK value of INSERTED/UPDATED row."); } }
public void collection_update_generates_update_for_all() { var updates = new List<Tuple<ParentDto, ParentDto>>() { Tuple.Create(new ParentDto { ParentKey = 1, IsActive = false }, new ParentDto { ParentKey = 1, IsActive = true}), Tuple.Create(new ParentDto { ParentKey = 2, IsActive = true }, new ParentDto { ParentKey = 2 , IsActive = false}) }; var logger = new MockSimpleSaveLogger(); SimpleSaveExtensions.Logger = logger; try { using (IDbConnection connection = new SqlConnection()) { connection.UpdateAll(updates); } } catch (InvalidOperationException) { // Because we haven't opened the connection (deliberately - we don't care; we just want to inspect the scripts) } var scripts = logger.Scripts; Assert.AreEqual(2, scripts.Count, "Unexpected number of scripts."); foreach (var script in scripts) { var sql = script.Buffer.ToString(); Assert.IsTrue(sql.Contains("UPDATE dbo.[Parent]"), "No update on parent."); Assert.AreEqual(1, Regex.Matches(sql, "UPDATE").Count, "Unexpected number of UPDATEs."); Assert.AreEqual(0, Regex.Matches(sql, "INSERT").Count, "Should be no INSERTs."); Assert.AreEqual(0, Regex.Matches(sql, "DELETE").Count, "Should be no DELETEs."); Assert.AreEqual(0, Regex.Matches(sql, "SELECT").Count, "Should be no SELECTs."); } }
public void delete_link_by_removing_child_does_not_delete_child_row_in_database() { var user = new UserDto() { UserKey = 27, Username = "******", FirstName = "John", LastName = "Doe" }; var users = new List<UserDto>() { user }; var oldObject = new TeleAppointerDto { UserKey = 555, Campaigns = new List<CampaignDto>() { new CampaignDto() { CampaignKey = 1, DaysSinceMerchantLastUpdated = 2, Description = "First campaign", Name = "First", CurrentlyAssignedTeleAppointers = users }, new CampaignDto() { CampaignKey = 2, DaysSinceMerchantLastUpdated = 3, Description = "Second campaign", Name = "Second", CurrentlyAssignedTeleAppointers = users }, new CampaignDto() { CampaignKey = 3, DaysSinceMerchantLastUpdated = 4, Description = "Third campaign", Name = "Third", CurrentlyAssignedTeleAppointers = users } } }; var newObject = new TeleAppointerDto { UserKey = 555, Campaigns = new List<CampaignDto>() { new CampaignDto() { CampaignKey = 4, DaysSinceMerchantLastUpdated = 5, Description = "Fourth campaign", Name = "Fourth", CurrentlyAssignedTeleAppointers = users } } }; var logger = new MockSimpleSaveLogger(); SimpleSaveExtensions.Logger = logger; try { using (IDbConnection connection = new SqlConnection()) { connection.Update(oldObject, newObject); } } catch (InvalidOperationException) { // Don't care } var scripts = logger.Scripts; Assert.AreEqual(1, scripts.Count, "Unexpected number of scripts."); scripts.AssertFragment(0, "DELETE FROM tele.TELE_APPOINTER_CAMPAIGN_LNK"); scripts.AssertFragment(0, "INSERT INTO tele.TELE_APPOINTER_CAMPAIGN_LNK"); scripts.AssertNoFragment(0, "DELETE FROM [tele].CAMPAIGN_MST"); scripts.AssertNoFragment(0, "DELETE FROM [user].USER_MST"); }
public void delete_link_by_removing_child_does_not_delete_child_row_in_database() { var user = new UserDto() { UserKey = 27, Username = "******", FirstName = "John", LastName = "Doe" }; var users = new List <UserDto>() { user }; var oldObject = new TeleAppointerDto { UserKey = 555, Campaigns = new List <CampaignDto>() { new CampaignDto() { CampaignKey = 1, DaysSinceMerchantLastUpdated = 2, Description = "First campaign", Name = "First", CurrentlyAssignedTeleAppointers = users }, new CampaignDto() { CampaignKey = 2, DaysSinceMerchantLastUpdated = 3, Description = "Second campaign", Name = "Second", CurrentlyAssignedTeleAppointers = users }, new CampaignDto() { CampaignKey = 3, DaysSinceMerchantLastUpdated = 4, Description = "Third campaign", Name = "Third", CurrentlyAssignedTeleAppointers = users } } }; var newObject = new TeleAppointerDto { UserKey = 555, Campaigns = new List <CampaignDto>() { new CampaignDto() { CampaignKey = 4, DaysSinceMerchantLastUpdated = 5, Description = "Fourth campaign", Name = "Fourth", CurrentlyAssignedTeleAppointers = users } } }; var logger = new MockSimpleSaveLogger(); SimpleSaveExtensions.Logger = logger; try { using (IDbConnection connection = new SqlConnection()) { connection.Update(oldObject, newObject); } } catch (InvalidOperationException) { // Don't care } var scripts = logger.Scripts; Assert.AreEqual(1, scripts.Count, "Unexpected number of scripts."); scripts.AssertFragment(0, "DELETE FROM tele.TELE_APPOINTER_CAMPAIGN_LNK"); scripts.AssertFragment(0, "INSERT INTO tele.TELE_APPOINTER_CAMPAIGN_LNK"); scripts.AssertNoFragment(0, "DELETE FROM [tele].CAMPAIGN_MST"); scripts.AssertNoFragment(0, "DELETE FROM [user].USER_MST"); }
public void delete_offer_deletes_equipment_offer_descendents() { var offerGuid = Guid.NewGuid(); var equipmentOfferGuid = Guid.NewGuid(); var offer = new OfferDto { OfferGuid = offerGuid, Equipment = new List <EquipmentOfferTrnDao> { new EquipmentOfferTrnDao { EquipmentOfferGuid = equipmentOfferGuid, OfferGuid = offerGuid, EquipmentOptions = new List <EquipmentOptionTrnDao> { new EquipmentOptionTrnDao { EquipmentOptionTrnGuid = Guid.NewGuid(), EquipmentOfferGuid = equipmentOfferGuid, Quantity = 23 } } } }, OfferReference = "Test offer", OpportunityGuid = Guid.NewGuid() }; var logger = new MockSimpleSaveLogger(); SimpleSaveExtensions.Logger = logger; try { using (IDbConnection connection = new SqlConnection()) { connection.Delete(offer); } } catch (InvalidOperationException) { // Don't care } var scripts = logger.Scripts; Assert.AreEqual(1, scripts.Count, "Unexpected number of scripts."); var sql = scripts[0].Buffer.ToString(); var deleteFromEquipmentOptionIndex = sql.IndexOf("DELETE FROM [opp].[EQUIPMENT_OPTION_TRN]"); Assert.IsTrue(deleteFromEquipmentOptionIndex >= 0, "No delete from [opp].[EQUIPMENT_OPTION_TRN]"); var deleteFromEquipmentOfferIndex = sql.IndexOf("DELETE FROM [opp].[EQUIPMENT_OFFER_TRN]"); Assert.IsTrue(deleteFromEquipmentOfferIndex >= 0, "No delete from [opp].[EQUIPMENT_OFFER_TRN]"); var deleteFromOfferIndex = sql.IndexOf("DELETE FROM [opp].[OFFER_TRN]"); Assert.IsTrue(deleteFromOfferIndex >= 0, "No delete from [opp].[OFFER_TRN]"); Assert.IsTrue( deleteFromEquipmentOptionIndex < deleteFromEquipmentOfferIndex, "Delete from [opp].[EQUIPMENT_OPTION_TRN] should appear before delete from [opp].[EQUIPMENT_OFFER_TRN]"); Assert.IsTrue( deleteFromEquipmentOfferIndex < deleteFromOfferIndex, "Delete from [opp].[EQUIPMENT_OFFER_TRN] should appear before delete from [opp].[OFFER_TRN]"); }
public void delete_offer_deletes_equipment_offer_descendents() { var offerGuid = Guid.NewGuid(); var equipmentOfferGuid = Guid.NewGuid(); var offer = new OfferDto { OfferGuid = offerGuid, Equipment = new List<EquipmentOfferTrnDao> { new EquipmentOfferTrnDao { EquipmentOfferGuid = equipmentOfferGuid, OfferGuid = offerGuid, EquipmentOptions = new List<EquipmentOptionTrnDao> { new EquipmentOptionTrnDao { EquipmentOptionTrnGuid = Guid.NewGuid(), EquipmentOfferGuid = equipmentOfferGuid, Quantity = 23 } } } }, OfferReference = "Test offer", OpportunityGuid = Guid.NewGuid() }; var logger = new MockSimpleSaveLogger(); SimpleSaveExtensions.Logger = logger; try { using (IDbConnection connection = new SqlConnection()) { connection.Delete(offer); } } catch (InvalidOperationException) { // Don't care } var scripts = logger.Scripts; Assert.AreEqual(1, scripts.Count, "Unexpected number of scripts."); var sql = scripts[0].Buffer.ToString(); var deleteFromEquipmentOptionIndex = sql.IndexOf("DELETE FROM [opp].[EQUIPMENT_OPTION_TRN]"); Assert.IsTrue(deleteFromEquipmentOptionIndex >= 0, "No delete from [opp].[EQUIPMENT_OPTION_TRN]"); var deleteFromEquipmentOfferIndex = sql.IndexOf("DELETE FROM [opp].[EQUIPMENT_OFFER_TRN]"); Assert.IsTrue(deleteFromEquipmentOfferIndex >= 0, "No delete from [opp].[EQUIPMENT_OFFER_TRN]"); var deleteFromOfferIndex = sql.IndexOf("DELETE FROM [opp].[OFFER_TRN]"); Assert.IsTrue(deleteFromOfferIndex >= 0, "No delete from [opp].[OFFER_TRN]"); Assert.IsTrue( deleteFromEquipmentOptionIndex < deleteFromEquipmentOfferIndex, "Delete from [opp].[EQUIPMENT_OPTION_TRN] should appear before delete from [opp].[EQUIPMENT_OFFER_TRN]"); Assert.IsTrue( deleteFromEquipmentOfferIndex < deleteFromOfferIndex, "Delete from [opp].[EQUIPMENT_OFFER_TRN] should appear before delete from [opp].[OFFER_TRN]"); }