Skip to content

Commit

Permalink
add test to ensure tg data parsing works, fix it in general, yippee (#64
Browse files Browse the repository at this point in the history
)
  • Loading branch information
bobbah authored Jan 31, 2025
1 parent 4d074af commit a9e3824
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
15 changes: 9 additions & 6 deletions CentCom.Server/Services/TgBanService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ public class TgBanService : RestBanService
{
private static readonly BanSource BanSource = new BanSource { Name = "tgstation" };

public static readonly JsonSerializerOptions JsonOptions = new()
{
PropertyNameCaseInsensitive = true,
Converters = { new JsonStringEnumConverter() },
NumberHandling = JsonNumberHandling.AllowReadingFromString
};

public TgBanService(ILogger<TgBanService> logger) : base(logger)
{
// Re-initialize to control JSON serialization behaviour
InitializeClient(o =>o.UseSystemTextJson(new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
Converters = { new JsonStringEnumConverter() }
}));
InitializeClient(o => o.UseSystemTextJson(JsonOptions));
}

protected override string BaseUrl => "https://statbus.space/";
Expand All @@ -50,7 +53,7 @@ public async Task<IEnumerable<Ban>> GetBansBatchedAsync()
var bans = await GetBansAsync(page);
if (bans.Count == 0)
break;

allBans.AddRange(bans);
page++;
}
Expand Down
1 change: 1 addition & 0 deletions CentCom.Test/BanServices/TgBanSample.json

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions CentCom.Test/BanServices/TgBanServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using CentCom.Server.External;
using CentCom.Server.External.Raw;
using CentCom.Server.Services;
using Xunit;

namespace CentCom.Test.BanServices;

public class TgBanServiceTests
{
[Fact]
public async Task TgBans_ShouldParseData()
{
var testData = await File.ReadAllTextAsync("BanServices/TgBanSample.json");
var result = JsonSerializer.Deserialize<TgApiResponse>(testData, TgBanService.JsonOptions);
var banData = result.Data.Select(x => x.AsBan(null));
Assert.NotNull(result);
Assert.NotEmpty(banData);
}
}
6 changes: 6 additions & 0 deletions CentCom.Test/CentCom.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,10 @@
<ProjectReference Include="..\CentCom.Server\CentCom.Server.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="BanServices\TgBanSample.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>

0 comments on commit a9e3824

Please sign in to comment.