From 8dbbd98d9271a9cc152729549791e28c080d0458 Mon Sep 17 00:00:00 2001 From: MSWS Date: Sat, 7 Sep 2024 00:53:07 -0700 Subject: [PATCH] Reorganize --- Commands/CommandManager.cs | 1 + Commands/{gang => Gang}/CreateCommand.cs | 3 +- Commands/{gang => Gang}/HelpCommand.cs | 2 +- Commands/GangCommand.cs | 3 +- Gangs/GangServiceCollection.cs | 4 +- GangsAPI/Services/{ => Gang}/IGangManager.cs | 2 +- .../Services/{ => Gang}/IGangStatManager.cs | 4 +- .../Services/{ => Player}/IPlayerManager.cs | 2 +- .../{ => Player}/IPlayerStatManager.cs | 20 +- GangsImpl/AbstractDB/AbstractDBGangManager.cs | 1 + GangsImpl/AbstractDB/AbstractDBStatManager.cs | 2 +- .../AbstractInstanceManager.cs} | 49 ++--- .../Mock/{MockStatManager.cs => Creation.cs} | 2 +- GangsImpl/Mock/MemoryImpl.cs | 2 +- GangsImpl/Mock/MockGangManager.cs | 2 + GangsImpl/Mock/MockInstanceStatManager.cs | 16 +- GangsImpl/Mock/MockPlayerManager.cs | 1 + GangsImpl/SQL/SQLGangInstanceManager.cs | 55 ++++++ GangsImpl/SQL/SQLGangManager.cs | 1 + GangsImpl/SQLite/SQLiteGangManager.cs | 1 + .../{ => Data/Command}/CommandWrapperTests.cs | 34 ++-- .../{ => API}/Permissions/GangRankTests.cs | 2 +- GangsTest/API/PlayerWrapperTests.cs | 28 +-- .../Commands/Command/Concrete}/CreateTests.cs | 73 +++---- .../Commands/Command/Concrete/GangTests.cs} | 7 +- .../Services/Commands/Command}/FieldTests.cs | 6 +- .../Services/Commands/Command}/LogicTests.cs | 14 +- .../Commands/Command}/PermissionTests.cs | 26 +-- .../Services/Commands/Command/TestData.cs} | 10 +- .../Services/Commands/Command/TestParent.cs} | 6 +- .../Commands/CommandManager}/AliasTests.cs | 12 +- .../CommandManager/PlayerOnlyTests.cs} | 6 +- .../CommandManager}/RegistrationTests.cs | 12 +- .../Commands/CommandManager/TestData.cs} | 10 +- .../Commands/CommandManager/TestParent.cs} | 4 +- .../Services/Gang/CreationTests.cs} | 17 +- .../Services/Gang/FetchTests.cs} | 17 +- GangsTest/API/Services/Gang/FieldTests.cs | 13 ++ .../Services/Gang/MutabilityTests.cs} | 14 +- .../Services/Gang/TestData.cs} | 8 +- .../Services/Gang/UpdateTests.cs} | 16 +- .../Services/Stat/Concrete/ConcreteTests.cs} | 6 +- .../Services/Stat/Concrete/TestData.cs} | 4 +- GangsTest/API/Services/Stat/EqualityTests.cs | 51 +++++ .../Services/Stat/FieldTests.cs} | 26 +-- .../Services/Stat/Instance/EqualityTests.cs} | 4 +- .../Stat/Instance/Gang/GangInstanceTests.cs | 119 ++++++++++++ .../Services/Stat/Instance/Gang/TestData.cs} | 14 +- .../Player/PlayerInstanceManagerData.cs | 25 +++ .../Services/Stat/Instance/Player/TestData.cs | 15 ++ .../API/Services/Stat/Instance/TestParent.cs | 71 +++++++ .../Services/Stat/Manager/CreationTests.cs} | 12 +- .../Stat/Manager/RegistrationTests.cs} | 22 +-- .../Services/Stat/Manager/RetainTests.cs} | 34 ++-- .../Services/Stat/Manager/TestData.cs} | 8 +- .../Services/Stat/PersistenceTests.cs} | 14 +- GangsTest/GangTests/GangFieldTests.cs | 13 -- GangsTest/GangTests/GangTestUtil.cs | 12 -- GangsTest/GangsTest.csproj | 2 +- GangsTest/Startup.cs | 4 +- .../InstanceManageTests/GangInstanceTests.cs | 183 ------------------ GangsTest/StatTests/StatEqualityTests.cs | 51 ----- GangsTest/StatTests/StatTestUtil.cs | 12 -- 63 files changed, 649 insertions(+), 561 deletions(-) rename Commands/{gang => Gang}/CreateCommand.cs (95%) rename Commands/{gang => Gang}/HelpCommand.cs (94%) rename GangsAPI/Services/{ => Gang}/IGangManager.cs (98%) rename GangsAPI/Services/{ => Gang}/IGangStatManager.cs (92%) rename GangsAPI/Services/{ => Player}/IPlayerManager.cs (98%) rename GangsAPI/Services/{ => Player}/IPlayerStatManager.cs (64%) rename GangsImpl/{SQL/SQLInstanceManager.cs => AbstractDB/AbstractInstanceManager.cs} (67%) rename GangsImpl/Mock/{MockStatManager.cs => Creation.cs} (96%) create mode 100644 GangsImpl/SQL/SQLGangInstanceManager.cs rename GangsTest/API/{ => Data/Command}/CommandWrapperTests.cs (75%) rename GangsTest/{ => API}/Permissions/GangRankTests.cs (95%) rename GangsTest/{Commands => API/Services/Commands/Command/Concrete}/CreateTests.cs (57%) rename GangsTest/{Commands/GangCommandTests.cs => API/Services/Commands/Command/Concrete/GangTests.cs} (85%) rename GangsTest/{Commands => API/Services/Commands/Command}/FieldTests.cs (76%) rename GangsTest/{Commands => API/Services/Commands/Command}/LogicTests.cs (73%) rename GangsTest/{Commands => API/Services/Commands/Command}/PermissionTests.cs (83%) rename GangsTest/{Commands/CommandsData.cs => API/Services/Commands/Command/TestData.cs} (76%) rename GangsTest/{Commands/CommandTests.cs => API/Services/Commands/Command/TestParent.cs} (69%) rename GangsTest/{Commands/ManagerTests => API/Services/Commands/CommandManager}/AliasTests.cs (76%) rename GangsTest/{Commands/ManagerTests/ManagerHandling.cs => API/Services/Commands/CommandManager/PlayerOnlyTests.cs} (83%) rename GangsTest/{Commands/ManagerTests => API/Services/Commands/CommandManager}/RegistrationTests.cs (77%) rename GangsTest/{Commands/ManagerTests/CommandManagerData.cs => API/Services/Commands/CommandManager/TestData.cs} (65%) rename GangsTest/{Commands/ManagerTests/ManagerTests.cs => API/Services/Commands/CommandManager/TestParent.cs} (91%) rename GangsTest/{GangTests/GangCreationTests.cs => API/Services/Gang/CreationTests.cs} (85%) rename GangsTest/{GangTests/GangFetchTests.cs => API/Services/Gang/FetchTests.cs} (85%) create mode 100644 GangsTest/API/Services/Gang/FieldTests.cs rename GangsTest/{GangTests/GangMutabilityTests.cs => API/Services/Gang/MutabilityTests.cs} (71%) rename GangsTest/{GangTests/GangManagerData.cs => API/Services/Gang/TestData.cs} (84%) rename GangsTest/{GangTests/GangManagerTests.cs => API/Services/Gang/UpdateTests.cs} (69%) rename GangsTest/{StatTests/InstanceTests/InstanceFieldTests.cs => API/Services/Stat/Concrete/ConcreteTests.cs} (52%) rename GangsTest/{StatTests/InstanceTests/StatInstanceData.cs => API/Services/Stat/Concrete/TestData.cs} (75%) create mode 100644 GangsTest/API/Services/Stat/EqualityTests.cs rename GangsTest/{StatTests/StatFieldTests.cs => API/Services/Stat/FieldTests.cs} (55%) rename GangsTest/{StatTests/StatInstanceEqualityTests.cs => API/Services/Stat/Instance/EqualityTests.cs} (94%) create mode 100644 GangsTest/API/Services/Stat/Instance/Gang/GangInstanceTests.cs rename GangsTest/{StatTests/InstanceManageTests/InstanceManageData.cs => API/Services/Stat/Instance/Gang/TestData.cs} (66%) create mode 100644 GangsTest/API/Services/Stat/Instance/Player/PlayerInstanceManagerData.cs create mode 100644 GangsTest/API/Services/Stat/Instance/Player/TestData.cs create mode 100644 GangsTest/API/Services/Stat/Instance/TestParent.cs rename GangsTest/{StatTests/ManageTests/MockStatManagerTests.cs => API/Services/Stat/Manager/CreationTests.cs} (60%) rename GangsTest/{StatTests/ManageTests/StatRegistrationTests.cs => API/Services/Stat/Manager/RegistrationTests.cs} (68%) rename GangsTest/{StatTests/ManageTests/StatRetainTests.cs => API/Services/Stat/Manager/RetainTests.cs} (51%) rename GangsTest/{StatTests/ManageTests/StatManagerData.cs => API/Services/Stat/Manager/TestData.cs} (80%) rename GangsTest/{StatTests/StatPersistenceTests.cs => API/Services/Stat/PersistenceTests.cs} (69%) delete mode 100644 GangsTest/GangTests/GangFieldTests.cs delete mode 100644 GangsTest/GangTests/GangTestUtil.cs delete mode 100644 GangsTest/StatTests/InstanceManageTests/GangInstanceTests.cs delete mode 100644 GangsTest/StatTests/StatEqualityTests.cs delete mode 100644 GangsTest/StatTests/StatTestUtil.cs diff --git a/Commands/CommandManager.cs b/Commands/CommandManager.cs index 8f30212..7be0450 100644 --- a/Commands/CommandManager.cs +++ b/Commands/CommandManager.cs @@ -5,6 +5,7 @@ using GangsAPI.Data.Command; using GangsAPI.Services; using GangsAPI.Services.Commands; +using GangsAPI.Services.Gang; using Mock; namespace Commands; diff --git a/Commands/gang/CreateCommand.cs b/Commands/Gang/CreateCommand.cs similarity index 95% rename from Commands/gang/CreateCommand.cs rename to Commands/Gang/CreateCommand.cs index 278f8d7..d1f716b 100644 --- a/Commands/gang/CreateCommand.cs +++ b/Commands/Gang/CreateCommand.cs @@ -2,8 +2,9 @@ using GangsAPI.Data.Command; using GangsAPI.Services; using GangsAPI.Services.Commands; +using GangsAPI.Services.Gang; -namespace Commands.gang; +namespace Commands.Gang; // create [name] public class CreateCommand(IGangManager gangs) : ICommand { diff --git a/Commands/gang/HelpCommand.cs b/Commands/Gang/HelpCommand.cs similarity index 94% rename from Commands/gang/HelpCommand.cs rename to Commands/Gang/HelpCommand.cs index 4c93599..bbf71e0 100644 --- a/Commands/gang/HelpCommand.cs +++ b/Commands/Gang/HelpCommand.cs @@ -2,7 +2,7 @@ using GangsAPI.Data.Command; using GangsAPI.Services.Commands; -namespace Commands.gang; +namespace Commands.Gang; public class HelpCommand : ICommand { public string Name => "help"; diff --git a/Commands/GangCommand.cs b/Commands/GangCommand.cs index ac38c13..72a5641 100644 --- a/Commands/GangCommand.cs +++ b/Commands/GangCommand.cs @@ -1,9 +1,10 @@ -using Commands.gang; +using Commands.Gang; using CounterStrikeSharp.API.Modules.Utils; using GangsAPI.Data; using GangsAPI.Data.Command; using GangsAPI.Services; using GangsAPI.Services.Commands; +using GangsAPI.Services.Gang; namespace Commands; diff --git a/Gangs/GangServiceCollection.cs b/Gangs/GangServiceCollection.cs index 5aa3751..9a2a0b3 100644 --- a/Gangs/GangServiceCollection.cs +++ b/Gangs/GangServiceCollection.cs @@ -3,6 +3,8 @@ using GangsAPI.Extensions; using GangsAPI.Services; using GangsAPI.Services.Commands; +using GangsAPI.Services.Gang; +using GangsAPI.Services.Player; using Microsoft.Extensions.DependencyInjection; using Mock; @@ -12,7 +14,7 @@ public class GangServiceCollection : IPluginServiceCollection { public void ConfigureServices(IServiceCollection serviceCollection) { serviceCollection.AddPluginBehavior(); serviceCollection.AddPluginBehavior(); - serviceCollection.AddPluginBehavior(); + serviceCollection.AddPluginBehavior(); serviceCollection .AddPluginBehavior(); serviceCollection diff --git a/GangsAPI/Services/IGangManager.cs b/GangsAPI/Services/Gang/IGangManager.cs similarity index 98% rename from GangsAPI/Services/IGangManager.cs rename to GangsAPI/Services/Gang/IGangManager.cs index 59e36a2..6e389bf 100644 --- a/GangsAPI/Services/IGangManager.cs +++ b/GangsAPI/Services/Gang/IGangManager.cs @@ -1,7 +1,7 @@ using GangsAPI.Data; using GangsAPI.Data.Gang; -namespace GangsAPI.Services; +namespace GangsAPI.Services.Gang; /// /// A manager for gangs. Allows for the creation, retrieval, updating, and deletion of gangs. diff --git a/GangsAPI/Services/IGangStatManager.cs b/GangsAPI/Services/Gang/IGangStatManager.cs similarity index 92% rename from GangsAPI/Services/IGangStatManager.cs rename to GangsAPI/Services/Gang/IGangStatManager.cs index 36b25fd..0076f6f 100644 --- a/GangsAPI/Services/IGangStatManager.cs +++ b/GangsAPI/Services/Gang/IGangStatManager.cs @@ -2,9 +2,9 @@ using GangsAPI.Data.Gang; using GangsAPI.Data.Stat; -namespace GangsAPI.Services; +namespace GangsAPI.Services.Gang; -public interface IGangStatManager : ICacher { +public interface IGangStatManager : IPluginBehavior, ICacher { Task<(bool, TV?)> GetForGang(int key, string statId); Task SetForGang(int gangId, string statId, TV value); Task RemoveFromGang(int gangId, string statId); diff --git a/GangsAPI/Services/IPlayerManager.cs b/GangsAPI/Services/Player/IPlayerManager.cs similarity index 98% rename from GangsAPI/Services/IPlayerManager.cs rename to GangsAPI/Services/Player/IPlayerManager.cs index c58d874..a77a924 100644 --- a/GangsAPI/Services/IPlayerManager.cs +++ b/GangsAPI/Services/Player/IPlayerManager.cs @@ -1,7 +1,7 @@ using GangsAPI.Data; using GangsAPI.Data.Gang; -namespace GangsAPI.Services; +namespace GangsAPI.Services.Player; /// /// A manager for players. Allows for the retrieval and creation of players. diff --git a/GangsAPI/Services/IPlayerStatManager.cs b/GangsAPI/Services/Player/IPlayerStatManager.cs similarity index 64% rename from GangsAPI/Services/IPlayerStatManager.cs rename to GangsAPI/Services/Player/IPlayerStatManager.cs index 6c34c18..2ff871b 100644 --- a/GangsAPI/Services/IPlayerStatManager.cs +++ b/GangsAPI/Services/Player/IPlayerStatManager.cs @@ -1,10 +1,10 @@ using GangsAPI.Data; using GangsAPI.Data.Stat; -namespace GangsAPI.Services; +namespace GangsAPI.Services.Player; public interface IPlayerStatManager : IPluginBehavior, ICacher { - Task GetForPlayer(ulong steam, string statId, out TV? result); + Task<(bool, TV?)> GetForPlayer(ulong steam, string statId); Task SetForPlayer(ulong steam, string statId, TV value); Task RemoveFromPlayer(ulong steam, string statId); @@ -12,20 +12,8 @@ public interface IPlayerStatManager : IPluginBehavior, ICacher { #region Get - Task GetForPlayer(PlayerWrapper wrapper, string statId, - out TV? holder) { - return GetForPlayer(wrapper.Steam, statId, out holder); - } - - async Task GetForPlayer(ulong steam, IStat holder) { - var success = await GetForPlayer(steam, holder.StatId, out TV? tmp); - if (!success || tmp == null) return false; - holder.Value = tmp; - return true; - } - - Task GetForPlayer(PlayerWrapper wrapper, IStat holder) { - return GetForPlayer(wrapper.Steam, holder); + Task<(bool, TV?)> GetForPlayer(PlayerWrapper wrapper, string statId) { + return GetForPlayer(wrapper.Steam, statId); } #endregion diff --git a/GangsImpl/AbstractDB/AbstractDBGangManager.cs b/GangsImpl/AbstractDB/AbstractDBGangManager.cs index 45db5fc..a8f7802 100644 --- a/GangsImpl/AbstractDB/AbstractDBGangManager.cs +++ b/GangsImpl/AbstractDB/AbstractDBGangManager.cs @@ -3,6 +3,7 @@ using Dapper; using GangsAPI.Data.Gang; using GangsAPI.Services; +using GangsAPI.Services.Player; using Mock; namespace GenericDB; diff --git a/GangsImpl/AbstractDB/AbstractDBStatManager.cs b/GangsImpl/AbstractDB/AbstractDBStatManager.cs index 7a14de0..0817b4c 100644 --- a/GangsImpl/AbstractDB/AbstractDBStatManager.cs +++ b/GangsImpl/AbstractDB/AbstractDBStatManager.cs @@ -7,7 +7,7 @@ namespace GenericDB; public abstract class AbstractDBStatManager(string connectionString, - string table = "gang_stats", bool testing = false) : MockStatManager { + string table = "gang_stats", bool testing = false) : Creation { private DbConnection connection = null!; private DbTransaction? transaction; diff --git a/GangsImpl/SQL/SQLInstanceManager.cs b/GangsImpl/AbstractDB/AbstractInstanceManager.cs similarity index 67% rename from GangsImpl/SQL/SQLInstanceManager.cs rename to GangsImpl/AbstractDB/AbstractInstanceManager.cs index 2e4ac2a..dfbe4b2 100644 --- a/GangsImpl/SQL/SQLInstanceManager.cs +++ b/GangsImpl/AbstractDB/AbstractInstanceManager.cs @@ -3,31 +3,30 @@ using System.Reflection; using CounterStrikeSharp.API.Core; using Dapper; -using GangsAPI; -using GangsAPI.Services; -using MySqlConnector; -namespace SQLImpl; +namespace GenericDB; -public class SQLInstanceManager(string connectionString, string table_prefix, - string gangsTable, bool testing = false) : IPluginBehavior, IGangStatManager { - private DbConnection connection = null!; +public abstract class AbstractInstanceManager(string connectionString, + string table_prefix, bool testing = false) { + protected DbConnection Connection; + abstract protected string PrimaryKey { get; } + private string primaryTypeString => GetDBType(typeof(TK)); public void ClearCache() { } public Task Load() { return Task.CompletedTask; } - public async Task<(bool, TV?)> GetForGang(int key, string statId) { + public async Task<(bool, TV?)> Get(TK key, string statId) { await createTable(statId); try { - var result = await connection.QuerySingleAsync( - $"SELECT {(typeof(TV).IsPrimitive ? statId : getFieldNames())} FROM {table_prefix}_{statId} WHERE GangId = @GangId", + var result = await Connection.QuerySingleAsync( + $"SELECT {(typeof(TV).IsPrimitive ? statId : getFieldNames())} FROM {table_prefix}_{statId} WHERE {PrimaryKey} = @{PrimaryKey}", new { GangId = key }); return (true, result); } catch (InvalidOperationException) { return (false, default); } } - public async Task SetForGang(int gangId, string statId, TV value) { + public async Task Set(TK key, string statId, TV value) { await createTable(statId); var fields = typeof(TV) .GetProperties(BindingFlags.Public | BindingFlags.Instance) @@ -45,12 +44,12 @@ public async Task SetForGang(int gangId, string statId, TV value) { fields.Select(f => $"{f.Name} = @{f.Name}")); if (typeof(TV).IsPrimitive) onDuplicate = $"{statId} = @{statId}"; var cmd = - $"INSERT INTO {table_prefix}_{statId} (GangId, {columns}) VALUES (@GangId, {values}) ON DUPLICATE KEY UPDATE {onDuplicate}"; + $"INSERT INTO {table_prefix}_{statId} ({PrimaryKey}, {columns}) VALUES (@{PrimaryKey}, {values}) ON DUPLICATE KEY UPDATE {onDuplicate}"; Debug.WriteLine(cmd); var fieldValues = new DynamicParameters(); - fieldValues.Add("@GangId", gangId); + fieldValues.Add("@" + PrimaryKey, key); if (typeof(TV).IsPrimitive) fieldValues.Add($"@{statId}", value); @@ -59,23 +58,25 @@ public async Task SetForGang(int gangId, string statId, TV value) { fieldValues.Add($"@{field.Name}", field.GetValue(value)); Console.WriteLine(cmd, fieldValues); - await connection.ExecuteAsync(cmd, fieldValues); + await Connection.ExecuteAsync(cmd, fieldValues); return true; } - public async Task RemoveFromGang(int gangId, string statId) { + public async Task Remove(TK key, string statId) { try { - await connection.ExecuteAsync( - $"DELETE FROM {table_prefix}_{statId} WHERE GangId = @GangId", - new { GangId = gangId }); + var dynamicParameters = new DynamicParameters(); + dynamicParameters.Add(PrimaryKey, key); + await Connection.ExecuteAsync( + $"DELETE FROM {table_prefix}_{statId} WHERE {PrimaryKey} = @{PrimaryKey}", + dynamicParameters); return true; } catch (DbException) { return false; } } public void Start(BasePlugin? plugin, bool hotReload) { - connection = new MySqlConnection(connectionString); + Connection = CreateDbConnection(connectionString); - connection.Open(); + Connection.Open(); } virtual protected string GetDBType(Type type) { @@ -107,8 +108,8 @@ private async Task createTable(string id) { .ToList(); var cmd = testing ? - $"CREATE TEMPORARY TABLE IF NOT EXISTS {table_prefix}_{id} (GangId INT NOT NULL PRIMARY KEY, " : - $"CREATE TABLE IF NOT EXISTS {table_prefix}_{id} (GangId INT NOT NULL PRIMARY KEY, "; + $"CREATE TEMPORARY TABLE IF NOT EXISTS {table_prefix}_{id} ({PrimaryKey} {primaryTypeString} NOT NULL PRIMARY KEY, " : + $"CREATE TABLE IF NOT EXISTS {table_prefix}_{id} ({PrimaryKey} {primaryTypeString} NOT NULL PRIMARY KEY, "; if (typeof(TV).IsPrimitive) { cmd += $"{id} {GetDBType(typeof(TV))}"; @@ -126,7 +127,7 @@ private async Task createTable(string id) { Console.WriteLine(cmd); - await connection.ExecuteAsync(cmd); + await Connection.ExecuteAsync(cmd); } private string getFieldNames(string prefix = "") { @@ -136,4 +137,6 @@ private string getFieldNames(string prefix = "") { return string.Join(", ", fields.Select(f => $"{prefix}{f.Name}")); } + + abstract protected DbConnection CreateDbConnection(string connectionString); } \ No newline at end of file diff --git a/GangsImpl/Mock/MockStatManager.cs b/GangsImpl/Mock/Creation.cs similarity index 96% rename from GangsImpl/Mock/MockStatManager.cs rename to GangsImpl/Mock/Creation.cs index 89bcfb0..82e9c2a 100644 --- a/GangsImpl/Mock/MockStatManager.cs +++ b/GangsImpl/Mock/Creation.cs @@ -4,7 +4,7 @@ namespace Mock; -public class MockStatManager : IStatManager { +public class Creation : IStatManager { private readonly HashSet backendStats = []; protected readonly HashSet CachedStats = []; diff --git a/GangsImpl/Mock/MemoryImpl.cs b/GangsImpl/Mock/MemoryImpl.cs index f9ca1d7..c2de237 100644 --- a/GangsImpl/Mock/MemoryImpl.cs +++ b/GangsImpl/Mock/MemoryImpl.cs @@ -6,6 +6,6 @@ namespace Mock; public static class MemoryImpl { public static void AddMemoryImpl(this IServiceCollection collection) { - collection.AddPluginBehavior(); + collection.AddPluginBehavior(); } } \ No newline at end of file diff --git a/GangsImpl/Mock/MockGangManager.cs b/GangsImpl/Mock/MockGangManager.cs index 9c50976..cf614ee 100644 --- a/GangsImpl/Mock/MockGangManager.cs +++ b/GangsImpl/Mock/MockGangManager.cs @@ -1,6 +1,8 @@ using CounterStrikeSharp.API.Core; using GangsAPI.Data.Gang; using GangsAPI.Services; +using GangsAPI.Services.Gang; +using GangsAPI.Services.Player; namespace Mock; diff --git a/GangsImpl/Mock/MockInstanceStatManager.cs b/GangsImpl/Mock/MockInstanceStatManager.cs index c236b4a..e46576f 100644 --- a/GangsImpl/Mock/MockInstanceStatManager.cs +++ b/GangsImpl/Mock/MockInstanceStatManager.cs @@ -1,4 +1,6 @@ using GangsAPI.Services; +using GangsAPI.Services.Gang; +using GangsAPI.Services.Player; namespace Mock; @@ -23,16 +25,14 @@ public Task Load() { private readonly Dictionary> cachedPlayerValues = [], backendPlayerValues = []; - public Task - GetForPlayer(ulong steam, string statId, out TV? result) { - result = default; + public Task<(bool, TV?)> GetForPlayer(ulong steam, string statId) { if (!cachedPlayerValues.TryGetValue(steam, out var playerStatMap)) - return Task.FromResult(false); + return Task.FromResult<(bool, TV?)>((false, default)); if (!playerStatMap.TryGetValue(statId, out var value)) - return Task.FromResult(false); - if (value is not TV v) return Task.FromResult(false); - result = v; - return Task.FromResult(true); + return Task.FromResult<(bool, TV?)>((false, default)); + return value is not TV val ? + Task.FromResult<(bool, TV?)>((false, default)) : + Task.FromResult((true, (TV?)val)); } public Task SetForPlayer(ulong steam, string statId, TV value) { diff --git a/GangsImpl/Mock/MockPlayerManager.cs b/GangsImpl/Mock/MockPlayerManager.cs index 70af644..b7d260d 100644 --- a/GangsImpl/Mock/MockPlayerManager.cs +++ b/GangsImpl/Mock/MockPlayerManager.cs @@ -1,5 +1,6 @@ using GangsAPI.Data.Gang; using GangsAPI.Services; +using GangsAPI.Services.Player; namespace Mock; diff --git a/GangsImpl/SQL/SQLGangInstanceManager.cs b/GangsImpl/SQL/SQLGangInstanceManager.cs new file mode 100644 index 0000000..d5f9250 --- /dev/null +++ b/GangsImpl/SQL/SQLGangInstanceManager.cs @@ -0,0 +1,55 @@ +using System.Data.Common; +using GangsAPI; +using GangsAPI.Services; +using GangsAPI.Services.Gang; +using GangsAPI.Services.Player; +using GenericDB; +using MySqlConnector; + +namespace SQLImpl; + +public class SQLGangInstanceManager(string connectionString, + string table_prefix, bool testing = false) + : AbstractInstanceManager(connectionString, table_prefix, testing), + IPluginBehavior, IGangStatManager { + override protected string PrimaryKey => "GangId"; + + public Task<(bool, TV?)> GetForGang(int key, string statId) { + return Get(key, statId); + } + + public Task SetForGang(int gangId, string statId, TV value) { + return Set(gangId, statId, value); + } + + public Task RemoveFromGang(int gangId, string statId) { + return Remove(gangId, statId); + } + + override protected DbConnection CreateDbConnection(string connectionString) { + return new MySqlConnection(connectionString); + } +} + +public class SQLPlayerInstanceManager(string connectionString, + string table_prefix, bool testing = false) + : AbstractInstanceManager(connectionString, table_prefix, testing), + IPlayerStatManager { + override protected string PrimaryKey => "GangId"; + + public Task<(bool, TV?)> GetForPlayer(ulong steam, string statId) { + return Get(steam, statId); + } + + public Task SetForPlayer(ulong steam, string statId, TV value) { + return Set(steam, statId, value); + } + + public Task RemoveFromPlayer(ulong steam, string statId) { + return Remove(steam, statId); + } + + override protected DbConnection CreateDbConnection(string connectionString) { + return new MySqlConnection(connectionString); + } +} \ No newline at end of file diff --git a/GangsImpl/SQL/SQLGangManager.cs b/GangsImpl/SQL/SQLGangManager.cs index c08b7a7..299a8d6 100644 --- a/GangsImpl/SQL/SQLGangManager.cs +++ b/GangsImpl/SQL/SQLGangManager.cs @@ -1,5 +1,6 @@ using System.Data.Common; using GangsAPI.Services; +using GangsAPI.Services.Player; using GenericDB; using MySqlConnector; diff --git a/GangsImpl/SQLite/SQLiteGangManager.cs b/GangsImpl/SQLite/SQLiteGangManager.cs index 24420e2..9f7e949 100644 --- a/GangsImpl/SQLite/SQLiteGangManager.cs +++ b/GangsImpl/SQLite/SQLiteGangManager.cs @@ -3,6 +3,7 @@ using Dapper; using GangsAPI.Data.Gang; using GangsAPI.Services; +using GangsAPI.Services.Player; using GenericDB; using Microsoft.Data.Sqlite; diff --git a/GangsTest/API/CommandWrapperTests.cs b/GangsTest/API/Data/Command/CommandWrapperTests.cs similarity index 75% rename from GangsTest/API/CommandWrapperTests.cs rename to GangsTest/API/Data/Command/CommandWrapperTests.cs index 09a9059..e44041a 100644 --- a/GangsTest/API/CommandWrapperTests.cs +++ b/GangsTest/API/Data/Command/CommandWrapperTests.cs @@ -1,14 +1,14 @@ using GangsAPI.Data; using GangsAPI.Data.Command; -namespace GangsTest.API; +namespace GangsTest.API.Data.Command; public class CommandWrapperTests { private readonly PlayerWrapper testPlayer = new((ulong)new Random().NextInt64(), "Test Player"); [Fact] - public void CommandWrapper_Player() { + public void Fields_Initialized() { var info = new CommandInfoWrapper(testPlayer, 0, "test"); Assert.NotNull(info.CallingPlayer); Assert.Equal(testPlayer.Steam, info.CallingPlayer.Steam); @@ -16,87 +16,87 @@ public void CommandWrapper_Player() { } [Fact] - public void CommandWrapper_Console() { + public void CallingPlayer_IsNull() { var info = new CommandInfoWrapper(null, 0, "test"); Assert.Null(info.CallingPlayer); } [Fact] - public void ArgCount_Single() { + public void ArgCountAt_Zero() { Assert.Equal(1, new CommandInfoWrapper(testPlayer, 0, "test").ArgCount); } [Fact] - public void ArgCount_Single_Space() { + public void ArgCountAt_One() { Assert.Equal(1, new CommandInfoWrapper(testPlayer, 0, "test ing").ArgCount); } [Fact] - public void Offset_ArgCount_Single() { + public void Offset_ArgCountAt_Zero() { Assert.Equal(0, new CommandInfoWrapper(testPlayer, 1, "test").ArgCount); } [Fact] - public void Offset_ArgCount_Space() { + public void Offset_ArgCountAt_One() { Assert.Equal(0, new CommandInfoWrapper(testPlayer, 1, "test ing").ArgCount); } [Fact] - public void Offset_ArgCount_One() { + public void Offset_ArgCount_One_Params() { Assert.Equal(1, new CommandInfoWrapper(testPlayer, 1, "test", "ing").ArgCount); } [Fact] - public void ArgString_Single() { + public void ArgString_IsEmpty() { Assert.Equal("", new CommandInfoWrapper(testPlayer, 0, "test").ArgString); } [Fact] - public void ArgString_Single_Space() { + public void ArgString_SingleWord() { Assert.Equal("ing", new CommandInfoWrapper(testPlayer, 0, "test ing").ArgString); } [Fact] - public void ArgString_Multi() { + public void ArgString_SingleWord_Params() { Assert.Equal("ing", new CommandInfoWrapper(testPlayer, 0, "test", "ing").ArgString); } [Fact] - public void ArgString_Multi_Space() { + public void ArgString_MultiWord_Params() { Assert.Equal("ing foobar", new CommandInfoWrapper(testPlayer, 0, "test", "ing", "foobar").ArgString); } [Fact] - public void CommandString_Simple() { + public void ArgString_MoreWord_Params() { Assert.Equal("test ing foobar", new CommandInfoWrapper(testPlayer, 0, "test", "ing", "foobar") .GetCommandString); } [Fact] - public void CommandString_SubCommand() { + public void ArgIndex_Zero_Params() { Assert.Equal("css_gang", new CommandInfoWrapper(testPlayer, 0, "css_gang", "create", "foobar")[0]); } [Fact] - public void CommandString_SubCommand_Sub() { + public void Offset_ArgIndex_Zero_Params() { Assert.Equal("create", new CommandInfoWrapper(testPlayer, 1, "css_gang", "create", "foobar")[0]); } [Fact] - public void CommandString_SubCommand_Sub1() { + public void Offset_ArgIndex_One_Params() { Assert.Equal("foobar", new CommandInfoWrapper(testPlayer, 1, "css_gang", "create", "foobar")[1]); } [Fact] - public void CommandString_SubCommand_Sub2() { + public void Offset_ArgIndex_Multi_One_Params() { Assert.Equal("create foobar", new CommandInfoWrapper(testPlayer, 1, "css_gang", "create", "foobar") .GetCommandString); diff --git a/GangsTest/Permissions/GangRankTests.cs b/GangsTest/API/Permissions/GangRankTests.cs similarity index 95% rename from GangsTest/Permissions/GangRankTests.cs rename to GangsTest/API/Permissions/GangRankTests.cs index 5d41395..efe8b2c 100644 --- a/GangsTest/Permissions/GangRankTests.cs +++ b/GangsTest/API/Permissions/GangRankTests.cs @@ -1,7 +1,7 @@ using GangsAPI.Permissions; using Mock; -namespace GangsTest.Permissions; +namespace GangsTest.API.Permissions; public class GangRankTests { [Fact] diff --git a/GangsTest/API/PlayerWrapperTests.cs b/GangsTest/API/PlayerWrapperTests.cs index 764dce2..7c3fbf7 100644 --- a/GangsTest/API/PlayerWrapperTests.cs +++ b/GangsTest/API/PlayerWrapperTests.cs @@ -11,7 +11,7 @@ public class PlayerWrapperTests { private static char GROUP_CHAR => PermissionCharacters.GroupPermissionChar; [Fact] - public void PlayerWrapper_Init() { + public void Fields_Initialized() { Assert.Null(testPlayer.Player); Assert.NotNull(testPlayer.Data); Assert.True(testPlayer.Data.Identity == testPlayer.Steam.ToString()); @@ -20,7 +20,7 @@ public void PlayerWrapper_Init() { } [Fact] - public void PlayerWrapper_Init_Flags() { + public void Flags_Initialized() { var player = testPlayer.WithFlags(USER_CHAR + "test/flag"); Assert.NotNull(player.Data); Assert.Single(player.Data.Flags); @@ -28,21 +28,21 @@ public void PlayerWrapper_Init_Flags() { } [Fact] - public void PlayerWrapper_Print_Chat() { + public void Output_Chat_Single() { testPlayer.PrintToChat("Test Message"); Assert.Single(testPlayer.ChatOutput); Assert.Equal("Test Message", testPlayer.ChatOutput[0]); } [Fact] - public void PlayerWrapper_Print_Console() { + public void Output_Console_Single() { testPlayer.PrintToConsole("Test Message"); Assert.Single(testPlayer.ConsoleOutput); Assert.Equal("Test Message", testPlayer.ConsoleOutput[0]); } [Fact] - public void PlayerWrapper_Print_Both() { + public void Output_Both_Single() { testPlayer.PrintToConsole("Test Message A"); testPlayer.PrintToChat("Test Message B"); Assert.Single(testPlayer.ConsoleOutput); @@ -52,7 +52,7 @@ public void PlayerWrapper_Print_Both() { } [Fact] - public void PlayerWrapper_Chat_Order() { + public void Output_Chat_Multi() { testPlayer.PrintToChat("Test Message 1"); testPlayer.PrintToChat("Test Message 2"); Assert.Equal(2, testPlayer.ChatOutput.Count); @@ -61,7 +61,7 @@ public void PlayerWrapper_Chat_Order() { } [Fact] - public void PlayerWrapper_Console_Order() { + public void Output_Console_Multi() { testPlayer.PrintToConsole("Test Message 1"); testPlayer.PrintToConsole("Test Message 2"); Assert.Equal(2, testPlayer.ConsoleOutput.Count); @@ -76,7 +76,7 @@ public void PlayerWrapper_Console_Order() { [InlineData("_test/flag")] [InlineData("@test")] [InlineData("@test/")] - public void PlayerWrapper_Init_InvalidFlag(string flag) { + public void WithInvalidFlag_Throws(string flag) { Assert.ThrowsAny(() => testPlayer.WithFlags(flag)); } @@ -87,33 +87,33 @@ public void PlayerWrapper_Init_InvalidFlag(string flag) { [InlineData("_test/flag")] [InlineData("@test")] [InlineData("@test/")] - public void PlayerWrapper_Test_InvalidFlag(string flag) { + public void HasInvalidFlag_Throws(string flag) { Assert.ThrowsAny(() => testPlayer.HasFlags(flag)); } [Fact] - public void PlayerWrapper_Perm_Flag() { + public void Permission_Pass() { var player = testPlayer.WithFlags(USER_CHAR + "test/flag"); Assert.True(player.HasFlags(USER_CHAR + "test/flag")); Assert.False(player.HasFlags(USER_CHAR + "test/other")); } [Fact] - public void PlayerWrapper_Perm_FlagChild() { + public void Permission_Pass_Child() { var player = testPlayer.WithFlags(USER_CHAR + "test/flag"); Assert.True(player.HasFlags(USER_CHAR + "test/flag")); Assert.True(player.HasFlags(USER_CHAR + "test/flag/child")); } [Fact] - public void PlayerWrapper_Perm_FlagParent() { + public void Permission_Pass_Strict_Child() { var player = testPlayer.WithFlags(USER_CHAR + "test/flag/child"); Assert.False(player.HasFlags(USER_CHAR + "test/flag")); Assert.True(player.HasFlags(USER_CHAR + "test/flag/child")); } [Fact] - public void PlayerWrapper_Perm_FlagRoot() { + public void Permission_Pass_Root() { var player = testPlayer.WithFlags(USER_CHAR + "test/root"); Assert.True(player.HasFlags(USER_CHAR + "test/root")); Assert.True(player.HasFlags(USER_CHAR + "test/flag")); @@ -121,7 +121,7 @@ public void PlayerWrapper_Perm_FlagRoot() { } [Fact] - public void PlayerWrapper_Perm_FlagRootChild() { + public void Permission_Pass_Strict_Root() { var player = testPlayer.WithFlags(USER_CHAR + "test/root"); Assert.True(player.HasFlags(USER_CHAR + "test/root")); Assert.True(player.HasFlags(USER_CHAR + "test/flag")); diff --git a/GangsTest/Commands/CreateTests.cs b/GangsTest/API/Services/Commands/Command/Concrete/CreateTests.cs similarity index 57% rename from GangsTest/Commands/CreateTests.cs rename to GangsTest/API/Services/Commands/Command/Concrete/CreateTests.cs index efca8c1..2492fa4 100644 --- a/GangsTest/Commands/CreateTests.cs +++ b/GangsTest/API/Services/Commands/Command/Concrete/CreateTests.cs @@ -1,13 +1,14 @@ -using Commands.gang; +using Commands.Gang; using GangsAPI.Data; using GangsAPI.Data.Command; using GangsAPI.Services; using GangsAPI.Services.Commands; +using GangsAPI.Services.Gang; -namespace GangsTest.Commands; +namespace GangsTest.API.Services.Commands.Command.Concrete; public class CreateTests(ICommandManager commands, IGangManager gangMgr) - : CommandTests(commands, new CreateCommand(gangMgr)) { + : TestParent(commands, new CreateCommand(gangMgr)) { private readonly PlayerWrapper player = new((ulong)new Random().NextInt64(), "Test Player"); @@ -24,40 +25,40 @@ public async Task Create_NoName() { Assert.Contains("Please provide a name for the gang", player.ConsoleOutput); } - // [Fact] - // public async Task Create_Simple() { - // var gang = await gangMgr.GetGang(player.Steam); - // Assert.Null(gang); - // Assert.Equal(CommandResult.SUCCESS, - // await Commands.ProcessCommand(player, "create", "foobar")); - // gang = await gangMgr.GetGang(player.Steam); - // Assert.NotNull(gang); - // Assert.Equal("foobar", gang.Name); - // Assert.Contains($"Gang 'foobar' (#{gang.GangId}) created successfully", - // player.ConsoleOutput); - // } + [Fact] + public async Task Create_Simple() { + var gang = await gangMgr.GetGang(player.Steam); + Assert.Null(gang); + Assert.Equal(CommandResult.SUCCESS, + await Commands.ProcessCommand(player, "create", "foobar")); + gang = await gangMgr.GetGang(player.Steam); + Assert.NotNull(gang); + Assert.Equal("foobar", gang.Name); + Assert.Contains($"Gang 'foobar' (#{gang.GangId}) created successfully", + player.ConsoleOutput); + } - // [Fact] - // public async Task Create_MultiWord() { - // var gang = await gangMgr.GetGang(player.Steam); - // Assert.Null(gang); - // Assert.Equal(CommandResult.SUCCESS, - // await Commands.ProcessCommand(player, "create", "foo bar")); - // gang = await gangMgr.GetGang(player.Steam); - // Assert.NotNull(gang); - // Assert.Equal("foo bar", gang.Name); - // } - // - // [Fact] - // public async Task Create_MultiParam() { - // var gang = await gangMgr.GetGang(player.Steam); - // Assert.Null(gang); - // Assert.Equal(CommandResult.SUCCESS, - // await Commands.ProcessCommand(player, "create", "foo bar", "baz")); - // gang = await gangMgr.GetGang(player.Steam); - // Assert.NotNull(gang); - // Assert.Equal("foo bar baz", gang.Name); - // } + [Fact] + public async Task Create_MultiWord() { + var gang = await gangMgr.GetGang(player.Steam); + Assert.Null(gang); + Assert.Equal(CommandResult.SUCCESS, + await Commands.ProcessCommand(player, "create", "foo bar")); + gang = await gangMgr.GetGang(player.Steam); + Assert.NotNull(gang); + Assert.Equal("foo bar", gang.Name); + } + + [Fact] + public async Task Create_MultiParam() { + var gang = await gangMgr.GetGang(player.Steam); + Assert.Null(gang); + Assert.Equal(CommandResult.SUCCESS, + await Commands.ProcessCommand(player, "create", "foo bar", "baz")); + gang = await gangMgr.GetGang(player.Steam); + Assert.NotNull(gang); + Assert.Equal("foo bar baz", gang.Name); + } [Fact] public async Task Create_Already_Ganged() { diff --git a/GangsTest/Commands/GangCommandTests.cs b/GangsTest/API/Services/Commands/Command/Concrete/GangTests.cs similarity index 85% rename from GangsTest/Commands/GangCommandTests.cs rename to GangsTest/API/Services/Commands/Command/Concrete/GangTests.cs index bcf09e6..f076cc0 100644 --- a/GangsTest/Commands/GangCommandTests.cs +++ b/GangsTest/API/Services/Commands/Command/Concrete/GangTests.cs @@ -2,11 +2,12 @@ using GangsAPI.Data.Command; using GangsAPI.Services; using GangsAPI.Services.Commands; +using GangsAPI.Services.Gang; -namespace GangsTest.Commands; +namespace GangsTest.API.Services.Commands.Command.Concrete; -public class GangCommandTests(ICommandManager commands, IGangManager gangMgr) - : CommandTests(commands, new GangCommand(gangMgr)) { +public class GangTests(ICommandManager commands, IGangManager gangMgr) + : TestParent(commands, new GangCommand(gangMgr)) { [Fact] public async Task Gang_TestBase() { Assert.Equal("css_gang", Command.Name); diff --git a/GangsTest/Commands/FieldTests.cs b/GangsTest/API/Services/Commands/Command/FieldTests.cs similarity index 76% rename from GangsTest/Commands/FieldTests.cs rename to GangsTest/API/Services/Commands/Command/FieldTests.cs index 0686dba..37353fb 100644 --- a/GangsTest/Commands/FieldTests.cs +++ b/GangsTest/API/Services/Commands/Command/FieldTests.cs @@ -1,10 +1,10 @@ using GangsAPI.Services.Commands; -namespace GangsTest.Commands; +namespace GangsTest.API.Services.Commands.Command; public class FieldTests { [Theory] - [ClassData(typeof(CommandsData))] + [ClassData(typeof(TestData))] public void Fields_Name(ICommand cmd) { Assert.NotEmpty(cmd.Name); Assert.False(cmd.Usage.StartsWith(cmd.Name), @@ -12,7 +12,7 @@ public void Fields_Name(ICommand cmd) { } [Theory] - [ClassData(typeof(CommandsData))] + [ClassData(typeof(TestData))] public void Fields_Aliases(ICommand cmd) { Assert.Contains(cmd.Name, cmd.Aliases); } diff --git a/GangsTest/Commands/LogicTests.cs b/GangsTest/API/Services/Commands/Command/LogicTests.cs similarity index 73% rename from GangsTest/Commands/LogicTests.cs rename to GangsTest/API/Services/Commands/Command/LogicTests.cs index c701635..d8a7510 100644 --- a/GangsTest/Commands/LogicTests.cs +++ b/GangsTest/API/Services/Commands/Command/LogicTests.cs @@ -1,12 +1,12 @@ using GangsAPI.Data.Command; using GangsAPI.Services.Commands; -using GangsTest.Commands.ManagerTests; +using GangsTest.API.Services.Commands.CommandManager; -namespace GangsTest.Commands; +namespace GangsTest.API.Services.Commands.Command; -public class LogicTests : ManagerTests.ManagerTests { +public class LogicTests : CommandManager.TestParent { [Theory] - [ClassData(typeof(CommandManagerData))] + [ClassData(typeof(CommandManager.TestData))] public async Task Command_Logic(ICommandManager mgr) { Assert.True(mgr.RegisterCommand(Dummy)); Assert.Equal(CommandResult.SUCCESS, @@ -14,7 +14,7 @@ public async Task Command_Logic(ICommandManager mgr) { } [Theory] - [ClassData(typeof(CommandManagerData))] + [ClassData(typeof(CommandManager.TestData))] public async Task Command_Logic_Fail(ICommandManager mgr) { Assert.True(mgr.RegisterCommand(Dummy)); Assert.Equal(CommandResult.FAILURE, @@ -22,7 +22,7 @@ public async Task Command_Logic_Fail(ICommandManager mgr) { } [Theory] - [ClassData(typeof(CommandManagerData))] + [ClassData(typeof(CommandManager.TestData))] public async Task Command_CaseInsensitive(ICommandManager mgr) { mgr.RegisterCommand(Dummy); Assert.Equal(CommandResult.SUCCESS, @@ -30,7 +30,7 @@ public async Task Command_CaseInsensitive(ICommandManager mgr) { } [Theory] - [ClassData(typeof(CommandManagerData))] + [ClassData(typeof(CommandManager.TestData))] public async Task Command_CaseInsensitive2(ICommandManager mgr) { mgr.RegisterCommand(Dummy); Assert.Equal(CommandResult.SUCCESS, diff --git a/GangsTest/Commands/PermissionTests.cs b/GangsTest/API/Services/Commands/Command/PermissionTests.cs similarity index 83% rename from GangsTest/Commands/PermissionTests.cs rename to GangsTest/API/Services/Commands/Command/PermissionTests.cs index 85aa201..cfc693c 100644 --- a/GangsTest/Commands/PermissionTests.cs +++ b/GangsTest/API/Services/Commands/Command/PermissionTests.cs @@ -1,13 +1,13 @@ using GangsAPI.Data; using GangsAPI.Data.Command; using GangsAPI.Services.Commands; -using GangsTest.Commands.ManagerTests; +using GangsTest.API.Services.Commands.CommandManager; -namespace GangsTest.Commands; +namespace GangsTest.API.Services.Commands.Command; -public class PermissionTests : ManagerTests.ManagerTests { +public class PermissionTests : CommandManager.TestParent { [Theory] - [ClassData(typeof(CommandManagerData))] + [ClassData(typeof(CommandManager.TestData))] public async Task Permission_Pass(ICommandManager mgr) { mgr.RegisterCommand(Dummy); Assert.Equal(CommandResult.SUCCESS, @@ -15,7 +15,7 @@ public async Task Permission_Pass(ICommandManager mgr) { } [Theory] - [ClassData(typeof(CommandManagerData))] + [ClassData(typeof(CommandManager.TestData))] public async Task Permission_Pass_Flag_Console(ICommandManager mgr) { mgr.RegisterCommand(new ElevatedCommand(["@test/flag"], [])); Assert.Equal(CommandResult.SUCCESS, @@ -23,7 +23,7 @@ public async Task Permission_Pass_Flag_Console(ICommandManager mgr) { } [Theory] - [ClassData(typeof(CommandManagerData))] + [ClassData(typeof(CommandManager.TestData))] public async Task Permission_Pass_Group_Console(ICommandManager mgr) { mgr.RegisterCommand(new ElevatedCommand([], ["#test/group"])); Assert.Equal(CommandResult.SUCCESS, @@ -31,7 +31,7 @@ public async Task Permission_Pass_Group_Console(ICommandManager mgr) { } [Theory] - [ClassData(typeof(CommandManagerData))] + [ClassData(typeof(CommandManager.TestData))] public async Task Permission_Fail_Flag(ICommandManager mgr) { mgr.RegisterCommand(new ElevatedCommand(["@test/flag"], [])); Assert.Equal(CommandResult.NO_PERMISSION, @@ -39,7 +39,7 @@ public async Task Permission_Fail_Flag(ICommandManager mgr) { } [Theory] - [ClassData(typeof(CommandManagerData))] + [ClassData(typeof(CommandManager.TestData))] public async Task Permission_Fail_Group(ICommandManager mgr) { mgr.RegisterCommand(new ElevatedCommand([], ["#test/group"])); Assert.Equal(CommandResult.NO_PERMISSION, @@ -47,7 +47,7 @@ public async Task Permission_Fail_Group(ICommandManager mgr) { } [Theory] - [ClassData(typeof(CommandManagerData))] + [ClassData(typeof(CommandManager.TestData))] public async Task Permission_Fail_Both_Flag(ICommandManager mgr) { mgr.RegisterCommand(new ElevatedCommand(["@test/flag"], ["#test/group"])); Assert.Equal(CommandResult.NO_PERMISSION, @@ -55,7 +55,7 @@ public async Task Permission_Fail_Both_Flag(ICommandManager mgr) { } [Theory] - [ClassData(typeof(CommandManagerData))] + [ClassData(typeof(CommandManager.TestData))] public async Task Permission_Fail_Both_Group(ICommandManager mgr) { mgr.RegisterCommand(new ElevatedCommand(["@test/flag"], ["#test/group"])); Assert.Equal(CommandResult.NO_PERMISSION, @@ -63,7 +63,7 @@ public async Task Permission_Fail_Both_Group(ICommandManager mgr) { } [Theory] - [ClassData(typeof(CommandManagerData))] + [ClassData(typeof(CommandManager.TestData))] public async Task Permission_Pass_Flag(ICommandManager mgr) { var elevatedPlayer = TestPlayer.WithFlags("@test/flag"); mgr.RegisterCommand(new ElevatedCommand(["@test/flag"], [])); @@ -72,7 +72,7 @@ public async Task Permission_Pass_Flag(ICommandManager mgr) { } [Theory] - [ClassData(typeof(CommandManagerData))] + [ClassData(typeof(CommandManager.TestData))] public async Task Permission_Pass_Group(ICommandManager mgr) { var elevatedPlayer = TestPlayer.WithGroups("#test/group"); mgr.RegisterCommand(new ElevatedCommand([], ["#test/group"])); @@ -81,7 +81,7 @@ public async Task Permission_Pass_Group(ICommandManager mgr) { } [Theory] - [ClassData(typeof(CommandManagerData))] + [ClassData(typeof(CommandManager.TestData))] public async Task Permission_Pass_Both(ICommandManager mgr) { var elevatedPlayer = TestPlayer.WithFlags("@test/flag").WithGroups("#test/group"); diff --git a/GangsTest/Commands/CommandsData.cs b/GangsTest/API/Services/Commands/Command/TestData.cs similarity index 76% rename from GangsTest/Commands/CommandsData.cs rename to GangsTest/API/Services/Commands/Command/TestData.cs index 6badaea..274224e 100644 --- a/GangsTest/Commands/CommandsData.cs +++ b/GangsTest/API/Services/Commands/Command/TestData.cs @@ -1,13 +1,15 @@ using System.Collections; using Commands; -using Commands.gang; +using Commands.Gang; using GangsAPI; using GangsAPI.Services; +using GangsAPI.Services.Gang; +using GangsAPI.Services.Player; using Mock; -namespace GangsTest.Commands; +namespace GangsTest.API.Services.Commands.Command; -public class CommandsData : IEnumerable { +public class TestData : IEnumerable { private static readonly IPlayerManager playerMgr = new MockPlayerManager(); private static readonly IGangManager manager = new MockGangManager(playerMgr); @@ -15,7 +17,7 @@ public class CommandsData : IEnumerable { new CreateCommand(manager), new HelpCommand(), new GangCommand(manager) ]; - public CommandsData() { + public TestData() { foreach (var behavior in behaviors) behavior.Start(); } diff --git a/GangsTest/Commands/CommandTests.cs b/GangsTest/API/Services/Commands/Command/TestParent.cs similarity index 69% rename from GangsTest/Commands/CommandTests.cs rename to GangsTest/API/Services/Commands/Command/TestParent.cs index 642c010..0599c5f 100644 --- a/GangsTest/Commands/CommandTests.cs +++ b/GangsTest/API/Services/Commands/Command/TestParent.cs @@ -1,16 +1,16 @@ using GangsAPI.Data; using GangsAPI.Services.Commands; -namespace GangsTest.Commands; +namespace GangsTest.API.Services.Commands.Command; -public abstract class CommandTests { +public abstract class TestParent { protected readonly ICommand Command; protected readonly ICommandManager Commands; protected readonly PlayerWrapper TestPlayer = new((ulong)new Random().NextInt64(), "Test Player"); - protected CommandTests(ICommandManager commands, ICommand command) { + protected TestParent(ICommandManager commands, ICommand command) { Commands = commands; Command = command; commands.RegisterCommand(command); diff --git a/GangsTest/Commands/ManagerTests/AliasTests.cs b/GangsTest/API/Services/Commands/CommandManager/AliasTests.cs similarity index 76% rename from GangsTest/Commands/ManagerTests/AliasTests.cs rename to GangsTest/API/Services/Commands/CommandManager/AliasTests.cs index 4dd79ba..32770bb 100644 --- a/GangsTest/Commands/ManagerTests/AliasTests.cs +++ b/GangsTest/API/Services/Commands/CommandManager/AliasTests.cs @@ -2,12 +2,12 @@ using GangsAPI.Data.Command; using GangsAPI.Services.Commands; -namespace GangsTest.Commands.ManagerTests; +namespace GangsTest.API.Services.Commands.CommandManager; -public class AliasTests : ManagerTests { +public class AliasTests : TestParent { [Theory] - [ClassData(typeof(CommandManagerData))] - public async Task Command_Alias(ICommandManager mgr) { + [ClassData(typeof(TestData))] + public async Task Alias_Success(ICommandManager mgr) { mgr.RegisterCommand(new AliasCommand()); Assert.Equal(CommandResult.SUCCESS, await mgr.ProcessCommand(TestPlayer, "css_alias", "foobar")); @@ -16,8 +16,8 @@ public async Task Command_Alias(ICommandManager mgr) { } [Theory] - [ClassData(typeof(CommandManagerData))] - public async Task Command_Alias_Case(ICommandManager mgr) { + [ClassData(typeof(TestData))] + public async Task Alias_Case(ICommandManager mgr) { mgr.RegisterCommand(new AliasCommand()); Assert.Equal(CommandResult.SUCCESS, await mgr.ProcessCommand(TestPlayer, "css_Alias", "foobar")); diff --git a/GangsTest/Commands/ManagerTests/ManagerHandling.cs b/GangsTest/API/Services/Commands/CommandManager/PlayerOnlyTests.cs similarity index 83% rename from GangsTest/Commands/ManagerTests/ManagerHandling.cs rename to GangsTest/API/Services/Commands/CommandManager/PlayerOnlyTests.cs index e184401..25a352e 100644 --- a/GangsTest/Commands/ManagerTests/ManagerHandling.cs +++ b/GangsTest/API/Services/Commands/CommandManager/PlayerOnlyTests.cs @@ -2,11 +2,11 @@ using GangsAPI.Data.Command; using GangsAPI.Services.Commands; -namespace GangsTest.Commands.ManagerTests; +namespace GangsTest.API.Services.Commands.CommandManager; -public class ManagerHandling : ManagerTests { +public class PlayerOnlyTests : TestParent { [Theory] - [ClassData(typeof(CommandManagerData))] + [ClassData(typeof(TestData))] public async Task Command_PlayerOnly(ICommandManager mgr) { mgr.RegisterCommand(new PlayerOnlyCommand()); Assert.Equal(CommandResult.PLAYER_ONLY, diff --git a/GangsTest/Commands/ManagerTests/RegistrationTests.cs b/GangsTest/API/Services/Commands/CommandManager/RegistrationTests.cs similarity index 77% rename from GangsTest/Commands/ManagerTests/RegistrationTests.cs rename to GangsTest/API/Services/Commands/CommandManager/RegistrationTests.cs index 9cd7df5..2901a19 100644 --- a/GangsTest/Commands/ManagerTests/RegistrationTests.cs +++ b/GangsTest/API/Services/Commands/CommandManager/RegistrationTests.cs @@ -1,11 +1,11 @@ using GangsAPI.Data.Command; using GangsAPI.Services.Commands; -namespace GangsTest.Commands.ManagerTests; +namespace GangsTest.API.Services.Commands.CommandManager; -public class RegistrationTests : ManagerTests { +public class RegistrationTests : TestParent { [Theory] - [ClassData(typeof(CommandManagerData))] + [ClassData(typeof(TestData))] public async Task Command_Register(ICommandManager mgr) { Assert.True(mgr.RegisterCommand(Dummy)); Assert.Equal(CommandResult.SUCCESS, @@ -13,14 +13,14 @@ public async Task Command_Register(ICommandManager mgr) { } [Theory] - [ClassData(typeof(CommandManagerData))] + [ClassData(typeof(TestData))] public async Task Command_Unregistered(ICommandManager mgr) { Assert.Equal(CommandResult.UNKNOWN_COMMAND, await mgr.ProcessCommand(TestPlayer, "css_dummy")); } [Theory] - [ClassData(typeof(CommandManagerData))] + [ClassData(typeof(TestData))] public async Task Command_Unregister(ICommandManager mgr) { Assert.True(mgr.RegisterCommand(Dummy)); Assert.True(mgr.UnregisterCommand(Dummy)); @@ -29,7 +29,7 @@ public async Task Command_Unregister(ICommandManager mgr) { } [Theory] - [ClassData(typeof(CommandManagerData))] + [ClassData(typeof(TestData))] public void Command_Unregister_Unregistered(ICommandManager mgr) { Assert.False(mgr.UnregisterCommand(Dummy)); } diff --git a/GangsTest/Commands/ManagerTests/CommandManagerData.cs b/GangsTest/API/Services/Commands/CommandManager/TestData.cs similarity index 65% rename from GangsTest/Commands/ManagerTests/CommandManagerData.cs rename to GangsTest/API/Services/Commands/CommandManager/TestData.cs index 8391e2e..6c4392c 100644 --- a/GangsTest/Commands/ManagerTests/CommandManagerData.cs +++ b/GangsTest/API/Services/Commands/CommandManager/TestData.cs @@ -1,19 +1,19 @@ using System.Collections; -using Commands; using GangsAPI; using GangsAPI.Services; +using GangsAPI.Services.Player; using Mock; -namespace GangsTest.Commands.ManagerTests; +namespace GangsTest.API.Services.Commands.CommandManager; -public class CommandManagerData : IEnumerable { +public class TestData : IEnumerable { private static readonly IPlayerManager playerMgr = new MockPlayerManager(); private readonly IBehavior[] behaviors = [ - new MockCommandManager(), new CommandManager(new MockGangManager(playerMgr)) + new MockCommandManager(), new global::Commands.CommandManager(new MockGangManager(playerMgr)) ]; - public CommandManagerData() { + public TestData() { foreach (var behavior in behaviors) behavior.Start(); } diff --git a/GangsTest/Commands/ManagerTests/ManagerTests.cs b/GangsTest/API/Services/Commands/CommandManager/TestParent.cs similarity index 91% rename from GangsTest/Commands/ManagerTests/ManagerTests.cs rename to GangsTest/API/Services/Commands/CommandManager/TestParent.cs index 50ec776..ffcdadd 100644 --- a/GangsTest/Commands/ManagerTests/ManagerTests.cs +++ b/GangsTest/API/Services/Commands/CommandManager/TestParent.cs @@ -2,9 +2,9 @@ using GangsAPI.Data.Command; using GangsAPI.Services.Commands; -namespace GangsTest.Commands.ManagerTests; +namespace GangsTest.API.Services.Commands.CommandManager; -public class ManagerTests { +public class TestParent { protected readonly ICommand Dummy = new DummyCommand(); protected readonly PlayerWrapper TestPlayer = diff --git a/GangsTest/GangTests/GangCreationTests.cs b/GangsTest/API/Services/Gang/CreationTests.cs similarity index 85% rename from GangsTest/GangTests/GangCreationTests.cs rename to GangsTest/API/Services/Gang/CreationTests.cs index 3ae85b7..b94b062 100644 --- a/GangsTest/GangTests/GangCreationTests.cs +++ b/GangsTest/API/Services/Gang/CreationTests.cs @@ -1,11 +1,12 @@ using GangsAPI.Data.Gang; -using GangsAPI.Services; +using GangsAPI.Services.Gang; +using GangsAPI.Services.Player; -namespace GangsTest.GangTests; +namespace GangsTest.API.Services.Gang; -public class GangCreationTests(IPlayerManager playerMgr) { +public class CreationTests(IPlayerManager playerMgr) { [Theory] - [ClassData(typeof(GangManagerData))] + [ClassData(typeof(TestData))] public async Task Gang_Create(IGangManager mgr) { var dummy = await mgr.CreateGang("foobar", 0); Assert.NotNull(dummy); @@ -13,7 +14,7 @@ public async Task Gang_Create(IGangManager mgr) { } [Theory] - [ClassData(typeof(GangManagerData))] + [ClassData(typeof(TestData))] public async Task Gang_CreateFromGangPlayer(IGangManager mgr) { var player = await playerMgr.CreatePlayer(0); var dummy = await mgr.CreateGang("foobar", player); @@ -22,7 +23,7 @@ public async Task Gang_CreateFromGangPlayer(IGangManager mgr) { } [Theory] - [ClassData(typeof(GangManagerData))] + [ClassData(typeof(TestData))] public async Task Gang_Clone(IGangManager mgr) { var dummy = await mgr.CreateGang("foobar", 0); Assert.NotNull(dummy); @@ -35,7 +36,7 @@ public async Task Gang_Clone(IGangManager mgr) { } [Theory] - [ClassData(typeof(GangManagerData))] + [ClassData(typeof(TestData))] public async Task Gang_CreateMultiple(IGangManager mgr) { var steam1 = (ulong)new Random().NextInt64(); var steam2 = (ulong)new Random().NextInt64(); @@ -50,7 +51,7 @@ public async Task Gang_CreateMultiple(IGangManager mgr) { } [Theory] - [ClassData(typeof(GangManagerData))] + [ClassData(typeof(TestData))] public async Task Gang_CreateMultipleFromGangPlayer(IGangManager mgr) { var player1 = await playerMgr.CreatePlayer((ulong)new Random().NextInt64()); var player2 = await playerMgr.CreatePlayer((ulong)new Random().NextInt64()); diff --git a/GangsTest/GangTests/GangFetchTests.cs b/GangsTest/API/Services/Gang/FetchTests.cs similarity index 85% rename from GangsTest/GangTests/GangFetchTests.cs rename to GangsTest/API/Services/Gang/FetchTests.cs index 510358d..8b35140 100644 --- a/GangsTest/GangTests/GangFetchTests.cs +++ b/GangsTest/API/Services/Gang/FetchTests.cs @@ -1,11 +1,12 @@ using GangsAPI.Data.Gang; -using GangsAPI.Services; +using GangsAPI.Services.Gang; +using GangsAPI.Services.Player; -namespace GangsTest.GangTests; +namespace GangsTest.API.Services.Gang; -public class GangFetchTests(IPlayerManager playerMgr) { +public class FetchTests(IPlayerManager playerMgr) { [Theory] - [ClassData(typeof(GangManagerData))] + [ClassData(typeof(TestData))] public async Task Gang_Create(IGangManager mgr) { Assert.Empty(await mgr.GetGangs()); var dummy = await mgr.CreateGang("foobar", 0); @@ -17,7 +18,7 @@ public async Task Gang_Create(IGangManager mgr) { } [Theory] - [ClassData(typeof(GangManagerData))] + [ClassData(typeof(TestData))] public async Task Gang_CreateFromGangPlayer(IGangManager mgr) { Assert.Empty(await mgr.GetGangs()); var player = await playerMgr.CreatePlayer(0); @@ -30,7 +31,7 @@ public async Task Gang_CreateFromGangPlayer(IGangManager mgr) { } [Theory] - [ClassData(typeof(GangManagerData))] + [ClassData(typeof(TestData))] public async Task Gang_Clone(IGangManager mgr) { var dummy = await mgr.CreateGang("foobar", 0); Assert.NotNull(dummy); @@ -41,7 +42,7 @@ public async Task Gang_Clone(IGangManager mgr) { } [Theory] - [ClassData(typeof(GangManagerData))] + [ClassData(typeof(TestData))] public async Task Gang_CreateMultiple(IGangManager mgr) { var dummy1 = await mgr.CreateGang("foobar", 0); Assert.NotNull(dummy1); @@ -56,7 +57,7 @@ public async Task Gang_CreateMultiple(IGangManager mgr) { } [Theory] - [ClassData(typeof(GangManagerData))] + [ClassData(typeof(TestData))] public async Task Gang_FetchId(IGangManager mgr) { var dummy = await mgr.CreateGang("foobar", (ulong)new Random().NextInt64()); Assert.NotNull(dummy); diff --git a/GangsTest/API/Services/Gang/FieldTests.cs b/GangsTest/API/Services/Gang/FieldTests.cs new file mode 100644 index 0000000..1bfe54c --- /dev/null +++ b/GangsTest/API/Services/Gang/FieldTests.cs @@ -0,0 +1,13 @@ +using GangsAPI.Services.Gang; + +namespace GangsTest.API.Services.Gang; + +public class FieldTests { + [Theory] + [ClassData(typeof(TestData))] + public async Task Gang_Fields_Name(IGangManager mgr) { + var dummy = await mgr.CreateGang("name", 0); + Assert.NotNull(dummy); + Assert.Equal("name", dummy.Name); + } +} \ No newline at end of file diff --git a/GangsTest/GangTests/GangMutabilityTests.cs b/GangsTest/API/Services/Gang/MutabilityTests.cs similarity index 71% rename from GangsTest/GangTests/GangMutabilityTests.cs rename to GangsTest/API/Services/Gang/MutabilityTests.cs index 4fb46a2..aa08e8b 100644 --- a/GangsTest/GangTests/GangMutabilityTests.cs +++ b/GangsTest/API/Services/Gang/MutabilityTests.cs @@ -1,12 +1,12 @@ -using GangsAPI.Services; +using GangsAPI.Services.Gang; -namespace GangsTest.GangTests; +namespace GangsTest.API.Services.Gang; -public class GangMutabilityTests { +public class MutabilityTests { [Theory] - [ClassData(typeof(GangManagerData))] + [ClassData(typeof(TestData))] public async Task GangMutability_Name(IGangManager mgr) { - var dummy = await GangTestUtil.CreateGang(mgr); + var dummy = await mgr.CreateGang("name", 0); Assert.NotNull(dummy); Assert.Equal("name", dummy.Name); dummy.Name = "foobar"; @@ -17,9 +17,9 @@ public async Task GangMutability_Name(IGangManager mgr) { } [Theory] - [ClassData(typeof(GangManagerData))] + [ClassData(typeof(TestData))] public async Task GangMutability_Update_Name(IGangManager mgr) { - var dummy = await GangTestUtil.CreateGang(mgr); + var dummy = await mgr.CreateGang("name", 0); Assert.NotNull(dummy); Assert.Equal("name", dummy.Name); dummy.Name = "foobar"; diff --git a/GangsTest/GangTests/GangManagerData.cs b/GangsTest/API/Services/Gang/TestData.cs similarity index 84% rename from GangsTest/GangTests/GangManagerData.cs rename to GangsTest/API/Services/Gang/TestData.cs index 2600597..b2c1ad5 100644 --- a/GangsTest/GangTests/GangManagerData.cs +++ b/GangsTest/API/Services/Gang/TestData.cs @@ -1,13 +1,13 @@ using System.Collections; using GangsAPI; -using GangsAPI.Services; +using GangsAPI.Services.Player; using Mock; using SQLImpl; using SQLite; -namespace GangsTest.GangTests; +namespace GangsTest.API.Services.Gang; -public class GangManagerData : IEnumerable { +public class TestData : IEnumerable { private static readonly IPlayerManager playerMgr = new MockPlayerManager(); private readonly IBehavior[] behaviors = [ @@ -19,7 +19,7 @@ public class GangManagerData : IEnumerable { true) ]; - public GangManagerData() { + public TestData() { foreach (var behavior in behaviors) behavior.Start(); } diff --git a/GangsTest/GangTests/GangManagerTests.cs b/GangsTest/API/Services/Gang/UpdateTests.cs similarity index 69% rename from GangsTest/GangTests/GangManagerTests.cs rename to GangsTest/API/Services/Gang/UpdateTests.cs index 39e4122..4e971d7 100644 --- a/GangsTest/GangTests/GangManagerTests.cs +++ b/GangsTest/API/Services/Gang/UpdateTests.cs @@ -1,21 +1,21 @@ -using GangsAPI.Services; +using GangsAPI.Services.Gang; using Mock; -namespace GangsTest.GangTests; +namespace GangsTest.API.Services.Gang; -public class GangManagerTests { +public class UpdateTests { [Theory] - [ClassData(typeof(GangManagerData))] + [ClassData(typeof(TestData))] public async Task GangManager_Update(IGangManager mgr) { - var dummy = await GangTestUtil.CreateGang(mgr); + var dummy = await mgr.CreateGang("name", 0); Assert.NotNull(dummy); Assert.True(await mgr.UpdateGang(dummy)); } [Theory] - [ClassData(typeof(GangManagerData))] + [ClassData(typeof(TestData))] public async Task GangManager_UpdateName(IGangManager mgr) { - var dummy = await GangTestUtil.CreateGang(mgr); + var dummy = await mgr.CreateGang("name", 0); Assert.NotNull(dummy); dummy.Name = "new name"; Assert.True(await mgr.UpdateGang(dummy)); @@ -25,7 +25,7 @@ public async Task GangManager_UpdateName(IGangManager mgr) { } [Theory] - [ClassData(typeof(GangManagerData))] + [ClassData(typeof(TestData))] public async Task GangManager_Update_Fail(IGangManager mgr) { var dummy = new MockGang(-1, "nonexistent"); Assert.False(await mgr.UpdateGang(dummy), diff --git a/GangsTest/StatTests/InstanceTests/InstanceFieldTests.cs b/GangsTest/API/Services/Stat/Concrete/ConcreteTests.cs similarity index 52% rename from GangsTest/StatTests/InstanceTests/InstanceFieldTests.cs rename to GangsTest/API/Services/Stat/Concrete/ConcreteTests.cs index 5b003f3..0a5763c 100644 --- a/GangsTest/StatTests/InstanceTests/InstanceFieldTests.cs +++ b/GangsTest/API/Services/Stat/Concrete/ConcreteTests.cs @@ -1,10 +1,10 @@ using GangsAPI.Data.Stat; -namespace GangsTest.StatTests.InstanceTests; +namespace GangsTest.API.Services.Stat.Concrete; -public class InstanceFieldTests { +public class ConcreteTests { [Theory] - [ClassData(typeof(StatInstanceData))] + [ClassData(typeof(TestData))] public void Instance_Id(IStat stat) { Assert.Matches("^[a-z0-9_]+$", stat.StatId); } diff --git a/GangsTest/StatTests/InstanceTests/StatInstanceData.cs b/GangsTest/API/Services/Stat/Concrete/TestData.cs similarity index 75% rename from GangsTest/StatTests/InstanceTests/StatInstanceData.cs rename to GangsTest/API/Services/Stat/Concrete/TestData.cs index 18c3123..eb2ac7f 100644 --- a/GangsTest/StatTests/InstanceTests/StatInstanceData.cs +++ b/GangsTest/API/Services/Stat/Concrete/TestData.cs @@ -2,9 +2,9 @@ using GangsAPI.Data.Stat; using Stats; -namespace GangsTest.StatTests.InstanceTests; +namespace GangsTest.API.Services.Stat.Concrete; -public class StatInstanceData : IEnumerable { +public class TestData : IEnumerable { private readonly IStat[] stats = [new GangBankStat()]; public IEnumerator GetEnumerator() { diff --git a/GangsTest/API/Services/Stat/EqualityTests.cs b/GangsTest/API/Services/Stat/EqualityTests.cs new file mode 100644 index 0000000..093bde3 --- /dev/null +++ b/GangsTest/API/Services/Stat/EqualityTests.cs @@ -0,0 +1,51 @@ +using GangsAPI.Services; +using GangsTest.API.Services.Stat.Manager; + +namespace GangsTest.API.Services.Stat; + +public class EqualityTests { + [Theory] + [ClassData(typeof(TestData))] + public async Task Stat_Equality_SameEverything(IStatManager mgr) { + var foo1 = await mgr.CreateStat("id", "name", "desc"); + var foo2 = await mgr.CreateStat("id", "name", "desc"); + Assert.Equal(foo1, foo2); + Assert.StrictEqual(foo1, foo2); + } + + [Theory] + [ClassData(typeof(TestData))] + public async Task Stat_Equality_DiffName(IStatManager mgr) { + var foo1 = await mgr.CreateStat("id", "name", "desc"); + var foo2 = await mgr.CreateStat("id", "foo", "desc"); + Assert.Equal(foo1, foo2); + Assert.StrictEqual(foo1, foo2); + } + + [Theory] + [ClassData(typeof(TestData))] + public async Task Stat_Equality_DiffDesc(IStatManager mgr) { + var foo1 = await mgr.CreateStat("id", "name", "desc"); + var foo2 = await mgr.CreateStat("id", "name", "foo"); + Assert.Equal(foo1, foo2); + Assert.StrictEqual(foo1, foo2); + } + + [Theory] + [ClassData(typeof(TestData))] + public async Task Stat_Equality_DiffBoth(IStatManager mgr) { + var foo1 = await mgr.CreateStat("id", "name", "desc"); + var foo2 = await mgr.CreateStat("id", "foo", "bar"); + Assert.Equal(foo1, foo2); + Assert.StrictEqual(foo1, foo2); + } + + [Theory] + [ClassData(typeof(TestData))] + public async Task Stat_Equality_DiffIDs(IStatManager mgr) { + var foo1 = await mgr.CreateStat("foo", "name", "desc"); + var foo2 = await mgr.CreateStat("bar", "name", "desc"); + Assert.NotStrictEqual(foo1, foo2); + Assert.NotEqual(foo1, foo2); + } +} \ No newline at end of file diff --git a/GangsTest/StatTests/StatFieldTests.cs b/GangsTest/API/Services/Stat/FieldTests.cs similarity index 55% rename from GangsTest/StatTests/StatFieldTests.cs rename to GangsTest/API/Services/Stat/FieldTests.cs index 73ba15e..f2e24c5 100644 --- a/GangsTest/StatTests/StatFieldTests.cs +++ b/GangsTest/API/Services/Stat/FieldTests.cs @@ -1,45 +1,45 @@ using GangsAPI.Services; -using GangsTest.StatTests.ManageTests; +using GangsTest.API.Services.Stat.Manager; -namespace GangsTest.StatTests; +namespace GangsTest.API.Services.Stat; -public class StatFieldTests { +public class FieldTests { [Theory] - [ClassData(typeof(StatManagerData))] + [ClassData(typeof(TestData))] public async Task Stat_Fields_Name(IStatManager mgr) { - var dummy = await StatTestUtil.CreateStat(mgr); + var dummy = await mgr.CreateStat("id", "name", "desc"); Assert.NotNull(dummy); Assert.Equal("name", dummy.Name); } [Theory] - [ClassData(typeof(StatManagerData))] + [ClassData(typeof(TestData))] public async Task Stat_Fields_Id(IStatManager mgr) { - var dummy = await StatTestUtil.CreateStat(mgr); + var dummy = await mgr.CreateStat("id", "name", "desc"); Assert.NotNull(dummy); Assert.Equal("id", dummy.StatId); } [Theory] - [ClassData(typeof(StatManagerData))] + [ClassData(typeof(TestData))] public async Task Stat_Fields_Desc_Null(IStatManager statManager) { - var dummy = await StatTestUtil.CreateStat(statManager, desc: null); + var dummy = await statManager.CreateStat("id", "name"); Assert.NotNull(dummy); Assert.Null(dummy.Description); } [Theory] - [ClassData(typeof(StatManagerData))] + [ClassData(typeof(TestData))] public async Task Stat_Fields_Desc_Empty(IStatManager statManager) { - var dummy = await StatTestUtil.CreateStat(statManager, desc: ""); + var dummy = await statManager.CreateStat("id", "name", ""); Assert.NotNull(dummy); Assert.Equal("", dummy.Description); } [Theory] - [ClassData(typeof(StatManagerData))] + [ClassData(typeof(TestData))] public async Task Stat_Fields_Desc_Basic(IStatManager statManager) { - var dummy = await StatTestUtil.CreateStat(statManager); + var dummy = await statManager.CreateStat("id", "name", "desc"); Assert.NotNull(dummy); Assert.Equal("desc", dummy.Description); } diff --git a/GangsTest/StatTests/StatInstanceEqualityTests.cs b/GangsTest/API/Services/Stat/Instance/EqualityTests.cs similarity index 94% rename from GangsTest/StatTests/StatInstanceEqualityTests.cs rename to GangsTest/API/Services/Stat/Instance/EqualityTests.cs index e55ecec..f405450 100644 --- a/GangsTest/StatTests/StatInstanceEqualityTests.cs +++ b/GangsTest/API/Services/Stat/Instance/EqualityTests.cs @@ -1,8 +1,8 @@ using Mock; -namespace GangsTest.StatTests; +namespace GangsTest.API.Services.Stat.Instance; -public class StatInstanceEqualityTests { +public class EqualityTests { [Fact] public void Stat_Equality_SameEverything() { var foo1 = new MockStat("foo", "bar", null, 0); diff --git a/GangsTest/API/Services/Stat/Instance/Gang/GangInstanceTests.cs b/GangsTest/API/Services/Stat/Instance/Gang/GangInstanceTests.cs new file mode 100644 index 0000000..b543d72 --- /dev/null +++ b/GangsTest/API/Services/Stat/Instance/Gang/GangInstanceTests.cs @@ -0,0 +1,119 @@ +using GangsAPI.Data.Gang; +using GangsAPI.Data.Stat; +using GangsAPI.Services.Gang; + +namespace GangsTest.API.Services.Stat.Instance.Gang; + +public class GangTests(IGangManager gangMgr) : TestParent { + private readonly IGang testGang = + gangMgr.CreateGang("Test Gang", (ulong)new Random().NextInt64()) + .GetAwaiter() + .GetResult() ?? throw new InvalidOperationException(); + + private readonly IStat testReputation = new ReputationStat(); + + [Theory] + [ClassData(typeof(TestData))] + public async Task Instance_Class_Push(IGangStatManager manager) { + Assert.True(await manager.SetForGang(testGang, testReputation)); + var (success, val) = + await manager.GetForGang(testGang, testReputation.StatId); + Assert.True(success); + Assert.Equal(testReputation.Value, val); + } + + [Theory] + [ClassData(typeof(TestData))] + public async Task Instance_Class_Push_Multiple(IGangStatManager manager) { + Assert.True( + await manager.SetForGang(testGang, "foo", new Reputation(0, 1))); + var (success, val) = await manager.GetForGang(testGang, "foo"); + Assert.True(success); + Assert.Equal(new Reputation(0, 1), val); + Assert.True( + await manager.SetForGang(testGang, "foo", new Reputation(1, 0))); + var (success2, val2) = + await manager.GetForGang(testGang, "foo"); + Assert.True(success2); + Assert.Equal(new Reputation(1, 0), val2); + } + + [Theory] + [ClassData(typeof(TestData))] + public async Task Instance_Push(IGangStatManager manager) { + Assert.True(await manager.SetForGang(testGang, TestStat)); + var (success, val) = + await manager.GetForGang(testGang, TestStat.StatId); + Assert.True(success); + Assert.Equal(TestStat.Value, val); + } + + [Theory] + [ClassData(typeof(TestData))] + public async Task Instance_Push_Default(IGangStatManager manager) { + TestStat.Value = 64; + Assert.True(await manager.SetForGang(testGang, TestStat)); + var (success, val) = + await manager.GetForGang(testGang, TestStat.StatId); + Assert.True(success); + Assert.Equal(64, val); + } + + [Theory] + [ClassData(typeof(TestData))] + public async Task Instance_Push_Changed_StatInst(IGangStatManager manager) { + TestStat.Value = 64; + Assert.True(await manager.SetForGang(testGang, TestStat)); + TestStat.Value = 128; + var (success, val) = + await manager.GetForGang(testGang, TestStat.StatId); + Assert.True(success); + Assert.Equal(64, val); + } + + [Theory] + [ClassData(typeof(TestData))] + public async Task Instance_Class_Push_Inst(IGangStatManager manager) { + testReputation.Value = new Reputation(11, 33); + Assert.True(await manager.SetForGang(testGang, testReputation)); + var (success, val) = + await manager.GetForGang(testGang, testReputation.StatId); + Assert.True(success); + Assert.Equal(testReputation.Value, val); + Assert.NotNull(val); + Assert.Equal(11, val.Positive); + Assert.Equal(33, val.Negative); + } + + [Theory] + [ClassData(typeof(TestData))] + public async Task Instance_Fetch_Unregistered(IGangStatManager manager) { + var (success, _) = await manager.GetForGang(testGang, TestStat.StatId); + Assert.False(success); + } + + [Theory] + [ClassData(typeof(TestData))] + public async Task Instance_Delete(IGangStatManager manager) { + Assert.True(await manager.SetForGang(testGang, TestStat)); + Assert.True(await manager.RemoveFromGang(testGang, TestStat.StatId)); + var (success, _) = await manager.GetForGang(testGang, TestStat.StatId); + Assert.False(success); + } + + [Theory] + [ClassData(typeof(TestData))] + public async Task Instance_Delete_Unregistered(IGangStatManager manager) { + Assert.False(await manager.RemoveFromGang(testGang, TestStat.StatId)); + } + + [Theory] + [ClassData(typeof(TestData))] + public async Task Instance_Primitives(IGangStatManager manager) { + Assert.True(await manager.SetForGang(testGang, "foobar", 32)); + Assert.True(await manager.SetForGang(testGang, "foobar", 32L)); + Assert.True(await manager.SetForGang(testGang, "foobar", 32f)); + Assert.True(await manager.SetForGang(testGang, "foobar", 32.0)); + Assert.True(await manager.SetForGang(testGang, "foobar", (ulong)32)); + } +} \ No newline at end of file diff --git a/GangsTest/StatTests/InstanceManageTests/InstanceManageData.cs b/GangsTest/API/Services/Stat/Instance/Gang/TestData.cs similarity index 66% rename from GangsTest/StatTests/InstanceManageTests/InstanceManageData.cs rename to GangsTest/API/Services/Stat/Instance/Gang/TestData.cs index 1aca6db..77ff028 100644 --- a/GangsTest/StatTests/InstanceManageTests/InstanceManageData.cs +++ b/GangsTest/API/Services/Stat/Instance/Gang/TestData.cs @@ -1,19 +1,19 @@ using System.Collections; -using GangsAPI; +using GangsAPI.Services.Gang; using Mock; using SQLImpl; -namespace GangsTest.StatTests.InstanceManageTests; +namespace GangsTest.API.Services.Stat.Instance.Gang; -public class InstanceManageData : IEnumerable { - private readonly IBehavior[] behaviors = [ +public class TestData : IEnumerable { + private readonly IGangStatManager[] behaviors = [ new MockInstanceStatManager(), - new SQLInstanceManager( + new SQLGangInstanceManager( Environment.GetEnvironmentVariable("DB_GANGS_CONNECTION") - ?? "Host=localhost;User=root;Database=gangs", "gang_inst_stats", "", true) + ?? "Host=localhost;User=root;Database=gangs", "gang_inst_stats", true) ]; - public InstanceManageData() { + public TestData() { foreach (var behavior in behaviors) behavior.Start(); } diff --git a/GangsTest/API/Services/Stat/Instance/Player/PlayerInstanceManagerData.cs b/GangsTest/API/Services/Stat/Instance/Player/PlayerInstanceManagerData.cs new file mode 100644 index 0000000..71de071 --- /dev/null +++ b/GangsTest/API/Services/Stat/Instance/Player/PlayerInstanceManagerData.cs @@ -0,0 +1,25 @@ +using System.Collections; +using GangsAPI.Services.Player; +using Mock; +using SQLImpl; + +namespace GangsTest.API.Services.Stat.Instance.Player; + +public class PlayerInstanceManagerData : IEnumerable { + private readonly IPlayerStatManager[] behaviors = [ + new MockInstanceStatManager(), + new SQLPlayerInstanceManager( + Environment.GetEnvironmentVariable("DB_GANGS_CONNECTION") + ?? "Host=localhost;User=root;Database=gangs", "gang_inst_stats", true) + ]; + + public PlayerInstanceManagerData() { + foreach (var behavior in behaviors) behavior.Start(); + } + + public IEnumerator GetEnumerator() { + return behaviors.Select(behavior => new[] { behavior }).GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } +} \ No newline at end of file diff --git a/GangsTest/API/Services/Stat/Instance/Player/TestData.cs b/GangsTest/API/Services/Stat/Instance/Player/TestData.cs new file mode 100644 index 0000000..bb3170d --- /dev/null +++ b/GangsTest/API/Services/Stat/Instance/Player/TestData.cs @@ -0,0 +1,15 @@ +using GangsAPI.Services.Player; + +namespace GangsTest.API.Services.Stat.Instance.Player; + +public class TestData : TestParent { + [Theory] + [ClassData(typeof(PlayerInstanceManagerData))] + public async Task Instance_Class_Push(IPlayerStatManager manager) { + Assert.True(await manager.SetForPlayer(1234567890, TestStat)); + var (success, val) = + await manager.GetForPlayer(1234567890, TestStat.StatId); + Assert.True(success); + Assert.Equal(TestStat.Value, val); + } +} \ No newline at end of file diff --git a/GangsTest/API/Services/Stat/Instance/TestParent.cs b/GangsTest/API/Services/Stat/Instance/TestParent.cs new file mode 100644 index 0000000..b3ef197 --- /dev/null +++ b/GangsTest/API/Services/Stat/Instance/TestParent.cs @@ -0,0 +1,71 @@ +using GangsAPI.Data.Stat; + +namespace GangsTest.API.Services.Stat.Instance; + +public class TestParent { + protected readonly TestIntStat TestStat = new(); + + protected class ReputationStat : IStat { + public string StatId => "reputation"; + public string Name => "Reputation"; + public string Description => "How popular are we?"; + + public Reputation Value { get; set; } = new(0, 0); + + public IStat Clone() { + return new ReputationStat { Value = Value }; + } + + public bool Equals(IStat? other) { + return other is not null && StatId == other.StatId; + } + + public bool Equals(IStat? other) { + return other is not null && StatId == other.StatId; + } + + public override int GetHashCode() { return StatId.GetHashCode(); } + } + + protected class TestIntStat : IStat { + public string StatId => "test_stat"; + public string Name => "Test Stat"; + public string Description => "A test stat."; + public int Value { get; set; } = 32; + + public IStat Clone() { return new TestIntStat { Value = Value }; } + + public bool Equals(IStat? other) { + return other is not null && StatId == other.StatId; + } + + public bool Equals(IStat? other) { + return other is not null && StatId == other.StatId; + } + + public override int GetHashCode() { return StatId.GetHashCode(); } + } + + protected class Reputation(int Positive, int negative) + : IEquatable { + public int Positive { get; } = Positive; + public int Negative { get; } = negative; + + public bool Equals(Reputation? other) { + if (other is null) return false; + if (ReferenceEquals(this, other)) return true; + return Positive == other.Positive && Negative == other.Negative; + } + + public override bool Equals(object? obj) { + if (obj is null) return false; + if (ReferenceEquals(this, obj)) return true; + if (obj.GetType() != GetType()) return false; + return Equals((Reputation)obj); + } + + public override int GetHashCode() { + return HashCode.Combine(Positive, Negative); + } + } +} \ No newline at end of file diff --git a/GangsTest/StatTests/ManageTests/MockStatManagerTests.cs b/GangsTest/API/Services/Stat/Manager/CreationTests.cs similarity index 60% rename from GangsTest/StatTests/ManageTests/MockStatManagerTests.cs rename to GangsTest/API/Services/Stat/Manager/CreationTests.cs index 1710ba0..f04f8ac 100644 --- a/GangsTest/StatTests/ManageTests/MockStatManagerTests.cs +++ b/GangsTest/API/Services/Stat/Manager/CreationTests.cs @@ -1,20 +1,20 @@ using GangsAPI.Services; -namespace GangsTest.StatTests.ManageTests; +namespace GangsTest.API.Services.Stat.Manager; -public class MockStatManagerTests { +public class CreationTests { [Theory] - [ClassData(typeof(StatManagerData))] + [ClassData(typeof(TestData))] public async Task Stat_Create(IStatManager mgr) { var dummy = await mgr.CreateStat("dummy", "name"); Assert.NotNull(dummy); } [Theory] - [ClassData(typeof(StatManagerData))] + [ClassData(typeof(TestData))] public async Task Stat_Create_Multiple(IStatManager mgr) { - var foo = await StatTestUtil.CreateStat(mgr, "foo"); - var bar = await StatTestUtil.CreateStat(mgr, "bar"); + var foo = await mgr.CreateStat("foo", "name", "desc"); + var bar = await mgr.CreateStat("bar", "name", "desc"); Assert.NotSame(foo, bar); Assert.NotNull(foo); Assert.NotNull(bar); diff --git a/GangsTest/StatTests/ManageTests/StatRegistrationTests.cs b/GangsTest/API/Services/Stat/Manager/RegistrationTests.cs similarity index 68% rename from GangsTest/StatTests/ManageTests/StatRegistrationTests.cs rename to GangsTest/API/Services/Stat/Manager/RegistrationTests.cs index 97e1696..c22674b 100644 --- a/GangsTest/StatTests/ManageTests/StatRegistrationTests.cs +++ b/GangsTest/API/Services/Stat/Manager/RegistrationTests.cs @@ -1,21 +1,21 @@ using GangsAPI.Services; -namespace GangsTest.StatTests.ManageTests; +namespace GangsTest.API.Services.Stat.Manager; -public class StatRegistrationTests { +public class RegistrationTests { [Theory] - [ClassData(typeof(StatManagerData))] + [ClassData(typeof(TestData))] public async Task Stat_Register(IStatManager mgr) { - var stat = await StatTestUtil.CreateStat(mgr); + var stat = await mgr.CreateStat("id", "name", "desc"); Assert.NotNull(stat); Assert.True(await mgr.RegisterStat(stat), "Failed to register new statistic"); } [Theory] - [ClassData(typeof(StatManagerData))] + [ClassData(typeof(TestData))] public async Task Stat_Register_Duplicate(IStatManager mgr) { - var stat = await StatTestUtil.CreateStat(mgr); + var stat = await mgr.CreateStat("id", "name", "desc"); Assert.NotNull(stat); Assert.True(await mgr.RegisterStat(stat), "Failed to register new statistic"); @@ -24,21 +24,21 @@ public async Task Stat_Register_Duplicate(IStatManager mgr) { } [Theory] - [ClassData(typeof(StatManagerData))] + [ClassData(typeof(TestData))] public async Task Stat_Unregistered(IStatManager mgr) { Assert.Null(await mgr.GetStat("id")); - await StatTestUtil.CreateStat(mgr); + await mgr.CreateStat("id", "name", "desc"); Assert.Null(await mgr.GetStat("id")); - var stat = await StatTestUtil.CreateStat(mgr); + var stat = await mgr.CreateStat("id", "name", "desc"); Assert.NotNull(stat); await mgr.RegisterStat(stat); Assert.NotNull(await mgr.GetStat("id")); } [Theory] - [ClassData(typeof(StatManagerData))] + [ClassData(typeof(TestData))] public async Task Stat_Unregister(IStatManager mgr) { - var stat = await StatTestUtil.CreateStat(mgr); + var stat = await mgr.CreateStat("id", "name", "desc"); Assert.NotNull(stat); Assert.True(await mgr.RegisterStat(stat), "Failed to register new statistic"); diff --git a/GangsTest/StatTests/ManageTests/StatRetainTests.cs b/GangsTest/API/Services/Stat/Manager/RetainTests.cs similarity index 51% rename from GangsTest/StatTests/ManageTests/StatRetainTests.cs rename to GangsTest/API/Services/Stat/Manager/RetainTests.cs index bcdf35f..820af94 100644 --- a/GangsTest/StatTests/ManageTests/StatRetainTests.cs +++ b/GangsTest/API/Services/Stat/Manager/RetainTests.cs @@ -1,57 +1,57 @@ using GangsAPI.Services; -namespace GangsTest.StatTests.ManageTests; +namespace GangsTest.API.Services.Stat.Manager; -public class StatRetainTests { +public class RetainTests { [Theory] - [ClassData(typeof(StatManagerData))] + [ClassData(typeof(TestData))] public async Task Stat_Retain(IStatManager mgr) { - var foo1 = await StatTestUtil.CreateStat(mgr); + var foo1 = await mgr.CreateStat("id", "name", "desc"); Assert.NotNull(foo1); await mgr.RegisterStat(foo1); - var foo2 = await StatTestUtil.CreateStat(mgr); + var foo2 = await mgr.CreateStat("id", "name", "desc"); Assert.Same(foo1, foo2); } [Theory] - [ClassData(typeof(StatManagerData))] + [ClassData(typeof(TestData))] public async Task Stat_Retain_Unregsitered(IStatManager mgr) { - var foo1 = await StatTestUtil.CreateStat(mgr); + var foo1 = await mgr.CreateStat("id", "name", "desc"); Assert.NotNull(foo1); - var foo2 = await StatTestUtil.CreateStat(mgr); + var foo2 = await mgr.CreateStat("id", "name", "desc"); Assert.NotSame(foo1, foo2); } [Theory] - [ClassData(typeof(StatManagerData))] + [ClassData(typeof(TestData))] public async Task Stat_Retain_NonTrivial_Name(IStatManager mgr) { - var foo1 = await StatTestUtil.CreateStat(mgr, "foo", "bar"); + var foo1 = await mgr.CreateStat("foo", "bar"); Assert.NotNull(foo1); await mgr.RegisterStat(foo1); - var foo2 = await StatTestUtil.CreateStat(mgr, "foo", "foobar"); + var foo2 = await mgr.CreateStat("foo", "foobar"); Assert.NotNull(foo2); Assert.Same(foo1, foo2); } [Theory] - [ClassData(typeof(StatManagerData))] + [ClassData(typeof(TestData))] public async Task Stat_Retain_NonTrivial_Desc(IStatManager mgr) { - var foo1 = await StatTestUtil.CreateStat(mgr, "foo", desc: "bar"); + var foo1 = await mgr.CreateStat("foo", "name", "bar"); Assert.NotNull(foo1); await mgr.RegisterStat(foo1); - var foo2 = await StatTestUtil.CreateStat(mgr, "foo", desc: "foobar"); + var foo2 = await mgr.CreateStat("foo", "name", "foobar"); Assert.NotNull(foo1); Assert.NotNull(foo2); Assert.Same(foo1, foo2); } [Theory] - [ClassData(typeof(StatManagerData))] + [ClassData(typeof(TestData))] public async Task Stat_Retain_NonTrivial_Both(IStatManager mgr) { - var foo1 = await StatTestUtil.CreateStat(mgr, "foo", "foobar", "barfoo"); + var foo1 = await mgr.CreateStat("foo", "foobar", "barfoo"); Assert.NotNull(foo1); await mgr.RegisterStat(foo1); - var foo2 = await StatTestUtil.CreateStat(mgr, "foo", "barfoo", "foobar"); + var foo2 = await mgr.CreateStat("foo", "barfoo", "foobar"); Assert.NotNull(foo1); Assert.NotNull(foo2); Assert.Same(foo1, foo2); diff --git a/GangsTest/StatTests/ManageTests/StatManagerData.cs b/GangsTest/API/Services/Stat/Manager/TestData.cs similarity index 80% rename from GangsTest/StatTests/ManageTests/StatManagerData.cs rename to GangsTest/API/Services/Stat/Manager/TestData.cs index 4fea038..9b988c4 100644 --- a/GangsTest/StatTests/ManageTests/StatManagerData.cs +++ b/GangsTest/API/Services/Stat/Manager/TestData.cs @@ -4,18 +4,18 @@ using SQLImpl; using SQLite; -namespace GangsTest.StatTests.ManageTests; +namespace GangsTest.API.Services.Stat.Manager; -public class StatManagerData : IEnumerable { +public class TestData : IEnumerable { private readonly IBehavior[] behaviors = [ - new MockStatManager(), + new Creation(), new SQLStatManager( Environment.GetEnvironmentVariable("DB_GANGS_CONNECTION") ?? "Host=localhost;User=root;Database=gangs", "gang_unit_test", true), new SQLiteStatManager("Data Source=:memory:", "gang_unit_test", true) ]; - public StatManagerData() { + public TestData() { foreach (var behavior in behaviors) behavior.Start(); } diff --git a/GangsTest/StatTests/StatPersistenceTests.cs b/GangsTest/API/Services/Stat/PersistenceTests.cs similarity index 69% rename from GangsTest/StatTests/StatPersistenceTests.cs rename to GangsTest/API/Services/Stat/PersistenceTests.cs index b556d68..0083879 100644 --- a/GangsTest/StatTests/StatPersistenceTests.cs +++ b/GangsTest/API/Services/Stat/PersistenceTests.cs @@ -1,14 +1,14 @@ using GangsAPI.Services; -using GangsTest.StatTests.ManageTests; +using GangsTest.API.Services.Stat.Manager; -namespace GangsTest.StatTests; +namespace GangsTest.API.Services.Stat; -public class StatPersistenceTests { +public class PersistenceTests { [Theory] - [ClassData(typeof(StatManagerData))] + [ClassData(typeof(TestData))] public async Task Persist_Cache_Clear(IStatManager mgr) { Assert.Empty(await mgr.GetStats()); - var dummy = await StatTestUtil.CreateStat(mgr); + var dummy = await mgr.CreateStat("id", "name", "desc"); Assert.NotNull(dummy); Assert.True(await mgr.RegisterStat(dummy)); Assert.Single(await mgr.GetStats()); @@ -17,10 +17,10 @@ public async Task Persist_Cache_Clear(IStatManager mgr) { } [Theory] - [ClassData(typeof(StatManagerData))] + [ClassData(typeof(TestData))] public async Task Persist_Cache_Load(IStatManager mgr) { Assert.Empty(await mgr.GetStats()); - var dummy = await StatTestUtil.CreateStat(mgr); + var dummy = await mgr.CreateStat("id", "name", "desc"); Assert.NotNull(dummy); Assert.True(await mgr.RegisterStat(dummy)); Assert.Single(await mgr.GetStats()); diff --git a/GangsTest/GangTests/GangFieldTests.cs b/GangsTest/GangTests/GangFieldTests.cs deleted file mode 100644 index c12bf8b..0000000 --- a/GangsTest/GangTests/GangFieldTests.cs +++ /dev/null @@ -1,13 +0,0 @@ -using GangsAPI.Services; - -namespace GangsTest.GangTests; - -public class GangFieldTests { - [Theory] - [ClassData(typeof(GangManagerData))] - public async Task Gang_Fields_Name(IGangManager mgr) { - var dummy = await GangTestUtil.CreateGang(mgr); - Assert.NotNull(dummy); - Assert.Equal("name", dummy.Name); - } -} \ No newline at end of file diff --git a/GangsTest/GangTests/GangTestUtil.cs b/GangsTest/GangTests/GangTestUtil.cs deleted file mode 100644 index 2778315..0000000 --- a/GangsTest/GangTests/GangTestUtil.cs +++ /dev/null @@ -1,12 +0,0 @@ -using GangsAPI.Data.Gang; -using GangsAPI.Services; - -namespace GangsTest.GangTests; - -public class GangTestUtil { - public static async Task CreateGang(IGangManager gangManager, - string name = "name", ulong owner = 0) { - var dummy = await gangManager.CreateGang(name, owner); - return dummy; - } -} \ No newline at end of file diff --git a/GangsTest/GangsTest.csproj b/GangsTest/GangsTest.csproj index 9c1804b..fb3f1aa 100644 --- a/GangsTest/GangsTest.csproj +++ b/GangsTest/GangsTest.csproj @@ -37,7 +37,7 @@ - + \ No newline at end of file diff --git a/GangsTest/Startup.cs b/GangsTest/Startup.cs index 782009d..f9740ec 100644 --- a/GangsTest/Startup.cs +++ b/GangsTest/Startup.cs @@ -1,5 +1,7 @@ using GangsAPI.Services; using GangsAPI.Services.Commands; +using GangsAPI.Services.Gang; +using GangsAPI.Services.Player; using Microsoft.Extensions.DependencyInjection; using Mock; @@ -9,7 +11,7 @@ public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddScoped(); services.AddScoped(); - services.AddScoped(); + services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); diff --git a/GangsTest/StatTests/InstanceManageTests/GangInstanceTests.cs b/GangsTest/StatTests/InstanceManageTests/GangInstanceTests.cs deleted file mode 100644 index a352c72..0000000 --- a/GangsTest/StatTests/InstanceManageTests/GangInstanceTests.cs +++ /dev/null @@ -1,183 +0,0 @@ -using GangsAPI.Data.Gang; -using GangsAPI.Data.Stat; -using GangsAPI.Services; - -namespace GangsTest.StatTests.InstanceManageTests; - -public class InstanceGangTests(IGangManager gangMgr) { - private readonly IGang testGang = - gangMgr.CreateGang("Test Gang", (ulong)new Random().NextInt64()) - .GetAwaiter() - .GetResult() ?? throw new InvalidOperationException(); - - private readonly IStat testReputation = new ReputationStat(); - - private readonly IStat testStat = new TestStatInstance(); - - [Theory] - [ClassData(typeof(InstanceManageData))] - public async Task Instance_Class_Push(IGangStatManager manager) { - Assert.True(await manager.SetForGang(testGang, testReputation)); - var (success, val) = - await manager.GetForGang(testGang, testReputation.StatId); - Assert.True(success); - Assert.Equal(testReputation.Value, val); - } - - [Theory] - [ClassData(typeof(InstanceManageData))] - public async Task Instance_Class_Push_Multiple(IGangStatManager manager) { - Assert.True( - await manager.SetForGang(testGang, "foo", new Reputation(0, 1))); - var (success, val) = await manager.GetForGang(testGang, "foo"); - Assert.True(success); - Assert.Equal(new Reputation(0, 1), val); - Assert.True( - await manager.SetForGang(testGang, "foo", new Reputation(1, 0))); - var (success2, val2) = - await manager.GetForGang(testGang, "foo"); - Assert.True(success2); - Assert.Equal(new Reputation(1, 0), val2); - } - - [Theory] - [ClassData(typeof(InstanceManageData))] - public async Task Instance_Push(IGangStatManager manager) { - Assert.True(await manager.SetForGang(testGang, testStat)); - var (success, val) = - await manager.GetForGang(testGang, testStat.StatId); - Assert.True(success); - Assert.Equal(testStat.Value, val); - } - - [Theory] - [ClassData(typeof(InstanceManageData))] - public async Task Instance_Push_Default(IGangStatManager manager) { - testStat.Value = 64; - Assert.True(await manager.SetForGang(testGang, testStat)); - var (success, val) = - await manager.GetForGang(testGang, testStat.StatId); - Assert.True(success); - Assert.Equal(64, val); - } - - [Theory] - [ClassData(typeof(InstanceManageData))] - public async Task Instance_Push_Changed_StatInst(IGangStatManager manager) { - testStat.Value = 64; - Assert.True(await manager.SetForGang(testGang, testStat)); - testStat.Value = 128; - var (success, val) = - await manager.GetForGang(testGang, testStat.StatId); - Assert.True(success); - Assert.Equal(64, val); - } - - [Theory] - [ClassData(typeof(InstanceManageData))] - public async Task Instance_Class_Push_Inst(IGangStatManager manager) { - testReputation.Value = new Reputation(11, 33); - Assert.True(await manager.SetForGang(testGang, testReputation)); - var (success, val) = - await manager.GetForGang(testGang, testReputation.StatId); - Assert.True(success); - Assert.Equal(testReputation.Value, val); - Assert.NotNull(val); - Assert.Equal(11, val.Positive); - Assert.Equal(33, val.Negative); - } - - [Theory] - [ClassData(typeof(InstanceManageData))] - public async Task Instance_Fetch_Unregistered(IGangStatManager manager) { - var (success, _) = await manager.GetForGang(testGang, testStat.StatId); - Assert.False(success); - } - - [Theory] - [ClassData(typeof(InstanceManageData))] - public async Task Instance_Delete(IGangStatManager manager) { - Assert.True(await manager.SetForGang(testGang, testStat)); - Assert.True(await manager.RemoveFromGang(testGang, testStat.StatId)); - var (success, _) = await manager.GetForGang(testGang, testStat.StatId); - Assert.False(success); - } - - [Theory] - [ClassData(typeof(InstanceManageData))] - public async Task Instance_Delete_Unregistered(IGangStatManager manager) { - Assert.False(await manager.RemoveFromGang(testGang, testStat.StatId)); - } - - [Theory] - [ClassData(typeof(InstanceManageData))] - public async Task Instance_Primitives(IGangStatManager manager) { - Assert.True(await manager.SetForGang(testGang, "foobar", 32)); - Assert.True(await manager.SetForGang(testGang, "foobar", 32L)); - Assert.True(await manager.SetForGang(testGang, "foobar", 32f)); - Assert.True(await manager.SetForGang(testGang, "foobar", 32.0)); - } - - public class Reputation(int Positive, int negative) : IEquatable { - public int Positive { get; } = Positive; - public int Negative { get; } = negative; - - public bool Equals(Reputation? other) { - if (other is null) return false; - if (ReferenceEquals(this, other)) return true; - return Positive == other.Positive && Negative == other.Negative; - } - - public override bool Equals(object? obj) { - if (obj is null) return false; - if (ReferenceEquals(this, obj)) return true; - if (obj.GetType() != GetType()) return false; - return Equals((Reputation)obj); - } - - public override int GetHashCode() { - return HashCode.Combine(Positive, Negative); - } - } - - private class ReputationStat : IStat { - public string StatId => "reputation"; - public string Name => "Reputation"; - public string Description => "The reputation of the gang."; - - public Reputation Value { get; set; } = new(0, 0); - - public IStat Clone() { - return new ReputationStat { Value = Value }; - } - - public bool Equals(IStat? other) { - return other is not null && StatId == other.StatId; - } - - public bool Equals(IStat? other) { - return other is not null && StatId == other.StatId; - } - - public override int GetHashCode() { return StatId.GetHashCode(); } - } - - private class TestStatInstance : IStat { - public string StatId => "test_stat"; - public string Name => "Test Stat"; - public string Description => "A test stat."; - public int Value { get; set; } = 32; - - public IStat Clone() { return new TestStatInstance { Value = Value }; } - - public bool Equals(IStat? other) { - return other is not null && StatId == other.StatId; - } - - public bool Equals(IStat? other) { - return other is not null && StatId == other.StatId; - } - - public override int GetHashCode() { return StatId.GetHashCode(); } - } -} \ No newline at end of file diff --git a/GangsTest/StatTests/StatEqualityTests.cs b/GangsTest/StatTests/StatEqualityTests.cs deleted file mode 100644 index 8c696eb..0000000 --- a/GangsTest/StatTests/StatEqualityTests.cs +++ /dev/null @@ -1,51 +0,0 @@ -using GangsAPI.Services; -using GangsTest.StatTests.ManageTests; - -namespace GangsTest.StatTests; - -public class StatEqualityTests { - [Theory] - [ClassData(typeof(StatManagerData))] - public async Task Stat_Equality_SameEverything(IStatManager mgr) { - var foo1 = await StatTestUtil.CreateStat(mgr); - var foo2 = await StatTestUtil.CreateStat(mgr); - Assert.Equal(foo1, foo2); - Assert.StrictEqual(foo1, foo2); - } - - [Theory] - [ClassData(typeof(StatManagerData))] - public async Task Stat_Equality_DiffName(IStatManager mgr) { - var foo1 = await StatTestUtil.CreateStat(mgr); - var foo2 = await StatTestUtil.CreateStat(mgr, name: "foo"); - Assert.Equal(foo1, foo2); - Assert.StrictEqual(foo1, foo2); - } - - [Theory] - [ClassData(typeof(StatManagerData))] - public async Task Stat_Equality_DiffDesc(IStatManager mgr) { - var foo1 = await StatTestUtil.CreateStat(mgr); - var foo2 = await StatTestUtil.CreateStat(mgr, desc: "foo"); - Assert.Equal(foo1, foo2); - Assert.StrictEqual(foo1, foo2); - } - - [Theory] - [ClassData(typeof(StatManagerData))] - public async Task Stat_Equality_DiffBoth(IStatManager mgr) { - var foo1 = await StatTestUtil.CreateStat(mgr); - var foo2 = await StatTestUtil.CreateStat(mgr, name: "foo", desc: "bar"); - Assert.Equal(foo1, foo2); - Assert.StrictEqual(foo1, foo2); - } - - [Theory] - [ClassData(typeof(StatManagerData))] - public async Task Stat_Equality_DiffIDs(IStatManager mgr) { - var foo1 = await StatTestUtil.CreateStat(mgr, "foo"); - var foo2 = await StatTestUtil.CreateStat(mgr, "bar"); - Assert.NotStrictEqual(foo1, foo2); - Assert.NotEqual(foo1, foo2); - } -} \ No newline at end of file diff --git a/GangsTest/StatTests/StatTestUtil.cs b/GangsTest/StatTests/StatTestUtil.cs deleted file mode 100644 index cb35dbd..0000000 --- a/GangsTest/StatTests/StatTestUtil.cs +++ /dev/null @@ -1,12 +0,0 @@ -using GangsAPI.Data.Stat; -using GangsAPI.Services; - -namespace GangsTest.StatTests; - -public class StatTestUtil { - public static async Task CreateStat(IStatManager statManager, - string id = "id", string name = "name", string? desc = "desc") { - var dummy = await statManager.CreateStat(id, name, desc); - return dummy; - } -} \ No newline at end of file