Exemplo n.º 1
0
        public void Add_inserts_new_record_in_log_table()
        {
            IAgentIdentity agentIdentity = new AgentIdentity
            {
                ID = "agentId" + DateTime.UtcNow.Ticks,
                PathToAgentDataDirectory = _pathToDbDirectory
            };

            SqliteLogRepositoryDecorator logRepository = new SqliteLogRepositoryDecorator(
                agentIdentity,
                new InMemoryLogWriter(100));

            logRepository.Initialize();

            logRepository.Add(new LogEntry(MessageLevel.Verbose, "add test", "should add this row to table"));

            using (SQLiteConnection dbConnection = logRepository.GetNewDbConnection())
            {
                dbConnection.Open();

                string sql = "SELECT * FROM Log";

                using (SQLiteCommand dbCommand = new SQLiteCommand(sql, dbConnection))
                {
                    using (SQLiteDataReader dataReader = dbCommand.ExecuteReader())
                    {
                        Assert.IsTrue(dataReader.HasRows, "Row not added to Log table.");

                        dataReader.Read();

                        object[] columnValues =
                            Enumerable.Range(0, dataReader.FieldCount).Select(dataReader.GetValue).ToArray();

                        Assert.IsTrue(columnValues[1] is DateTime, "'Time' column value incorrect.");
                        Assert.AreEqual(
                            (int)MessageLevel.Verbose,
                            Convert.ToInt32(columnValues[2]),
                            "'Level' column value incorrect.");
                        Assert.AreEqual("add test", columnValues[3], "'Source' column value incorrect.");
                        Assert.AreEqual(
                            "should add this row to table",
                            columnValues[4],
                            "'Message' column value incorrect.");
                    }
                }
            }
        }
Exemplo n.º 2
0
        public void Initialize_adds_log_table_to_db()
        {
            IAgentIdentity agentIdentity = new AgentIdentity
            {
                ID = "agentId" + DateTime.UtcNow.Ticks,
                PathToAgentDataDirectory = _pathToDbDirectory
            };

            SqliteLogRepositoryDecorator logRepository = new SqliteLogRepositoryDecorator(
                agentIdentity,
                new InMemoryLogWriter(100));

            logRepository.Initialize();

            using (SQLiteConnection dbConnection = logRepository.GetNewDbConnection())
            {
                dbConnection.Open();

                bool logTableExists;

                string sql = "SELECT count(name) FROM sqlite_master WHERE name = 'log'";

                using (SQLiteCommand testCommand = new SQLiteCommand(sql, dbConnection))
                {
                    logTableExists = (long)testCommand.ExecuteScalar() > 0;
                }

                Assert.IsTrue(logTableExists, "Log table not created.");

                sql = "select * from log limit 1";

                using (SQLiteCommand testCommand = new SQLiteCommand(sql, dbConnection))
                {
                    using (SQLiteDataReader dataReader = testCommand.ExecuteReader())
                    {
                        string[] columnNames =
                            Enumerable.Range(0, dataReader.FieldCount).Select(dataReader.GetName).ToArray();

                        Assert.IsTrue(columnNames.Contains("ID"), "ID column missing from Log table.");
                        Assert.IsTrue(columnNames.Contains("Time"), "Time column missing from Log table.");
                        Assert.IsTrue(columnNames.Contains("Level"), "Level column missing from Log table.");
                        Assert.IsTrue(columnNames.Contains("Source"), "Source column missing from Log table.");
                        Assert.IsTrue(columnNames.Contains("Message"), "Message column missing from Log table.");
                    }
                }
            }
        }