54 lines
2.0 KiB
C#
54 lines
2.0 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Strata.Analytics.Biz.DbContexts;
|
|
using Strata.Analytics.Models.Workbooks;
|
|
using Strata.CoreLib.Claims;
|
|
using System;
|
|
using System.IO;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
|
|
namespace Strata.Analytics.Test.Unit.Biz.TestData
|
|
{
|
|
internal class AnalyticsDbContextMock
|
|
{
|
|
public static AnalyticsDbContext GetAnalyticsDbContext(IClaimsPrincipalAccessor claimsPrincipalAccessor)
|
|
{
|
|
var options = new DbContextOptionsBuilder<AnalyticsDbContext>().UseInMemoryDatabase(Guid.NewGuid().ToString()).EnableSensitiveDataLogging().Options;
|
|
return new AnalyticsDbContextJsonSafe(options, claimsPrincipalAccessor);
|
|
}
|
|
}
|
|
|
|
internal class AnalyticsDbContextJsonSafe : AnalyticsDbContext
|
|
{
|
|
public AnalyticsDbContextJsonSafe(DbContextOptions<AnalyticsDbContext> options, IClaimsPrincipalAccessor claimsPrincipalAccessor) : base(options, claimsPrincipalAccessor)
|
|
{
|
|
}
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
modelBuilder.Entity<Worksheet>().Property(p => p.Content)
|
|
.HasConversion(
|
|
v => JsonDocumentToString(v),
|
|
v => JsonDocument.Parse(v, new JsonDocumentOptions()));
|
|
|
|
modelBuilder.Entity<SavedFilter>().Property(p => p.FiltersJson)
|
|
.HasConversion(
|
|
v => JsonDocumentToString(v),
|
|
v => JsonDocument.Parse(v, new JsonDocumentOptions()));
|
|
|
|
base.OnModelCreating(modelBuilder);
|
|
}
|
|
|
|
internal static string JsonDocumentToString(JsonDocument document)
|
|
{
|
|
using (var stream = new MemoryStream())
|
|
{
|
|
var writer = new Utf8JsonWriter(stream, new JsonWriterOptions { Indented = true });
|
|
document.WriteTo(writer);
|
|
writer.Flush();
|
|
return Encoding.UTF8.GetString(stream.ToArray());
|
|
}
|
|
}
|
|
}
|
|
}
|