From 4650eb70acc480d24525156fef080da16e899305 Mon Sep 17 00:00:00 2001 From: NoahLan <6995syu@163.com> Date: Wed, 10 Apr 2024 15:11:50 +0800 Subject: [PATCH] feat: test project --- test/NPin.Test/Demo/HttpUser_Test.cs | 15 ++++++ test/NPin.Test/Demo/User_Test.cs | 12 +++++ test/NPin.Test/NPin.Test.csproj | 42 ++++++++++++++++ test/NPin.Test/NPinTestBase.cs | 42 ++++++++++++++++ test/NPin.Test/NPinTestModule.cs | 19 ++++++++ test/NPin.Test/NPinTestWebBase.cs | 46 ++++++++++++++++++ test/NPin.Test/appsettings.json | 71 ++++++++++++++++++++++++++++ 7 files changed, 247 insertions(+) create mode 100644 test/NPin.Test/Demo/HttpUser_Test.cs create mode 100644 test/NPin.Test/Demo/User_Test.cs create mode 100644 test/NPin.Test/NPin.Test.csproj create mode 100644 test/NPin.Test/NPinTestBase.cs create mode 100644 test/NPin.Test/NPinTestModule.cs create mode 100644 test/NPin.Test/NPinTestWebBase.cs create mode 100644 test/NPin.Test/appsettings.json diff --git a/test/NPin.Test/Demo/HttpUser_Test.cs b/test/NPin.Test/Demo/HttpUser_Test.cs new file mode 100644 index 0000000..44f0c9d --- /dev/null +++ b/test/NPin.Test/Demo/HttpUser_Test.cs @@ -0,0 +1,15 @@ +using Microsoft.AspNetCore.Http; +using Shouldly; +using Xunit; + +namespace NPin.Test.Demo; + +public class HttpUser_Test : NPinTestWebBase +{ + [Fact] + public void Http_Test() + { + var httpContext = GetRequiredService(); + httpContext.HttpContext.Request.Path.ToString().ShouldBe("/test"); + } +} \ No newline at end of file diff --git a/test/NPin.Test/Demo/User_Test.cs b/test/NPin.Test/Demo/User_Test.cs new file mode 100644 index 0000000..9e939c9 --- /dev/null +++ b/test/NPin.Test/Demo/User_Test.cs @@ -0,0 +1,12 @@ +using Xunit; + +namespace NPin.Test.Demo; + +public class User_Test: NPinTestBase +{ + [Fact] + public async Task Get_User_List_Test() + { + // var service = GetRequiredService() + } +} \ No newline at end of file diff --git a/test/NPin.Test/NPin.Test.csproj b/test/NPin.Test/NPin.Test.csproj new file mode 100644 index 0000000..740c2f4 --- /dev/null +++ b/test/NPin.Test/NPin.Test.csproj @@ -0,0 +1,42 @@ + + + + net8.0 + enable + enable + + + + + + + Always + true + PreserveNewest + + + + + + + + + all + runtime; build; native; contentfiles; analyzers + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/test/NPin.Test/NPinTestBase.cs b/test/NPin.Test/NPinTestBase.cs new file mode 100644 index 0000000..5c9feee --- /dev/null +++ b/test/NPin.Test/NPinTestBase.cs @@ -0,0 +1,42 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace NPin.Test; + +public class NPinTestBase : AbpTestBaseWithServiceProvider +{ + public ILogger Logger { get; private set; } + + protected IServiceScope TestServiceScope { get; } + + public NPinTestBase() + { + IHost host = Host.CreateDefaultBuilder() + .UseAutofac() + .ConfigureServices((host, service) => + { + ConfigureServices(host, service); + service.AddLogging(builder => builder.ClearProviders().AddConsole().AddDebug()); + service.AddApplicationAsync().Wait(); + }) + .ConfigureAppConfiguration(ConfigureAppConfiguration) + .Build(); + + ServiceProvider = host.Services; + TestServiceScope = ServiceProvider.CreateScope(); + Logger = (ILogger)ServiceProvider.GetRequiredService(typeof(ILogger<>).MakeGenericType(GetType())); + + host.InitializeAsync().Wait(); + } + + public virtual void ConfigureServices(HostBuilderContext host, IServiceCollection service) + { + } + + protected virtual void ConfigureAppConfiguration(IConfigurationBuilder configurationBuilder) + { + configurationBuilder.AddJsonFile("appsettings.json"); + } +} \ No newline at end of file diff --git a/test/NPin.Test/NPinTestModule.cs b/test/NPin.Test/NPinTestModule.cs new file mode 100644 index 0000000..4b6bf21 --- /dev/null +++ b/test/NPin.Test/NPinTestModule.cs @@ -0,0 +1,19 @@ +using NPin.Application; +using NPin.SqlSugarCore; +using Volo.Abp.Auditing; +using Volo.Abp.Autofac; + +namespace NPin.Test; + +[DependsOn( + typeof(NPinSqlSugarCoreModule), + typeof(NPinApplicationModule), + typeof(AbpAutofacModule), + typeof(AbpAuditingModule) +)] +public class NPinTestModule : AbpTestBaseModule +{ + public override void ConfigureServices(ServiceConfigurationContext context) + { + } +} \ No newline at end of file diff --git a/test/NPin.Test/NPinTestWebBase.cs b/test/NPin.Test/NPinTestWebBase.cs new file mode 100644 index 0000000..8c3ab87 --- /dev/null +++ b/test/NPin.Test/NPinTestWebBase.cs @@ -0,0 +1,46 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using Microsoft.Extensions.Hosting; + +namespace NPin.Test; + +public class NPinTestWebBase : NPinTestBase +{ + public HttpContext HttpContext { get; private set; } + + public NPinTestWebBase() : base() + { + var httpContext = DefaultHttpContextAccessor.CurrentHttpContext; + ConfigureHttpContext(httpContext); + HttpContext = httpContext; + + var app = new ApplicationBuilder(ServiceProvider); + var httpDelegate = app.Build(); + httpDelegate.Invoke(httpContext); + } + + public override void ConfigureServices(HostBuilderContext host, IServiceCollection service) + { + service.Replace(new ServiceDescriptor(typeof(IHttpContextAccessor), typeof(DefaultHttpContextAccessor), + ServiceLifetime.Singleton)); + base.ConfigureServices(host, service); + } + + protected virtual void ConfigureHttpContext(HttpContext httpContext) + { + httpContext.Request.Path = "/test"; + } +} + +internal class DefaultHttpContextAccessor : IHttpContextAccessor +{ + internal static HttpContext? CurrentHttpContext { get; set; } = new DefaultHttpContext(); + + public HttpContext? HttpContext + { + get => CurrentHttpContext; + set => throw new NotImplementedException(); + } +} \ No newline at end of file diff --git a/test/NPin.Test/appsettings.json b/test/NPin.Test/appsettings.json new file mode 100644 index 0000000..40a7b8c --- /dev/null +++ b/test/NPin.Test/appsettings.json @@ -0,0 +1,71 @@ +{ + "Logging": { + "LogLevel": { + //"Default": "Information", + "Default": "Debug", + "Microsoft.AspNetCore": "Warning" + } + }, + //应用启动 + "App": { + "SelfUrl": "http://*:19001", + "CorsOrigins": "http://localhost:19001;http://localhost:18000" + }, + + //数据库类型列表 + "DbList": [ "Sqlite", "Mysql", "Sqlserver", "Oracle" ], + + "DbConnOptions": { + "Url": "DataSource=npin-dev.db", + "DbType": "Sqlite", + "EnabledReadWrite": false, + "EnabledCodeFirst": true, + "EnabledSqlLog": true, + "EnabledDbSeed": true + //读写分离地址 + //"ReadUrl": [ + // "DataSource=[xxxx]", //Sqlite + // "server=[xxxx];port=3306;database=[xxxx];user id=[xxxx];password=[xxxx]", //Mysql + // "Data Source=[xxxx];Initial Catalog=[xxxx];User ID=[xxxx];password=[xxxx]" //Sqlserver + //] + }, + + //鉴权 + "JwtOptions": { + "Issuer": "NoahLan", + "Audience": "NoahLan", + "SecurityKey": "zqxwcevrbtnymu312412ihe9rfwhe78rh23djoi32hrui3ryf9e8wfh34iuj54y0934uti4h97fgw7hf97wyh8yy69520", + "ExpiresMinuteTime": 86400 + }, + + //第三方登录 + "OAuth": { + //QQ + "QQ": { + "ClientId": "", + "ClientSecret": "", + "RedirectUri": "" + }, + //码云 + "Gitee": { + "ClientId": "", + "ClientSecret": "", + "RedirectUri": "" + } + }, + + //Rbac模块 + "RbacOptions": { + //超级管理员种子数据默认密码 + "AdminPassword": "123456", + + //是否开启验证码验证 + "EnableCaptcha": true, + + //是否开启注册功能 + "EnableRegister": false, + + //开启定时数据库备份 + "EnableDataBaseBackup": false + } +}