Translate the test case from main over

Revert back to open and close design.
Move test cases to unit test.
This commit is contained in:
Lunastra 2021-12-14 20:37:43 -08:00
parent a2c5e0a1f9
commit 03cfb10c10
5 changed files with 225 additions and 47 deletions

View File

@ -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(){

View File

@ -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();
}
}

View File

@ -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);
}
}
}

View File

@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\TeamHobby.HobbyProjectGenerator.Archive\TeamHobby.HobbyProjectGenerator.Archive.csproj" />
</ItemGroup>
</Project>

View File

@ -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