Example #1
0
        public void FastUpdate()
        {
            using (var container = new RhetosTestContainer())
            {
                container.Resolve<ISqlExecuter>().ExecuteSql(new[] { "DELETE FROM TestHistory.Simple" });
                var repository = container.Resolve<Common.DomRepository>();
                var simpleRepos = repository.TestHistory.Simple;

                Assert.AreEqual("", DumpFull(simpleRepos.All()));

                var sw = Stopwatch.StartNew();
                var s1 = new TestHistory.Simple { Code = 1, Name = "a" };
                simpleRepos.Insert(new[] { s1 });
                Assert.AreEqual(1, simpleRepos.All().Single().Code);

                int lastCode = 1;
                var s1Array = new[] { s1 };
                const bool SlowTest = false;
                const int tests1 = 5 * (SlowTest ? 10 : 1);
                const int tests2 = 3 * (SlowTest ? 10 : 1);
                for (int i = 0; i < tests1; i++)
                {
                    for (int j = 0; j < tests2; j++)
                    {
                        s1.ActiveSince = null;
                        s1.Code = ++lastCode;
                        simpleRepos.Update(s1Array);
                    }
                    Assert.AreEqual(lastCode, simpleRepos.All().Single().Code);
                }
                sw.Stop();

                var h = repository.TestHistory.Simple_Changes.Query().OrderBy(item => item.ActiveSince).ToArray();
                Console.WriteLine(DumpFull(h));
                string msg = string.Format(
                    "Number of history records ({0}) is expected to be between the number of elapsed seconds ({1}) and the number of updates ({2}), depending on the DateTime precision.",
                    h.Count(), Math.Floor(sw.Elapsed.TotalSeconds), tests1 * tests2);
                Console.WriteLine(msg);
                Assert.IsTrue(Math.Floor(sw.Elapsed.TotalSeconds) <= h.Count() && h.Count() <= tests1 * tests2, msg);
            }
        }
Example #2
0
        public void UpdateWithoutSettingActiveSince()
        {
            using (var container = new RhetosTestContainer())
            {
                container.Resolve<ISqlExecuter>().ExecuteSql(new[] { "DELETE FROM TestHistory.Simple" });
                var repository = container.Resolve<Common.DomRepository>();

                var inPast = SqlUtility.GetDatabaseTime(container.Resolve<ISqlExecuter>()).AddMinutes(-1);
                var s = new TestHistory.Simple { ID = Guid.NewGuid(), Code = 1, ActiveSince = inPast };
                repository.TestHistory.Simple.Insert(new[] { s });

                s.ActiveSince = null;
                s.Name = "test";

                repository.TestHistory.Simple.Update(new[] { s });
            }
        }
Example #3
0
        public void CrudWithExplicitTime()
        {
            using (var container = new RhetosTestContainer())
            {
                container.Resolve<ISqlExecuter>().ExecuteSql(new[] { "DELETE FROM TestHistory.Simple" });
                var repository = container.Resolve<Common.DomRepository>();

                // Insert:
                var s = new TestHistory.Simple { ID = Guid.NewGuid(), Code = 1, ActiveSince = Day(1), Name = "a" };
                repository.TestHistory.Simple.Insert(new[] { s });
                Assert.AreEqual(1, repository.TestHistory.Simple_History.All().Count());

                container.Resolve<Common.ExecutionContext>().EntityFrameworkContext.ClearCache();
                Assert.AreEqual("1 a 2001-01-01T00:00:00", DumpFull(repository.TestHistory.Simple.All()));
                Assert.AreEqual("", DumpFull(repository.TestHistory.Simple_Changes.All()));

                // Update:
                s.Code = 2;
                s.Name = "b";
                s.ActiveSince = Day(2);
                repository.TestHistory.Simple.Update(new[] { s });

                container.Resolve<Common.ExecutionContext>().EntityFrameworkContext.ClearCache();
                Assert.AreEqual("2 b 2001-01-02T00:00:00", DumpFull(repository.TestHistory.Simple.All()));
                Assert.AreEqual("1 2001-01-01T00:00:00", DumpFull(repository.TestHistory.Simple_Changes.All()));
                Assert.AreEqual(2, repository.TestHistory.Simple_History.All().Count());

                // Another update:
                s.Code = 3;
                s.Name = "c";
                s.ActiveSince = Day(3);
                repository.TestHistory.Simple.Update(new[] { s });

                container.Resolve<Common.ExecutionContext>().EntityFrameworkContext.ClearCache();
                Assert.AreEqual("3 c 2001-01-03T00:00:00", DumpFull(repository.TestHistory.Simple.All()));
                Assert.AreEqual("1 2001-01-01T00:00:00, 2 2001-01-02T00:00:00", DumpFull(repository.TestHistory.Simple_Changes.All()));
                Assert.AreEqual(3, repository.TestHistory.Simple_History.All().Count());

                // Delete:
                repository.TestHistory.Simple.Delete(new[] { s });

                container.Resolve<Common.ExecutionContext>().EntityFrameworkContext.ClearCache();
                Assert.AreEqual("", DumpFull(repository.TestHistory.Simple.All()));
                Assert.AreEqual("", DumpFull(repository.TestHistory.Simple_Changes.All()));
            }
        }
Example #4
0
        public void UpdateFuture()
        {
            using (var container = new RhetosTestContainer())
            {
                container.Resolve<ISqlExecuter>().ExecuteSql(new[] { "DELETE FROM TestHistory.Simple" });
                var repository = container.Resolve<Common.DomRepository>();

                var s = new TestHistory.Simple { ID = Guid.NewGuid(), Code = 1 };
                repository.TestHistory.Simple.Insert(new[] { s });

                var future = SqlUtility.GetDatabaseTime(container.Resolve<ISqlExecuter>())
                    .Rounded().AddMinutes(1);
                s.ActiveSince = future;
                repository.TestHistory.Simple.Update(new[] { s });

                Assert.AreEqual("1  " + future.Dump(), DumpFull(repository.TestHistory.Simple.All()));
            }
        }
Example #5
0
        public void UpdateHistoryFuture()
        {
            using (var container = new RhetosTestContainer())
            {
                container.Resolve<ISqlExecuter>().ExecuteSql(new[] { "DELETE FROM TestHistory.Simple" });
                var repository = container.Resolve<Common.DomRepository>();

                var now = GetServerTime(container).Rounded();
                DateTime future1 = now.AddMinutes(1);
                DateTime future2 = future1.AddMinutes(1);
                DateTime future3 = future2.AddMinutes(1);

                TestUtility.Dump(new[] { future1, future2, future3 }, item => item.ToString("o"));

                // The last record (whether in future or not) should be regarded as "current record" by the History concept.
                var lastRecordInFuture = new TestHistory.Simple { ID = Guid.NewGuid(), Code = 1, ActiveSince = future3 };
                repository.TestHistory.Simple.Insert(new[] { lastRecordInFuture });

                var historyRecord = new TestHistory.Simple_Changes { EntityID = lastRecordInFuture.ID, Code = 2, ActiveSince = future1 };
                repository.TestHistory.Simple_Changes.Insert(new[] { historyRecord });

                historyRecord.ActiveSince = future2;
                repository.TestHistory.Simple_Changes.Update(new[] { historyRecord });

                var currentRecord = repository.TestHistory.Simple.All().Single();
                Console.WriteLine("currentRecord.ActiveSince: " + currentRecord.ActiveSince.Value.ToString("o"));
                Assert.AreEqual("1  " + future3.Dump(), DumpFull(new[] { currentRecord }));
            }
        }
Example #6
0
        public void InsertHistoryFuture()
        {
            using (var container = new RhetosTestContainer())
            {
                container.Resolve<ISqlExecuter>().ExecuteSql(new[] { "DELETE FROM TestHistory.Simple" });
                var repository = container.Resolve<Common.DomRepository>();

                var future1 = SqlUtility.GetDatabaseTime(container.Resolve<ISqlExecuter>())
                    .Rounded().AddMinutes(1);
                var future2 = future1.AddMinutes(1);

                var e = new TestHistory.Simple { Code = 1, ActiveSince = future2 };
                repository.TestHistory.Simple.Insert(new[] { e });

                repository.TestHistory.Simple_Changes.Insert(new[] { new TestHistory.Simple_Changes { EntityID = e.ID, ActiveSince = future1 } });
                Assert.AreEqual("1  " + future2.Dump(), DumpFull(repository.TestHistory.Simple.All()));
            }
        }
Example #7
0
        public void PartialHistoryAtTime()
        {
            using (var container = new RhetosTestContainer())
            {
                container.Resolve<ISqlExecuter>().ExecuteSql(new[] { "DELETE FROM TestHistory.Simple" });
                var repository = container.Resolve<Common.DomRepository>();
                var er = repository.TestHistory.Simple;
                var hr = repository.TestHistory.Simple_Changes;

                var e = new TestHistory.Simple { Code = 1, Name = "a" };
                er.Insert(new[] { e });
                DateTime t1 = GetServerTime(container);
                Assert.AreEqual("1 a", Dump(er.All()));

                System.Threading.Thread.Sleep(DatabaseDateTimeImprecision + DatabaseDateTimeImprecision);

                e.Code = 2;
                e.Name = "b";
                e.ActiveSince = null;
                er.Update(new[] { e });
                DateTime t2 = GetServerTime(container);
                Assert.AreEqual("2 b", Dump(er.All()));

                System.Threading.Thread.Sleep(DatabaseDateTimeImprecision + DatabaseDateTimeImprecision);

                e.Code = 3;
                e.Name = "c";
                e.ActiveSince = null;
                er.Update(new[] { e });
                DateTime t3 = GetServerTime(container);
                Assert.AreEqual("3 c", Dump(er.All()));

                Console.WriteLine("t1: " + t1.ToString("o"));
                Console.WriteLine("t2: " + t2.ToString("o"));
                Console.WriteLine("t3: " + t3.ToString("o"));

                container.Resolve<Common.ExecutionContext>().EntityFrameworkContext.ClearCache();
                Assert.AreEqual("1", Dump(hr.Filter(t1.Add(DatabaseDateTimeImprecision))), "At time 1");
                container.Resolve<Common.ExecutionContext>().EntityFrameworkContext.ClearCache();
                Assert.AreEqual("2", Dump(hr.Filter(t2.Add(DatabaseDateTimeImprecision))), "At time 2");
                container.Resolve<Common.ExecutionContext>().EntityFrameworkContext.ClearCache();
                Assert.AreEqual("3", Dump(hr.Filter(t3.Add(DatabaseDateTimeImprecision))), "At time 3");
            }
        }
Example #8
0
        public void CrudWithExplicitTime()
        {
            using (var executionContext = new CommonTestExecutionContext())
            {
                executionContext.SqlExecuter.ExecuteSql(new[] { "DELETE FROM TestHistory.Simple" });
                var repository = new Common.DomRepository(executionContext);

                // Insert:
                var s = new TestHistory.Simple { ID = Guid.NewGuid(), Code = 1, ActiveSince = Day(1), Name = "a" };
                repository.TestHistory.Simple.Insert(new[] { s });
                Assert.AreEqual(1, repository.TestHistory.Simple_History.All().Count());

                executionContext.NHibernateSession.Clear();
                Assert.AreEqual("1 a 2001-01-01T00:00:00", DumpFull(repository.TestHistory.Simple.All()));
                Assert.AreEqual("", DumpFull(repository.TestHistory.Simple_Changes.All()));

                // Update:
                s.Code = 2;
                s.Name = "b";
                s.ActiveSince = Day(2);
                repository.TestHistory.Simple.Update(new[] { s });

                executionContext.NHibernateSession.Clear();
                Assert.AreEqual("2 b 2001-01-02T00:00:00", DumpFull(repository.TestHistory.Simple.All()));
                Assert.AreEqual("1 2001-01-01T00:00:00", DumpFull(repository.TestHistory.Simple_Changes.All()));
                Assert.AreEqual(2, repository.TestHistory.Simple_History.All().Count());

                // Another update:
                s.Code = 3;
                s.Name = "c";
                s.ActiveSince = Day(3);
                repository.TestHistory.Simple.Update(new[] { s });

                executionContext.NHibernateSession.Clear();
                Assert.AreEqual("3 c 2001-01-03T00:00:00", DumpFull(repository.TestHistory.Simple.All()));
                Assert.AreEqual("1 2001-01-01T00:00:00, 2 2001-01-02T00:00:00", DumpFull(repository.TestHistory.Simple_Changes.All()));
                Assert.AreEqual(3, repository.TestHistory.Simple_History.All().Count());

                // Delete:
                repository.TestHistory.Simple.Delete(new[] { s });

                executionContext.NHibernateSession.Clear();
                Assert.AreEqual("", DumpFull(repository.TestHistory.Simple.All()));
                Assert.AreEqual("", DumpFull(repository.TestHistory.Simple_Changes.All()));
            }
        }
Example #9
0
        public void UpdateWithoutSettingActiveSince()
        {
            using (var executionContext = new CommonTestExecutionContext())
            {
                executionContext.SqlExecuter.ExecuteSql(new[] { "DELETE FROM TestHistory.Simple" });
                var repository = new Common.DomRepository(executionContext);

                var inPast = SqlUtility.GetDatabaseTime(executionContext.SqlExecuter).AddMinutes(-1);
                var s = new TestHistory.Simple { ID = Guid.NewGuid(), Code = 1, ActiveSince = inPast };
                repository.TestHistory.Simple.Insert(new[] { s });

                s.ActiveSince = null;
                s.Name = "test";

                repository.TestHistory.Simple.Update(new[] { s });
            }
        }
Example #10
0
        public void UpdateHistoryFuture()
        {
            using (var executionContext = new CommonTestExecutionContext())
            {
                executionContext.SqlExecuter.ExecuteSql(new[] { "DELETE FROM TestHistory.Simple" });
                var repository = new Common.DomRepository(executionContext);

                var s = new TestHistory.Simple { ID = Guid.NewGuid(), Code = 1, ActiveSince = Day(2) };
                repository.TestHistory.Simple.Insert(new[] { s });

                var h = new TestHistory.Simple_Changes { EntityID = s.ID, Code = 2, ActiveSince = Day(1) };
                repository.TestHistory.Simple_Changes.Insert(new[] { h });

                var future = SqlUtility.GetDatabaseTime(executionContext.SqlExecuter).AddMinutes(1);
                h.ActiveSince = future;

                TestUtility.ShouldFail(
                    () => repository.TestHistory.Simple_Changes.Update(new[] { h }),
                    "ActiveSince", "TestHistory.Simple_Changes", "future");
            }
        }
Example #11
0
        public void PartialHistoryAtTime()
        {
            using (var executionContext = new CommonTestExecutionContext())
            {
                executionContext.SqlExecuter.ExecuteSql(new[] { "DELETE FROM TestHistory.Simple" });
                var repository = new Common.DomRepository(executionContext);
                var er = repository.TestHistory.Simple;
                var hr = repository.TestHistory.Simple_Changes;

                var e = new TestHistory.Simple { Code = 1, Name = "a" };
                er.Insert(new[] { e });
                DateTime t1 = GetServerTime(executionContext);
                Assert.AreEqual("1 a", Dump(er.All()));

                System.Threading.Thread.Sleep(DatabaseDateTimeImprecision + DatabaseDateTimeImprecision);

                e.Code = 2;
                e.Name = "b";
                e.ActiveSince = null;
                er.Update(new[] { e });
                DateTime t2 = GetServerTime(executionContext);
                Assert.AreEqual("2 b", Dump(er.All()));

                System.Threading.Thread.Sleep(DatabaseDateTimeImprecision + DatabaseDateTimeImprecision);

                e.Code = 3;
                e.Name = "c";
                e.ActiveSince = null;
                er.Update(new[] { e });
                DateTime t3 = GetServerTime(executionContext);
                Assert.AreEqual("3 c", Dump(er.All()));

                Console.WriteLine("t1: " + t1.ToString("o"));
                Console.WriteLine("t2: " + t2.ToString("o"));
                Console.WriteLine("t3: " + t3.ToString("o"));

                executionContext.NHibernateSession.Clear();
                Assert.AreEqual("1", Dump(hr.Filter(t1.Add(DatabaseDateTimeImprecision))), "At time 1");
                executionContext.NHibernateSession.Clear();
                Assert.AreEqual("2", Dump(hr.Filter(t2.Add(DatabaseDateTimeImprecision))), "At time 2");
                executionContext.NHibernateSession.Clear();
                Assert.AreEqual("3", Dump(hr.Filter(t3.Add(DatabaseDateTimeImprecision))), "At time 3");
            }
        }
Example #12
0
        public void InsertHistoryFuture()
        {
            using (var executionContext = new CommonTestExecutionContext())
            {
                executionContext.SqlExecuter.ExecuteSql(new[] { "DELETE FROM TestHistory.Simple" });
                var repository = new Common.DomRepository(executionContext);

                var e = new TestHistory.Simple { Code = 1 };
                repository.TestHistory.Simple.Insert(new[] { e });

                var future = SqlUtility.GetDatabaseTime(executionContext.SqlExecuter).AddMinutes(1);

                TestUtility.ShouldFail(
                    () => repository.TestHistory.Simple_Changes.Insert(new[] { new TestHistory.Simple_Changes { Entity = e, ActiveSince = future } }),
                    "ActiveSince", "TestHistory.Simple_Changes", "future");
            }
        }