Adding unit tests

This commit is contained in:
hz
2025-06-04 21:33:44 -05:00
parent 638d73561d
commit ec38498957
17 changed files with 468 additions and 33 deletions
+2 -2
View File
@@ -17,13 +17,13 @@ public class BlacklistService
_blacklist = db.GetCollection<BlacklistedToken>("BlacklistedTokens");
}
public async Task AddToBlacklistAsync(string token, DateTime expiry)
public virtual async Task AddToBlacklistAsync(string token, DateTime expiry)
{
var entry = new BlacklistedToken { Token = token, Expiry = expiry };
await _blacklist.InsertOneAsync(entry);
}
public async Task<bool> IsTokenBlacklistedAsync(string token)
public virtual async Task<bool> IsTokenBlacklistedAsync(string token)
{
var exists = await _blacklist.Find(x => x.Token == token && x.Expiry > DateTime.UtcNow).FirstOrDefaultAsync();
return exists != null;
+4 -4
View File
@@ -17,22 +17,22 @@ public class ItemService
_items = db.GetCollection<Item>("Items");
}
public async Task CreateAsync(Item item)
public virtual async Task CreateAsync(Item item)
{
await _items.InsertOneAsync(item);
}
public async Task<List<Item>> GetByLocationAsync(string locationId)
public virtual async Task<List<Item>> GetByLocationAsync(string locationId)
{
return await _items.Find(i => i.LocationId == locationId).ToListAsync();
}
public async Task<Item> GetByIdAsync(string id)
public virtual async Task<Item> GetByIdAsync(string id)
{
return await _items.Find(i => i.Id == id).FirstOrDefaultAsync();
}
public async Task UpdateAsync(Item item)
public virtual async Task UpdateAsync(Item item)
{
await _items.ReplaceOneAsync(i => i.Id == item.Id, item);
}
+10 -10
View File
@@ -17,7 +17,7 @@ public class LocationService
_locations = db.GetCollection<Location>("Locations");
}
public async Task<Location> GetByIdAsync(string id)
public virtual async Task<Location> GetByIdAsync(string id)
{
if (!ObjectId.TryParse(id, out _))
return null; // prevent crash if id is invalid
@@ -25,43 +25,43 @@ public class LocationService
return await _locations.Find(x => x.Id == id).FirstOrDefaultAsync();
}
public async Task CreateAsync(Location loc) =>
public virtual async Task CreateAsync(Location loc) =>
await _locations.InsertOneAsync(loc);
public async Task DeleteAsync(string id) =>
public virtual async Task DeleteAsync(string id) =>
await _locations.DeleteOneAsync(loc => loc.Id == id);
public async Task UpdateAsync(Location loc) =>
public virtual async Task UpdateAsync(Location loc) =>
await _locations.ReplaceOneAsync(l => l.Id == loc.Id, loc);
public async Task AddPlayerToLocationAsync(string locationId, string playerId)
public virtual async Task AddPlayerToLocationAsync(string locationId, string playerId)
{
var update = Builders<Location>.Update.AddToSet(l => l.Players, playerId);
await _locations.UpdateOneAsync(l => l.Id == locationId, update);
}
public async Task<Location> GetByCoordinatesAsync(int x, int y, int z)
public virtual async Task<Location> GetByCoordinatesAsync(int x, int y, int z)
{
return await _locations.Find(loc => loc.X == x && loc.Y == y && loc.Z == z).FirstOrDefaultAsync();
}
public async Task<Location> GetSpawnLocationAsync()
public virtual async Task<Location> GetSpawnLocationAsync()
{
return await _locations.Find(loc => loc.X == 0 && loc.Y == 0).FirstOrDefaultAsync();
}
public async Task RemovePlayerFromLocationAsync(string locationId, string playerId)
public virtual async Task RemovePlayerFromLocationAsync(string locationId, string playerId)
{
var update = Builders<Location>.Update.Pull(l => l.Players, playerId);
await _locations.UpdateOneAsync(l => l.Id == locationId, update);
}
public async Task AddItemToLocationAsync(string locationId, string itemId)
public virtual async Task AddItemToLocationAsync(string locationId, string itemId)
{
var update = Builders<Location>.Update.Push(l => l.Content, itemId);
await _locations.UpdateOneAsync(l => l.Id == locationId, update);
}
public async Task RemoveItemFromLocationAsync(string locationId, string itemId)
public virtual async Task RemoveItemFromLocationAsync(string locationId, string itemId)
{
var update = Builders<Location>.Update.Pull(l => l.Content, itemId);
await _locations.UpdateOneAsync(l => l.Id == locationId, update);
+6 -6
View File
@@ -18,26 +18,26 @@ public class PlayerService
_players = db.GetCollection<Player>("Players");
}
public async Task<Player> GetByUserIdAsync(string userId) =>
public virtual async Task<Player> GetByUserIdAsync(string userId) =>
await _players.Find(p => p.UserId == userId).FirstOrDefaultAsync();
public async Task<List<Player>> GetByLocationIdAsync(string locationId)
public virtual async Task<List<Player>> GetByLocationIdAsync(string locationId)
{
return await _players.Find(p => p.LocationId == locationId).ToListAsync();
}
public async Task<Player> GetByIdAsync(string id) =>
public virtual async Task<Player> GetByIdAsync(string id) =>
await _players.Find(p => p.Id == id).FirstOrDefaultAsync();
public async Task CreateAsync(Player player) =>
public virtual async Task CreateAsync(Player player) =>
await _players.InsertOneAsync(player);
public async Task UpdateAsync(Player player)
public virtual async Task UpdateAsync(Player player)
{
await _players.ReplaceOneAsync(p => p.Id == player.Id, player);
}
public async Task DeleteAsync(string id)
public virtual async Task DeleteAsync(string id)
{
await _players.DeleteOneAsync(p => p.Id == id);
}
+2 -2
View File
@@ -17,12 +17,12 @@ public class SpawnableService
_spawnables = db.GetCollection<Spawnable>("Spawnables");
}
public async Task CreateAsync(Spawnable spawnable)
public virtual async Task CreateAsync(Spawnable spawnable)
{
await _spawnables.InsertOneAsync(spawnable);
}
public async Task<List<Spawnable>> GetAllAsync()
public virtual async Task<List<Spawnable>> GetAllAsync()
{
return await _spawnables.Find(_ => true).ToListAsync();
}
+5 -5
View File
@@ -17,21 +17,21 @@ public class UserService
_users = database.GetCollection<User>("Users");
}
public async Task<User> GetByUsernameAsync(string username) =>
public virtual async Task<User> GetByUsernameAsync(string username) =>
await _users.Find(u => u.Username == username).FirstOrDefaultAsync();
public async Task<List<User>> GetAllAsync() =>
public virtual async Task<List<User>> GetAllAsync() =>
await _users.Find(_ => true).ToListAsync();
public async Task<bool> DeleteAsync(string username)
public virtual async Task<bool> DeleteAsync(string username)
{
var result = await _users.DeleteOneAsync(u => u.Username == username);
return result.DeletedCount > 0;
}
public async Task CreateAsync(User user) =>
public virtual async Task CreateAsync(User user) =>
await _users.InsertOneAsync(user);
public async Task UpdateAsync(User user) =>
public virtual async Task UpdateAsync(User user) =>
await _users.ReplaceOneAsync(u => u.Id == user.Id, user);
}