This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from Oskarowski/dev-task-2
Task 2 - 0
- Loading branch information
Showing
141 changed files
with
7,729 additions
and
796 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.