public void Test()
        {
            var spec    = entitySpec.Begin().Add("*");
            var testObj = new WithDictionary
            {
                AnnoyingThing = new AnnoyingThing
                {
                    Dic = new Dictionary <string, object>
                    {
                        { "nisse", "kalle" },
                        { "sture", null },
                        { "ulrik", 7 }
                    }
                }
            };

            SqlTestHelpers.WithNewDb("Gurka", conn =>
            {
                var tm          = new SqlTableManager(SqlTestHelpers.ConnectionString("Gurka"));
                var dataExtract = new DataExtract <WithDictionary>(spec, tm);
                dataExtract.Run(testObj);
                var result = SqlTestHelpers.SimpleQuery(conn, "SELECT * FROM WithDictionary");
                Assert.AreEqual(1, result.NameAndTypes.Length);
                result = SqlTestHelpers.SimpleQuery(conn, "SELECT * FROM WithDictionary_AnnoyingThingDic");
                Assert.AreEqual(2, result.NameAndTypes.Length);
            });
        }
Beispiel #2
0
        public void SetUp()
        {
            if (SqlTestHelpers.ConnectionString(null) == null)
            {
                return;
            }
            SqlTestHelpers.WithNewDb("LogTest", conn =>
            {
                var tableFactory = new SqlTableManager(SqlTestHelpers.ConnectionString("LogTest"), "log");
                var export       = new DataExtract <School>(Spec, tableFactory);
                export.Run(new[] { School });

                tableFactory = new SqlTableManager(SqlTestHelpers.ConnectionString("LogTest"), "log");
                tableFactory.WriteLog("Explicit logging from the outside A");
                export = new DataExtract <School>(Spec, tableFactory);
                export.Run(new[] { School });
                tableFactory.WriteLog("Explicit logging from the outside B");

                tableFactory = new SqlTableManager(SqlTestHelpers.ConnectionString("LogTest"), "log");
                tableFactory.WriteLog("Explicit logging from the outside C");

                _logRows   = SqlTestHelpers.SimpleQuery(conn, "SELECT * FROM log").Rows.Select(_ => new logEntry(_)).ToList();
                var lookup = _logRows.ToLookup(_ => _.SessionId, _ => _);
                _log1      = lookup.FirstOrDefault(_ => _.Count() == 3)?.ToList();
                _log2      = lookup.FirstOrDefault(_ => _.Count() == 6)?.ToList();
                _log3      = lookup.FirstOrDefault(_ => _.Count() == 1)?.ToList();
                _log1?.Sort((x, y) => x.SeqId.CompareTo(y.SeqId));
                _log2?.Sort((x, y) => x.SeqId.CompareTo(y.SeqId));
                _log3?.Sort((x, y) => x.SeqId.CompareTo(y.SeqId));
            });
        }
Beispiel #3
0
 public void ThenThereAreTimeStamps()
 {
     if (SqlTestHelpers.ConnectionString(null) == null)
     {
         return;
     }
     Assert.IsTrue(_logRows.TrueForAll(_ => Math.Abs((DateTime.Now - _.When).TotalSeconds) < 15));
 }
Beispiel #4
0
 public void ThenTheCorrectNumberOfClassesAreInTheTable()
 {
     if (SqlTestHelpers.ConnectionString(null) == null)
     {
         return;
     }
     Assert.AreEqual(NumberOfSchools * 6, _classQueryResult.Rows.Count);
 }
Beispiel #5
0
 public void ThenAllClassTableColumnNamesAreCorrect()
 {
     if (SqlTestHelpers.ConnectionString(null) == null)
     {
         return;
     }
     CollectionAssert.AreEqual(new[] { "_id_", "School_id_", "Name" }, _classQueryResult.NameAndTypes.Select(_ => _.Name));
 }
Beispiel #6
0
 public void ThenTheVerificationLogEntryIsWritten()
 {
     if (SqlTestHelpers.ConnectionString(null) == null)
     {
         return;
     }
     Assert.AreEqual("Verified table row counts: School_Classes_Students:10000;School_Classes:600;School:100", _logQueryResult.Rows.Last().Last());
 }
Beispiel #7
0
 public void ThenTheCorrectNumberOfLogEntriesAreInTable()
 {
     if (SqlTestHelpers.ConnectionString(null) == null)
     {
         return;
     }
     Assert.AreEqual(3, _logQueryResult.Rows.Count);
 }
Beispiel #8
0
 public void ThenTheCorrectNumberOfStudentsAreInTheTable()
 {
     if (SqlTestHelpers.ConnectionString(null) == null)
     {
         return;
     }
     Assert.AreEqual(NumberOfSchools * 100, _studentCount);
 }
Beispiel #9
0
 public void ThenAllStudentTableColumnNamesAreCorrect()
 {
     if (SqlTestHelpers.ConnectionString(null) == null)
     {
         return;
     }
     CollectionAssert.AreEqual(new[] { "School_Classes_id_", "FirstName", "LastName" }, _studentQueryResult.NameAndTypes.Select(_ => _.Name));
 }
Beispiel #10
0
 public void ThenAllClassTableColumnTypesAreCorrect()
 {
     if (SqlTestHelpers.ConnectionString(null) == null)
     {
         return;
     }
     CollectionAssert.AreEqual(new[] { typeof(Guid), typeof(Guid), typeof(string) }, _classQueryResult.NameAndTypes.Select(_ => _.Type));
 }
Beispiel #11
0
 public void ThenThereAreSequenceIds()
 {
     if (SqlTestHelpers.ConnectionString(null) == null)
     {
         return;
     }
     Assert.AreEqual(0, _logRows.Min(_ => _.SeqId));
     Assert.AreEqual(5, _logRows.Max(_ => _.SeqId));
 }
Beispiel #12
0
        public void ThenThereAreTenRowsInTheLogTable()
        {
            if (SqlTestHelpers.ConnectionString(null) == null)
            {
                return;
            }
            var lookup = _logRows.ToLookup(_ => _.SessionId, _ => _);

            Assert.AreEqual(10, _logRows.Count);
        }
Beispiel #13
0
 public void ThenTheExplicitSessionContainsTheCorrectRows()
 {
     if (SqlTestHelpers.ConnectionString(null) == null)
     {
         return;
     }
     CollectionAssert.AreEqual(new[]
     {
         "Explicit logging from the outside C"
     }, _log3.Select(_ => _.Text));
 }
Beispiel #14
0
 public void ThenTheFirstSessionContainsTheCorrectRows()
 {
     if (SqlTestHelpers.ConnectionString(null) == null)
     {
         return;
     }
     CollectionAssert.AreEqual(new[]
     {
         "CREATE TABLE [School_Classes_Students_tmp] ([School_Classes_id_] uniqueidentifier not null,[FirstName] nvarchar(max),[LastName] nvarchar(max));CREATE TABLE [School_Classes_tmp] ([_id_] uniqueidentifier not null,[School_id_] uniqueidentifier not null,[Name] nvarchar(max));CREATE TABLE [School_tmp] ([_id_] uniqueidentifier not null,[Name] nvarchar(max))",
         "EXEC sp_rename 'School_Classes_Students_tmp', 'School_Classes_Students';EXEC sp_rename 'School_Classes_tmp', 'School_Classes';EXEC sp_rename 'School_tmp', 'School'",
         "Verified table row counts: School_Classes_Students:100;School_Classes:6;School:1"
     }, _log1.Select(_ => _.Text));
 }
Beispiel #15
0
        public void TestTableProgression()
        {
            SqlTestHelpers.WithNewDb("SqlTableProgression", conn =>
            {
                using (var cmd = new SqlCommand("CREATE TABLE School_bck (x int)", conn))
                    cmd.ExecuteNonQuery();
                using (var cmd = new SqlCommand("CREATE TABLE School_Classes (x int)", conn))
                    cmd.ExecuteNonQuery();
                using (var cmd = new SqlCommand("CREATE TABLE School_Classes_Students_tmp (x int)", conn))
                    cmd.ExecuteNonQuery();

                var tableManager    = new SqlTableManager(SqlTestHelpers.ConnectionString("SqlTableProgression"));
                var dataExtract     = new DataExtract <School>(Spec, tableManager);
                var entityWithTable = new EntityWithTable(dataExtract.TopEntity, tableManager);

                // just to make some use of entityWithTable
                Assert.IsNotNull(entityWithTable);

                // pre-assert
                var tables = tableManager.GetExistingTableNames(conn, false);
                CollectionAssert.AreEquivalent(new[] { "School_bck", "School_Classes", "School_Classes_Students_tmp" }, tables);

                // after first begin, we should have a full set of _tmp, nothing else touched
                tableManager.Begin();
                tables = tableManager.GetExistingTableNames(conn, false);
                CollectionAssert.AreEquivalent(new[] { "School_bck", "School_Classes", "School_Classes_Students_tmp", "School_tmp", "School_Classes_tmp" }, tables);

                // after first end, we should have a _bck of the faked School_Classes and the three real tables and no _tmp
                tableManager.End();
                tables = tableManager.GetExistingTableNames(conn, false);
                CollectionAssert.AreEquivalent(new[] { "School_Classes_bck", "School", "School_Classes", "School_Classes_Students" }, tables);

                // after second begin, we should have a full set of _tmp, nothing else touched since last check
                tableManager.Begin();
                tables = tableManager.GetExistingTableNames(conn, false);
                CollectionAssert.AreEquivalent(new[] { "School_Classes_bck", "School", "School_Classes", "School_Classes_Students", "School_Classes_Students_tmp", "School_tmp", "School_Classes_tmp" }, tables);

                // efter second, we should have a full set of _bck and and real files and no _tmp
                tableManager.End();
                tables = tableManager.GetExistingTableNames(conn, false);
                CollectionAssert.AreEquivalent(new[] { "School_bck", "School_Classes_bck", "School_Classes_Students_bck", "School", "School_Classes", "School_Classes_Students" }, tables);
            });
        }
Beispiel #16
0
        public void SetUp()
        {
            if (SqlTestHelpers.ConnectionString(null) == null)
            {
                return;
            }
            var tableFactory = new SqlTableManager(SqlTestHelpers.ConnectionString("SchoolTest"), "log");
            var export       = new DataExtract <School>(Spec, tableFactory);

            SqlTestHelpers.WithNewDb("SchoolTest", conn =>
            {
                var sw = Stopwatch.StartNew();
                export.Run(Enumerable.Range(0, NumberOfSchools).Select(_ => School));
                Console.WriteLine(sw.ElapsedMilliseconds.ToString());
                _schoolQueryResult  = SqlTestHelpers.SimpleQuery(conn, "SELECT * FROM school");
                _classQueryResult   = SqlTestHelpers.SimpleQuery(conn, "SELECT * FROM school_classes");
                _studentQueryResult = SqlTestHelpers.SimpleQuery(conn, "SELECT TOP 1 * FROM school_classes_students");
                _studentCount       = SqlTestHelpers.SimpleQuery <int>(conn, "SELECT count(*) FROM school_classes_students");
                _logQueryResult     = SqlTestHelpers.SimpleQuery(conn, "SELECT * FROM log");
            });
        }
Beispiel #17
0
        public void Test()
        {
            var spec = entitySpec.Begin()
                       .Add("Blob1")
                       .Add("Blob2")
                       .Add("Blob3");
            var testObj = new WithBlobs
            {
                Blob1 = new byte[] { 1, 2, 3, 4, 5 },
                Blob3 = new byte[] { 6, 7, 8, 9, 0 }
            };

            SqlTestHelpers.WithNewDb("Gurka", conn =>
            {
                var tm          = new SqlTableManager(SqlTestHelpers.ConnectionString("Gurka"));
                var dataExtract = new DataExtract <WithBlobs>(spec, tm);
                dataExtract.Run(testObj);
                var result = SqlTestHelpers.SimpleQuery(conn, "SELECT * FROM WithBlobs");
                Assert.AreEqual(3, result.NameAndTypes.Length);
            });
        }
Beispiel #18
0
        public void TestThatWrongNumberOfRowsIsCaught()
        {
            SqlTestHelpers.WithNewDb("SqlTableSimulateBadWrite", conn =>
            {
                var tableManager    = new SqlTableManager(SqlTestHelpers.ConnectionString("SqlTableSimulateBadWrite"));
                var dataExtract     = new DataExtract <School>(Spec, tableManager);
                var entityWithTable = new EntityWithTable(dataExtract.TopEntity, tableManager);

                // just to make some use of entityWithTable
                Assert.IsNotNull(entityWithTable);

                // after first begin, we should have a full set of _tmp, nothing else touched
                tableManager.Begin();

                using (var cmd = new SqlCommand($"INSERT INTO school_tmp (_id_,name) VALUES ('{Guid.NewGuid()}','nisse')", conn))
                    cmd.ExecuteNonQuery();

                var ex = Assert.Throws <Exception>(() => tableManager.End());
                Assert.AreEqual("Found 1 rows in table School but expected 0", ex.Message);
            });
        }