Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #11 from Oskarowski/dev-task-2
Browse files Browse the repository at this point in the history
Task 2 - 0
  • Loading branch information
Oskarowski authored May 27, 2024
2 parents 7d12107 + d7765a9 commit 0d933b7
Show file tree
Hide file tree
Showing 141 changed files with 7,729 additions and 796 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,6 @@ ServiceFabricBackup/
*.rptproj.bak

# SQL Server files
*.mdf
*.ldf
*.ndf

Expand Down Expand Up @@ -415,4 +414,6 @@ FodyWeavers.xsd
# Built Visual Studio Code Extensions
*.vsix

test_results.txt
test_results.txt
/Library/Tests/Instrumentation/Library.mdf
/run_tests.sh
114 changes: 114 additions & 0 deletions DataLayerTests/DataLayerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using DataLayer.API;
using Microsoft.Data.SqlClient;

namespace DataLayerTests
{
[TestClass]
[DeploymentItem("MockDB.mdf")] // DB is copied to the deployment directory where the test is executed.
public class DataLayerTests
{
private static string connectionString;

// private readonly IDataRepository _dataRepository = IDataRepository.NewInstance(IDataContext.NewInstance(connectionString));

private IDataRepository _dataRepository;


[ClassInitialize]
public static void ClassInitializeMethod(TestContext context)
{
string _DBRelativePath = @"MockDB.mdf";
string _projectRootDir = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName;
string _DBPath = Path.Combine(_projectRootDir, _DBRelativePath);
FileInfo _databaseFile = new FileInfo(_DBPath);
Assert.IsTrue(_databaseFile.Exists, $"{Environment.CurrentDirectory}");

connectionString = $@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename={_DBPath};Integrated Security = True; Connect Timeout = 30;";
}

[TestInitialize]
public void TestInitialize()
{
IDataContext dataContext = IDataContext.NewInstance(connectionString);
_dataRepository = IDataRepository.NewInstance(dataContext);
}

[TestMethod]
public void TestDatabaseConnection()
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open();
Assert.IsTrue(connection.State == System.Data.ConnectionState.Open, "Connection to the database failed.");
}
catch (Exception ex)
{
Assert.Fail($"Exception occurred while connecting to the database: {ex.Message}");
}
}
}

[TestMethod]
public async Task UserTests()
{
string userGuid = "cfaf2913-3677-4c56-a1cd-fa1647";

// Add a new user
string firstName = "John";
string lastName = "Wick";
string email = "johnwick@continental.com";
double balance = 1000000;
string phoneNumber = "315-194-6020";
await _dataRepository.AddUserAsync(userGuid, firstName, lastName, email, balance, phoneNumber);

IUser retrivedUser = await _dataRepository.GetUserAsync(userGuid);

// Assert that the user is correctly retrieved and the data is correct
Assert.IsNotNull(retrivedUser);
Assert.AreEqual(userGuid, retrivedUser.Guid);
Assert.AreEqual(firstName, retrivedUser.FirstName);
Assert.AreEqual(email, retrivedUser.Email);
Assert.AreEqual(balance, retrivedUser.Balance);
Assert.AreEqual(phoneNumber, retrivedUser.PhoneNumber);

Assert.IsNotNull(await _dataRepository.GetAllUsersAsync());
Assert.IsTrue(await _dataRepository.GetUsersCountAsync() > 0);

string notExistingUserGuid = "afaf2913-1234-4c56-a1cd-fa1647";
await Assert.ThrowsExceptionAsync<Exception>(async () => await _dataRepository.GetUserAsync(notExistingUserGuid));

// John Wick moves to a new place to start a new life
string newFirstName = "Tom";
string newLastName = "Hanks";
string newEmail = "notjohnwick@continental.com";
double newBalance = 9000000;
string newPhoneNumber = "1-951-239-0523";
await _dataRepository.UpdateUserAsync(userGuid, newFirstName, newLastName, newEmail, newBalance, newPhoneNumber);

// Retrieve the updated data of now not John Wick
IUser updatedUser = await _dataRepository.GetUserAsync(userGuid);

// Assert that the the identity change was successful
Assert.IsNotNull(updatedUser);
Assert.AreEqual(newFirstName, updatedUser.FirstName);
Assert.AreEqual(newLastName, updatedUser.LastName);
Assert.AreEqual(newEmail, updatedUser.Email);
Assert.AreEqual(newBalance, updatedUser.Balance);
Assert.AreEqual(newPhoneNumber, updatedUser.PhoneNumber);

await Assert.ThrowsExceptionAsync<Exception>(async () => await _dataRepository.UpdateUserAsync(notExistingUserGuid,
"John", "Doe", "johndow@wp.pl", 666, "123321123"));

// John Wick disappears from the system
await _dataRepository.DeleteUserAsync(userGuid);

// Assert that an exception is thrown when trying to retrieve the deleted user
await Assert.ThrowsExceptionAsync<Exception>(async () => await _dataRepository.GetUserAsync(userGuid));

// Assert that an exception is thrown when trying to delete the already deleted user
await Assert.ThrowsExceptionAsync<Exception>(async () => await _dataRepository.DeleteUserAsync(userGuid));
}
}
}
28 changes: 28 additions & 0 deletions DataLayerTests/DataLayerTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="3.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Library\DataLayer\DataLayer.csproj" />
</ItemGroup>

<ItemGroup>
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
</ItemGroup>


</Project>
Binary file added DataLayerTests/MockDB.mdf
Binary file not shown.
53 changes: 38 additions & 15 deletions Library.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.002.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataLayer", "Library\DataLayer\DataLayer.csproj", "{37F35CAD-0E16-44DE-B1EC-B8806B87AF27}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Library\Tests\Tests.csproj", "{E482E32E-3CA7-46DD-83C9-B094E0AD8BA3}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DataLayer", "Library\DataLayer\DataLayer.csproj", "{37F35CAD-0E16-44DE-B1EC-B8806B87AF27}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Library", "Library", "{B858A003-F5DD-4F95-99D9-5A7560A0B8CF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LogicLayer", "Library\LogicLayer\LogicLayer.csproj", "{34D82E95-A0C6-4C12-8BB0-6836280E2491}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Presentation", "Library\Presentation\Presentation.csproj", "{C69FD601-389B-4CD1-A1FF-899261179F05}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ServiceLayerTests", "Library\ServiceLayerTests\ServiceLayerTests.csproj", "{0AB5386A-930A-4A24-A6E5-ED00D8E5355E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DataLayerTests", "DataLayerTests\DataLayerTests.csproj", "{66C9D42F-4CBD-474F-B4FB-5FDFAB58C2A1}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Service", "Library\Service\Service.csproj", "{AD1B47F1-3FB2-41A7-9C6C-41F294FF835E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PresentationLayerTests", "Library\PresentationLayerTests\PresentationLayerTests.csproj", "{D30BB53A-B80E-45CF-B3F1-B8C39A8C849D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -21,22 +27,39 @@ Global
{37F35CAD-0E16-44DE-B1EC-B8806B87AF27}.Debug|Any CPU.Build.0 = Debug|Any CPU
{37F35CAD-0E16-44DE-B1EC-B8806B87AF27}.Release|Any CPU.ActiveCfg = Release|Any CPU
{37F35CAD-0E16-44DE-B1EC-B8806B87AF27}.Release|Any CPU.Build.0 = Release|Any CPU
{E482E32E-3CA7-46DD-83C9-B094E0AD8BA3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E482E32E-3CA7-46DD-83C9-B094E0AD8BA3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E482E32E-3CA7-46DD-83C9-B094E0AD8BA3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E482E32E-3CA7-46DD-83C9-B094E0AD8BA3}.Release|Any CPU.Build.0 = Release|Any CPU
{34D82E95-A0C6-4C12-8BB0-6836280E2491}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{34D82E95-A0C6-4C12-8BB0-6836280E2491}.Debug|Any CPU.Build.0 = Debug|Any CPU
{34D82E95-A0C6-4C12-8BB0-6836280E2491}.Release|Any CPU.ActiveCfg = Release|Any CPU
{34D82E95-A0C6-4C12-8BB0-6836280E2491}.Release|Any CPU.Build.0 = Release|Any CPU
{C69FD601-389B-4CD1-A1FF-899261179F05}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C69FD601-389B-4CD1-A1FF-899261179F05}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C69FD601-389B-4CD1-A1FF-899261179F05}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C69FD601-389B-4CD1-A1FF-899261179F05}.Release|Any CPU.Build.0 = Release|Any CPU
{0AB5386A-930A-4A24-A6E5-ED00D8E5355E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0AB5386A-930A-4A24-A6E5-ED00D8E5355E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0AB5386A-930A-4A24-A6E5-ED00D8E5355E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0AB5386A-930A-4A24-A6E5-ED00D8E5355E}.Release|Any CPU.Build.0 = Release|Any CPU
{66C9D42F-4CBD-474F-B4FB-5FDFAB58C2A1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{66C9D42F-4CBD-474F-B4FB-5FDFAB58C2A1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{66C9D42F-4CBD-474F-B4FB-5FDFAB58C2A1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{66C9D42F-4CBD-474F-B4FB-5FDFAB58C2A1}.Release|Any CPU.Build.0 = Release|Any CPU
{AD1B47F1-3FB2-41A7-9C6C-41F294FF835E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AD1B47F1-3FB2-41A7-9C6C-41F294FF835E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AD1B47F1-3FB2-41A7-9C6C-41F294FF835E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AD1B47F1-3FB2-41A7-9C6C-41F294FF835E}.Release|Any CPU.Build.0 = Release|Any CPU
{D30BB53A-B80E-45CF-B3F1-B8C39A8C849D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D30BB53A-B80E-45CF-B3F1-B8C39A8C849D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D30BB53A-B80E-45CF-B3F1-B8C39A8C849D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D30BB53A-B80E-45CF-B3F1-B8C39A8C849D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{37F35CAD-0E16-44DE-B1EC-B8806B87AF27} = {B858A003-F5DD-4F95-99D9-5A7560A0B8CF}
{C69FD601-389B-4CD1-A1FF-899261179F05} = {B858A003-F5DD-4F95-99D9-5A7560A0B8CF}
{0AB5386A-930A-4A24-A6E5-ED00D8E5355E} = {B858A003-F5DD-4F95-99D9-5A7560A0B8CF}
{66C9D42F-4CBD-474F-B4FB-5FDFAB58C2A1} = {B858A003-F5DD-4F95-99D9-5A7560A0B8CF}
{AD1B47F1-3FB2-41A7-9C6C-41F294FF835E} = {B858A003-F5DD-4F95-99D9-5A7560A0B8CF}
{D30BB53A-B80E-45CF-B3F1-B8C39A8C849D} = {B858A003-F5DD-4F95-99D9-5A7560A0B8CF}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B8DC2F37-8FC7-433C-BA78-56CAE077B47B}
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{34D82E95-A0C6-4C12-8BB0-6836280E2491} = {B858A003-F5DD-4F95-99D9-5A7560A0B8CF}
EndGlobalSection
EndGlobal
59 changes: 55 additions & 4 deletions Library/DataLayer/API/IDataContext.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,64 @@
using DataLayer.Implementations;

namespace DataLayer.API
{
public interface IDataContext
{
public List<IUser> Users { get; set; }
static IDataContext NewInstance(string? connectionString = null) {
return new DataContext(connectionString);
}

#region User CRUD

Task AddUserAsync(IUser user);
Task<IUser?> GetUserAsync(string guid);
Task UpdateUserAsync(IUser user);
Task DeleteUserAsync(string guid);
Task<Dictionary<string, IUser>> GetAllUsersAsync();
Task<int> GetUsersCountAsync();

#endregion User CRUD

#region Product CRUD

Task AddProductAsync(IBook product);
Task<IBook?> GetProductAsync(string guid);
Task UpdateProductAsync(IBook product);
Task DeleteProductAsync(string guid);
Task<Dictionary<string, IBook>> GetAllProductsAsync();
Task<int> GetProductsCountAsync();

#endregion

#region State CRUD

Task AddStateAsync(IState state);
Task<IState?> GetStateAsync(string guid);
Task UpdateStateAsync(IState state);
Task DeleteStateAsync(string guid);
Task<Dictionary<string, IState>> GetAllStatesAsync();
Task<int> GetStatesCountAsync();

#endregion

#region Event CRUD

Task AddEventAsync(IEvent even);
Task<IEvent?> GetEventAsync(string guid);
Task UpdateEventAsync(IEvent even);
Task DeleteEventAsync(string guid);
Task<Dictionary<string, IEvent>> GetAllEventsAsync();
Task<int> GetEventsCountAsync();

#endregion

public List<IProduct> Products { get; set; }
#region Helpers

public List<IEvent> Events { get; set; }
Task<bool> CheckIfUserExists(string guid);
Task<bool> CheckIfProductExists(string guid);
Task<bool> CheckIfStateExists(string guid);
Task<bool> CheckIfEventExists(string guid, string type);

public List<IState> States { get; set; }
#endregion
}
}
77 changes: 49 additions & 28 deletions Library/DataLayer/API/IDataRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,63 @@ namespace DataLayer.API
{
public interface IDataRepository
{
static IDataRepository NewInstance(IDataContext? dataContext = null)
{
return new DataRepository(dataContext ?? DataContext.NewInstance());
}

public void Seed(IDataFiller dataSeeder);

#region User
void AddUser(IUser user);
IUser GetUser(string guid);
List<IUser> GetAllUsers();
void RemoveUser(string guid);
bool DoesUserExist(string guid);
void UpdateUser(IUser updateUser);
#endregion
static IDataRepository CreateDatabase(IDataContext? dataContext = null)
{
return DataRepository.NewInstance(dataContext ?? new DataContext());
}

#region User CRUD

Task AddUserAsync(string guid, string firstName, string lastName, string email, double balance, string phoneNumber);
Task<IUser> GetUserAsync(string guid);
Task UpdateUserAsync(string guid, string firstName, string lastName, string email, double balance, string phoneNumber);
Task DeleteUserAsync(string guid);
Task<Dictionary<string, IUser>> GetAllUsersAsync();
Task<int> GetUsersCountAsync();

#endregion User CRUD


#region Product CRUD

#region Product
void AddProduct(IProduct product);
IProduct GetProduct(string guid);
List<IProduct> GetAllProducts();
IProduct GetProductByState(string stateGuid);
bool DoesProductExist(string guid);
Task AddProductAsync(string guid, string name, double price, string author, string publisher, int pages, DateTime publicationDate);
Task<IBook> GetProductAsync(string guid);
Task UpdateProductAsync(string guid, string name, double price, string author, string publisher, int pages, DateTime publicationDate);
Task DeleteProductAsync(string guid);
Task<Dictionary<string, IBook>> GetAllProductsAsync();
Task<int> GetProductsCountAsync();

void RemoveProduct(string guid);
#endregion

#region Event
void AddEvent(IEvent @event);
IEvent GetEvent(string guid);
List<IEvent> GetAllEvents();
List<IEvent> GetEventsByUser(string userGuid);
List<IEvent> GetEventsByProduct(string productGuid);
List<IEvent> GetEventsByState(string stateGuid);
void RemoveEvent(string guid);

#region State CRUD

Task AddStateAsync(string guid, string productGuid, int quantity);
Task<IState> GetStateAsync(string guid);
Task UpdateStateAsync(string guid, string productGuid, int quantity);
Task DeleteStateAsync(string guid);
Task<Dictionary<string, IState>> GetAllStatesAsync();
Task<int> GetStatesCountAsync();

#endregion

#region State
void AddState(IState state);
IState GetState(string guid);
List<IState> GetAllStates();
void RemoveState(string guid);

#region Event CRUD

Task AddEventAsync(string guid, string stateGuid, string userGuid, DateTime createdAt, string type);
Task<IEvent> GetEventAsync(string guid);
Task UpdateEventAsync(string guid, string stateGuid, string userGuid, DateTime createdAt, string type);
Task DeleteEventAsync(string guid);
Task<Dictionary<string, IEvent>> GetAllEventsAsync();
Task<int> GetEventsCountAsync();

#endregion
}
}
Loading

0 comments on commit 0d933b7

Please sign in to comment.