コード例 #1
0
        public void WhenSpecifingToOnlyLogMessagesShouldOnlyLogMessages()
        {
            var logger = new DatabaseLogger(dbHelper.ConnectionString, LogMessageType.Message);

            logger.LogMessage("Error Message", LogMessageType.Error);
            logger.LogMessage("Warning Message", LogMessageType.Warning);
            logger.LogMessage("Info Message", LogMessageType.Message);

            var logRecords = dbHelper.GetLogContent();

            Assert.AreEqual(logRecords.Count(), 1);
            Assert.IsFalse(logRecords.Any(r => r.Type == LogMessageType.Error));
            Assert.IsFalse(logRecords.Any(r => r.Type == LogMessageType.Warning));
            Assert.IsTrue(logRecords.Any(r => r.Type == LogMessageType.Message));
        }
コード例 #2
0
        public void DefaultConstructorShouldLogAllKindOfMessages()
        {
            var logger = new DatabaseLogger(dbHelper.ConnectionString);

            logger.LogMessage("Error Message", LogMessageType.Error);
            logger.LogMessage("Warning Message", LogMessageType.Warning);
            logger.LogMessage("Info Message", LogMessageType.Message);

            var logRecords = dbHelper.GetLogContent();

            Assert.AreEqual(logRecords.Count(), 3);
            Assert.IsTrue(logRecords.Any(r => r.Type == LogMessageType.Error));
            Assert.IsTrue(logRecords.Any(r => r.Type == LogMessageType.Warning));
            Assert.IsTrue(logRecords.Any(r => r.Type == LogMessageType.Message));
        }
コード例 #3
0
        public void LogMessage_NotAllowedMessageType_ThrowMessageTypeNotAllowedException()
        {
            // Arrange
            _levelLoggerMock.Setup(x => x.GetAllowedLevels()).Returns(new List <MessageType>());

            // Act & Assert
            var consoleLogger = new DatabaseLogger(_levelLoggerMock.Object, _databaseLoggerHelperMock.Object);

            consoleLogger.LogMessage(MessageType.Message, "message");
        }
コード例 #4
0
        public void LogMessage_MessageTypeMessageAllowed_MethodToSaveInDBCalled()
        {
            // Arrange
            _levelLoggerMock.Setup(x => x.GetAllowedLevels()).Returns(new List <MessageType> {
                MessageType.Message
            });

            // Act
            var consoleLogger = new DatabaseLogger(_levelLoggerMock.Object, _databaseLoggerHelperMock.Object);

            consoleLogger.LogMessage(MessageType.Message, "message");

            //Assert
            _databaseLoggerHelperMock.Verify(x => x.SaveLog(MessageType.Message, "message"), Times.Exactly(1));
        }
コード例 #5
0
        public void SaveIntoDatabaseTest()
        {
            //set high verbosity
            //hardcode string connection for database
            //build database configuration
            var _connectionString = "Server=localhost\\SQLEXPRESS;Database=tarmacChallenge;Trusted_Connection=true";
            var dbConfig          = new DatabaseConfiguration(_connectionString, "JobLogger");
            var _logger           = new DatabaseLogger(
                Verbosity.VerbosityTypes.high,
                dbConfig
                );

            //messages as error
            var typeOfMessages = TypeOfMessage.error;

            foreach (var msg in _listOfMessages)
            {
                var toWrite         = _logger.BuildMessage(msg, typeOfMessages);
                var resultOperation = _logger.LogMessage(toWrite);
                // check that ran withtou error
                Assert.Equal(0, resultOperation);
            }
        }
コード例 #6
0
        public void CheckContentIntoDatabaseTest()
        {
            var _connectionString = "Server=localhost\\SQLEXPRESS;Database=tarmacChallenge;Trusted_Connection=true";
            var dbConfig          = new DatabaseConfiguration(_connectionString, "JobLogger");
            var _logger           = new DatabaseLogger(
                Verbosity.VerbosityTypes.low,
                dbConfig
                );
            var _messagesToWrite = new List <string>();

            foreach (var msg in _listOfMessages)
            {
                var msgResult = _logger.BuildMessage(msg, TypeOfMessage.message);
                _logger.LogMessage(msgResult);
                _messagesToWrite.Add(msgResult);
            }

            using (var _connection = new SqlConnection(dbConfig.ConnectionString))
            {
                //we take the first message to compare
                var checkMessage = _messagesToWrite.First();
                _connection.Open();
                // we keep only the message that are equal to checkMessage
                var cmdText = string.Format(@"SELECT [message] FROM {0} WHERE message = '{1}'",
                                            dbConfig.InsertTableName,
                                            checkMessage);
                SqlCommand selectCmd = new SqlCommand(cmdText, _connection);

                var result = selectCmd.ExecuteScalar();
                //should exist
                Assert.NotEqual(string.Empty, result);
                // and be equal
                Assert.Equal(checkMessage, result);
                _connection.Close();
            }
        }
コード例 #7
0
        public void LogMessage_ErrorType_Successfully([Frozen] Mock <IConfiguration> configuration, DatabaseLogger databaseLogger, string message)
        {
            configuration.Setup(x => x[It.IsAny <string>()]).Returns(connectionString);

            Assert.Throws <SqlException>(() => databaseLogger.LogMessage(message, LogType.Error));
        }