Beispiel #1
0
        public void ChildListTest()
        {
            Expect.IsTrue(_testDatabases.Count > 0);
            string methodName = MethodBase.GetCurrentMethod().Name;

            _testDatabases.Each(db =>
            {
                OutLineFormat("{0}.{1}: {2}", ConsoleColor.DarkYellow, this.GetType().Name, methodName, db.GetType().Name);
                TestTable parent = new TestTable(db);
                parent.Name      = "Parent Test";
                parent.Save(db);
                TestFkTable child = parent.TestFkTablesByTestTableId.AddNew();
                child.Name        = "Child ({0})"._Format(6.RandomLetters());

                parent.Save(db);

                TestTable retrieved = TestTable.OneWhere(c => c.Id == parent.Id, db);
                Expect.AreEqual(1, retrieved.TestFkTablesByTestTableId.Count);
                Expect.AreEqual(child.Name, retrieved.TestFkTablesByTestTableId[0].Name);

                TestFkTable child2 = new TestFkTable();
                child2.Name        = "Child 2 ({0})"._Format(6.RandomLetters());
                parent.TestFkTablesByTestTableId.Add(child2);
                parent.Save(db);

                retrieved = TestTable.OneWhere(c => c.Id == parent.Id, db);
                Expect.AreEqual(2, retrieved.TestFkTablesByTestTableId.Count);
                List <string> names = retrieved.TestFkTablesByTestTableId.Select(c => c.Name).ToList();
                Expect.IsTrue(names.Contains(child.Name));
                Expect.IsTrue(names.Contains(child2.Name));
            });
        }
        public void ChildCollectionsFunctionCorrectly()
        {
            SQLiteDatabase db = new SQLiteDatabase(nameof(XrefListTest));

            db.TryEnsureSchema <TestTable>();
            TestTable.LoadAll(db).Delete();
            TestFkTable.LoadAll(db).Delete();

            TestTable testTable = new TestTable {
                Name = "TestTable_".RandomLetters(4)
            };
            TestFkTable fkTable = new TestFkTable {
                Name = "TestFkTable_".RandomLetters(6)
            };

            testTable.Save(db);
            testTable.TestFkTablesByTestTableId.Add(fkTable);

            fkTable.Id.ShouldBeNull();
            testTable.Save(db);
            fkTable.Id.ShouldNotBeNull();

            TestTable retrieved = TestTable.GetById(testTable.Id, db);

            Expect.AreEqual(1, retrieved.TestFkTablesByTestTableId.Count);
            Expect.AreEqual(fkTable.ToJsonSafe().ToJson(), retrieved.TestFkTablesByTestTableId[0].ToJsonSafe().ToJson());
        }
Beispiel #3
0
        public static void FilterOrExpression()
        {
            Database      database = new SQLiteDatabase(".\\", MethodBase.GetCurrentMethod().Name);
            ConsoleLogger logger   = PrepareDatabaseAndGetLogger(database);

            TestTable one = new TestTable();

            one.Name = "banana";
            one.Save(database);

            TestTable two = new TestTable();

            two.Name = "blah";
            two.Save(database);

            DaoExpressionFilter v          = new DaoExpressionFilter(logger);
            QueryFilter         testFilter = v.Where <TestTable>((t) => t.Name == one.Name).Or <TestTable>((t) => t.Name == two.Name);

            TestTableCollection check = TestTable.Where(c => c.Name == "banana" || c.Name == "blah", database);

            Expect.IsNotNull(check);

            TestTableCollection evalCheck = TestTable.Where(testFilter, database);

            Expect.IsNotNull(evalCheck);

            Expect.AreEqual(2, evalCheck.Count);
            Out(v.TraceLog, ConsoleColor.Cyan);
        }
Beispiel #4
0
        public void ShouldBeAbleToRetrieveWithDynamicReturn()
        {
            HashSet <Database> dbs = DataTools.Setup();

            dbs.Each(db =>
            {
                try
                {
                    string name        = "Name_".RandomLetters(8);
                    TestTable instance = new TestTable();
                    instance.Name      = name;
                    instance.Save(db);
                    TestTable retrieved = RetrieveByNameAndValidate(db, instance);

                    SqlStringBuilder sql = db.Sql().Select(nameof(TestTable)).Where(new { Name = name });
                    dynamic queried      = sql.ExecuteDynamicReader(db).SingleOrDefault();
                    OutLineFormat("Result type is ({0})", queried.GetType().Name, ConsoleColor.Cyan);

                    Expect.AreEqual(retrieved.Id, (long)queried.Id);
                    Expect.AreEqual(retrieved.Uuid, queried.Uuid);
                    Expect.AreEqual(retrieved.Name, queried.Name);

                    Pass($"{db.GetType().Name} passed");
                }
                catch (Exception ex)
                {
                    Error($"{db.GetType().Name}: failed: {ex.Message}", ex);
                }
            });
            DataTools.Cleanup(dbs);
        }
Beispiel #5
0
        public static void DaoExpressionFilterTest()
        {
            SQLiteBitMonitor.MonitorBitness();
            Database      database = new SQLiteDatabase(".\\", MethodBase.GetCurrentMethod().Name);
            ConsoleLogger logger   = PrepareDatabaseAndGetLogger(database);

            TestTable testInstance = new TestTable();

            testInstance.Name = "banana";
            testInstance.Save(database);

            DaoExpressionFilter v          = new DaoExpressionFilter(logger);
            QueryFilter         testFilter = v.Where <TestTable>((t) => t.Name == testInstance.Name);

            TestTable check = TestTable.Where(Filter.Column("Name") == "banana", database).FirstOrDefault();

            Expect.IsNotNull(check);

            TestTable evalCheck = TestTable.Where(testFilter, database).FirstOrDefault();

            Expect.IsNotNull(evalCheck);

            Expect.AreEqual(check.Id, evalCheck.Id);
            Out(v.TraceLog, ConsoleColor.Cyan);
        }
Beispiel #6
0
        public static TestTable CreateTestTable(string name, string description, Database db, bool save = true)
        {
            TestTable table = new TestTable(db);

            table.Name        = name;
            table.Description = description;
            if (save)
            {
                table.Save(db);
            }
            return(table);
        }
Beispiel #7
0
        public void CreateTest()
        {
            Expect.IsTrue(_testDatabases.Count > 0);
            string methodName = MethodBase.GetCurrentMethod().Name;

            _testDatabases.Each(db =>
            {
                OutLineFormat("{0}.{1}: {2}", ConsoleColor.DarkYellow, this.GetType().Name, methodName, db.GetType().Name);
                TestTable test = new TestTable(db);
                test.Name      = 8.RandomLetters();
                test.Save(db);

                Expect.IsGreaterThan(test.Id.Value, 0, "Id should have been greater than 0");
                OutLineFormat("{0}", ConsoleColor.Cyan, test.PropertiesToString());
            });
        }
Beispiel #8
0
        public void DeleteTest()
        {
            Expect.IsTrue(_testDatabases.Count > 0);
            string methodName = MethodBase.GetCurrentMethod().Name;

            _testDatabases.Each(db =>
            {
                OutLineFormat("{0}.{1}: {2}", ConsoleColor.DarkYellow, this.GetType().Name, methodName, db.GetType().Name);
                string name    = 6.RandomLetters();
                TestTable test = new TestTable(db);
                test.Name      = name;
                test.Save(db);

                TestTable toDelete = TestTable.OneWhere(t => t.Id == test.Id, db);
                Expect.IsNotNull(toDelete);
                toDelete.Delete(db);

                TestTable shouldBeNull = TestTable.OneWhere(t => t.Id == test.Id, db);
                Expect.IsNull(shouldBeNull);
            });
        }
Beispiel #9
0
        public static void ExecuteReaderTest()
        {
            Database      database = new SQLiteDatabase(".\\", MethodBase.GetCurrentMethod().Name);
            ConsoleLogger logger   = PrepareDatabaseAndGetLogger(database);
            TestTable     one      = new TestTable();

            one.Name = "banana";
            one.Save(database);
            TestTable two = new TestTable();

            two.Name = one.Name;
            two.Save(database);
            SqlStringBuilder sql = database.GetSqlStringBuilder();

            sql.Select("TestTable").Where("Name", "banana");
            List <TestTable> retrieved = database.ExecuteReader <TestTable>(sql).ToList();

            Expect.AreEqual(2, retrieved.Count);
            retrieved.Each(t =>
            {
                OutLineFormat("{0}, {1}", t.Id, t.Name);
            });
        }
Beispiel #10
0
        public void ShouldBeAbleToUpdateWithDynamicParameters()
        {
            HashSet <Database> dbs = DataTools.Setup();

            dbs.Each(db =>
            {
                try
                {
                    string name        = "Name_".RandomLetters(8);
                    TestTable instance = new TestTable();
                    instance.Name      = name;
                    instance.Save(db);
                    TestTable retrieved = RetrieveByNameAndValidate(db, instance);

                    string updatedName    = "Updated+".RandomLetters(5);
                    SqlStringBuilder sql2 = db.Sql().Update(nameof(TestTable), new { Name = updatedName }).Where(new { Id = retrieved.Id });
                    db.ExecuteSql(sql2);

                    SqlStringBuilder sql3 = db.Sql();
                    sql3.Select(nameof(TestTable), "*").Where(new { Name = updatedName });
                    TestTable retrieved2 = db.ExecuteReader <TestTable>(sql3).SingleOrDefault();
                    Expect.IsNotNull(retrieved2);
                    Expect.AreEqual(instance.Id, retrieved2.Id);
                    Expect.AreEqual(instance.Uuid, retrieved2.Uuid);
                    Expect.AreEqual(instance.Cuid, retrieved2.Cuid);
                    Expect.AreEqual(updatedName, retrieved2.Name);

                    Pass($"{db.GetType().Name} passed");
                }
                catch (Exception ex)
                {
                    Error($"{db.GetType().Name}: failed: {ex.Message}", ex);
                }
            });
            DataTools.Cleanup(dbs);
        }
Beispiel #11
0
        public void UpdateTest()
        {
            Expect.IsTrue(_testDatabases.Count > 0);
            string methodName = MethodBase.GetCurrentMethod().Name;

            _testDatabases.Each(db =>
            {
                OutLineFormat("{0}.{1}: {2}", ConsoleColor.DarkYellow, this.GetType().Name, methodName, db.GetType().Name);
                string name    = 6.RandomLetters();
                TestTable test = new TestTable(db);
                test.Name      = name;
                test.Save(db);

                TestTable toUpdate = TestTable.OneWhere(t => t.Name == name, db);
                string changeTo    = 8.RandomLetters();
                toUpdate.Name      = changeTo;
                toUpdate.Save(db);

                TestTable check = TestTable.OneWhere(t => t.Name == changeTo, db);
                Expect.AreEqual(test.Id, check.Id);
                Expect.AreEqual(toUpdate.Id, check.Id);
                Expect.AreEqual(changeTo, check.Name);
            });
        }
Beispiel #12
0
        public void RetrieveTest()
        {
            Expect.IsTrue(_testDatabases.Count > 0);
            string methodName = MethodBase.GetCurrentMethod().Name;

            _testDatabases.Each(db =>
            {
                OutLineFormat("{0}.{1}: {2}", ConsoleColor.DarkYellow, this.GetType().Name, methodName, db.GetType().Name);
                string name    = 6.RandomLetters();
                TestTable test = new TestTable(db);
                test.Name      = name;
                test.Save(db);

                TestTable check = TestTable.OneWhere(t => t.Name == name, db);
                Expect.IsNotNull(check);
                Expect.AreEqual(test.Id, check.Id);
                Expect.AreEqual(name, check.Name);

                TestTable check2 = TestTable.OneWhere(t => t.Id == test.Id, db);
                Expect.IsNotNull(check2);
                Expect.AreEqual(test.Id, check2.Id);
                Expect.AreEqual(name, check2.Name);
            });
        }