public void Interceptor_BeforeCompile_FromTest()
        {
            var query = string.Empty;
            var where = new DelegateQueryPart(OperationType.Where, () => "ID = 2");

            var provider = new SqlContextProvider("connectionstring");
            provider.Interceptor(() => new
            {
                ID = 0
            }).BeforeExecute(q => query = q.QueryString).AsExecute(e => _warriors.Select(w => new
            {
                ID = w.ID
            }));

            provider.Interceptor<Warrior>().BeforeCompile(c => c.Parts.First(p => p.OperationType == OperationType.From).Add(where));
            using (var context = provider.Open())
            {
                context.From<Warrior>().Select(() => new
                {
                    ID = 0
                });

                Assert.AreEqual(query.Flatten(), "SELECT ID FROM Warrior WHERE ID = 2");
            }
        }
        public void QueryCompilerCompileDeleteTest()
        {
            var part = new DelegateQueryPart(OperationType.Delete, () => "Table");
            var parts = new QueryPartsContainer();
            parts.Add(part);

            var compiler = new QueryCompiler();
            var query = compiler.Compile(parts, new InterceptorCollection());

            Assert.AreEqual(query.QueryString, "DELETE FROM Table");
        }
        public void QueryCompilerCompileAndTest()
        {
            var part = new DelegateQueryPart(OperationType.And, () => "Field = 1");
            var parts = new QueryPartsContainer();
            parts.Add(part);

            var compiler = new QueryCompiler();
            var query = compiler.Compile(parts, new InterceptorCollection());

            Assert.AreEqual(query.QueryString, "\r\n AND Field = 1");
        }
        public void SqlServer_QueryCompiler_Compile_AlterTableTest()
        {
            var part = new DelegateQueryPart(OperationType.AlterTable, () => "Table");
            var parts = new QueryPartsContainer();
            parts.Add(part);

            var compiler = new QueryCompiler();
            var query = compiler.Compile(parts, new InterceptorCollection());

            Assert.AreEqual(query.QueryString, "ALTER TABLE Table ");
        }
        public void Interceptor_BeforeCompile_SelectTest()
        {
            var query = string.Empty;
            var where = new DelegateQueryPart(OperationType.Where, () => "ID = 2");

            var provider = BuildContext();
            provider.Interceptor<Warrior>()
                .BeforeCompile(c => c.Parts.First(p => p.OperationType == OperationType.From).Add(where))
                .BeforeExecute(q => query = q.QueryString);

            using (var context = provider.Open())
            {
                context.Select<Warrior>();

                Assert.AreEqual(query.Flatten(), "SELECT ID, Name, WeaponID, Race, SpecialSkill FROM Warrior WHERE ID = 2");
            }
        }
        public void Interceptor_BeforeCompile_DeleteTest()
        {
            var query = string.Empty;
            var where = new DelegateQueryPart(OperationType.Where, () => "ID = 2");

            var provider = BuildContext();
            provider.Interceptors.Remove<Warrior>();
            provider.Interceptor<Warrior>().BeforeExecute(q => query = q.QueryString).AsExecute(a => { });
            provider.Interceptor<Warrior>().BeforeCompile(c => c.Parts.First(p => p.OperationType == OperationType.Delete).Add(where));
            using (var context = provider.Open())
            {
                context.Delete<Warrior>();

                context.Commit();

                Assert.AreEqual(query.Flatten(), "DELETE FROM Warrior WHERE ID = 2");
            }
        }
        public void SqlServer_QueryCompiler_Compile_ProcedureTest()
        {
            var part = new DelegateQueryPart(OperationType.Procedure, () => "ProcName");
            var parts = new QueryPartsContainer();
            parts.Add(part);

            var compiler = new QueryCompiler();
            var query = compiler.Compile(parts, new InterceptorCollection());

            Assert.AreEqual(query.QueryString, "EXEC ProcName ");
        }
        public void QueryCompilerCompileUpdateWithUpdateValueTest()
        {
            var part = new DelegateQueryPart(OperationType.Update, () => "Table");
            part.Add(new DelegateQueryPart(OperationType.UpdateValue, () => "Field1=Value1, "));
            part.Add(new DelegateQueryPart(OperationType.UpdateValue, () => "Field2=Value2"));
            var parts = new QueryPartsContainer();
            parts.Add(part);

            var compiler = new QueryCompiler();
            var query = compiler.Compile(parts, new InterceptorCollection());

            Assert.AreEqual(query.QueryString, "UPDATE Table SET Field1=Value1, Field2=Value2");
        }
        public void QueryCompilerCompileThenByDescTest()
        {
            var part = new DelegateQueryPart(OperationType.ThenByDesc, () => "Field");
            var parts = new QueryPartsContainer();
            parts.Add(part);

            var compiler = new QueryCompiler();
            var query = compiler.Compile(parts, new InterceptorCollection());

            Assert.AreEqual(query.QueryString, ", Field DESC");
        }
        public void QueryCompilerCompileSimpleJoinTest()
        {
            var part = new DelegateQueryPart(OperationType.Join, () => "Table");
            var parts = new QueryPartsContainer();
            parts.Add(part);

            var compiler = new QueryCompiler();
            var query = compiler.Compile(parts, new InterceptorCollection());

            Assert.AreEqual(query.QueryString, " \r\nJOIN Table");
        }
        public void QueryCompilerCompileOrderByTest()
        {
            var part = new DelegateQueryPart(OperationType.OrderBy, () => "Field");
            var parts = new QueryPartsContainer();
            parts.Add(part);

            var compiler = new QueryCompiler();
            var query = compiler.Compile(parts, new InterceptorCollection());

            Assert.AreEqual(query.QueryString, " \r\nORDER BY Field ASC");
        }
        public void QueryCompilerCompileInsertWithInsertMemberTest()
        {
            var part = new DelegateQueryPart(OperationType.Insert, () => "TableName");
            part.Add(new DelegateQueryPart(OperationType.InsertMember, () => "Field1"));
            part.Add(new DelegateQueryPart(OperationType.InsertMember, () => "Field2"));
            var parts = new QueryPartsContainer();
            parts.Add(part);

            var compiler = new QueryCompiler();
            var query = compiler.Compile(parts, new InterceptorCollection());

            Assert.AreEqual(query.QueryString, "INSERT INTO TableName (Field1, Field2)");
        }
        public void SqlServer_QueryCompiler_Compile_OutputParameterSetTest()
        {
            var part = new DelegateQueryPart(OperationType.OutParameterSet, () => "ParamName=1");
            var parts = new QueryPartsContainer();
            parts.Add(part);

            var compiler = new QueryCompiler();
            var query = compiler.Compile(parts, new InterceptorCollection());

            Assert.AreEqual(query.QueryString, "SET @ParamName=1\r\n");
        }
        public void SqlServer_QueryCompiler_Compile_DroptDatabaseTest()
        {
            var part = new DelegateQueryPart(OperationType.DropDatabase, () => "DbName");
            var parts = new QueryPartsContainer();
            parts.Add(part);

            var compiler = new QueryCompiler();
            var query = compiler.Compile(parts, new InterceptorCollection());

            Assert.AreEqual($"DROP DATABASE DbName", query.QueryString);
        }
        public void SqlServer_QueryCompiler_Compile_DropFieldTest()
        {
            var part = new DelegateQueryPart(OperationType.DropColumn, () => "ColumnName");
            var parts = new QueryPartsContainer();
            parts.Add(part);

            var compiler = new QueryCompiler();
            var query = compiler.Compile(parts, new InterceptorCollection());

            Assert.AreEqual(query.QueryString, "DROP COLUMN ColumnName");
        }
        public void SqlServer_QueryCompiler_Compile_DetachDatabaseTest()
        {
            var part = new DelegateQueryPart(OperationType.DetachDatabase, () => "DbName");
            var parts = new QueryPartsContainer();
            parts.Add(part);

            var compiler = new QueryCompiler();
            var query = compiler.Compile(parts, new InterceptorCollection());

            Assert.AreEqual($"ALTER DATABASE DbName SET SINGLE_USER WITH ROLLBACK IMMEDIATE; exec sp_detach_db 'DbName'", query.QueryString);
        }
        public void SqlServer_QueryCompiler_Compile_CreateDatabaseTest()
        {
            var part = new DelegateQueryPart(OperationType.CreateDatabase, () => "DbName");
            var parts = new QueryPartsContainer();
            parts.Add(part);

            var compiler = new QueryCompiler();
            var query = compiler.Compile(parts, new InterceptorCollection());

            Assert.IsTrue(query.QueryString.Contains("CREATE DATABASE DbName"));
            Assert.IsTrue(query.QueryString.Contains("ON PRIMARY (NAME = N''DbName'', FILENAME = N''' + @device_directory + N'DbName.mdf'')"));
            Assert.IsTrue(query.QueryString.Contains("LOG ON (NAME = N''DbName_log'',  FILENAME = N''' + @device_directory + N'DbName_log.ldf'')"));
        }