Exemplo n.º 1
0
        public void Does_call_Filters_on_Save()
        {
            var insertDate = new DateTime(2014, 1, 1);
            var updateDate = new DateTime(2015, 1, 1);

            OrmLiteConfig.InsertFilter = (dbCmd, row) =>
            {
                var auditRow = row as IAudit;
                if (auditRow != null)
                {
                    auditRow.CreatedDate = auditRow.ModifiedDate = insertDate;
                }
            };

            OrmLiteConfig.UpdateFilter = (dbCmd, row) =>
            {
                var auditRow = row as IAudit;
                if (auditRow != null)
                {
                    auditRow.ModifiedDate = updateDate;
                }
            };

            using (var db = OpenDbConnection())
            {
                db.DropAndCreateTable <AuditTableA>();
                db.DropAndCreateTable <AuditTableB>();

                var a = new AuditTableA();
                var b = new AuditTableB();
                db.Save(a);
                db.Save(b);

                var insertRowA = db.SingleById <AuditTableA>(a.Id);
                var insertRowB = db.SingleById <AuditTableB>(b.Id);

                Assert.That(insertRowA.CreatedDate, Is.EqualTo(insertDate));
                Assert.That(insertRowA.ModifiedDate, Is.EqualTo(insertDate));

                Assert.That(insertRowB.CreatedDate, Is.EqualTo(insertDate));
                Assert.That(insertRowB.ModifiedDate, Is.EqualTo(insertDate));

                a.ModifiedBy = "Updated";
                db.Save(a);

                a = db.SingleById <AuditTableA>(a.Id);
                Assert.That(a.ModifiedDate, Is.EqualTo(updateDate));
            }

            OrmLiteConfig.InsertFilter = OrmLiteConfig.UpdateFilter = null;
        }
Exemplo n.º 2
0
        public void Exceptions_in_filters_prevents_insert_and_update()
        {
            OrmLiteConfig.InsertFilter = OrmLiteConfig.UpdateFilter = (dbCmd, row) =>
            {
                var auditRow = row as IAudit;
                if (auditRow != null)
                {
                    if (auditRow.ModifiedBy == null)
                    {
                        throw new ArgumentNullException("ModifiedBy");
                    }
                }
            };

            using (var db = OpenDbConnection())
            {
                db.DropAndCreateTable <AuditTableA>();
                db.DropAndCreateTable <AuditTableB>();

                try
                {
                    db.Insert(new AuditTableA());
                    Assert.Fail("Should throw");
                }
                catch (ArgumentNullException) { }
                Assert.That(db.Count <AuditTableA>(), Is.EqualTo(0));

                var a = new AuditTableA {
                    ModifiedBy = "Me!"
                };
                db.Insert(a);

                a.ModifiedBy = null;
                try
                {
                    db.Update(a);
                    Assert.Fail("Should throw");
                }
                catch (ArgumentNullException) { }

                a.ModifiedBy = "Me2!";
                db.Update(a);
            }

            OrmLiteConfig.InsertFilter = OrmLiteConfig.UpdateFilter = null;
        }
Exemplo n.º 3
0
        public void Does_call_UpdateFilter_on_anonymous_Type()
        {
            var called = false;

            OrmLiteConfig.UpdateFilter = (dbCmd, row) => { called = true; };

            using (var db = OpenDbConnection())
            {
                db.DropAndCreateTable <AuditTableA>();

                var a  = new AuditTableA();
                var id = db.Insert(a, selectIdentity: true);

                db.Update <AuditTableA>(new { ModifiedBy = "Updated" }, where : x => x.Id == id);

                Assert.That(db.SingleById <AuditTableA>(id).ModifiedBy, Is.EqualTo("Updated"));

                Assert.That(called);
            }

            OrmLiteConfig.UpdateFilter = null;
        }
        public void Does_call_Filters_on_Save()
        {
            var insertDate = new DateTime(2014, 1, 1);
            var updateDate = new DateTime(2015, 1, 1);

            OrmLiteConfig.InsertFilter = (dbCmd, row) =>
            {
                var auditRow = row as IAudit;
                if (auditRow != null)
                {
                    auditRow.CreatedDate = auditRow.ModifiedDate = insertDate;
                }
            };

            OrmLiteConfig.UpdateFilter = (dbCmd, row) =>
            {
                var auditRow = row as IAudit;
                if (auditRow != null)
                {
                    auditRow.ModifiedDate = updateDate;
                }
            };

            using (var db = OpenDbConnection())
            {
                db.DropAndCreateTable<AuditTableA>();
                db.DropAndCreateTable<AuditTableB>();

                var a = new AuditTableA();
                var b = new AuditTableB();
                db.Save(a);
                db.Save(b);

                var insertRowA = db.SingleById<AuditTableA>(a.Id);
                var insertRowB = db.SingleById<AuditTableB>(b.Id);

                Assert.That(insertRowA.CreatedDate, Is.EqualTo(insertDate));
                Assert.That(insertRowA.ModifiedDate, Is.EqualTo(insertDate));

                Assert.That(insertRowB.CreatedDate, Is.EqualTo(insertDate));
                Assert.That(insertRowB.ModifiedDate, Is.EqualTo(insertDate));

                a.ModifiedBy = "Updated";
                db.Save(a);

                a = db.SingleById<AuditTableA>(a.Id);
                Assert.That(a.ModifiedDate, Is.EqualTo(updateDate));
            }

            OrmLiteConfig.InsertFilter = OrmLiteConfig.UpdateFilter = null;
        }
        public void Exceptions_in_filters_prevents_insert_and_update()
        {
            OrmLiteConfig.InsertFilter = OrmLiteConfig.UpdateFilter = (dbCmd, row) =>
            {
                var auditRow = row as IAudit;
                if (auditRow != null)
                {
                    if (auditRow.ModifiedBy == null)
                        throw new ArgumentNullException("ModifiedBy");
                }
            };

            using (var db = OpenDbConnection())
            {
                db.DropAndCreateTable<AuditTableA>();
                db.DropAndCreateTable<AuditTableB>();

                try
                {
                    db.Insert(new AuditTableA());
                    Assert.Fail("Should throw");
                }
                catch (ArgumentNullException) { }
                Assert.That(db.Count<AuditTableA>(), Is.EqualTo(0));

                var a = new AuditTableA { ModifiedBy = "Me!" };
                db.Insert(a);

                a.ModifiedBy = null;
                try
                {
                    db.Update(a);
                    Assert.Fail("Should throw");
                }
                catch (ArgumentNullException) { }

                a.ModifiedBy = "Me2!";
                db.Update(a);
            }

            OrmLiteConfig.InsertFilter = OrmLiteConfig.UpdateFilter = null;
        }