Exemple #1
0
        public void Mock_ExecuteDataset_WithFunction_Test()
        {
            var conn = new MockDbConnection();

            MockTable table1 = MockTable.WithColumns("Col1", "Col2")
                               .AddRow(11, 12);

            MockTable table2 = MockTable.WithColumns("Col3", "Col4")
                               .AddRow("MyString", 3.4);

            conn.Mocks
            .When(null)
            .ReturnsDataset(cmd => new[] { table1, table2 });

            using (var cmd = new DatabaseCommand(conn))
            {
                cmd.CommandText.AppendLine("SELECT ...");
                var result = cmd.ExecuteDataSet(new { Col1 = 0, Col2 = 0 },
                                                new { Col3 = "", Col4 = 0.0 });

                Assert.AreEqual(1, result.Item1.Count());          // 1 row
                Assert.AreEqual(11, result.Item1.First().Col1);
                Assert.AreEqual(12, result.Item1.First().Col2);

                Assert.AreEqual(1, result.Item2.Count());          // 1 row
                Assert.AreEqual("MyString", result.Item2.First().Col3);
                Assert.AreEqual(3.4, result.Item2.First().Col4);
            }
        }
        public void ExecuteSystemDataSet_CountForTableName_Test()
        {
            using (var cmd = new DatabaseCommand(_connection))
            {
                cmd.Log         = Console.WriteLine;
                cmd.CommandText = @" SELECT DEPTNO, COUNT(*) FROM EMP GROUP BY DEPTNO ";

                var dataset = cmd.ExecuteDataSet();

                Assert.AreEqual("EMP", dataset.Tables[0].TableName);
            }
        }
        public void ExecuteSystemDataSet_OneTable_Test()
        {
            using (var cmd = new DatabaseCommand(_connection))
            {
                cmd.Log = Console.WriteLine;
                cmd.CommandText.AppendLine(" SELECT EMPNO, ENAME FROM EMP ");

                var dataset = cmd.ExecuteDataSet();
                var smith   = dataset.Tables[0].Rows[0];

                Assert.AreEqual(EMP.Smith.EmpNo, smith["EmpNo"]);
                Assert.AreEqual(EMP.Smith.EName, smith["EName"]);
            }
        }
        public void ExecuteSystemDataSet_TableNames_Test()
        {
            using (var cmd = new DatabaseCommand(_connection))
            {
                cmd.Log = Console.WriteLine;
                cmd.CommandText.AppendLine(" SELECT EMPNO  FROM EMP ");                     // With rows
                cmd.CommandText.AppendLine(" SELECT DEPTNO FROM DEPT WHERE DEPTNO = 0 ");   // No rows

                var dataset = cmd.ExecuteDataSet();

                Assert.AreEqual("EMP", dataset.Tables[0].TableName);
                Assert.AreEqual("DEPT", dataset.Tables[1].TableName);
            }
        }
        public void ExecuteDataSet_NoData_Test()
        {
            using (var cmd = new DatabaseCommand(_connection))
            {
                cmd.Log = Console.WriteLine;
                cmd.CommandText.AppendLine(" SELECT EMPNO, ENAME FROM EMP  WHERE EMPNO = 99999");
                cmd.CommandText.AppendLine(" SELECT * FROM DEPT WHERE DEPTNO = 99999");

                var data = cmd.ExecuteDataSet <EMP, DEPT>();

                Assert.AreEqual(0, data.Item1.Count());
                Assert.AreEqual(0, data.Item2.Count());
            }
        }
        public void ExecuteSystemDataSet_JoinForTableName_Test()
        {
            using (var cmd = new DatabaseCommand(_connection))
            {
                cmd.Log         = Console.WriteLine;
                cmd.CommandText = @" SELECT EMP.ENAME, 
                                            DEPT.DNAME
                                       FROM EMP 
                                      INNER JOIN DEPT ON DEPT.DEPTNO = EMP.DEPTNO ";

                var dataset = cmd.ExecuteDataSet();

                Assert.AreEqual("Table1", dataset.Tables[0].TableName);
            }
        }
        public void ExecuteDataSet_2Types_Test()
        {
            using (var cmd = new DatabaseCommand(_connection))
            {
                string sql = " SELECT EMPNO, ENAME FROM EMP; ";

                cmd.Log = Console.WriteLine;
                cmd.CommandText.AppendLine(sql);
                cmd.CommandText.AppendLine(sql);

                var data = cmd.ExecuteDataSet <EMP, EMP>();

                Assert.AreEqual(14, data.Item1.Count());
                Assert.AreEqual(14, data.Item2.Count());
            }
        }
        public void ExecuteDataSet_2AnonymousTypes_Test()
        {
            using (var cmd = new DatabaseCommand(_connection))
            {
                string sql = " SELECT EMPNO, ENAME FROM EMP; ";
                var    emp = new { Empno = 0, Ename = "" };

                cmd.Log = Console.WriteLine;
                cmd.CommandText.AppendLine(sql);
                cmd.CommandText.AppendLine(sql);

                var data = cmd.ExecuteDataSet(emp, emp);

                Assert.AreEqual(14, data.Item1.Count());
                Assert.AreEqual(14, data.Item2.Count());
            }
        }
        public void ExecuteSystemDataSet_TwoTables_Test()
        {
            using (var cmd = new DatabaseCommand(_connection))
            {
                cmd.Log = Console.WriteLine;
                cmd.CommandText.AppendLine(" SELECT EMPNO, ENAME FROM EMP ");
                cmd.CommandText.AppendLine(" SELECT * FROM DEPT ");

                var dataset    = cmd.ExecuteDataSet();
                var smith      = dataset.Tables[0].Rows[0];
                var accounting = dataset.Tables[1].Rows[0];

                Assert.AreEqual(EMP.Smith.EmpNo, smith["EmpNo"]);
                Assert.AreEqual(EMP.Smith.EName, smith["EName"]);
                Assert.AreEqual(DEPT.Accounting.DName, accounting["DName"]);
                Assert.AreEqual(DEPT.Accounting.Loc, accounting["Loc"]);
            }
        }
        public void ExecuteDataSetTyped_Test()
        {
            using (var cmd = new DatabaseCommand(_connection))
            {
                cmd.Log = Console.WriteLine;
                cmd.CommandText.AppendLine(" SELECT EMPNO, ENAME FROM EMP ");
                cmd.CommandText.AppendLine(" SELECT * FROM DEPT ");

                var data       = cmd.ExecuteDataSet <EMP, DEPT>();
                var smith      = data.Item1.FirstOrDefault(i => i.EmpNo == 7369);
                var accounting = data.Item2.FirstOrDefault(i => i.DeptNo == 10);

                Assert.AreEqual(EMP.Smith.EmpNo, smith.EmpNo);
                Assert.AreEqual(EMP.Smith.EName, smith.EName);
                Assert.AreEqual(DEPT.Accounting.DName, accounting.DName);
                Assert.AreEqual(DEPT.Accounting.Loc, accounting.Loc);
            }
        }
Exemple #11
0
        public void ExecuteDataSet_ActionAfter_Test()
        {
            bool isPassed = false;

            using (var cmd = new DatabaseCommand(_connection))
            {
                cmd.Log = Console.WriteLine;
                cmd.CommandText.AppendLine(" SELECT * FROM EMP; ");
                cmd.CommandText.AppendLine(" SELECT * FROM DEPT; ");

                cmd.ActionAfterExecution = (command, tables) =>
                {
                    isPassed = true;
                };

                var data = cmd.ExecuteDataSet <EMP, DEPT>();

                Assert.IsTrue(isPassed);
                Assert.AreEqual(14, data.Item1.Count());
                Assert.AreEqual(4, data.Item2.Count());
            }
        }
Exemple #12
0
        public void ExecuteDataSet_ActionBefore_Test()
        {
            bool isPassed = false;

            using (var cmd = new DatabaseCommand(_connection))
            {
                cmd.Log = Console.WriteLine;
                cmd.CommandText.AppendLine(" SELECT * FROM EMP; ");
                cmd.CommandText.AppendLine(" SELECT * FROM DEPT; ");

                cmd.ActionBeforeExecution = (command) =>
                {
                    cmd.CommandText.Replace("FROM EMP;", "FROM EMP WHERE EMPNO > 7369; ");
                    isPassed = true;
                };

                var data = cmd.ExecuteDataSet <EMP, DEPT>();

                Assert.IsTrue(isPassed);
                Assert.AreEqual(13, data.Item1.Count());
                Assert.AreEqual(4, data.Item2.Count());
            }
        }