Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
teraa committed Aug 20, 2024
1 parent 5e9b22b commit 6eac668
Show file tree
Hide file tree
Showing 2 changed files with 209 additions and 0 deletions.
4 changes: 4 additions & 0 deletions BattleTrace.Tests/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using BattleTrace.Data;
using Microsoft.Extensions.DependencyInjection;

namespace BattleTrace.Tests;
Expand Down Expand Up @@ -36,4 +37,7 @@ public static IServiceCollection RemoveService<TService>(this IServiceCollection
public static T GetRequiredService<T>(this IServiceScope scope)
where T : notnull
=> scope.ServiceProvider.GetRequiredService<T>();

public static AppDbContext GetContext(this IServiceScope scope)
=> scope.GetRequiredService<AppDbContext>();
}
205 changes: 205 additions & 0 deletions BattleTrace.Tests/Players/IndexTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,209 @@ await AddPlayers(
]
);
}

[Fact]
public async Task IdsQuery_ReturnsMatching()
{
// Arrange
using (var scope = CreateScope())
{
var ctx = scope.GetContext();

var server = Server with {Id = "s1"};
var player = Player.Db with {Server = server};

ctx.Servers.Add(server);

ctx.Players.AddRange(
[
player with {Id = "a"},
player with {Id = "b"},
player with {Id = "c"},
]
);

await ctx.SaveChangesAsync();
}

var query = new Index.Query(Ids: ["a", "b"]);


// Act
var result = await Send(query);


result.Should().BeOfType<Ok<List<Index.Result>>>()
.Subject.Value.Should().Equal(
[
Player.Api with {Id = "a", ServerId = "s1"},
Player.Api with {Id = "b", ServerId = "s1"},
]
);
}

[Fact]
public async Task ActiveOnlyQuery_ReturnsActive()
{
// Arrange
var time = DateTimeOffset.Parse("2000-01-01T00:00Z");
var second = TimeSpan.FromSeconds(1);

using (var scope = CreateScope())
{
var ctx = scope.GetContext();

var server = Server with {Id = "s1"};
var player = Player.Db with {Server = server};

ctx.Players.AddRange(
[
player with {Id = "a", UpdatedAt = time + second},
player with {Id = "b", UpdatedAt = time},
player with {Id = "c", UpdatedAt = time - second},
]
);

ctx.PlayerScans.Add(
new PlayerScan
{
PlayerCount = 1,
Timestamp = time,
}
);

await ctx.SaveChangesAsync();
}

var query = new Index.Query(ActiveOnly: true);


// Act
var result = await Send(query);
result.Should().BeOfType<Ok<List<Index.Result>>>()
.Subject.Value.Should().BeEquivalentTo(
[
Player.Api with {Id = "a", ServerId = "s1", UpdatedAt = time + second},
Player.Api with {Id = "b", ServerId = "s1", UpdatedAt = time},
]
);
}

[Fact]
public async Task NamePatternQuery_ReturnsMatching()
{
// Arrange
using (var scope = CreateScope())
{
var ctx = scope.GetContext();

var server = Server with {Id = "s1"};
var player = Player.Db with {Server = server};

ctx.Servers.Add(server);

ctx.Players.AddRange(
[
player with {Id = "a", Name = "foo", NormalizedName = "foo"},
player with {Id = "b", Name = "bar", NormalizedName = "bar"},
player with {Id = "c", Name = "baz!", NormalizedName = "baz!"},
]
);

await ctx.SaveChangesAsync();
}

var query = new Index.Query(NamePattern: "ba?");


// Act
var result = await Send(query);


result.Should().BeOfType<Ok<List<Index.Result>>>()
.Subject.Value.Should().Equal(
[
Player.Api with {Id = "b", ServerId = "s1", Name = "bar"},
]
);
}

[Fact]
public async Task TagPatternQuery_ReturnsMatching()
{
// Arrange
using (var scope = CreateScope())
{
var ctx = scope.GetContext();

var server = Server with {Id = "s1"};
var player = Player.Db with {Server = server};

ctx.Servers.Add(server);

ctx.Players.AddRange(
[
player with {Id = "a", Tag = "foo"},
player with {Id = "b", Tag = "bar"},
player with {Id = "c", Tag = "baz!"},
]
);

await ctx.SaveChangesAsync();
}

var query = new Index.Query(TagPattern: "ba?");


// Act
var result = await Send(query);


result.Should().BeOfType<Ok<List<Index.Result>>>()
.Subject.Value.Should().Equal(
[
Player.Api with {Id = "b", ServerId = "s1", Tag = "bar"},
]
);
}

[Fact]
public async Task Limit_ReturnsAtMostLimit()
{
// Arrange
using (var scope = CreateScope())
{
var ctx = scope.GetContext();

var server = Server with {Id = "s1"};
var players = Player.Db with {Server = server};

ctx.Servers.Add(server);
ctx.Players.AddRange(
[
players with {Id = "a"},
players with {Id = "b"},
players with {Id = "c"},
]
);

await ctx.SaveChangesAsync();
}

var query = new Index.Query(Limit: 2);


// Act
var result = await Send(query);


// Assert
result.Should().BeOfType<Ok<List<Index.Result>>>()
.Subject.Value.Should().Equal(
[
Player.Api with {Id = "a", ServerId = "s1"},
Player.Api with {Id = "b", ServerId = "s1"},
]
);
}
}

0 comments on commit 6eac668

Please sign in to comment.