From 03cfb10c1042679bea06e5eb4b4e9aa71b4adf3e Mon Sep 17 00:00:00 2001 From: Lunastra Date: Tue, 14 Dec 2021 20:37:43 -0800 Subject: [PATCH] Translate the test case from main over Revert back to open and close design. Move test cases to unit test. --- .../ArchiveManager.cs | 23 --- .../Implementations/SqlDAO.cs | 28 +-- .../ArchivingXUnitTest.cs | 178 ++++++++++++++++++ .../TeamHobby.Archiving.xTests.csproj | 27 +++ .../TeamHobby.HobbyProjectGenerator.sln | 16 +- 5 files changed, 225 insertions(+), 47 deletions(-) create mode 100644 Source Code/TeamHobby.HobbyProjectGenerator/TeamHobby.Archiving.xTests/ArchivingXUnitTest.cs create mode 100644 Source Code/TeamHobby.HobbyProjectGenerator/TeamHobby.Archiving.xTests/TeamHobby.Archiving.xTests.csproj diff --git a/Source Code/TeamHobby.HobbyProjectGenerator.Archive/ArchiveManager.cs b/Source Code/TeamHobby.HobbyProjectGenerator.Archive/ArchiveManager.cs index 2eee2fe..051ec59 100644 --- a/Source Code/TeamHobby.HobbyProjectGenerator.Archive/ArchiveManager.cs +++ b/Source Code/TeamHobby.HobbyProjectGenerator.Archive/ArchiveManager.cs @@ -72,29 +72,6 @@ namespace TeamHobby.HobbyProjectGenerator.Archive } } - // Method overload to take in a specific path - public string CreateOutFileName(string archivePath) - { - try - { - //string path = archivePath + "\\HobbyArchive"; - Console.WriteLine("The current directory is {0}", archivePath); - //string date = DateTime.Now.ToString("M_d_yyyy_H:mm:ss"); - string date = DateTime.Now.ToString("M_d_yyyy"); - string fileName = date + "_archive.csv"; - string filePath = Path.Combine(archivePath, fileName); - - Console.WriteLine("Date: {0}", date); - Console.WriteLine("Filepath: {0}", filePath); - return filePath; - } - catch - { - Console.WriteLine("ArchiveCon: Creating a file name failed. "); - return ""; - } - } - // put everything toget here public bool Controller(){ diff --git a/Source Code/TeamHobby.HobbyProjectGenerator.DataAccess/Implementations/SqlDAO.cs b/Source Code/TeamHobby.HobbyProjectGenerator.DataAccess/Implementations/SqlDAO.cs index 90f8416..127d03e 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(); } } @@ -94,12 +94,12 @@ namespace TeamHobby.HobbyProjectGenerator.DataAccess { try { - //_conn.Open(); + _conn.Open(); OdbcCommand command = new OdbcCommand(cmd, _conn); command.ExecuteNonQuery(); - //_conn.Close(); + _conn.Close(); return true; } @@ -111,7 +111,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(); } } @@ -177,7 +177,7 @@ namespace TeamHobby.HobbyProjectGenerator.DataAccess public bool CopyToFile(string filePath){ try { - //_conn.Open(); + _conn.Open(); // Conver backward slash in to forward slash filePath = filePath.Replace("\\", "/"); string sqlQuery = "SELECT 'LtimeStamp', 'logID', 'LvName', 'catName', 'userOP', 'logMessage' " + @@ -200,19 +200,19 @@ namespace TeamHobby.HobbyProjectGenerator.DataAccess Console.WriteLine("Output to a file completed. "); - //_conn.Close(); + _conn.Close(); return true; } catch (Exception ex) { - //_conn.Close(); + _conn.Close(); Console.WriteLine("Error when copying query to a file !!, will be handled higher up the call stack"); throw; } finally { - //_conn.Close(); + _conn.Close(); } } diff --git a/Source Code/TeamHobby.HobbyProjectGenerator/TeamHobby.Archiving.xTests/ArchivingXUnitTest.cs b/Source Code/TeamHobby.HobbyProjectGenerator/TeamHobby.Archiving.xTests/ArchivingXUnitTest.cs new file mode 100644 index 0000000..71b5de3 --- /dev/null +++ b/Source Code/TeamHobby.HobbyProjectGenerator/TeamHobby.Archiving.xTests/ArchivingXUnitTest.cs @@ -0,0 +1,178 @@ +using System; +using System.Data.Odbc; +using System.IO; +using TeamHobby.HobbyProjectGenerator.Archive; +using TeamHobby.HobbyProjectGenerator.DataAccess; +using Xunit; + +namespace TeamHobby.Archiving.xTests +{ + public class ArchivingXUnitTest + { + string dbInfo = "DRIVER={MariaDB ODBC 3.1 Driver};" + + "SERVER=localhost;" + + "DATABASE=hobby;" + + "UID=root;" + + "PASSWORD=Teamhobby;" + + "OPTION=3"; + + [Fact] + public void IsArchiveFolderCreated() + { + // Arrange + SqlDAO sqlDAO = new SqlDAO(dbInfo); + ArchiveManager archiveManager = new ArchiveManager(sqlDAO); + string folderPath = @"C:/HobbyArchive"; + bool expectedVal = true; + + // Act + archiveManager.CreateArchiveFolder(); + + + // Assert + bool actualVal = Directory.Exists(folderPath); + Assert.Equal(expectedVal, actualVal); + // Cleaning up directory after test + try + { + Directory.Delete(folderPath, true); + } + catch + { + Console.WriteLine("Folder failed to be deleted"); + } + } + + [Fact] + public void IsFileNameCreated() + { + // Arrange + SqlDAO sqlDAO = new SqlDAO(dbInfo); + string foldPath = @"C:\HobbyArchive"; + string fileName = DateTime.Now.ToString("M_d_yyyy") + "_archive.csv"; + string expectedFilePath = System.IO.Path.Combine(foldPath, fileName); + ArchiveManager archiveManager = new ArchiveManager(sqlDAO); + + // Act + string actualFilePath = archiveManager.CreateOutFileName(); + + // Assert + + Assert.Equal(expectedFilePath, actualFilePath); + } + + [Fact] + public void IsCSVFileExist() + { + // Arrange + SqlDAO sqlDAO = new SqlDAO(dbInfo); + ArchiveManager archiveManager = new ArchiveManager(sqlDAO); + SqlDAO sqlDS = null; + string folderPath = @"C:/HobbyArchive"; + + try + { + bool folderRes = archiveManager.CreateArchiveFolder(); + } + catch (Exception ex) + { + Console.WriteLine("Folder creation for copying failed"); + } + string filePath = archiveManager.CreateOutFileName(); + bool expectedVal = true; + + if (sqlDAO.GetType() == typeof(SqlDAO)) + { + sqlDS = (SqlDAO)sqlDAO; + } + + // Act + if (sqlDS != null) + { + try + { + sqlDS.CopyToFile(filePath); + } + catch (Exception ex) + { + Console.WriteLine("Copying to a file failed !!"); + Console.WriteLine(ex.Message); + } + } + + // Assert + bool actualVal = File.Exists(filePath); + Assert.Equal(expectedVal, actualVal); + + try + { + Directory.Delete(folderPath, true); + } + catch + { + Console.WriteLine("Folder failed to be deleted"); + } + } + + [Fact] + public void RemoveEntriesTest() + { + // Arrange + SqlDAO sqlDAO = new SqlDAO(dbInfo); + ArchiveManager archiveManager = new ArchiveManager(sqlDAO); + OdbcDataReader odbcObj = null; + Object resultData = null; + bool expectedVal = false; + + sqlDAO.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'),"); + + // Act + sqlDAO.RemoveEntries(); + + // Assert + bool actualVal; + + resultData = sqlDAO.ReadData("SELECT * FROM log WHERE DATEDIFF(CURRENT_TIMESTAMP, log.LtimeStamp) > 30;"); + if ((resultData != null) && (resultData.GetType() == typeof(OdbcDataReader))) + { + odbcObj = (OdbcDataReader)resultData; + } + OdbcConnection conn = sqlDAO.GetConnection(); + actualVal = odbcObj.HasRows; + conn.Close(); + + Assert.Equal(expectedVal, actualVal); + + } + + + [Fact] + public void RemoveOutputFileTest() + { + // Arrange + SqlDAO sqlDAO = new SqlDAO(dbInfo); + ArchiveManager archiveManager = new ArchiveManager(sqlDAO); + bool expectedVal = false; + + // Create the folder and the csv file for compressing + archiveManager.CreateArchiveFolder(); + string filepath = archiveManager.CreateOutFileName(); + sqlDAO.CopyToFile(filepath); + + // Act + if (sqlDAO != null) + { + sqlDAO.RemoveOutputFile(filepath); + } + + // Assert + bool actualVal = File.Exists(filepath); + Assert.Equal(expectedVal, actualVal); + } + } +} \ No newline at end of file diff --git a/Source Code/TeamHobby.HobbyProjectGenerator/TeamHobby.Archiving.xTests/TeamHobby.Archiving.xTests.csproj b/Source Code/TeamHobby.HobbyProjectGenerator/TeamHobby.Archiving.xTests/TeamHobby.Archiving.xTests.csproj new file mode 100644 index 0000000..308e40a --- /dev/null +++ b/Source Code/TeamHobby.HobbyProjectGenerator/TeamHobby.Archiving.xTests/TeamHobby.Archiving.xTests.csproj @@ -0,0 +1,27 @@ + + + + net6.0 + enable + + false + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + diff --git a/Source Code/TeamHobby.HobbyProjectGenerator/TeamHobby.HobbyProjectGenerator.sln b/Source Code/TeamHobby.HobbyProjectGenerator/TeamHobby.HobbyProjectGenerator.sln index 62b8c20..8ab06bf 100644 --- a/Source Code/TeamHobby.HobbyProjectGenerator/TeamHobby.HobbyProjectGenerator.sln +++ b/Source Code/TeamHobby.HobbyProjectGenerator/TeamHobby.HobbyProjectGenerator.sln @@ -30,6 +30,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution TeamHobby.HobbyProjectGenerator.csproj = TeamHobby.HobbyProjectGenerator.csproj EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TeamHobby.Archiving.xTests", "TeamHobby.Archiving.xTests\TeamHobby.Archiving.xTests.csproj", "{8C039F49-13D3-4AEE-A1C3-A880751852BB}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -51,17 +53,10 @@ Global {B88ED0D9-72E2-4245-BD8F-856FF42E500C}.Debug|Any CPU.Build.0 = Debug|Any CPU {B88ED0D9-72E2-4245-BD8F-856FF42E500C}.Release|Any CPU.ActiveCfg = Release|Any CPU {B88ED0D9-72E2-4245-BD8F-856FF42E500C}.Release|Any CPU.Build.0 = Release|Any CPU - {ED126EFB-B337-42F9-BE4B-65A5AE90503B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {ED126EFB-B337-42F9-BE4B-65A5AE90503B}.Release|Any CPU.ActiveCfg = Release|Any CPU - {ED126EFB-B337-42F9-BE4B-65A5AE90503B}.Release|Any CPU.Build.0 = Release|Any CPU {AA48A66C-FA36-4AF9-A782-CEC22838EB8F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {AA48A66C-FA36-4AF9-A782-CEC22838EB8F}.Debug|Any CPU.Build.0 = Debug|Any CPU {AA48A66C-FA36-4AF9-A782-CEC22838EB8F}.Release|Any CPU.ActiveCfg = Release|Any CPU {AA48A66C-FA36-4AF9-A782-CEC22838EB8F}.Release|Any CPU.Build.0 = Release|Any CPU - {C0494115-838E-43A3-8896-133E5D1E874A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {C0494115-838E-43A3-8896-133E5D1E874A}.Debug|Any CPU.Build.0 = Debug|Any CPU - {C0494115-838E-43A3-8896-133E5D1E874A}.Release|Any CPU.ActiveCfg = Release|Any CPU - {C0494115-838E-43A3-8896-133E5D1E874A}.Release|Any CPU.Build.0 = Release|Any CPU {2E7193B8-86B6-48DA-9671-CD84615A5F5D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {2E7193B8-86B6-48DA-9671-CD84615A5F5D}.Debug|Any CPU.Build.0 = Debug|Any CPU {2E7193B8-86B6-48DA-9671-CD84615A5F5D}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -74,13 +69,14 @@ Global {C5EBD1F8-C806-4BF9-B2D7-8876072630FD}.Debug|Any CPU.Build.0 = Debug|Any CPU {C5EBD1F8-C806-4BF9-B2D7-8876072630FD}.Release|Any CPU.ActiveCfg = Release|Any CPU {C5EBD1F8-C806-4BF9-B2D7-8876072630FD}.Release|Any CPU.Build.0 = Release|Any CPU - {C5EBD1F8-C806-4BF9-B2D7-8876072630FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {C5EBD1F8-C806-4BF9-B2D7-8876072630FD}.Release|Any CPU.ActiveCfg = Release|Any CPU - {C5EBD1F8-C806-4BF9-B2D7-8876072630FD}.Release|Any CPU.Build.0 = Release|Any CPU {6D575AF1-C138-44C5-B701-5AEC4ACEAA7A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {6D575AF1-C138-44C5-B701-5AEC4ACEAA7A}.Debug|Any CPU.Build.0 = Debug|Any CPU {6D575AF1-C138-44C5-B701-5AEC4ACEAA7A}.Release|Any CPU.ActiveCfg = Release|Any CPU {6D575AF1-C138-44C5-B701-5AEC4ACEAA7A}.Release|Any CPU.Build.0 = Release|Any CPU + {8C039F49-13D3-4AEE-A1C3-A880751852BB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8C039F49-13D3-4AEE-A1C3-A880751852BB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8C039F49-13D3-4AEE-A1C3-A880751852BB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8C039F49-13D3-4AEE-A1C3-A880751852BB}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE