feat: initial commit

This commit is contained in:
Thom Lamb
2026-06-23 11:18:54 -05:00
parent fc3ce9a074
commit 1bb980f070
790 changed files with 149255 additions and 218 deletions
@@ -0,0 +1,53 @@
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());
}
}
}
}
@@ -0,0 +1,25 @@
using AutoMapper;
using Strata.Analytics.Biz.ContentMigration;
using Strata.Analytics.Biz.DataSources;
using Strata.Analytics.Biz.Extracts;
using Strata.Analytics.Biz.Workbooks;
namespace Strata.Analytics.Test.Unit.Biz.TestData
{
internal static class AutoMapperMock
{
public static IMapper GetAutoMapper()
{
var mapperConfig = new MapperConfiguration(cfg =>
{
cfg.AddProfile(new DataSourceAutoMapperProfile());
cfg.AddProfile(new WorkbookAutoMapperProfile());
cfg.AddProfile(new WorkbookSaveInfoAutoMapperProfile());
cfg.AddProfile(new ExtractAutoMapperProfile());
cfg.AddProfile(new ExtractSaveInfoAutoMapperProfile());
cfg.AddProfile(new ContentMigrationAutoMapperProfile());
});
return mapperConfig.CreateMapper();
}
}
}
@@ -0,0 +1,15 @@
using AutoMapper;
using Strata.Analytics.Biz.Extracts.Models;
namespace Strata.Analytics.Test.Unit.Biz.TestData
{
public class ExtractSaveInfoAutoMapperProfile : Profile
{
public ExtractSaveInfoAutoMapperProfile()
{
CreateMap<Extract, ExtractSaveInfo > ();
CreateMap<ExtractExportHistory, ExtractExportHistorySaveInfo>();
CreateMap<ExtractSchedule, ExtractScheduleSaveInfo>();
}
}
}
@@ -0,0 +1,178 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Strata.Analytics.Biz.Extracts.Models;
namespace Strata.Analytics.Test.Unit.Biz.TestData
{
internal static class ExtractTestData
{
private static readonly Guid UserGuid1 = Guid.NewGuid();
private static readonly Guid UserGuid2 = Guid.NewGuid();
private static readonly DateTimeOffset Now = DateTimeOffset.UtcNow;
private static readonly DateOnly NowDate = DateOnly.Parse(DateTime.Today.ToShortDateString());
private static readonly TimeOnly NowTime = TimeOnly.Parse(DateTime.Today.ToShortTimeString());
public static IQueryable<Extract> Extracts()
{
var extracts = new List<Extract>
{
new()
{
ExtractId = 1,
Name = "Extract 1",
Description = "This is Extract 1",
ExtractType = ExtractType.CustomSql,
RawSql = "SELECT * FROM ALLTABLES",
CreatedByUserGuid = UserGuid1,
UpdatedByUserGuid = UserGuid1,
CreatedDate = Now.AddDays(-1),
UpdatedDate = Now.AddHours(-1)
},
new()
{
ExtractId = 2,
Name = "Extract 2",
Description = "This is Extract 2",
ExtractType = ExtractType.CustomSql,
RawSql = "SELECT * FROM JustTheOneTable",
CreatedByUserGuid = UserGuid1,
UpdatedByUserGuid = UserGuid1,
CreatedDate = Now.AddDays(-1),
UpdatedDate = Now.AddHours(-1)
}
};
return extracts.AsQueryable();
}
public static IQueryable<ExtractExportHistory> ExtractExportHistories()
{
var extractExportHistories = new List<ExtractExportHistory>
{
new()
{
ExtractHistoryId = 101,
ExtractId = 1,
OutputFilename = "ExtractExportHistory 101",
ExportStartDate = Now.AddDays(-3).AddMinutes(-50),
ExportEndDate = Now.AddDays(-3).AddMinutes(-5),
SqlExecuted = "This is the Sql Executed",
TotalFileSize = 1024,
TotalRowCount = 8675309,
CreatedByUserGuid = UserGuid1,
CreatedDate = Now.AddDays(-3),
},
new()
{
ExtractHistoryId = 102,
ExtractId = 1,
OutputFilename = "ExtractExportHistory 102",
ExportStartDate = Now.AddDays(-2).AddMinutes(-50),
ExportEndDate = Now.AddDays(-2).AddMinutes(-4),
SqlExecuted = "This is the Sql Executed",
TotalFileSize = 1025,
TotalRowCount = 8675310,
CreatedByUserGuid = UserGuid1,
CreatedDate = Now.AddDays(-2),
},
new()
{
ExtractHistoryId = 103,
ExtractId = 1,
OutputFilename = "ExtractExportHistory 103",
ExportStartDate = Now.AddDays(-1).AddMinutes(-50),
ExportEndDate = Now.AddDays(-1).AddMinutes(-1),
SqlExecuted = "This is the Sql Executed",
TotalFileSize = 1500,
TotalRowCount = 9000001,
CreatedByUserGuid = UserGuid1,
CreatedDate = Now.AddDays(-1),
},
new()
{
ExtractHistoryId = 201,
ExtractId = 2,
OutputFilename = "ExtractExportHistory 201",
ExportStartDate = Now.AddDays(-3).AddMinutes(-3),
ExportEndDate = Now.AddDays(-3).AddMinutes(-1),
SqlExecuted = "This is the Sql Executed",
TotalFileSize = 9890498,
TotalRowCount = 106840864650680,
CreatedByUserGuid = UserGuid2,
CreatedDate = Now.AddDays(-3),
},
new()
{
ExtractHistoryId = 202,
ExtractId = 1,
OutputFilename = "ExtractExportHistory 102",
ExportStartDate = Now.AddMinutes(-1),
ExportEndDate = Now.AddSeconds(-1),
SqlExecuted = "This is the Sql Executed",
TotalFileSize = 1025,
TotalRowCount = 8675310,
CreatedByUserGuid = UserGuid2,
CreatedDate = Now.AddDays(-2),
}
};
return extractExportHistories.AsQueryable();
}
public static IQueryable<ExtractSchedule> ExtractSchedules()
{
var schedules = new List<ExtractSchedule>
{
new()
{
ExtractId = 1,
ExtractScheduleId = 1,
Enabled = true,
Repeater = ScheduleRepeaterType.Daily,
OnDays = "",
StartDate = NowDate,
StartTime = NowTime,
EndDate = NowDate.AddYears(1),
CreatedByUserGuid = UserGuid1,
UpdatedByUserGuid = UserGuid1,
CreatedDate = Now.AddDays(-1),
UpdatedDate = Now.AddDays(-1),
LastRunDate = Now
},
new()
{
ExtractId = 2,
ExtractScheduleId = 2,
Enabled = true,
Repeater = ScheduleRepeaterType.Weekly,
OnDays = "MON,WED,FRI",
StartDate = NowDate,
StartTime = NowTime,
MaxOccurrences = 12,
CreatedByUserGuid = UserGuid2,
UpdatedByUserGuid = UserGuid2,
CreatedDate = Now.AddDays(-1),
UpdatedDate = Now.AddDays(-1),
LastRunDate = Now
},
new()
{
ExtractId = 3,
ExtractScheduleId = 3,
Enabled = true,
Repeater = ScheduleRepeaterType.Monthly,
OnDays = "",
StartDate = NowDate,
StartTime = NowTime,
MaxOccurrences = 12,
CreatedByUserGuid = UserGuid2,
UpdatedByUserGuid = UserGuid2,
CreatedDate = Now.AddDays(-1),
UpdatedDate = Now.AddDays(-1),
LastRunDate = Now
}
};
return schedules.AsQueryable();
}
}
}
@@ -0,0 +1,39 @@
using IdentityModel;
using Moq;
using Strata.CoreLib.Claims;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
namespace Strata.Analytics.Test.Unit.Biz.TestData
{
internal class IClaimsPrincipalAccessorMock
{
public static IClaimsPrincipalAccessor GetClaimsPrincipalAccessor(int strataId = 5, bool isStrataUser = false, bool isAdmin = true, IEnumerable<string> additionalUserGroups = null)
{
var userGroups = new List<string> { "Everyone" };
if (additionalUserGroups != null)
{
userGroups.AddRange(additionalUserGroups);
}
var claims = new List<Claim> {
new Claim(StrataClaims.StrataId, strataId.ToString()),
new Claim(StrataClaims.IsSdtEmployee, isStrataUser.ToString()),
new Claim(StrataClaims.UserGuid, "9d1bf676-b8cc-4403-a5fc-686f33538d3b"),
new Claim(StrataClaims.FirstName, "Test"),
new Claim(StrataClaims.LastName, "User"),
new Claim(StrataClaims.Username, "testuser"),
new Claim(StrataClaims.IsAdmin, isAdmin.ToString()),
new Claim(StrataClaims.DatabaseGuid, "3ABAEFE1-A5D3-4F6E-80DD-8967B255206A"),
new Claim(StrataClaims.Timezone, "Central Standard Time")
};
claims.AddRange(userGroups.Select(userGroup => new Claim(JwtClaimTypes.Role, userGroup)));
var principal = new ClaimsPrincipal(new ClaimsIdentity(claims));
return Mock.Of<IClaimsPrincipalAccessor>(a => a.GetCurrentClaimsPrincipal() == principal);
}
}
}
@@ -0,0 +1,68 @@
using System;
using Hangfire;
using Hangfire.Common;
using Hangfire.Server;
using Hangfire.Storage;
using Moq;
// https://github.com/HangfireIO/Hangfire/blob/master/tests/Hangfire.Core.Tests/Mocks/PerformContextMock.cs
namespace Strata.Analytics.Test.Unit.Biz.TestData
{
public class PerformContextMock
{
private readonly Lazy<PerformContext> _context;
public PerformContextMock()
{
Storage = new Mock<JobStorage>();
Connection = new Mock<IStorageConnection>();
BackgroundJob = new BackgroundJobMock();
CancellationToken = new Mock<IJobCancellationToken>();
_context = new Lazy<PerformContext>(
() => new PerformContext(Storage.Object, Connection.Object, BackgroundJob.Object, CancellationToken.Object));
}
public Mock<JobStorage> Storage { get; set; }
public Mock<IStorageConnection> Connection { get; set; }
public BackgroundJobMock BackgroundJob { get; set; }
public Mock<IJobCancellationToken> CancellationToken { get; set; }
public PerformContext Object => _context.Value;
public PerformingContext GetPerformingContext()
{
return new PerformingContext(Object);
}
public PerformedContext GetPerformedContext(object result = null, bool canceled = false, Exception exception = null)
{
return new PerformedContext(Object, result, canceled, exception);
}
}
public class BackgroundJobMock
{
private readonly Lazy<BackgroundJob> _object;
public BackgroundJobMock()
{
Id = "JobId";
Job = Job.FromExpression(() => SomeMethod());
CreatedAt = DateTime.UtcNow;
_object = new Lazy<BackgroundJob>(
() => new BackgroundJob(Id, Job, CreatedAt));
}
public string Id { get; set; }
public Job Job { get; set; }
public DateTime CreatedAt { get; set; }
public BackgroundJob Object => _object.Value;
public static void SomeMethod() { }
}
}
@@ -0,0 +1,16 @@
using AutoMapper;
using Strata.Analytics.Models.Workbooks;
namespace Strata.Analytics.Test.Unit.Biz.TestData
{
public class WorkbookSaveInfoAutoMapperProfile : Profile
{
public WorkbookSaveInfoAutoMapperProfile()
{
CreateMap<Workbook, WorkbookSaveInfo>();
CreateMap<Workbook, WorkbookCreateInfo>();
CreateMap<Worksheet, WorksheetSaveInfo>();
CreateMap<WorkbookRoleAssignment, WorkbookRoleAssignmentSaveInfo>();
}
}
}
@@ -0,0 +1,108 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using Strata.Analytics.Models.Workbooks;
namespace Strata.Analytics.Test.Unit.Biz.TestData
{
internal static class WorkbookTestData
{
private static readonly Guid UserGuid1 = Guid.NewGuid();
private static readonly Guid UserGuid2 = Guid.NewGuid();
public static IQueryable<Workbook> Workbooks()
{
var workbooks = new List<Workbook>
{
new()
{
WorkbookId = 1,
Name = "Workbook 1",
Description = "This is Workbook 1",
DefaultDataSourceId = 1001,
CreatedByUserGuid = UserGuid1,
UpdatedByUserGuid = UserGuid1,
CreatedDate = DateTime.UtcNow.AddDays(-1),
UpdatedDate = DateTime.UtcNow.AddHours(-1)
},
new()
{
WorkbookId = 2,
Name = "Workbook 2",
Description = "This is Workbook 2",
DefaultDataSourceId = 2001,
CreatedByUserGuid = UserGuid1,
UpdatedByUserGuid = UserGuid1,
CreatedDate = DateTime.UtcNow.AddDays(-1),
UpdatedDate = DateTime.UtcNow.AddHours(-1)
}
};
return workbooks.AsQueryable();
}
public static IQueryable<Worksheet> Worksheets()
{
var worksheets = new List<Worksheet>
{
new()
{
WorksheetId = 101,
WorkbookId = 1,
Name = "Worksheet 101",
WorksheetType = WorksheetType.Adhoc,
Content = JsonDocument.Parse("{\"MyContent\": \"WS 101\"}"),
CreatedByUserGuid = UserGuid1,
UpdatedByUserGuid = UserGuid2,
CreatedDate = DateTime.UtcNow.AddDays(-1),
UpdatedDate = DateTime.UtcNow.AddHours(-1)
},
new()
{
WorksheetId = 102,
WorkbookId = 1,
Name = "Worksheet 102",
WorksheetType = WorksheetType.Blank,
Content = JsonDocument.Parse("{\"MyContent\": \"WS 102\"}"),
CreatedByUserGuid = UserGuid1,
UpdatedByUserGuid = UserGuid1,
CreatedDate = DateTime.UtcNow.AddDays(-1),
UpdatedDate = DateTime.UtcNow.AddHours(-1)
},
new()
{
WorksheetId = 103,
WorkbookId = 1,
Name = "Worksheet 103",
WorksheetType = WorksheetType.Dashboard,
Content = JsonDocument.Parse("{\"MyContent\": \"WS 103\"}"),
CreatedByUserGuid = UserGuid1,
UpdatedByUserGuid = UserGuid1,
CreatedDate = DateTime.UtcNow.AddDays(-1),
UpdatedDate = DateTime.UtcNow.AddHours(-1)
},
new()
{
WorksheetId = 201,
WorkbookId = 2,
Name = "Worksheet 201",
CreatedByUserGuid = UserGuid1,
UpdatedByUserGuid = UserGuid2,
CreatedDate = DateTime.UtcNow.AddDays(-1),
UpdatedDate = DateTime.UtcNow.AddHours(-1)
},
new()
{
WorksheetId = 202,
WorkbookId = 2,
Name = "Worksheet 202",
CreatedByUserGuid = UserGuid1,
UpdatedByUserGuid = UserGuid2,
CreatedDate = DateTime.UtcNow.AddDays(-1),
UpdatedDate = DateTime.UtcNow.AddHours(-1)
}
};
return worksheets.AsQueryable();
}
}
}