예제 #1
0
 private static long GetElapsed(DbCommandLogger logger)
 {
     logger.Stopwatch.Restart();
     Thread.Sleep(10);
     logger.Stopwatch.Stop();
     return(logger.Stopwatch.ElapsedMilliseconds);
 }
예제 #2
0
            public void LogResult_logs_elapsed_time_for_completed_commands()
            {
                var writer  = new StringWriter();
                var logger  = new DbCommandLogger(writer.Write);
                var elapsed = GetElapsed(logger);

                logger.LogResult(new Mock <DbCommand>().Object, 77, new DbCommandInterceptionContext <int>());

                Assert.Equal(Strings.CommandLogComplete(elapsed, "77", ""), GetSingleLine(writer));
            }
예제 #3
0
            public void LogCommand_validates_arguments()
            {
                var logger = new DbCommandLogger(new StringWriter().Write);

                Assert.Equal(
                    "command",
                    Assert.Throws <ArgumentNullException>(() => logger.LogCommand(null, new DbCommandInterceptionContext <int>())).ParamName);
                Assert.Equal(
                    "interceptionContext",
                    Assert.Throws <ArgumentNullException>(() => logger.LogCommand(new Mock <DbCommand>().Object, null)).ParamName);
            }
예제 #4
0
            public void Executed_filters_by_context_when_set()
            {
                var context1 = new Mock <DbContext>().Object;
                var context2 = new Mock <DbContext>().Object;

                var writer = new StringWriter();
                var logger = new DbCommandLogger(writer.Write);

                logger.Executed(CreateCommand(""), "Sam-I-am", new DbCommandInterceptionContext <int>().WithDbContext(context1));
                logger.Executed(CreateCommand(""), "I do not like", new DbCommandInterceptionContext <int>().WithDbContext(context2));
                logger.Executed(CreateCommand(""), "Green eggs and ham", new DbCommandInterceptionContext <int>());

                Assert.Equal(Strings.CommandLogComplete(0, "Sam-I-am", ""), GetSingleLine(writer));
            }
예제 #5
0
            public void Executing_filters_by_context_when_set()
            {
                var context1 = new Mock <DbContext>().Object;
                var context2 = new Mock <DbContext>().Object;

                var writer = new StringWriter();
                var logger = new DbCommandLogger(writer.Write);

                logger.Executing(CreateCommand("That Sam-I-am!"), new DbCommandInterceptionContext <int>().WithDbContext(context1));
                logger.Executing(CreateCommand("I do not like"), new DbCommandInterceptionContext <int>().WithDbContext(context2));
                logger.Executing(CreateCommand("that Sam-I-am"), new DbCommandInterceptionContext <int>());

                Assert.Equal("That Sam-I-am!", GetSingleLine(writer));
            }
예제 #6
0
            public void LogResult_logs_elapsed_time_for_canceled_commands()
            {
                var writer  = new StringWriter();
                var logger  = new DbCommandLogger(writer.Write);
                var elapsed = GetElapsed(logger);

                var interceptionContext = new DbCommandInterceptionContext <int>();

                interceptionContext.MutableData.TaskStatus = TaskStatus.Canceled;

                logger.LogResult(
                    new Mock <DbCommand>().Object, 77, interceptionContext);

                Assert.Equal(Strings.CommandLogCanceled(elapsed, ""), GetSingleLine(writer));
            }
예제 #7
0
            public void LogResult_logs_elapsed_time_for_failed_commands()
            {
                var writer  = new StringWriter();
                var logger  = new DbCommandLogger(writer.Write);
                var elapsed = GetElapsed(logger);

                logger.LogResult(
                    new Mock <DbCommand>().Object,
                    77,
                    new DbCommandInterceptionContext <int> {
                    Exception = new Exception("I do not like them!")
                });

                Assert.Equal(Strings.CommandLogFailed(elapsed, "I do not like them!", ""), GetSingleLine(writer));
            }
예제 #8
0
            public void Executed_logs_every_command_when_context_not_set()
            {
                var context1 = new Mock <DbContext>().Object;
                var context2 = new Mock <DbContext>().Object;

                var writer = new StringWriter();
                var logger = new DbCommandLogger(writer.Write);

                logger.Executed(CreateCommand(""), "Would you like them", new DbCommandInterceptionContext <int>());
                logger.Executed(CreateCommand(""), "Here or there?", new DbCommandInterceptionContext <int>().WithDbContext(context1));
                logger.Executed(CreateCommand(""), "I would not like them", new DbCommandInterceptionContext <int>().WithDbContext(context2));

                var lines = GetLines(writer);

                Assert.Equal(Strings.CommandLogComplete(0, "Would you like them", ""), lines[0]);
                Assert.Equal(Strings.CommandLogComplete(0, "Here or there?", ""), lines[2]);
                Assert.Equal(Strings.CommandLogComplete(0, "I would not like them", ""), lines[4]);
            }
예제 #9
0
            public void Executing_logs_every_command_when_context_not_set()
            {
                var context1 = new Mock <DbContext>().Object;
                var context2 = new Mock <DbContext>().Object;

                var writer = new StringWriter();
                var logger = new DbCommandLogger(writer.Write);

                logger.Executing(CreateCommand("Do you like"), new DbCommandInterceptionContext <int>());
                logger.Executing(CreateCommand("Green eggs and ham?"), new DbCommandInterceptionContext <int>().WithDbContext(context1));
                logger.Executing(CreateCommand("I do not like them"), new DbCommandInterceptionContext <int>().WithDbContext(context2));

                var lines = GetLines(writer);

                Assert.Equal("Do you like", lines[0]);
                Assert.Equal("Green eggs and ham?", lines[2]);
                Assert.Equal("I do not like them", lines[4]);
            }