test(Core): merge base class (#4182)

* fix: 更新逻辑

* doc: 更正示例锚点

* doc: 更新文档

* doc: 更新示例文档

* test: 重构 EFCore 单元测试

* test: 精简单元测试基类

* test: 移除 ExportPdfTest 基类

* test: 移除 FooContext 类

* test: 更新 Tab 单元测试

* chore: bump version 8.8.5-beta09
This commit is contained in:
Argo Zhang 2024-08-30 11:17:28 +08:00 committed by GitHub
parent 8ae4b5dac6
commit 70e2847970
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 84 additions and 129 deletions

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">
<PropertyGroup>
<Version>8.8.5-beta08</Version>
<Version>8.8.5-beta09</Version>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">

View File

@ -2,10 +2,18 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/
using System.Text;
namespace UnitTest.Components;
public class ExportPdfButtonTest : ExportPdfTestBase
public class ExportPdfButtonTest : BootstrapBlazorTestBase
{
protected override void ConfigureServices(IServiceCollection services)
{
services.AddBootstrapBlazor();
services.AddSingleton<IHtml2Pdf, MockHtml2PdfService>();
}
[Fact]
public async Task Normal_Ok()
{
@ -145,4 +153,24 @@ public class ExportPdfButtonTest : ExportPdfTestBase
Assert.NotNull(button.OnAfterDownload);
Assert.True(button.IsAsync);
}
class MockHtml2PdfService : IHtml2Pdf
{
public Task<byte[]> PdfDataAsync(string url)
{
throw new NotImplementedException();
}
public Task<byte[]> PdfDataFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null)
{
throw new NotImplementedException();
}
public Task<Stream> PdfStreamAsync(string url)
{
throw new NotImplementedException();
}
public Task<Stream> PdfStreamFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null) => Task.FromResult<Stream>(new MemoryStream(Encoding.UTF8.GetBytes("Hello World")));
}
}

View File

@ -9,8 +9,20 @@ using UnitTest.Misc;
namespace UnitTest.Components;
public class TabTest : TabTestBase
public class TabTest : BootstrapBlazorTestBase
{
protected override void ConfigureServices(IServiceCollection services)
{
services.AddBootstrapBlazor(op => op.ToastDelay = 2000);
services.ConfigureTabItemMenuBindOptions(options =>
{
options.Binders = new()
{
{ "/Binder", new() { Text = "Index_Binder_Test" } }
};
});
}
[Fact]
public void TabItem_Ok()
{

View File

@ -6,8 +6,17 @@ using Microsoft.EntityFrameworkCore;
namespace UnitTest.Components;
public class TableTestEFCore : EFCoreTableTestBase
public class TableTestEFCore : BootstrapBlazorTestBase
{
protected override void ConfigureServices(IServiceCollection services)
{
base.ConfigureServices(services);
services.AddDbContextFactory<FooContext>(option =>
{
option.UseSqlite("Data Source=FooTest.db;");
});
}
[Fact]
public async Task SearchText_Ok()
{
@ -88,4 +97,28 @@ public class TableTestEFCore : EFCoreTableTestBase
Assert.NotNull(conditions.Filters);
Assert.Single(conditions.Filters);
}
class FooContext(DbContextOptions<FooContext> options) : DbContext(options)
{
/// <summary>
///
/// </summary>
[NotNull]
public DbSet<Foo>? Foos { get; set; }
/// <summary>
/// <inheritdoc />
/// </summary>
/// <param name="modelBuilder"></param>
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Foo>().ToTable("Foo");
modelBuilder.Entity<Foo>().Ignore(f => f.DateTime);
modelBuilder.Entity<Foo>().Ignore(f => f.Count);
modelBuilder.Entity<Foo>().Ignore(f => f.Complete);
modelBuilder.Entity<Foo>().Ignore(f => f.Education);
modelBuilder.Entity<Foo>().Ignore(f => f.Hobby);
modelBuilder.Entity<Foo>().Ignore(f => f.ReadonlyColumn);
}
}
}

View File

@ -9,8 +9,14 @@ using System.ComponentModel.DataAnnotations;
namespace UnitTest.Components;
public class ValidateFormTest : ValidateFormTestBase
public class ValidateFormTest : BootstrapBlazorTestBase
{
protected override void ConfigureServices(IServiceCollection services)
{
services.AddBootstrapBlazor();
services.ConfigureJsonLocalizationOptions(op => op.AdditionalJsonAssemblies = new[] { GetType().Assembly });
}
[Fact]
public void BootstrapBlazorDataAnnotationsValidator_Error()
{

View File

@ -1,19 +0,0 @@
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/
using Microsoft.EntityFrameworkCore;
namespace UnitTest.Core;
public class EFCoreTableTestBase : BootstrapBlazorTestBase
{
protected override void ConfigureServices(IServiceCollection services)
{
services.AddBootstrapBlazor(op => op.ToastDelay = 2000);
services.AddDbContextFactory<FooContext>(option =>
{
option.UseSqlite("Data Source=FooTest.db;");
});
}
}

View File

@ -1,36 +0,0 @@
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/
using System.Text;
namespace UnitTest.Core;
public class ExportPdfTestBase : BootstrapBlazorTestBase
{
protected override void ConfigureServices(IServiceCollection services)
{
services.AddBootstrapBlazor();
services.AddSingleton<IHtml2Pdf, MockHtml2PdfService>();
}
}
class MockHtml2PdfService : IHtml2Pdf
{
public Task<byte[]> PdfDataAsync(string url)
{
throw new NotImplementedException();
}
public Task<byte[]> PdfDataFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null)
{
throw new NotImplementedException();
}
public Task<Stream> PdfStreamAsync(string url)
{
throw new NotImplementedException();
}
public Task<Stream> PdfStreamFromHtmlAsync(string html, IEnumerable<string>? links = null, IEnumerable<string>? scripts = null) => Task.FromResult<Stream>(new MemoryStream(Encoding.UTF8.GetBytes("Hello World")));
}

View File

@ -1,35 +0,0 @@
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/
using Microsoft.EntityFrameworkCore;
namespace UnitTest.Core;
/// <summary>
///
/// </summary>
/// <param name="options"></param>
public class FooContext(DbContextOptions<FooContext> options) : DbContext(options)
{
/// <summary>
///
/// </summary>
[NotNull]
public DbSet<Foo>? Foos { get; set; }
/// <summary>
/// <inheritdoc />
/// </summary>
/// <param name="modelBuilder"></param>
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Foo>().ToTable("Foo");
modelBuilder.Entity<Foo>().Ignore(f => f.DateTime);
modelBuilder.Entity<Foo>().Ignore(f => f.Count);
modelBuilder.Entity<Foo>().Ignore(f => f.Complete);
modelBuilder.Entity<Foo>().Ignore(f => f.Education);
modelBuilder.Entity<Foo>().Ignore(f => f.Hobby);
modelBuilder.Entity<Foo>().Ignore(f => f.ReadonlyColumn);
}
}

View File

@ -1,20 +0,0 @@
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/
namespace UnitTest.Core;
public class TabTestBase : BootstrapBlazorTestBase
{
protected override void ConfigureServices(IServiceCollection services)
{
services.AddBootstrapBlazor(op => op.ToastDelay = 2000);
services.ConfigureTabItemMenuBindOptions(options =>
{
options.Binders = new()
{
{ "/Binder", new() { Text = "Index_Binder_Test" } }
};
});
}
}

View File

@ -1,14 +0,0 @@
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/
namespace UnitTest.Core;
public class ValidateFormTestBase : BootstrapBlazorTestBase
{
protected override void ConfigureServices(IServiceCollection services)
{
services.AddBootstrapBlazor();
services.ConfigureJsonLocalizationOptions(op => op.AdditionalJsonAssemblies = new[] { GetType().Assembly });
}
}