public void DbLogger_Log()
        {
            #region Arrange

            var connectionStr = "connectionStr";
            var log1          = new LogRecord("log1");
            var log2          = new LogRecord("log2");

            var connectionStub = new DbConnectionStub {
                ConnectionString = connectionStr
            };

            var schemaMock = new Mock <SqlLogSchema>(connectionStr, null, null, null)
            {
                CallBase = true
            };
            schemaMock.Setup(x => x.CreateConnection())
            .Returns(connectionStub);

            var loggerMock = new Mock <DbLoggerBase>(schemaMock.Object)
            {
                CallBase = true
            };
            var loggerMockProtected = loggerMock.Protected();

            var firstCommand  = default(DbCommand);
            var secondCommand = default(DbCommand);

            #endregion Arrange

            #region Act

            loggerMock.Object.Log(log1);
            firstCommand = connectionStub.LastCommand;

            loggerMock.Object.Log(log2);
            secondCommand = connectionStub.LastCommand;

            #endregion Act

            #region Assert

            //Assert.AreEqual(connectionStr, loggerMock.Object.Schema.ConnectionString);
            AssertDefaultSchemaDbCommand(firstCommand, log1);
            AssertDefaultSchemaDbCommand(secondCommand, log2);
            loggerMockProtected.Verify("ExecuteInitScript", Times.Once());
            loggerMockProtected.Verify("WriteRecord", Times.Exactly(2), ItExpr.IsAny <LogRecord>());

            #endregion Assert
        }
        public void Select_LabelIsNull_NoLabelComment()
        {
            using (var connection = new DbConnectionStub())
                using (var context = new DataContext(connection)
                {
                    StatementsLabel = null
                })
                {
                    var query = context.GetTable <SimpleEntity>().Where(t => t.Id == 1);
                    using (var command = context.GetCommand(query))
                    {
                        var expectedFirstPart =
                            "SELECT [t0].[Id], [t0].[Discriminator], [t0].[X]" + Environment.NewLine;

                        Assert.IsTrue(command.CommandText.StartsWith(expectedFirstPart), command.CommandText);
                    }
                }
        }
Exemple #3
0
 public void QueryWithUserDefinedFunctionNotInDataContextTranslationTest()
 {
     using (var connection = new DbConnectionStub())
     {
         using (var context = new DataContext(connection))
         {
             var query = context.GetTable <SimpleEntity>().OrderBy(t => UserDefinedFunctions.Random());
             using (var command = context.GetCommand(query))
             {
                 Assert.AreEqual(
                     "SELECT [t0].[Id], [t0].[Discriminator], [t0].[X]" + Environment.NewLine +
                     "FROM [SimpleTable] AS [t0]" + Environment.NewLine +
                     "ORDER BY NEWID()",
                     command.CommandText);
             }
         }
     }
 }
Exemple #4
0
 public void SipmleQueryTranslation()
 {
     using (var connection = new DbConnectionStub())
     {
         using (var context = new DataContext(connection))
         {
             var query = context.GetTable <SimpleEntity>().Where(t => t.Id > 1);
             using (var command = context.GetCommand(query))
             {
                 Assert.AreEqual(
                     "SELECT [t0].[Id], [t0].[Discriminator], [t0].[X]" + Environment.NewLine +
                     "FROM [SimpleTable] AS [t0]" + Environment.NewLine +
                     "WHERE [t0].[Id] > @p0",
                     command.CommandText);
             }
         }
     }
 }
        public void Select_LabelIsNotNull_LabelAddedToSqlTextOnce()
        {
            using (var connection = new DbConnectionStub())
                using (var context = new DataContext(connection)
                {
                    StatementsLabel = Label
                })
                {
                    var query = context.GetTable <SimpleEntity>().Where(t => t.Id == 1);
                    using (var command = context.GetCommand(query))
                    {
                        var expectedFirstPart =
                            "SELECT " + Environment.NewLine + $"{LabelWithCommentSymbols}" + Environment.NewLine;

                        Assert.IsTrue(command.CommandText.StartsWith(expectedFirstPart), command.CommandText);

                        AssertThatLabelPresentedInCommandTextOnce(command.CommandText);
                    }
                }
        }
        public void Insert_LabelIsNull_NoLabelComment()
        {
            using (var connection = new DbConnectionStub())
                using (var context = new DataContext(connection)
                {
                    StatementsLabel = null
                })
                {
                    context.GetTable <SimpleEntity>().InsertOnSubmit(new SimpleEntity());

                    var commandText = context.GetChangeText();

                    var expectedFirstPart =
                        "INSERT INTO [SimpleTable]([Id], [Discriminator], [X])"
                        + Environment.NewLine
                        + "VALUES (@p0, @p1, @p2)";

                    Assert.IsTrue(commandText.StartsWith(expectedFirstPart), commandText);
                }
        }
        public void Insert_LabelIsNotNull_LabelAddedToSqlTextOnce()
        {
            using (var connection = new DbConnectionStub())
                using (var context = new DataContext(connection)
                {
                    StatementsLabel = Label
                })
                {
                    context.GetTable <SimpleEntity>().InsertOnSubmit(new SimpleEntity());

                    var commandText = context.GetChangeText();

                    var expectedFirstPart =
                        "INSERT INTO " + Environment.NewLine + $"{LabelWithCommentSymbols}" + Environment.NewLine;

                    Assert.IsTrue(commandText.StartsWith(expectedFirstPart), commandText);

                    AssertThatLabelPresentedInCommandTextOnce(commandText);
                }
        }
        public void Delete_LabelIsNull_NoLabelComment()
        {
            using (var connection = new DbConnectionStub())
                using (var context = new DataContext(connection)
                {
                    StatementsLabel = null
                })
                {
                    var table  = context.GetTable <SimpleEntity>();
                    var entity = new SimpleEntity();

                    table.Attach(entity);
                    table.DeleteOnSubmit(entity);

                    var commandText = context.GetChangeText();

                    var expectedFirstPart = "DELETE FROM [SimpleTable]" + Environment.NewLine;

                    Assert.IsTrue(commandText.StartsWith(expectedFirstPart), commandText);
                }
        }
        public void Update_LabelIsNotNull_LabelAddedToSqlTextOnce()
        {
            using (var connection = new DbConnectionStub())
                using (var context = new DataContext(connection)
                {
                    StatementsLabel = Label
                })
                {
                    var table  = context.GetTable <SimpleEntity>();
                    var entity = new SimpleEntity();

                    table.Attach(entity);
                    entity.X = 10;

                    var commandText = context.GetChangeText();

                    var expectedFirstPart =
                        "UPDATE " + Environment.NewLine + $"{LabelWithCommentSymbols}" + Environment.NewLine;

                    Assert.IsTrue(commandText.StartsWith(expectedFirstPart), commandText);

                    AssertThatLabelPresentedInCommandTextOnce(commandText);
                }
        }