Exemplo n.º 1
0
        public void DateTime2ConventionStoresDateTimeWithMilliseconds()
        {
            this.fluentMapperConfigurer.RegisterConvention <DateTime2Convention>();

            var dateTime = new DateTime(2012, 12, 19, 21, 56, 7, 456);

            using (var sessionFactory = this.configuration.CreateSessionFactory())
            {
                using (var unitOfWorkFactory = new SQLiteUnitOfWorkFactory(this.configuration, sessionFactory, null, null, null, null, new ConsoleLogger()))
                {
                    long fooId;

                    using (var unitOfWork = unitOfWorkFactory.Create())
                    {
                        var foo = new Foo {
                            DateTimeValue = dateTime
                        };

                        unitOfWork.Add(foo);
                        unitOfWork.Save();

                        fooId = foo.ID;
                    }

                    using (var unitOfWork = unitOfWorkFactory.Create())
                    {
                        var foo = unitOfWork.Query <Foo>().Single(c => c.ID == fooId);

                        Assert.AreEqual(dateTime, foo.DateTimeValue);
                    }
                }
            }
        }
Exemplo n.º 2
0
        public void IfExtensionMethodCanBeUsedWithReferenceProperty()
        {
            using (var sessionFactory = this.configuration.CreateSessionFactory())
            {
                using (var unitOfWorkFactory = new SQLiteUnitOfWorkFactory(this.configuration, sessionFactory, null, null, null, null, new ConsoleLogger()))
                {
                    unitOfWorkFactory.ExecuteSql(string.Format("insert into Foo (Guid, IntEnum, IntValue) values ('{0}', 0, null)", Guid.NewGuid()));
                    unitOfWorkFactory.ExecuteSql(string.Format("insert into Foo (Guid, IntEnum, IntValue) values ('{0}', 0, null)", Guid.NewGuid()));

                    unitOfWorkFactory.ExecuteSql(string.Format("insert into BaseClass (Guid, IntEnum, FooID) values ('{0}', 0, null)", Guid.NewGuid()));
                    unitOfWorkFactory.ExecuteSql(string.Format("insert into BaseClass (Guid, IntEnum, FooID) values ('{0}', 0, 1)", Guid.NewGuid()));
                    unitOfWorkFactory.ExecuteSql(string.Format("insert into BaseClass (Guid, IntEnum, FooID) values ('{0}', 0, 2)", Guid.NewGuid()));
                    unitOfWorkFactory.ExecuteSql(string.Format("insert into BaseClass (Guid, IntEnum, FooID) values ('{0}', 1, 2)", Guid.NewGuid()));

                    using (var unitOfWork = unitOfWorkFactory.Create())
                    {
                        var x =
                            unitOfWork.Query <BaseClass>()
                            .GroupBy(c => c.IntEnum)
                            .Select(c => new Tuple <IntEnum, int, int>(c.Key, c.Count(), c.Sum(d => Extensions.If(d.Foo == null, 0, 1))))
                            .ToList();

                        Assert.AreEqual(2, x.Count);
                        Assert.AreEqual(IntEnum.Zero, x[0].Item1);
                        Assert.AreEqual(3, x[0].Item2);
                        Assert.AreEqual(2, x[0].Item3);
                        Assert.AreEqual(IntEnum.One, x[1].Item1);
                        Assert.AreEqual(1, x[1].Item2);
                        Assert.AreEqual(1, x[1].Item3);
                    }
                }
            }
        }
Exemplo n.º 3
0
        public void InsertingDuplicateBarNameInDifferentFactoryDoesNotThrowException()
        {
            using (var unitOfWorkFactory = new SQLiteUnitOfWorkFactory(this.configuration, this.sessionFactory, null, null, null, null, new ConsoleLogger {
                DebugLoggingIsEnabled = false
            }))
            {
                using (var unitOfWork = unitOfWorkFactory.Create())
                {
                    unitOfWork.Add(new Bar {
                        Name = "myName"
                    });
                    unitOfWork.Save();
                }
            }

            using (var unitOfWorkFactory = new SQLiteUnitOfWorkFactory(this.configuration, this.sessionFactory, null, null, null, null, new ConsoleLogger {
                DebugLoggingIsEnabled = false
            }))
            {
                using (var unitOfWork = unitOfWorkFactory.Create())
                {
                    Assert.That(() => unitOfWork.Add(new Bar {
                        Name = "myName"
                    }), Throws.Nothing);
                }
            }
        }
Exemplo n.º 4
0
        public void ExceptionThrownWhenTryingToPersistEntityNotAutoMapped()
        {
            this.automappingConfiguration.ShouldMapType(c => false);
            this.fluentMapperConfigurer.AutoMapEntitiesFromAssemblyOf <Foo>();

            using (var sessionFactory = this.configuration.CreateSessionFactory())
            {
                using (var unitOfWorkFactory = new SQLiteUnitOfWorkFactory(this.configuration, sessionFactory, null, null, null, null, new ConsoleLogger()))
                {
                    PersistEntity(unitOfWorkFactory, () => new Foo {
                        StringValue = "myValue"
                    });
                }
            }
        }
Exemplo n.º 5
0
        public void SimpleEntityIsPersisted()
        {
            this.automappingConfiguration.ShouldMapType(c => c == typeof(Foo));
            this.fluentMapperConfigurer.AutoMapEntitiesFromAssemblyOf <Foo>();

            using (var sessionFactory = this.configuration.CreateSessionFactory())
            {
                using (var unitOfWorkFactory = new SQLiteUnitOfWorkFactory(this.configuration, sessionFactory, null, null, null, null, new ConsoleLogger()))
                {
                    var entity = PersistEntity(unitOfWorkFactory, () => new Foo {
                        StringValue = "myValue"
                    });

                    Assert.AreEqual(1, entity.ID);
                }
            }
        }
Exemplo n.º 6
0
        public void XElementPropertyIsPersisted()
        {
            this.automappingConfiguration.ShouldMapType(c => c == typeof(XmlProperty));
            this.fluentMapperConfigurer.AutoMapEntitiesFromAssemblyOf <XmlProperty>();

            this.fluentMapperConfigurer.RegisterConvention <XElementConvention>();

            using (var sessionFactory = this.configuration.CreateSessionFactory())
            {
                using (var unitOfWorkFactory = new SQLiteUnitOfWorkFactory(this.configuration, sessionFactory, null, null, null, null, new ConsoleLogger()))
                {
                    PersistEntity(unitOfWorkFactory, () => new XmlProperty {
                        Xml = XElement.Parse("<xml/>")
                    });
                }
            }
        }
Exemplo n.º 7
0
        public void GuidKeyEntityIsPersisted()
        {
            this.automappingConfiguration.ShouldMapType(c => c == typeof(GuidParent));
            this.fluentMapperConfigurer.AutoMapEntitiesFromAssemblyOf <GuidParent>();
            this.fluentMapperConfigurer.RegisterOverride <GuidParent>(c => c.Id(p => p.Guid));

            using (var sessionFactory = this.configuration.CreateSessionFactory())
            {
                using (var unitOfWorkFactory = new SQLiteUnitOfWorkFactory(this.configuration, sessionFactory, null, null, null, null, new ConsoleLogger()))
                {
                    var parent = PersistEntity(unitOfWorkFactory, () => new GuidParent {
                        Name = "myName"
                    });

                    Assert.IsTrue(parent.Guid != Guid.Empty);
                }
            }
        }
Exemplo n.º 8
0
        public void InsertingDuplicateBarNameInSameUnitOfWorkThrowsException()
        {
            using (var unitOfWorkFactory = new SQLiteUnitOfWorkFactory(this.configuration, this.sessionFactory, null, null, null, null, new ConsoleLogger {
                DebugLoggingIsEnabled = false
            }))
            {
                using (var unitOfWork = unitOfWorkFactory.Create())
                {
                    unitOfWork.Add(new Bar {
                        Name = "myName"
                    });

                    Assert.That(() => unitOfWork.Add(new Bar {
                        Name = "myName"
                    }), Throws.InstanceOf <GenericADOException>());
                }
            }
        }
Exemplo n.º 9
0
        public void OptimisticLockingEntityCanBeReloaded()
        {
            this.automappingConfiguration.ShouldMapType(c => c == typeof(Optimistic));
            this.fluentMapperConfigurer.AutoMapEntitiesFromAssemblyOf <Optimistic>();

            using (var sessionFactory = this.configuration.CreateSessionFactory())
            {
                using (var unitOfWorkFactory = new SQLiteUnitOfWorkFactory(this.configuration, sessionFactory, null, null, null, null, new ConsoleLogger()))
                {
                    var persistedEntity = PersistEntity(unitOfWorkFactory, () => new Optimistic {
                        StringValue = "myValue"
                    });

                    var entity = LoadEntity <Optimistic>(unitOfWorkFactory, c => c.ID == persistedEntity.ID);

                    Assert.AreEqual(persistedEntity.StringValue, entity.StringValue);
                }
            }
        }
Exemplo n.º 10
0
        public void ExceptionThrownWhenTryingToPersistOptimisticLockingEntityWithIncorrectAutoMappingConfiguration()
        {
            var myAutomappingConfiguration = new AtlasAutoMappingConfiguration();

            myAutomappingConfiguration.ShouldMapType(c => c == typeof(Optimistic));

            this.fluentMapperConfigurer.AutoMappingConfiguration(myAutomappingConfiguration);
            this.fluentMapperConfigurer.AutoMapEntitiesFromAssemblyOf <Optimistic>();

            using (var sessionFactory = this.configuration.CreateSessionFactory())
            {
                using (var unitOfWorkFactory = new SQLiteUnitOfWorkFactory(this.configuration, sessionFactory, null, null, null, null, new ConsoleLogger()))
                {
                    PersistEntity(unitOfWorkFactory, () => new Optimistic {
                        StringValue = "myValue"
                    });
                }
            }
        }
Exemplo n.º 11
0
        public void ExceptionThrownIfIgnoreVersionConventionNotRegistered()
        {
            using (var sessionFactory = this.configuration.CreateSessionFactory())
            {
                using (var unitOfWorkFactory = new SQLiteUnitOfWorkFactory(this.configuration, sessionFactory, null, null, null, null, new ConsoleLogger()))
                {
                    using (var unitOfWork = unitOfWorkFactory.Create())
                    {
                        var safeUnitOfWork = unitOfWork;

                        var optimistic = new Optimistic {
                            StringValue = "test"
                        };

                        Assert.That(() => safeUnitOfWork.Add(optimistic), Throws.InstanceOf <PropertyValueException>());
                    }
                }
            }
        }
Exemplo n.º 12
0
        public void OptimisticLockingEntityCanBeAttachedAndChanged()
        {
            this.automappingConfiguration.ShouldMapType(c => c == typeof(Optimistic));
            this.fluentMapperConfigurer.AutoMapEntitiesFromAssemblyOf <Optimistic>();

            using (var sessionFactory = this.configuration.CreateSessionFactory())
            {
                using (var unitOfWorkFactory = new SQLiteUnitOfWorkFactory(this.configuration, sessionFactory, null, null, null, null, new ConsoleLogger()))
                {
                    var persistedEntity = PersistEntity(unitOfWorkFactory, () => new Optimistic {
                        StringValue = "myValue"
                    });

                    using (var unitOfWork = unitOfWorkFactory.Create())
                    {
                        unitOfWork.Attach(persistedEntity);

                        persistedEntity.StringValue = "newStringValue";
                        unitOfWork.Save();
                    }
                }
            }
        }