142 lines
4.9 KiB
C#
142 lines
4.9 KiB
C#
using EFCore.BulkExtensions;
|
|
using Microsoft.AspNetCore.SignalR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Moq;
|
|
using Npgsql;
|
|
using NUnit.Framework;
|
|
using Strata.CoreLib.Claims;
|
|
using Strata.RxNorm.Biz.DbContexts;
|
|
using Strata.RxNorm.Biz.Notification;
|
|
using Strata.RxNorm.Biz.Pharmacy;
|
|
using Strata.RxNorm.Biz.RxNorm;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Security.Claims;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Strata.RxNorm.Biz.Test.Unit.RxNorm;
|
|
|
|
[TestFixture]
|
|
internal class RxNormServiceTests
|
|
{
|
|
private RxNormDbContext _dbContext;
|
|
|
|
[SetUp]
|
|
public void Setup()
|
|
{
|
|
var builder = new NpgsqlConnectionStringBuilder()
|
|
{
|
|
Database = "rxnorm",
|
|
Host = "localhost",
|
|
Port = 5555,
|
|
Username = "postgres",
|
|
Password = "postgres",
|
|
WriteBufferSize = 1024 * 1024 * 16
|
|
};
|
|
|
|
var options = new DbContextOptionsBuilder<RxNormDbContext>()
|
|
.UseNpgsql(builder.ToString())
|
|
.UseSnakeCaseNamingConvention()
|
|
.Options;
|
|
|
|
_dbContext = new RxNormDbContext(options);
|
|
}
|
|
|
|
[Explicit("Make sure local database is up before running tests")]
|
|
[TestCase("RXNSAT1A.RRF", TypeArgs = [typeof(RxnAttribute)], TestName = "RxnAttribute Small 50MB")]
|
|
[TestCase("RXNSAT.RRF", TypeArgs = [typeof(RxnAttribute)], TestName = "RxnAttribute Big 250MB")]
|
|
[TestCase("RXNCONSO.RRF", TypeArgs = [typeof(RxnConcept)], TestName = "RxnConcept 30MB")]
|
|
[TestCase("RXNREL1A.RRF", TypeArgs = [typeof(RxnRelation)], TestName = "RxnRelation 50MB")]
|
|
public async Task SeedTableTests<TEntity>(string fileName) where TEntity : class
|
|
{
|
|
var pharmacyUpdateService = Mock.Of<IPharmacyUpdateService>();
|
|
Mock.Get(pharmacyUpdateService)
|
|
.Setup(p => p.RunPharmacyUpdateClientJob(It.IsAny<bool>(), It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync("Success");
|
|
var rxNormService = new RxNormService(_dbContext, pharmacyUpdateService, NullLogger<RxNormService>.Instance, Mock.Of<IHubContext<NotificationHub, INotificationHub>>(), new StubClaimsPrincipalAccessor(username: "user"));
|
|
|
|
const string testFileDirectory = "../../../Rrf_Files";
|
|
var fullFilePath = Path.Combine(testFileDirectory, fileName);
|
|
await using var fileStream = File.OpenRead(fullFilePath);
|
|
|
|
var fileSizeInMb = Math.Round(fileStream.Length / 1024.0 / 1024.0, 1);
|
|
TestContext.WriteLine($"{fileName} is {fileSizeInMb} MB");
|
|
|
|
await _dbContext.TruncateAsync<TEntity>(cancellationToken: TestContext.CurrentContext.CancellationToken);
|
|
var rowCount = await _dbContext.Set<TEntity>().CountAsync(TestContext.CurrentContext.CancellationToken);
|
|
// make sure we start with clean slate
|
|
Assert.That(rowCount, Is.Zero);
|
|
|
|
await rxNormService.SeedTable<TEntity>(fileStream, TestContext.CurrentContext.CancellationToken);
|
|
|
|
// assert that SOME rows were added
|
|
rowCount = await _dbContext.Set<TEntity>().CountAsync(TestContext.CurrentContext.CancellationToken);
|
|
Assert.That(rowCount, Is.GreaterThan(0));
|
|
}
|
|
}
|
|
|
|
public class StubClaimsPrincipalAccessor : IClaimsPrincipalAccessor
|
|
{
|
|
private readonly ClaimsPrincipal _principal;
|
|
|
|
/// <summary>
|
|
/// Will always return the given <see cref="ClaimsPrincipal"/>
|
|
/// </summary>
|
|
public StubClaimsPrincipalAccessor(ClaimsPrincipal principal)
|
|
{
|
|
_principal = principal;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a <see cref="ClaimsPrincipal"/> with the given strata specific claims values
|
|
/// and will always return that principal
|
|
/// </summary>
|
|
public StubClaimsPrincipalAccessor(
|
|
Guid? databaseGuid = null,
|
|
int? strataId = null,
|
|
string username = null,
|
|
Guid? userGuid = null,
|
|
IEnumerable<KeyValuePair<string, string>> additionalClaims = null)
|
|
{
|
|
var claims = new List<Claim>();
|
|
if (databaseGuid.HasValue)
|
|
{
|
|
claims.Add(new Claim(StrataClaims.DatabaseGuid, databaseGuid.ToString()));
|
|
}
|
|
|
|
if (strataId.HasValue)
|
|
{
|
|
claims.Add(new Claim(StrataClaims.StrataId, strataId.ToString()));
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(username))
|
|
{
|
|
claims.Add(new Claim(StrataClaims.Username, username));
|
|
}
|
|
|
|
if (userGuid.HasValue)
|
|
{
|
|
claims.Add(new Claim(StrataClaims.UserGuid, userGuid.ToString()));
|
|
}
|
|
|
|
if (additionalClaims != null)
|
|
{
|
|
foreach (var (key, value) in additionalClaims)
|
|
{
|
|
claims.Add(new Claim(key, value));
|
|
}
|
|
}
|
|
|
|
var claimsIdentity = new ClaimsIdentity(claims, "IntegrationTest");
|
|
_principal = new ClaimsPrincipal(claimsIdentity);
|
|
}
|
|
|
|
public ClaimsPrincipal GetCurrentClaimsPrincipal()
|
|
{
|
|
return _principal;
|
|
}
|
|
}
|