public void insert_with_fk_on_existing_special_child_inserts_parent_and_not_child()
        {
            var logger = CreateMockLogger();

            var newDto = new ParentDto()
            {
                OneToOneSpecialChildDtoWithFk = new OneToOneSpecialChildDtoWithFk
                {
                    ChildKey = 100,
                    Name = "You, sir, are drunk!"
                }
            };

            try
            {
                using (var connection = new SqlConnection())
                {
                    connection.Create(newDto);
                }
            }
            catch (InvalidOperationException)
            {
                // Do nothing because we deliberately didn't open the connection
            }

            var scripts = logger.Scripts;
            Assert.AreEqual(2, scripts.Count, "Unexpected number of scripts.");

            var sql = scripts[0].Buffer.ToString();
            Assert.IsTrue(sql.Contains("INSERT INTO dbo.[Parent]"), "No INSERT on parent.");

            sql = scripts[1].Buffer.ToString();
            Assert.IsTrue(sql.Contains("UPDATE dbo.OneToOneSpecialChildWithFk"), "Should be an UPDATE on child.");
        }
        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, "p7");
            Assert.AreEqual(
                SamplePopulatedDtos.Contact.MainPhone.PhoneGuid,
                guid,
                string.Format("Invalid main phone GUID: {0}", guid));

            guid = ExtractGuid(script, "p8");
            Assert.AreEqual(
                SamplePopulatedDtos.Contact.MobilePhone.PhoneGuid,
                guid,
                string.Format("Invalid main phone GUID: {0}", guid));
        }
        public void user_assigned_primary_key_is_included_in_insert()
        {
            var dto = new AuthorisationFee
            {
                FieldItemKey = 4096,
                Value = 1000,
                Revenue = 100,
                Cost = 1500,
                CalculatorVersionKey = 23,
                Name = "Dr Doolittle"
            };

            var logger = CreateMockLogger();
            try
            {
                using (var connection = new SqlConnection())
                {
                    connection.Create(dto);
                }
            }
            catch (InvalidOperationException ioe)
            {
                Assert.IsTrue(ioe.Message.Contains(
                    "Invalid operation. The connection is closed."),
                    string.Format("Right type of exception, wrong message: {0}\r\n{1}", ioe.Message, ioe.StackTrace));
            }

            var scripts = logger.Scripts;
            Assert.AreEqual(1, scripts.Count, "Unexpected number of scripts.");

            var sql = scripts[0].Buffer.ToString();
            Assert.IsTrue(sql.Contains("FieldItemKey"), "Should insert into PK column.");
            Assert.IsFalse(sql.Contains("SELECT SCOPE_IDENTITY();"), "Should not return PK value.");
        }
        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]"));
        }
 public void throws_on_insert_with_duplicate_property_mappings_by_default()
 {
     var newObject = new TableB() { Name = "Captain Jack Sparrow" };
     using (IDbConnection connection = new SqlConnection())
     {
         connection.Create(newObject);
     }
 }
        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");
        }