Skip to content

Commit

Permalink
🎨 test fixs
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzaz committed Dec 26, 2023
1 parent e9d318e commit 8f6db14
Show file tree
Hide file tree
Showing 11 changed files with 226 additions and 235 deletions.
115 changes: 106 additions & 9 deletions src/BD.SteamClient8.UnitTest/Abstractions/ServiceTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,95 @@ namespace BD.SteamClient8.UnitTest.Abstractions;
/// </summary>
abstract class ServiceTestBase
{
IServiceProvider? serviceProvider;
static IServiceProvider? serviceProvider;

/// <summary>
/// Steam 登录状态
/// </summary>
protected static SteamLoginState SteamLoginState { get; set; } = null!;

/// <summary>
/// Steam 令牌
/// </summary>
protected static SteamAuthenticator SteamAuthenticator { get; set; } = null!;

/// <summary>
/// 依赖注入服务提供程序
/// </summary>
protected IServiceProvider ServiceProvider
protected static IServiceProvider ServiceProvider
{
get => serviceProvider.ThrowIsNull();
private set => serviceProvider = value;
}

#region Static

/// <summary>
/// <see cref="GetInventories"/> 获取的用户库存,访问有频率限制
/// </summary>
protected static InventoryPageResponse? InventoryPageResponse { get; set; }

static readonly AsyncExclusiveLock lock_GetInventoryPageResponseAsync = new();
#endregion

/// <summary>
/// 获取用户库存信息
/// </summary>
/// <param name="steamAccountService"></param>
/// <param name="steam_id"></param>
/// <param name="app_id"></param>
/// <param name="context_id"></param>
/// <returns></returns>
public virtual async ValueTask GetInventories(ISteamAccountService steamAccountService, ulong steam_id, string app_id, string context_id)
{
using (await lock_GetInventoryPageResponseAsync.AcquireLockAsync(CancellationToken.None))
{
if (InventoryPageResponse is null)
{
var response = await steamAccountService.GetInventories(steam_id, app_id, context_id, 50);
Assert.Multiple(() =>
{
Assert.That(response.IsSuccess);
Assert.That(response.Content, Is.Not.Null);
Assert.That(response.Content!.Success, Is.EqualTo(1));
});

InventoryPageResponse = response.Content;
}
}
}

/// <inheritdoc cref="ServiceProviderServiceExtensions.GetRequiredService{T}(IServiceProvider)"/>
protected T GetRequiredService<T>() where T : notnull
=> serviceProvider.ThrowIsNull().GetRequiredService<T>();

static bool IsInit;

/// <inheritdoc cref="SetUpAttribute"/>
public virtual ValueTask Setup()
public virtual async ValueTask Setup()
{
var services = new ServiceCollection();
ConfigureServices(services);
serviceProvider = services.BuildServiceProvider();
Ioc.ConfigureServices(serviceProvider);
return default;
using (await lock_GetInventoryPageResponseAsync.AcquireLockAsync(CancellationToken.None))
{
if (IsInit)
return;

var services = new ServiceCollection();
ConfigureServices(services);
serviceProvider = services.BuildServiceProvider();
Ioc.ConfigureServices(serviceProvider);
IsInit = true;

SteamAuthenticator = (await GetSteamAuthenticatorAsync(GetRequiredService<IConfiguration>(), GetRequiredService<ISteamAuthenticatorService>())).ThrowIsNull();
SteamLoginState = await GetSteamLoginStateAsync(GetRequiredService<IConfiguration>(), GetRequiredService<ISteamAccountService>(), GetRequiredService<ISteamSessionService>())!;
}
}

/// <summary>
/// 配置依赖注入服务
/// </summary>
/// <param name="services"></param>
protected virtual void ConfigureServices(IServiceCollection services)
/// <param name="needLoginState"></param>
protected void ConfigureServices(IServiceCollection services, bool needLoginState)
{
ConfigurationBuilder builder = new();
ConfigureConfiguration(builder);
Expand All @@ -45,6 +104,28 @@ protected virtual void ConfigureServices(IServiceCollection services)
services.AddLogging(ConfigureLogging);
services.TryAddHttpPlatformHelper();
services.AddFusilladeHttpClientFactory();

services.AddSteamAccountService();
services.AddSteamAuthenticatorService();
services.AddSteamIdleCardService();
services.AddSteamMarketService();
services.AddSteamTradeService();
#if (WINDOWS || MACCATALYST || MACOS || LINUX) && !(IOS || ANDROID)
services.AddSingleton<ISteamService, TestSteamServiceImpl>();
services.TryAddSteamworksLocalApiService();
#endif
services.AddSteamDbWebApiService();
services.AddSteamworksWebApiService();
services.AddSteamGridDBWebApiService();
}

/// <summary>
/// 配置依赖注入服务
/// </summary>
/// <param name="services"></param>
protected virtual void ConfigureServices(IServiceCollection services)
{
ConfigureServices(services, true);
}

/// <summary>
Expand All @@ -64,4 +145,20 @@ protected virtual void ConfigureLogging(ILoggingBuilder builder)
{
builder.AddConsole();
}

#if (WINDOWS || MACCATALYST || MACOS || LINUX) && !(IOS || ANDROID)

sealed class TestSteamServiceImpl(ILoggerFactory loggerFactory) : SteamServiceImpl(loggerFactory)
{
public override ISteamConnectService Conn => throw new NotImplementedException();

protected override string? StratSteamDefaultParameter => default;

protected override bool IsRunSteamAdministrator => default;

protected override Dictionary<uint, string?>? HideGameList => default;

protected override string? GetString(string name) => default;
}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>net$(DotNet_Version)</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">net$(DotNet_Version);net$(DotNet_Version)-windows$(WinSDK_Version)</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">net$(DotNet_Version)-windows$(WinSDK_Version)</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
Expand Down
22 changes: 0 additions & 22 deletions src/BD.SteamClient8.UnitTest/PInvokeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@ sealed class PInvokeTest : ServiceTestBase
ISteamService steamService = null!;
ISteamworksLocalApiService steamworksLocalApiService = null!;

/// <inheritdoc/>
protected override void ConfigureServices(IServiceCollection services)
{
base.ConfigureServices(services);

services.AddSingleton<ISteamService, TestSteamServiceImpl>();
services.TryAddSteamworksLocalApiService();
}

/// <inheritdoc/>
[SetUp]
public override async ValueTask Setup()
Expand Down Expand Up @@ -139,18 +130,5 @@ public void TestGetDownloadingAppList()
});
Assert.That(list, Is.Not.Empty);
}

sealed class TestSteamServiceImpl(ILoggerFactory loggerFactory) : SteamServiceImpl(loggerFactory)
{
public override ISteamConnectService Conn => throw new NotImplementedException();

protected override string? StratSteamDefaultParameter => default;

protected override bool IsRunSteamAdministrator => default;

protected override Dictionary<uint, string?>? HideGameList => default;

protected override string? GetString(string name) => default;
}
}
#endif
41 changes: 14 additions & 27 deletions src/BD.SteamClient8.UnitTest/SteamAccountServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,7 @@ namespace BD.SteamClient8.UnitTest;
/// </summary>
sealed class SteamAccountServiceTest : ServiceTestBase
{
SteamLoginState? steamLoginState;
ISteamAccountService steamAccountService = null!;
IConfiguration configuration = null!;

/// <inheritdoc/>
protected override void ConfigureServices(IServiceCollection services)
{
base.ConfigureServices(services);

services.AddSteamAccountService();
}

/// <inheritdoc/>
[SetUp]
Expand All @@ -24,25 +14,22 @@ public override async ValueTask Setup()
await base.Setup();

steamAccountService = GetRequiredService<ISteamAccountService>();
configuration = GetRequiredService<IConfiguration>();

steamLoginState = await GetSteamLoginStateAsync(configuration, steamAccountService, GetRequiredService<ISteamSessionService>());
SteamLoginState.ThrowIsNull();
}

/// <summary>
/// 测试获取库存列表
/// </summary>
/// <param name="steamId"></param>
/// <param name="steam_id"></param>
/// <param name="appId"></param>
/// <param name="contextId"></param>
/// <returns></returns>
[TestCase(76561199494800019UL, "730", "2")]
[TestCase(76561199495399375UL, "570", "2")]
[Test]
public async Task TestGetInventories(ulong steamId, string appId, string contextId)
public async Task TestGetInventories(ulong steam_id, string appId, string contextId)
{
var rsp = await steamAccountService.GetInventories(steamId, appId, contextId, 1);

Assert.That(rsp.Content.ThrowIsNull().Success, Is.EqualTo(1));
await GetInventories(steamAccountService, steam_id, appId, contextId);
}

/// <summary>
Expand All @@ -57,11 +44,11 @@ public async Task TestGetInventories(ulong steamId, string appId, string context
[Test]
public async Task TestGetAndParseInventoryTradingHistory(int[]? appFilter)
{
if (steamLoginState != null)
if (SteamLoginState != null)
{
InventoryTradeHistoryRenderPageResponse.InventoryTradeHistoryCursor? cursor = null;

var rsp = await steamAccountService.GetInventoryTradeHistory(steamLoginState!, appFilter, cursor);
var rsp = await steamAccountService.GetInventoryTradeHistory(SteamLoginState!, appFilter, cursor);

Assert.Multiple(() =>
{
Expand All @@ -84,13 +71,13 @@ public async Task TestGetAndParseInventoryTradingHistory(int[]? appFilter)
[Test]
public async Task TestGetApiKey()
{
if (steamLoginState != null)
if (SteamLoginState != null)
{
string? apiKey = (await steamAccountService.GetApiKey(steamLoginState!)).Content;
string? apiKey = (await steamAccountService.GetApiKey(SteamLoginState!)).Content;

if (string.IsNullOrEmpty(apiKey))
{
apiKey = (await steamAccountService.RegisterApiKey(steamLoginState!)).Content;
apiKey = (await steamAccountService.RegisterApiKey(SteamLoginState!)).Content;
}

Assert.That(apiKey, Is.Not.Null);
Expand All @@ -105,9 +92,9 @@ public async Task TestGetApiKey()
[Test]
public async Task TestGetSendGiftHistory()
{
if (steamLoginState != null)
if (SteamLoginState != null)
{
var history = await steamAccountService.GetSendGiftHistories(steamLoginState!);
var history = await steamAccountService.GetSendGiftHistories(SteamLoginState!);

Assert.That(history, Is.Not.Null);
}
Expand All @@ -120,9 +107,9 @@ public async Task TestGetSendGiftHistory()
[Test]
public async Task TestGetLoginHistory()
{
if (steamLoginState != null)
if (SteamLoginState != null)
{
var result = steamAccountService.GetLoginHistory(steamLoginState);
var result = steamAccountService.GetLoginHistory(SteamLoginState);

Assert.That(result, Is.Not.Null);

Expand Down
10 changes: 1 addition & 9 deletions src/BD.SteamClient8.UnitTest/SteamDBWebApiTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,7 @@ namespace BD.SteamClient8.UnitTest;
/// </summary>
sealed class SteamDBWebApiTest : ServiceTestBase
{
ISteamDbWebApiService steamDbWebApiService = null!;

/// <inheritdoc/>
protected override void ConfigureServices(IServiceCollection services)
{
base.ConfigureServices(services);

services.AddSteamDbWebApiService();
}
static ISteamDbWebApiService steamDbWebApiService = null!;

/// <inheritdoc/>
[SetUp]
Expand Down
8 changes: 0 additions & 8 deletions src/BD.SteamClient8.UnitTest/SteamGridDBWebApiTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@ sealed class SteamGridDBWebApiTest : ServiceTestBase
{
ISteamGridDBWebApiServiceImpl steamGridDBWebApiService = null!;

/// <inheritdoc/>
protected override void ConfigureServices(IServiceCollection services)
{
base.ConfigureServices(services);

services.AddSteamGridDBWebApiService();
}

/// <inheritdoc/>
[SetUp]
public override async ValueTask Setup()
Expand Down
29 changes: 4 additions & 25 deletions src/BD.SteamClient8.UnitTest/SteamIdleServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,6 @@ namespace BD.SteamClient8.UnitTest;
sealed class SteamIdleServiceTest : ServiceTestBase
{
ISteamIdleCardService steamIdleCardService = null!;
ISteamAccountService steamAccountService = null!;
ISteamSessionService steamSessionService = null!;
IConfiguration configuration = null!;

/// <inheritdoc/>
protected override void ConfigureServices(IServiceCollection services)
{
base.ConfigureServices(services);

services.AddSteamAccountService();
services.AddSteamIdleCardService();
services.AddSteamAuthenticatorService();
}

/// <inheritdoc/>
[SetUp]
Expand All @@ -27,26 +14,18 @@ public override async ValueTask Setup()
await base.Setup();

steamIdleCardService = GetRequiredService<ISteamIdleCardService>();
steamAccountService = GetRequiredService<ISteamAccountService>();
steamSessionService = GetRequiredService<ISteamSessionService>();
configuration = GetRequiredService<IConfiguration>();

_ = await GetSteamAuthenticatorAsync(configuration, GetRequiredService<ISteamAuthenticatorService>());
_ = await GetSteamLoginStateAsync(configuration, steamAccountService, steamSessionService);

Assert.That(steamSessionService.RentSession("76561199494800019"), Is.Not.Null);
}

/// <summary>
/// 测试获取用户徽章和卡片数据
/// </summary>
/// <param name="steam_id"></param>
/// <returns></returns>
[TestCase("76561199494800019")]
[Test]
public async Task TestGetBadgesAsync(string steam_id)
public async Task TestGetBadgesAsync()
{
var rsp = await steamIdleCardService.GetBadgesAsync(steam_id);
Assert.That(SteamLoginState?.SteamId, Is.Not.Null);
var rsp = await steamIdleCardService.GetBadgesAsync(SteamLoginState.SteamId.ToString());

Assert.That(rsp, Is.Not.Null);
Assert.Multiple(() =>
Expand Down Expand Up @@ -86,7 +65,7 @@ public async Task TestGetAppCardsAvgPrice(uint[] appIds, string currency)
/// <returns></returns>
[TestCase(730U, "CNY")]
[Test]
public async Task TestGetAppCardsAvgPrice(uint appId, string currency)
public async Task TestGetAppCardsMarketPrice(uint appId, string currency)
{
var rsp = await steamIdleCardService.GetCardsMarketPrice(appId, currency);

Expand Down
Loading

0 comments on commit 8f6db14

Please sign in to comment.