コード例 #1
0
        protected MockSimpleSaveLogger CreateMockLogger()
        {
            var logger = new MockSimpleSaveLogger();

            SimpleSaveExtensions.Logger = logger;
            return(logger);
        }
        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 collection_insert_generates_insert_for_all()
        {
            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(0, Regex.Matches(sql, "UPDATE").Count, "Should be no UPDATEs.");
                Assert.AreEqual(0, Regex.Matches(sql, "DELETE").Count, "Should be no DELETEs.");
                Assert.AreEqual(1, Regex.Matches(sql, "SELECT").Count, "Should be one SELECT to return inserted IDENTITY value.");
            }
        }
        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.");
            }
        }