TourIAm/TIAM.Database.Test/AdminDalTest.cs

248 lines
10 KiB
C#

using AyCode.Database.Tests;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AyCode.Database.DataLayers.Users;
using AyCode.Utils.Extensions;
using Newtonsoft.Json;
using TIAM.Database.DataLayers.Admins;
//using TIAM.Database.DataLayers.ServiceProviders;
using TIAM.Database.DataLayers.Users;
using TIAM.Database.DbContexts.Admins;
using TIAM.Database.DbContexts.ServiceProviders;
using TIAM.Database.DbSets.Permissions;
using TIAM.Entities.Products;
using TIAM.Entities.Users;
using TIAM.Models.Dtos.Users;
using TIAM.Entities.TransferDestinations;
namespace TIAM.Database.Test
{
[TestClass]
public class AdminDalTest : AcDatabaseTestModelBase<AdminDal, AdminDbContext>
{
[TestInitialize]
public void Setup()
{
}
[TestCleanup]
public void TearDown()
{ }
[DataTestMethod]
[DataRow("42968456-6EF3-4D9C-8BC4-0569A129AC05")]
public void GetPermissionViewBySubjectId_ReturnsPermissionContextMapping_WhenPermissionContextMappingExists(string subjectIdString)
{
var subjectId= Guid.Parse(subjectIdString);
var permMapping = Dal.GetPermissionContextsViewBySubjectId(subjectId).ToList();
Assert.IsNotNull(permMapping, "PermissionContextsView is null");
Assert.IsTrue(permMapping.Count > 0, "PermissionContextsView count: 0");
}
[DataTestMethod]
[DataRow("814b5495-c2e9-4f1d-a73f-37cd5d353078")]
public void GetPermissionViewByContextId_ReturnsPermissionContextMapping_WhenPermissionContextMappingExists(string contextIdString)
{
var contextId = Guid.Parse(contextIdString);
var permMapping = Dal.GetPermissionContextsViewByContextId(contextId).ToList();
Assert.IsNotNull(permMapping, "PermissionContextsView is null");
Assert.IsTrue(permMapping.Count > 0, "PermissionContextsView count: 0");
}
[DataTestMethod]
[DataRow("814b5495-c2e9-4f1d-a73f-37cd5d353078")]
public async Task GetPermissionContextMappingByContextIdAsync_ReturnsPermissionContextMapping_WhenPermissionContextMappingExists(string contextIdString)
{
var contextId = Guid.Parse(contextIdString);
var permMapping = await Dal.GetPermissionContextsViewByContextIdAsync(contextId);
Assert.IsNotNull(permMapping, "PermissionContextsView is null");
Assert.IsTrue(permMapping.Count > 0, "PermissionContextsView count: 0");
}
public Product GetProductById_ReturnsProduct_WhenProductExists(string productIdString)
{
var productId = Guid.Parse(productIdString);
var product = Dal.GetProductById(productId);
Assert.IsNotNull(product, "Product is null");
return product;
}
[DataTestMethod]
[DataRow("814b5495-c2e9-4f1d-a73f-37cd5d353078")]
public void GeProductById_ReturnsProduct_WhenHasUserProductMappingRelation(string productIdString)
{
var product = GetProductById_ReturnsProduct_WhenProductExists(productIdString);
Assert.IsTrue(product.UserProductMappings.Count > 0, "UserProductMappings count: 0");
Assert.IsNotNull(product.UserProductMappings[0].User, "User is null");
Assert.IsNotNull(product.ServiceProvider, "ServiceProvider is null");
}
[DataTestMethod]
[DataRow("540271F6-C604-4C16-8160-D5A7CAFEDF00")]
public void GetUserById_ReturnsUser_WhenHasUserProductMappingRelation(string userIdString)
{
var userId = Guid.Parse(userIdString);
var user = Dal.GetUserById(userId);
Assert.IsNotNull(user);
Assert.IsTrue(user.Id == userId, "user.Id != userId");
Assert.IsTrue(user.UserProductMappings.Count > 0, "UserProductMappings count: 0");
Assert.IsNotNull(user.UserProductMappings[0].Product, "Product is null");
}
#region UserProductMapping
[DataTestMethod]
[DataRow("a24bf07a-76a7-48a4-813f-4a77e515b2f3")]
public void GetUserProductMappingById_ReturnsUserProductMapping_WhenHasUserAndProductRelation(string userProductMappingIdString)
{
var userProductMappingId = Guid.Parse(userProductMappingIdString);
var userProductMapping = Dal.GetUserProductMappingById(userProductMappingId, true);
Assert.IsNotNull(userProductMapping);
Assert.IsNotNull(userProductMapping.User, "User is null");
Assert.IsNotNull(userProductMapping.Product, "Product is null");
Assert.IsTrue(userProductMapping.Id == userProductMappingId, "userProductMapping.Id != userProductMappingId");
}
[DataTestMethod]
[DataRow(new[] { "8EF2FC69-2338-4D9F-91B4-B1E15C241E1C", "814b5495-c2e9-4f1d-a73f-37cd5d353078" })]
public async Task SetUserProductMappingById_ReturnsUserProductMapping_WhenHasUserAndProductRelation(string[] userIdProductIdStrings)
{
var userId = Guid.Parse(userIdProductIdStrings[0]);
var productId = Guid.Parse(userIdProductIdStrings[1]);
var userProductMappingId = Guid.NewGuid();
await Dal.RemoveUserProductMappingAsync(userId, productId); //kitöröljük a szemetet, ha korábbról bentmaradt - J.
var userProductMapping = await Dal.AddUserProductMappingAsync(userProductMappingId, userId, productId, 2);
Assert.IsNotNull(userProductMapping);
userProductMapping = await Dal.GetUserProductMappingByIdAsync(userProductMappingId, true);
Assert.IsNotNull(userProductMapping);
//userProductMapping.Permissions = 1;
Assert.IsNotNull(await Dal.UpdateUserProductMappingAsync(userProductMappingId, 1));
userProductMapping = await Dal.GetUserProductMappingByIdAsync(userProductMappingId, true);
Assert.IsNotNull(userProductMapping);
Assert.IsTrue(userProductMapping.Permissions == 1);
Assert.IsNotNull(userProductMapping.User, "User is null");
Assert.IsNotNull(userProductMapping.Product, "Product is null");
Assert.IsTrue(userProductMapping.Id == userProductMappingId, "userProductMapping.Id != userProductMappingId");
Assert.IsTrue(await Dal.RemoveUserProductMappingAsync(userProductMappingId)); //mielőbb kitöröljük, h ne maradjon szemét a db-ben - J.
userProductMapping = await Dal.GetUserProductMappingByIdAsync(userProductMappingId, false);
Assert.IsNull(userProductMapping); //a korábbi törlés miatt NULL kell legyen - J.
}
#endregion UserProductMapping
[DataTestMethod]
[DataRow("540271F6-C604-4C16-8160-D5A7CAFEDF00")]
[DataRow("dcf451d2-cc4c-4ac2-8c1f-da00041be1fd")]
[DataRow("ac612aa8-863b-4b4f-9d63-f5d261b5c5f9")]
public void SerializeUserModelDto_ReturnDeserializedUser_WhenUserAndRelationsExists(string userIdString)
{
var userId = Guid.Parse(userIdString);
JsonSerializerSettings options = new()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore
};
var userModel = Dal.GetUserModelDtoById(userId);
var serializedUserModel = JsonConvert.SerializeObject(userModel, options);
userModel = JsonConvert.DeserializeObject<UserModelDto>(serializedUserModel);
Assert.IsNotNull(userModel);
Assert.IsNotNull(userModel.UserDto);
Assert.IsNotNull(userModel.Profile);
Assert.IsTrue(userModel.Id == userId, "userModel.Id != userId");
if (userId != Guid.Parse("540271F6-C604-4C16-8160-D5A7CAFEDF00")) return; //csak az "540271F6.."-nek van product-ja! - J.
Assert.IsTrue(userModel.Products.Count > 0);
Assert.IsTrue(userModel.UserProductMappings.Count > 0);
Assert.IsTrue(userModel.ServiceProviders.Count > 0);
Assert.IsTrue(userModel.UserToServiceProviders.Count > 0);
}
[DataTestMethod]
[DataRow("540271F6-C604-4C16-8160-D5A7CAFEDF00")]
[DataRow("dcf451d2-cc4c-4ac2-8c1f-da00041be1fd")]
[DataRow("ac612aa8-863b-4b4f-9d63-f5d261b5c5f9")]
public void SerializeUserEntity_ReturnDeserializedUser_WhenUserAndRelationsExists(string userIdString)
{
var userId = Guid.Parse(userIdString);
var userJson = Dal.GetUserJsonById(userId);
Assert.IsFalse(userJson?.IsNullOrWhiteSpace());
var user = JsonConvert.DeserializeObject<User>(userJson);
Assert.IsNotNull(user);
Assert.IsNotNull(user.Profile);
Assert.IsTrue(user.Id == userId, "user.Id != userId");
if (userId != Guid.Parse("540271F6-C604-4C16-8160-D5A7CAFEDF00")) return; //csak az "540271F6.."-nek van product-ja! - J.
Assert.IsTrue(user.Products.Count > 0);
Assert.IsTrue(user.UserProductMappings.Count > 0);
Assert.IsTrue(user.ServiceProviders.Count > 0);
Assert.IsTrue(user.UserToServiceProviders.Count > 0);
}
[TestMethod]
public void SerializeUsers_ReturnDeserializedUsers_WhenUsersAndRelationsExists()
{
var userJson = Dal.GetUsersJson();
Assert.IsFalse(userJson.IsNullOrWhiteSpace());
var users = JsonConvert.DeserializeObject<List<User>>(userJson);
Assert.IsNotNull(users);
Assert.IsTrue(users.Count>0);
}
[DataTestMethod]
[DataRow("273EFE3C-D19F-4C2A-BF19-7397DC835C60")]
public void GetTransferDestionationById_ReturnsTransferDestination_WhenHasAddressRelation(string transferDestinationIdString)
{
var transferDestinationId = Guid.Parse(transferDestinationIdString);
var transferDestination = Dal.GetTransferDestinationById(transferDestinationId);
Assert.IsNotNull(transferDestination);
Assert.IsNotNull(transferDestination.Address);
Assert.IsTrue(transferDestination.Id == transferDestinationId, "transferDestination.Id != transferDestinationId");
//Assert.IsTrue(user.UserProductMappings.Count > 0, "UserProductMappings count: 0");
//Assert.IsNotNull(user.UserProductMappings[0].Product, "Product is null");
}
}
}