diff --git a/Source Code/TeamHobby.HobbyProjectGenerator.ArchiveTests/ArchivingTests.cs b/Source Code/TeamHobby.HobbyProjectGenerator.ArchiveTests/ArchivingTests.cs index 7da2223..62d06e3 100644 --- a/Source Code/TeamHobby.HobbyProjectGenerator.ArchiveTests/ArchivingTests.cs +++ b/Source Code/TeamHobby.HobbyProjectGenerator.ArchiveTests/ArchivingTests.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Data.Odbc; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -197,6 +198,75 @@ namespace TeamHobby.HobbyProjectGenerator.ArchiveTests } + public void RemoveEntriesTest(IDataSource sqlDAO) + { + // Arrange + ArchiveManager archiveManager = new ArchiveManager(sqlDAO); + SqlDAO sqlDS = null; + + if (sqlDAO.GetType() == typeof(SqlDAO)) + { + sqlDS = (SqlDAO)sqlDAO; + sqlDS.GetConnection().Open(); + + // Act + Console.WriteLine("Inserting into archive..."); + sqlDS.WriteData("INSERT into log(LtimeStamp, LvName, catname, userop, logmessage) values " + + "('2021-08-07 23:00:00', 'Info', 'View', 'create some projects', 'new account created')," + + "('2021-06-04 23:00:00', 'Info', 'Business', 'create some projects', 'new projects made')," + + "('2021-07-02 23:00:00', 'Info', 'View', 'log out', 'log out successful')," + + "('2021-09-03 23:00:00', 'Info', 'Business', 'log in', 'log in successfully')," + + "('2021-10-20 23:00:00', 'Info', 'View', 'search for projects', 'result return')," + + "('2021-09-03 23:00:00', 'Info', 'Business', 'log in', 'log in successfully');"); + + OdbcDataReader odbcObj = (OdbcDataReader)sqlDS.ReadData("SELECT * FROM log WHERE DATEDIFF(CURRENT_TIMESTAMP, log.LtimeStamp) > 30;"); + + int count = 0; + while (odbcObj.Read()) { ++count; } + Console.WriteLine("Number of entries > 30 days old: " + count); + + Console.WriteLine(""); + Console.WriteLine("Removing entries from archive..."); + sqlDS.RemoveEntries(); + + odbcObj = (OdbcDataReader)sqlDS.ReadData("SELECT * FROM log WHERE DATEDIFF(CURRENT_TIMESTAMP, log.LtimeStamp) > 30;"); + + count = 0; + while (odbcObj.Read()) { ++count; } + Console.WriteLine("Number of entries > 30 days old: " + count); + + // Assert + bool expectedVal = true; + bool actualVal = !Convert.ToBoolean(count); + + if (actualVal) + { + Console.WriteLine("Expected: {0}, Actual: {0}. Entries removed correctly.", expectedVal, actualVal); + } + else + { + Console.WriteLine("Expected: {0}, Actual: {1}. Entries not removed.", expectedVal, actualVal); + } + + sqlDS.GetConnection().Close(); + } + + return; + } + + public void RemoveOutputFileTest(IDataSource sqlDAO) + { + // Arrange + ArchiveManager archiveManager = new ArchiveManager(sqlDAO); + + // Act + + + // Assert + + return; + } + public static void Main(string[] args) { string dbInfo = "DRIVER={MariaDB ODBC 3.1 Driver};" + @@ -226,12 +296,21 @@ namespace TeamHobby.HobbyProjectGenerator.ArchiveTests Console.WriteLine(""); - // Testing clean up sequence //test.IsCleaningUpCompleted(sqlDAO); //Console.WriteLine("-----------------"); //Console.WriteLine(""); + // Testing remove entries + test.RemoveEntriesTest(sqlDAO); + Console.WriteLine("-----------------"); + Console.WriteLine(""); + + // Testing remove output file + test.RemoveOutputFileTest(sqlDAO); + Console.WriteLine("-----------------"); + Console.WriteLine(""); + } } diff --git a/Source Code/TeamHobby.HobbyProjectGenerator.DataAccess/Implementations/SqlDAO.cs b/Source Code/TeamHobby.HobbyProjectGenerator.DataAccess/Implementations/SqlDAO.cs index e3b7356..6eee8c8 100644 --- a/Source Code/TeamHobby.HobbyProjectGenerator.DataAccess/Implementations/SqlDAO.cs +++ b/Source Code/TeamHobby.HobbyProjectGenerator.DataAccess/Implementations/SqlDAO.cs @@ -42,7 +42,7 @@ namespace TeamHobby.HobbyProjectGenerator.DataAccess { try { - _conn.Open(); + //_conn.Open(); OdbcCommand command = new OdbcCommand(cmd, _conn); OdbcDataReader reader = command.ExecuteReader(); @@ -67,12 +67,12 @@ namespace TeamHobby.HobbyProjectGenerator.DataAccess { try { - _conn.Open(); - + //_conn.Open(); + OdbcCommand command = new OdbcCommand(cmd, _conn); command.ExecuteNonQuery(); - - _conn.Close(); + + //_conn.Close(); return true; } @@ -84,7 +84,7 @@ namespace TeamHobby.HobbyProjectGenerator.DataAccess } finally { - _conn.Close(); + //_conn.Close(); } } @@ -120,12 +120,12 @@ namespace TeamHobby.HobbyProjectGenerator.DataAccess { try { - _conn.Open(); + //_conn.Open(); OdbcCommand command = new OdbcCommand(cmd, _conn); command.ExecuteNonQuery(); - _conn.Close(); + //_conn.Close(); return true; } @@ -137,7 +137,7 @@ namespace TeamHobby.HobbyProjectGenerator.DataAccess } finally { - _conn.Close(); + //_conn.Close(); } }