Skip to content

Commit

Permalink
test: add simple tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bsdayo committed May 5, 2023
1 parent 73c904d commit aa11917
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ obj/
riderModule.iml
/_ReSharper.Caches/
.idea/
*.user
14 changes: 14 additions & 0 deletions UnofficialArcaeaAPI.Lib.Tests/TestAssetsApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using UnofficialArcaeaAPI.Lib.Models;

namespace UnofficialArcaeaAPI.Lib.Tests;

public class TestAssetsApi : TestBase
{
[Fact]
public async Task TestAff()
{
var affText = await DefaultClient.Assets.GetAffAsync("inkarusi", AuaSongQueryType.SongId);

Assert.Contains("arctap", affText);
}
}
17 changes: 17 additions & 0 deletions UnofficialArcaeaAPI.Lib.Tests/TestBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace UnofficialArcaeaAPI.Lib.Tests;

public abstract class TestBase
{
public static string? ApiUrl => Environment.GetEnvironmentVariable("UAA_API_URL");
public static string? Token => Environment.GetEnvironmentVariable("UAA_TOKEN");
public static string? UserAgent => "UnofficialArcaeaAPI.Lib Unit Test";
public static TimeSpan Timeout => TimeSpan.FromSeconds(60);

public static UaaClient DefaultClient => new(new UaaClientOptions
{
ApiUrl = ApiUrl,
Token = Token,
UserAgent = UserAgent,
Timeout = Timeout,
});
}
28 changes: 28 additions & 0 deletions UnofficialArcaeaAPI.Lib.Tests/TestUserApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using UnofficialArcaeaAPI.Lib.Models;

namespace UnofficialArcaeaAPI.Lib.Tests;

public class TestUserApi : TestBase
{
[Fact]
public async Task TestInfo()
{
var userInfo = await DefaultClient.User.GetInfoAsync("ToasterKoishi", 1, AuaReplyWith.All);

Assert.True(userInfo.AccountInfo.Rating > 1200);

Assert.NotNull(userInfo.RecentScore);
Assert.Single(userInfo.RecentScore);

Assert.NotNull(userInfo.SongInfo);
}

[Fact]
public async Task TestBestsSession()
{
var sessionInfo = await DefaultClient.User.GetBestsSessionAsync("ToasterKoishi");

Assert.True(sessionInfo.SessionInfo.Contains('@'));
Assert.True(Guid.TryParse(sessionInfo.SessionInfo[2..], out _));
}
}
29 changes: 29 additions & 0 deletions UnofficialArcaeaAPI.Lib.Tests/UnofficialArcaeaAPI.Lib.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.2.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\UnofficialArcaeaAPI.Lib\UnofficialArcaeaAPI.Lib.csproj" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions UnofficialArcaeaAPI.Lib.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Xunit;
6 changes: 6 additions & 0 deletions UnofficialArcaeaAPI.Lib.sln
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnofficialArcaeaAPI.Lib", "UnofficialArcaeaAPI.Lib\UnofficialArcaeaAPI.Lib.csproj", "{D3A220B7-7DB5-4A60-A3A7-F8DD97EDFDFD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnofficialArcaeaAPI.Lib.Tests", "UnofficialArcaeaAPI.Lib.Tests\UnofficialArcaeaAPI.Lib.Tests.csproj", "{A67CE8F2-75FA-42AC-9B6A-AA9BDC4462EA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -12,5 +14,9 @@ Global
{D3A220B7-7DB5-4A60-A3A7-F8DD97EDFDFD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D3A220B7-7DB5-4A60-A3A7-F8DD97EDFDFD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D3A220B7-7DB5-4A60-A3A7-F8DD97EDFDFD}.Release|Any CPU.Build.0 = Release|Any CPU
{A67CE8F2-75FA-42AC-9B6A-AA9BDC4462EA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A67CE8F2-75FA-42AC-9B6A-AA9BDC4462EA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A67CE8F2-75FA-42AC-9B6A-AA9BDC4462EA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A67CE8F2-75FA-42AC-9B6A-AA9BDC4462EA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
14 changes: 10 additions & 4 deletions UnofficialArcaeaAPI.Lib/Core/UaaAssetsApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public Task<byte[]> GetSongAsync(string songName, ArcaeaDifficulty difficulty)

#region /assets/aff

private async Task<byte[]> GetAffAsyncCore(string songName, AuaSongQueryType queryType,
private async Task<string> GetAffAsyncCore(string songName, AuaSongQueryType queryType,
ArcaeaDifficulty difficulty)
{
var qb = new QueryBuilder();
Expand All @@ -126,7 +126,13 @@ private async Task<byte[]> GetAffAsyncCore(string songName, AuaSongQueryType que
qb.Add("difficulty", ((int)difficulty).ToString());

var resp = await _client.GetAsync("assets/aff" + qb.Build());
return await EnsureSuccess(resp);
if (resp.StatusCode != HttpStatusCode.OK)
{
var errJson = JsonSerializer.Deserialize<UaaResponse>(await resp.Content.ReadAsStringAsync())!;
throw new UaaRequestException(errJson.Status, errJson.Message!);
}

return await resp.Content.ReadAsStringAsync();
}

/// <summary>
Expand All @@ -137,7 +143,7 @@ private async Task<byte[]> GetAffAsyncCore(string songName, AuaSongQueryType que
/// <param name="difficulty">Song difficulty</param>
/// <returns>Byte array represents the image</returns>
/// <remarks>It is not recommended to use this API frequently, and this API only returns affs from the installation package.</remarks>
public Task<byte[]> GetAffAsync(string songName, AuaSongQueryType queryType = AuaSongQueryType.SongName,
public Task<string> GetAffAsync(string songName, AuaSongQueryType queryType = AuaSongQueryType.SongName,
ArcaeaDifficulty difficulty = ArcaeaDifficulty.Future)
=> GetAffAsyncCore(songName, queryType, difficulty);

Expand All @@ -148,7 +154,7 @@ public Task<byte[]> GetAffAsync(string songName, AuaSongQueryType queryType = Au
/// <param name="difficulty">Song difficulty</param>
/// <returns>Byte array represents the image</returns>
/// <remarks>It is not recommended to use this API frequently, and this API only returns affs from the installation package.</remarks>
public Task<byte[]> GetAffAsync(string songName, ArcaeaDifficulty difficulty)
public Task<string> GetAffAsync(string songName, ArcaeaDifficulty difficulty)
=> GetAffAsyncCore(songName, AuaSongQueryType.SongName, difficulty);

#endregion /assets/aff
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
namespace UnofficialArcaeaAPI.Lib.Responses;
using System.Text.Json.Serialization;

namespace UnofficialArcaeaAPI.Lib.Responses;

public sealed class UaaUserBestsSessionContent
{
[JsonPropertyName("session_info")]
public string SessionInfo { get; init; } = null!;

public bool IsCacheSession { get; internal set; }
Expand Down
2 changes: 1 addition & 1 deletion UnofficialArcaeaAPI.Lib/Responses/UaaUserInfoContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public class UaaUserInfoContent
[JsonPropertyName("recent_score")]
public UaaRecord[]? RecentScore { get; set; }

[JsonPropertyName("songinfo")]
[JsonPropertyName("song_info")]
public UaaChartInfo[]? SongInfo { get; set; }
}
12 changes: 6 additions & 6 deletions UnofficialArcaeaAPI.Lib/UnofficialArcaeaAPI.Lib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

<PropertyGroup>
<Title>UnofficialArcaeaAPI.Lib</Title>
<PackageVersion>2.2.0</PackageVersion>
<Authors>b1acksoil</Authors>
<Description>Lib for ArcaeaUnlimitedAPI.</Description>
<PackageTags>Arcaea</PackageTags>
<PackageVersion>3.0.0</PackageVersion>
<Authors>bsdayo</Authors>
<Description>API wrapper for UnofficialArcaeaAPI.</Description>
<PackageTags>arcaea;api</PackageTags>

<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>1591</NoWarn>

<PackageProjectUrl>https://github.com/Arcaea-Infinity/ArcaeaUnlimitedAPI.Lib</PackageProjectUrl>
<RepositoryUrl>https://github.com/Arcaea-Infinity/ArcaeaUnlimitedAPI.Lib.git</RepositoryUrl>
<PackageProjectUrl>https://github.com/Arcaea-Infinity/UnofficialArcaeaAPI.Lib</PackageProjectUrl>
<RepositoryUrl>https://github.com/Arcaea-Infinity/UnofficialArcaeaAPI.Lib.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
</PropertyGroup>
Expand Down

0 comments on commit aa11917

Please sign in to comment.