diff --git a/Code2Gether-Discord-Bot.Tests/CreateProjectTests.cs b/Code2Gether-Discord-Bot.Tests/CreateProjectTests.cs deleted file mode 100644 index d81bf70..0000000 --- a/Code2Gether-Discord-Bot.Tests/CreateProjectTests.cs +++ /dev/null @@ -1,82 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Threading.Tasks; -using Code2Gether_Discord_Bot.Library.BusinessLogic; -using Code2Gether_Discord_Bot.Library.Models; -using Code2Gether_Discord_Bot.Library.Models.Repositories.ProjectRepository; -using Code2Gether_Discord_Bot.Static; -using Code2Gether_Discord_Bot.Tests.Fakes; -using NUnit.Framework; - -namespace Code2Gether_Discord_Bot.Tests -{ - internal class CreateProjectTests - { - private IBusinessLogic _logic; - private IProjectRepository _repo; - - [SetUp] - public void Setup() - { - var user = new FakeUser() - { - Username = "UnitTest", - DiscriminatorValue = 1234, - Id = 123456789123456789 - }; - - var client = new FakeDiscordClient() - { - FakeApplication = new FakeApplication() - { - Owner = user - } - }; - - var guild = new FakeGuild() - { - - }; - - var messageChannel = new FakeMessageChannel() - { - - }; - - var message = new FakeUserMessage() - { - Author = user - }; - - _repo = new FakeProjectRepository() - { - Projects = new Dictionary() - { - {0, new Project(0, "unittest", user)}, - } - }; - - _logic = new CreateProjectLogic(UtilityFactory.GetLogger(GetType()), new FakeCommandContext() - { - Channel = messageChannel, - Client = client, - Guild = guild, - Message = message, - User = user - }, new FakeProjectManager(_repo), "unittest"); - } - - [Test] - public void InstantiationTest() => - Assert.IsTrue(_logic != null); - - [Test] - public async Task ExecutionTest() - { - await _logic.ExecuteAsync(); - - Assert.IsTrue(_repo.ReadAll().Count > 0); - } - } -} diff --git a/Code2Gether-Discord-Bot.Tests/ExcuseGeneratorTest.cs b/Code2Gether-Discord-Bot.Tests/ExcuseGeneratorTest.cs index e09ef91..de38c43 100644 --- a/Code2Gether-Discord-Bot.Tests/ExcuseGeneratorTest.cs +++ b/Code2Gether-Discord-Bot.Tests/ExcuseGeneratorTest.cs @@ -1,8 +1,5 @@ using System.Threading.Tasks; using Code2Gether_Discord_Bot.Library.BusinessLogic; -using Code2Gether_Discord_Bot.Library.Models; -using Code2Gether_Discord_Bot.Static; -using Code2Gether_Discord_Bot.Tests.Fakes; using NUnit.Framework; namespace Code2Gether_Discord_Bot.Tests @@ -12,47 +9,8 @@ internal class ExcuseGeneratorTest IBusinessLogic _logic; [SetUp] - public void Setup() - { - var user = new FakeUser() - { - Username = "UnitTest", - DiscriminatorValue = 1234, - Id = 123456789123456789 - }; - - var client = new FakeDiscordClient() - { - FakeApplication = new FakeApplication() - { - Owner = user - } - }; - - var guild = new FakeGuild() - { - - }; - - var messageChannel = new FakeMessageChannel() - { - - }; - - var message = new FakeUserMessage() - { - Author = user - }; - - _logic = BusinessLogicFactory.ExcuseGeneratorLogic(GetType(), new FakeCommandContext() - { - Client = client, - Guild = guild, - User = user, - Message = message, - Channel = messageChannel - }); - } + public void Setup() => + _logic = TestConfig.ExcuseGeneratorLogic(); [Test] diff --git a/Code2Gether-Discord-Bot.Tests/Fakes/FakeApplication.cs b/Code2Gether-Discord-Bot.Tests/Fakes/FakeApplication.cs index 108ff44..3df7ce7 100644 --- a/Code2Gether-Discord-Bot.Tests/Fakes/FakeApplication.cs +++ b/Code2Gether-Discord-Bot.Tests/Fakes/FakeApplication.cs @@ -1,7 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Text; -using Discord; +using Discord; +using System; namespace Code2Gether_Discord_Bot.Tests.Fakes { diff --git a/Code2Gether-Discord-Bot.Tests/Fakes/FakeDiscordClient.cs b/Code2Gether-Discord-Bot.Tests/Fakes/FakeDiscordClient.cs index 85ee32b..ded1a53 100644 --- a/Code2Gether-Discord-Bot.Tests/Fakes/FakeDiscordClient.cs +++ b/Code2Gether-Discord-Bot.Tests/Fakes/FakeDiscordClient.cs @@ -2,16 +2,15 @@ using System; using System.Collections.Generic; using System.IO; -using System.Text; using System.Threading.Tasks; namespace Code2Gether_Discord_Bot.Tests.Fakes { internal class FakeDiscordClient : IDiscordClient { - public IReadOnlyCollection Guilds { get; set; } + /*public IReadOnlyCollection Guilds { get; set; } - public IApplication FakeApplication { get; set; } + public IApplication FakeApplication { get; set; }*/ public ConnectionState ConnectionState { get; set; } @@ -31,7 +30,8 @@ public void Dispose() public Task GetApplicationInfoAsync(RequestOptions options = null) { - return Task.FromResult(FakeApplication); + throw new NotImplementedException(); + // return Task.FromResult(FakeApplication); } public Task GetChannelAsync(ulong id, CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null) @@ -61,7 +61,8 @@ public Task GetGuildAsync(ulong id, CacheMode mode = CacheMode.AllowDown public Task> GetGuildsAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null) { - return Task.FromResult(Guilds); + throw new NotImplementedException(); + // return Task.FromResult(Guilds); } public Task GetInviteAsync(string inviteId, RequestOptions options = null) diff --git a/Code2Gether-Discord-Bot.Tests/Fakes/FakeGuild.cs b/Code2Gether-Discord-Bot.Tests/Fakes/FakeGuild.cs index 0714df7..3ac8dcc 100644 --- a/Code2Gether-Discord-Bot.Tests/Fakes/FakeGuild.cs +++ b/Code2Gether-Discord-Bot.Tests/Fakes/FakeGuild.cs @@ -2,20 +2,13 @@ using Discord.Audio; using System; using System.Collections.Generic; -using System.Collections.Immutable; using System.Globalization; -using System.Text; using System.Threading.Tasks; namespace Code2Gether_Discord_Bot.Tests.Fakes { internal class FakeGuild : IGuild { - private Dictionary _guildChannels = new Dictionary() - { - { 123456789123456789, new FakeGuildChannel() } - }; - public string Name { get; set; } public int AFKTimeout { get; set; } @@ -188,16 +181,18 @@ public Task> GetCategoriesAsync(CacheMode public Task GetChannelAsync(ulong id, CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null) { - return Task.Run(() => + throw new NotImplementedException(); + /*return Task.Run(() => { _guildChannels.TryGetValue(id, out IGuildChannel channel); return channel; - }); + });*/ } public Task> GetChannelsAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null) { - return Task.Run(() => (IReadOnlyCollection)_guildChannels); + throw new NotImplementedException(); + // return Task.Run(() => (IReadOnlyCollection)_guildChannels); } public Task GetCurrentUserAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null) diff --git a/Code2Gether-Discord-Bot.Tests/Fakes/FakeGuildChannel.cs b/Code2Gether-Discord-Bot.Tests/Fakes/FakeGuildChannel.cs index cac0a52..b420180 100644 --- a/Code2Gether-Discord-Bot.Tests/Fakes/FakeGuildChannel.cs +++ b/Code2Gether-Discord-Bot.Tests/Fakes/FakeGuildChannel.cs @@ -1,15 +1,20 @@ -using System; +using Discord; +using System; using System.Collections.Generic; -using System.Text; using System.Threading.Tasks; -using Discord; namespace Code2Gether_Discord_Bot.Tests.Fakes { internal class FakeGuildChannel : IGuildChannel { - public ulong Id { get; } public DateTimeOffset CreatedAt { get; } + public IGuild Guild { get; } + public ulong GuildId { get; } + public ulong Id { get; } + public string Name { get; } + public IReadOnlyCollection PermissionOverwrites { get; } + public int Position { get; } + public Task ModifyAsync(Action func, RequestOptions options = null) { throw new NotImplementedException(); @@ -55,11 +60,6 @@ Task IGuildChannel.GetUserAsync(ulong id, CacheMode mode, RequestOpt throw new NotImplementedException(); } - public int Position { get; } - public IGuild Guild { get; } - public ulong GuildId { get; } - public IReadOnlyCollection PermissionOverwrites { get; } - IAsyncEnumerable> IChannel.GetUsersAsync(CacheMode mode, RequestOptions options) { return GetUsersAsync(mode, options); @@ -70,7 +70,6 @@ Task IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions optio throw new NotImplementedException(); } - public string Name { get; } public Task DeleteAsync(RequestOptions options = null) { throw new NotImplementedException(); diff --git a/Code2Gether-Discord-Bot.Tests/Fakes/FakeLogger.cs b/Code2Gether-Discord-Bot.Tests/Fakes/FakeLogger.cs new file mode 100644 index 0000000..27e7a17 --- /dev/null +++ b/Code2Gether-Discord-Bot.Tests/Fakes/FakeLogger.cs @@ -0,0 +1,21 @@ +using Code2Gether_Discord_Bot.Library.Models; +using Discord; +using Discord.Commands; +using System; + +namespace Code2Gether_Discord_Bot.Tests.Fakes +{ + /// + /// Just a logger that logs nothing ¯\_(ツ)_/¯ + /// + class FakeLogger : ILogger + { + public void Log(LogSeverity level, string message) { } + + public void Log(LogSeverity level, Exception exception) { } + + public void Log(LogSeverity level, string message, Exception exception) { } + + public void Log(ICommandContext context) { } + } +} diff --git a/Code2Gether-Discord-Bot.Tests/Fakes/FakeMessageChannel.cs b/Code2Gether-Discord-Bot.Tests/Fakes/FakeMessageChannel.cs index 7fdda2e..3b468b3 100644 --- a/Code2Gether-Discord-Bot.Tests/Fakes/FakeMessageChannel.cs +++ b/Code2Gether-Discord-Bot.Tests/Fakes/FakeMessageChannel.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Text; using System.Threading.Tasks; namespace Code2Gether_Discord_Bot.Tests.Fakes diff --git a/Code2Gether-Discord-Bot.Tests/Fakes/FakeProjectManager.cs b/Code2Gether-Discord-Bot.Tests/Fakes/FakeProjectManager.cs deleted file mode 100644 index 6065fed..0000000 --- a/Code2Gether-Discord-Bot.Tests/Fakes/FakeProjectManager.cs +++ /dev/null @@ -1,66 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Code2Gether_Discord_Bot.Library.Models; -using Code2Gether_Discord_Bot.Library.Models.Repositories.ProjectRepository; -using Discord; - -namespace Code2Gether_Discord_Bot.Tests.Fakes -{ - class FakeProjectManager : IProjectManager - { - private IProjectRepository _projectRepository; - - public FakeProjectManager(IProjectRepository projectRepository) - { - _projectRepository = projectRepository; - } - - public bool DoesProjectExist(string projectName) => - _projectRepository.Read(projectName) != null; - - public bool DoesProjectExist(string projectName, out Project project) - { - project = _projectRepository.Read(projectName); - return project != null; - } - - public Project CreateProject(string projectName, IUser author) - { - var newId = GetNextId(); - var newProject = new Project(newId, projectName, author); - if (_projectRepository.Create(newProject)) - { - JoinProject(projectName, author, out newProject); - return newProject; - } - throw new Exception($"Failed to create new project: {newProject}!"); - } - - public bool JoinProject(string projectName, IUser user, out Project project) - { - project = _projectRepository.Read(projectName); - - if (project == null) return false; // Project must exist - if (project.ProjectMembers.Contains(user)) return false; // User may not already be in project - - project.ProjectMembers.Add(user); - - return _projectRepository.Update(project); - } - - private int GetNextId() - { - int i = 0; - - try - { - i = _projectRepository.ReadAll().Keys.Max(); - } - catch (InvalidOperationException) { } // No projects available yet - - return i; - } - } -} diff --git a/Code2Gether-Discord-Bot.Tests/Fakes/FakeProjectRepository.cs b/Code2Gether-Discord-Bot.Tests/Fakes/FakeProjectRepository.cs index f254691..a5d73c1 100644 --- a/Code2Gether-Discord-Bot.Tests/Fakes/FakeProjectRepository.cs +++ b/Code2Gether-Discord-Bot.Tests/Fakes/FakeProjectRepository.cs @@ -1,47 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Code2Gether_Discord_Bot.Library.Models; -using Code2Gether_Discord_Bot.Library.Models.Repositories.ProjectRepository; +using Code2Gether_Discord_Bot.Library.Models.Repositories.ProjectRepository; namespace Code2Gether_Discord_Bot.Tests.Fakes { - internal class FakeProjectRepository : IProjectRepository - { - public Dictionary Projects = new Dictionary(); - - public bool Create(Project newProject) - { - return Projects.TryAdd(newProject.ID, newProject); - } - - public Project Read(int id) - { - if (Projects.TryGetValue(id, out Project project)) - return project; - throw new Exception($"Failed to read project with ID {id}"); - } - - public Project Read(string projectName) - { - return ReadAll().Values.FirstOrDefault(p => p.Name == projectName); - } - - public IDictionary ReadAll() - { - return Projects; - } - - public bool Update(Project newProject) - { - Delete(newProject.ID); - return Create(newProject); - } - - public bool Delete(int id) - { - return Projects.Remove(id, out _); - } - } + /// + /// Represents a fake . This class cannot be inherited. + /// + internal sealed class FakeProjectRepository : ProjectDAL { } } diff --git a/Code2Gether-Discord-Bot.Tests/Fakes/FakeUser.cs b/Code2Gether-Discord-Bot.Tests/Fakes/FakeUser.cs index 4e6b885..83ab95b 100644 --- a/Code2Gether-Discord-Bot.Tests/Fakes/FakeUser.cs +++ b/Code2Gether-Discord-Bot.Tests/Fakes/FakeUser.cs @@ -1,8 +1,6 @@ using Discord; using System; -using System.Collections.Generic; using System.Collections.Immutable; -using System.Text; using System.Threading.Tasks; namespace Code2Gether_Discord_Bot.Tests.Fakes diff --git a/Code2Gether-Discord-Bot.Tests/Fakes/FakeUserMessage.cs b/Code2Gether-Discord-Bot.Tests/Fakes/FakeUserMessage.cs index 0bae743..5215c15 100644 --- a/Code2Gether-Discord-Bot.Tests/Fakes/FakeUserMessage.cs +++ b/Code2Gether-Discord-Bot.Tests/Fakes/FakeUserMessage.cs @@ -1,7 +1,6 @@ using Discord; using System; using System.Collections.Generic; -using System.Text; using System.Threading.Tasks; namespace Code2Gether_Discord_Bot.Tests.Fakes diff --git a/Code2Gether-Discord-Bot.Tests/InfoLogicTests.cs b/Code2Gether-Discord-Bot.Tests/InfoLogicTests.cs index ae152c0..04357ea 100644 --- a/Code2Gether-Discord-Bot.Tests/InfoLogicTests.cs +++ b/Code2Gether-Discord-Bot.Tests/InfoLogicTests.cs @@ -1,7 +1,4 @@ using Code2Gether_Discord_Bot.Library.BusinessLogic; -using Code2Gether_Discord_Bot.Library.Models; -using Code2Gether_Discord_Bot.Static; -using Code2Gether_Discord_Bot.Tests.Fakes; using NUnit.Framework; namespace Code2Gether_Discord_Bot.Tests @@ -11,47 +8,8 @@ internal class InfoLogicTests IBusinessLogic _logic; [SetUp] - public void Setup() - { - var user = new FakeUser() - { - Username = "UnitTest", - DiscriminatorValue = 1234, - Id = 123456789123456789 - }; - - var client = new FakeDiscordClient() - { - FakeApplication = new FakeApplication() - { - Owner = user - } - }; - - var guild = new FakeGuild() - { - - }; - - var messageChannel = new FakeMessageChannel() - { - - }; - - var message = new FakeUserMessage() - { - Author = user - }; - - _logic = BusinessLogicFactory.GetInfoLogic(GetType(), new FakeCommandContext() - { - Client = client, - Guild = guild, - User = user, - Message = message, - Channel = messageChannel - }); - } + public void Setup() => + _logic = TestConfig.InfoLogic(); [Test] public void InstantiationTest() => diff --git a/Code2Gether-Discord-Bot.Tests/JoinProjectTests.cs b/Code2Gether-Discord-Bot.Tests/JoinProjectTests.cs index 8878495..fa8ca51 100644 --- a/Code2Gether-Discord-Bot.Tests/JoinProjectTests.cs +++ b/Code2Gether-Discord-Bot.Tests/JoinProjectTests.cs @@ -1,13 +1,7 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Threading.Tasks; -using Code2Gether_Discord_Bot.Library.BusinessLogic; -using Code2Gether_Discord_Bot.Library.Models; +using Code2Gether_Discord_Bot.Library.BusinessLogic; using Code2Gether_Discord_Bot.Library.Models.Repositories.ProjectRepository; -using Code2Gether_Discord_Bot.Static; -using Code2Gether_Discord_Bot.Tests.Fakes; using NUnit.Framework; +using System.Threading.Tasks; namespace Code2Gether_Discord_Bot.Tests { @@ -19,52 +13,12 @@ internal class JoinProjectTests [SetUp] public void Setup() { - var user = new FakeUser() - { - Username = "UnitTest", - DiscriminatorValue = 1234, - Id = 123456789123456789 - }; - - var client = new FakeDiscordClient() - { - FakeApplication = new FakeApplication() - { - Owner = user - } - }; - - var guild = new FakeGuild() - { - - }; + _repo = TestConfig.ProjectRepository(); - var messageChannel = new FakeMessageChannel() - { + const string PROJECT_NAME = "testProj"; + _repo.Create(TestConfig.Project(0, PROJECT_NAME)); - }; - - var message = new FakeUserMessage() - { - Author = user - }; - - _repo = new FakeProjectRepository() - { - Projects = new Dictionary() - { - {0, new Project(0, "unittest", user)}, - } - }; - - _logic = new JoinProjectLogic(UtilityFactory.GetLogger(GetType()), new FakeCommandContext() - { - Channel = messageChannel, - Client = client, - Guild = guild, - Message = message, - User = user - }, new FakeProjectManager(_repo), "unittest"); + _logic = TestConfig.JoinProjectLogic(_repo, PROJECT_NAME); } [Test] @@ -74,8 +28,8 @@ public void InstantiationTest() => [Test] public async Task ExecutionTest() { - await _logic.ExecuteAsync(); - + var task = await _logic.ExecuteAsync(); + Assert.IsTrue(_repo.Read(0).ProjectMembers.Count > 0); } } diff --git a/Code2Gether-Discord-Bot.Tests/ListProjectsTests.cs b/Code2Gether-Discord-Bot.Tests/ListProjectsTests.cs index ff5fcfc..d538bcf 100644 --- a/Code2Gether-Discord-Bot.Tests/ListProjectsTests.cs +++ b/Code2Gether-Discord-Bot.Tests/ListProjectsTests.cs @@ -1,13 +1,7 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Threading.Tasks; -using Code2Gether_Discord_Bot.Library.BusinessLogic; -using Code2Gether_Discord_Bot.Library.Models; +using Code2Gether_Discord_Bot.Library.BusinessLogic; using Code2Gether_Discord_Bot.Library.Models.Repositories.ProjectRepository; -using Code2Gether_Discord_Bot.Static; -using Code2Gether_Discord_Bot.Tests.Fakes; using NUnit.Framework; +using System.Threading.Tasks; namespace Code2Gether_Discord_Bot.Tests { @@ -19,52 +13,10 @@ internal class ListProjectsTests [SetUp] public void Setup() { - var user = new FakeUser() - { - Username = "UnitTest", - DiscriminatorValue = 1234, - Id = 123456789123456789 - }; - - var client = new FakeDiscordClient() - { - FakeApplication = new FakeApplication() - { - Owner = user - } - }; - - var guild = new FakeGuild() - { - - }; - - var messageChannel = new FakeMessageChannel() - { - - }; - - var message = new FakeUserMessage() - { - Author = user - }; - - _repo = new FakeProjectRepository() - { - Projects = new Dictionary() - { - {0, new Project(0, "unittest", user)}, - } - }; + _repo = TestConfig.ProjectRepository(); + _repo.Create(TestConfig.Project(0)); - _logic = new ListProjectsLogic(UtilityFactory.GetLogger(GetType()), new FakeCommandContext() - { - Channel = messageChannel, - Client = client, - Guild = guild, - Message = message, - User = user - }, _repo); + _logic = TestConfig.ListProjectsLogic(_repo); } [Test] diff --git a/Code2Gether-Discord-Bot.Tests/MakeChannelTests.cs b/Code2Gether-Discord-Bot.Tests/MakeChannelTests.cs index 1f73ea8..aa429bd 100644 --- a/Code2Gether-Discord-Bot.Tests/MakeChannelTests.cs +++ b/Code2Gether-Discord-Bot.Tests/MakeChannelTests.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; -using Code2Gether_Discord_Bot.Library.BusinessLogic; -using Code2Gether_Discord_Bot.Library.Models; -using Code2Gether_Discord_Bot.Static; -using Code2Gether_Discord_Bot.Tests.Fakes; +using Code2Gether_Discord_Bot.Library.BusinessLogic; using NUnit.Framework; namespace Code2Gether_Discord_Bot.Tests @@ -14,48 +8,8 @@ public class MakeChannelTests IBusinessLogic _logic; [SetUp] - public void Setup() - { - var user = new FakeUser() - { - Username = "UnitTest", - DiscriminatorValue = 1234, - Id = 123456789123456789 - }; - - var client = new FakeDiscordClient() - { - FakeApplication = new FakeApplication() - { - Owner = user - } - }; - - var guild = new FakeGuild() - { - - }; - - var messageChannel = new FakeMessageChannel() - { - - }; - - var message = new FakeUserMessage() - { - Author = user, - Content = "debug!makechannel make-me" - }; - - _logic = BusinessLogicFactory.GetMakeChannelLogic(GetType(), new FakeCommandContext() - { - Client = client, - Guild = guild, - User = user, - Message = message, - Channel = messageChannel - }, "unit-test"); - } + public void Setup() => + _logic = TestConfig.MakeChannelLogic(); [Test] public void InstantiationTest() => diff --git a/Code2Gether-Discord-Bot.Tests/PingLogicTests.cs b/Code2Gether-Discord-Bot.Tests/PingLogicTests.cs index 6438bbc..3c97e86 100644 --- a/Code2Gether-Discord-Bot.Tests/PingLogicTests.cs +++ b/Code2Gether-Discord-Bot.Tests/PingLogicTests.cs @@ -1,9 +1,6 @@ -using System.Threading.Tasks; using Code2Gether_Discord_Bot.Library.BusinessLogic; -using Code2Gether_Discord_Bot.Library.Models; -using Code2Gether_Discord_Bot.Static; -using Code2Gether_Discord_Bot.Tests.Fakes; using NUnit.Framework; +using System.Threading.Tasks; namespace Code2Gether_Discord_Bot.Tests { @@ -12,47 +9,8 @@ public class PingLogicTests IBusinessLogic _logic; [SetUp] - public void Setup() - { - var user = new FakeUser() - { - Username = "UnitTest", - DiscriminatorValue = 1234, - Id = 123456789123456789 - }; - - var client = new FakeDiscordClient() - { - FakeApplication = new FakeApplication() - { - Owner = user - } - }; - - var guild = new FakeGuild() - { - - }; - - var messageChannel = new FakeMessageChannel() - { - - }; - - var message = new FakeUserMessage() - { - Author = user - }; - - _logic = BusinessLogicFactory.GetPingLogic(GetType(), new FakeCommandContext() - { - Client = client, - Guild = guild, - User = user, - Message = message, - Channel = messageChannel - }, 999); - } + public void Setup() => + _logic = TestConfig.PingLogic(); [Test] public void InstantiationTest() => diff --git a/Code2Gether-Discord-Bot.Tests/ProjectManagerTests.cs b/Code2Gether-Discord-Bot.Tests/ProjectManagerTests.cs new file mode 100644 index 0000000..e0ee758 --- /dev/null +++ b/Code2Gether-Discord-Bot.Tests/ProjectManagerTests.cs @@ -0,0 +1,197 @@ +using NUnit.Framework; + +namespace Code2Gether_Discord_Bot.Tests +{ + internal class ProjectManagerTests + { + #region CreateProject Method + [Test] + public void CreateProject_SingleProject_ReturnsProject() + { + var stubUser = TestConfig.User(); + var stubRepository = TestConfig.ProjectRepository(); + var projectManager = TestConfig.ProjectManager(stubRepository); + const string PROJECT_NAME = "proj"; + + var createdProject = projectManager.CreateProject(PROJECT_NAME, stubUser); + + Assert.IsNotNull(createdProject); + } + [Test] + public void CreateProject_SingleProjectProperties_ChecksIfAuthorIsRight() + { + var stubRepository = TestConfig.ProjectRepository(); + var projectManager = TestConfig.ProjectManager(stubRepository); + var mockUser = TestConfig.User(); + const string PROJECT_NAME = "proj"; + + var createdProject = projectManager.CreateProject(PROJECT_NAME, mockUser); + + Assert.AreEqual(mockUser, createdProject.Author); + } + [Test] + [TestCase(0, "a", "a", "b", "c", "d", "e")] + [TestCase(1, "b", "a", "b", "c", "d", "e")] + [TestCase(2, "c", "a", "b", "c", "d", "e")] + public void CreateProject_ManyProjectsProperties_ChecksIfIdIsRight(int expectedId, string selectedProjectName, params string[] projectsName) + { + var stubUser = TestConfig.User(); + var mockRepository = TestConfig.ProjectRepository(); + var projectManager = TestConfig.ProjectManager(mockRepository); + + foreach (var name in projectsName) + projectManager.CreateProject(name, stubUser); + var selectedProject = mockRepository.Read(selectedProjectName); + + Assert.AreEqual(expectedId, selectedProject.ID); + } + [Test] + public void CreateProject_SingleProjectProperties_ChecksIfNameIsRight() + { + var stubUser = TestConfig.User(); + var stubRepository = TestConfig.ProjectRepository(); + var projectManager = TestConfig.ProjectManager(stubRepository); + const string PROJECT_NAME = "proj"; + + var createdProject = projectManager.CreateProject(PROJECT_NAME, stubUser); + + Assert.AreEqual(PROJECT_NAME, createdProject.Name); + } + [Test] + public void CreateProject_SingleProjectProperties_ChecksIfProjectMembersIsRight() + { + var stubRepository = TestConfig.ProjectRepository(); + var projectManager = TestConfig.ProjectManager(stubRepository); + var mockUser = TestConfig.User(); + const string PROJECT_NAME = "proj"; + + var createdProject = projectManager.CreateProject(PROJECT_NAME, mockUser); + + Assert.IsTrue(createdProject.ProjectMembers.Count == 1 && createdProject.ProjectMembers.Contains(mockUser)); + } + #endregion + + #region DoesProjectExist Method + [Test] + public void DoesProjectExist_CreateSingleProject_ReturnsProject() + { + var stubRepository = TestConfig.ProjectRepository(); + var projectManager = TestConfig.ProjectManager(stubRepository); + var project = TestConfig.Project(0); + stubRepository.Create(project); + + projectManager.DoesProjectExist(project.Name, out var actualProject); + + Assert.IsNotNull(actualProject); + } + [Test] + public void DoesProjectExist_CreateSingleProject_ChecksIfProjectExist() + { + var stubRepository = TestConfig.ProjectRepository(); + var projectManager = TestConfig.ProjectManager(stubRepository); + var project = TestConfig.Project(0); + stubRepository.Create(project); + + var doesExist = projectManager.DoesProjectExist(project.Name); + + Assert.IsTrue(doesExist); + } + [Test] + public void DoesProjectExist_CreateSingleProject_ChecksIfReturnedProjectIsTheCreated() + { + var stubRepository = TestConfig.ProjectRepository(); + var projectManager = TestConfig.ProjectManager(stubRepository); + var expectedProject = TestConfig.Project(0); + stubRepository.Create(expectedProject); + + projectManager.DoesProjectExist(expectedProject.Name, out var actualProject); + + Assert.AreEqual(expectedProject, actualProject); + } + #endregion + + #region JoinProjectTests Method + [Test] + public static void JoinProject_UserJoinsProject_ChecksWhetherUserHasActuallyJoined() + { + var stubUser = TestConfig.User(); + var stubRepository = TestConfig.ProjectRepository(); + var projectManager = TestConfig.ProjectManager(stubRepository); + const string PROJECT_NAME = "proj"; + stubRepository.Create(TestConfig.Project(0, PROJECT_NAME, stubUser)); + + projectManager.JoinProject(PROJECT_NAME, stubUser, out var actualProject); + var isAmongMembers = actualProject.ProjectMembers.Contains(stubUser); + + Assert.IsTrue(isAmongMembers); + } + [Test] + public static void JoinProject_UserJoinsProject_ReturnsProject() + { + var stubUser = TestConfig.User(); + var stubRepository = TestConfig.ProjectRepository(); + var projectManager = TestConfig.ProjectManager(stubRepository); + const string PROJECT_NAME = "proj"; + stubRepository.Create(TestConfig.Project(0, PROJECT_NAME, stubUser)); + + projectManager.JoinProject(PROJECT_NAME, stubUser, out var actualProject); + + Assert.IsNotNull(actualProject); + } + [Test] + public static void JoinProject_UserJoinsProject_ReturnsTrue() + { + var stubUser = TestConfig.User(); + var stubRepository = TestConfig.ProjectRepository(); + var projectManager = TestConfig.ProjectManager(stubRepository); + const string PROJECT_NAME = "proj"; + stubRepository.Create(TestConfig.Project(0, PROJECT_NAME, stubUser)); + + var actual = projectManager.JoinProject(PROJECT_NAME, stubUser, out _); + + Assert.IsTrue(actual); + } + [Test] + public static void JoinProject_UserAlreadyIsIn_ChecksProjectHasNotChanged() + { + var stubUser = TestConfig.User(); + var stubRepository = TestConfig.ProjectRepository(); + var projectManager = TestConfig.ProjectManager(stubRepository); + const string PROJECT_NAME = "proj"; + stubRepository.Create(TestConfig.Project(0, PROJECT_NAME, stubUser)); + var expectedProject = stubRepository.Read(0); + expectedProject.ProjectMembers.Add(stubUser); + + projectManager.JoinProject(PROJECT_NAME, stubUser, out var actualProject); + + Assert.AreEqual(expectedProject, actualProject); + } + [Test] + public static void JoinProject_UserAlreadyIsIn_ReturnsFalse() + { + var stubUser = TestConfig.User(); + var stubRepository = TestConfig.ProjectRepository(); + var projectManager = TestConfig.ProjectManager(stubRepository); + const string PROJECT_NAME = "proj"; + stubRepository.Create(TestConfig.Project(0, PROJECT_NAME, stubUser)); + stubRepository.Read(0).ProjectMembers.Add(stubUser); + + var actual = projectManager.JoinProject(PROJECT_NAME, stubUser, out _); + + Assert.IsFalse(actual); + } + [Test] + public static void JoinProject_ProjectNotExist_ReturnsFalse() + { + var stubUser = TestConfig.User(); + var stubRepository = TestConfig.ProjectRepository(); + var projectManager = TestConfig.ProjectManager(stubRepository); + const string PROJECT_NAME = "proj"; + + var actual = projectManager.JoinProject(PROJECT_NAME, stubUser, out _); + + Assert.IsFalse(actual); + } + #endregion + } +} diff --git a/Code2Gether-Discord-Bot.Tests/TestConfig.cs b/Code2Gether-Discord-Bot.Tests/TestConfig.cs new file mode 100644 index 0000000..8553b18 --- /dev/null +++ b/Code2Gether-Discord-Bot.Tests/TestConfig.cs @@ -0,0 +1,218 @@ +using Code2Gether_Discord_Bot.Library.BusinessLogic; +using Code2Gether_Discord_Bot.Library.Models; +using Code2Gether_Discord_Bot.Library.Models.Repositories.ProjectRepository; +using Code2Gether_Discord_Bot.Tests.Fakes; + +namespace Code2Gether_Discord_Bot.Tests +{ + /// + /// This class intantiates all fake objects used inside in unit tests. + /// + internal static class TestConfig + { + #region BusinessLogic + /// + /// Instantiates a generic . + /// + /// ExcuseGeneratorLogic with irrelevant properties. + public static IBusinessLogic ExcuseGeneratorLogic() + { + var logger = Logger(); + var context = CommandContext(); + + return new ExcuseGeneratorLogic(logger, context); + } + /// + /// Instantiates a generic . + /// + /// InfoLogic with irrelevant properties. + public static IBusinessLogic InfoLogic() + { + var logger = Logger(); + var context = CommandContext(); + + return new InfoLogic(logger, context); + } + /// + /// Instantiates a generic with a custom project repository. + /// + /// This instance's project repository. + /// The name of the project to join. + /// JoinProjectLogic with a custom project repository and irrelevant properties. + public static IBusinessLogic JoinProjectLogic(IProjectRepository projectRepository, string projectName) + { + var logger = Logger(); + var context = CommandContext(); + var projectManager = ProjectManager(projectRepository); + + return new JoinProjectLogic(logger, context, projectManager, projectName); + } + /// + /// Instantiates a generic with a custom project repository. + /// + /// This instance's project repository. + /// ListProjectsLogic with a custom project repository and irrelevant properties. + public static IBusinessLogic ListProjectsLogic(IProjectRepository projectRepository) + { + var logger = Logger(); + var context = CommandContext(); + + return new ListProjectsLogic(logger, context, projectRepository); + } + /// + /// Instantiates a generic . + /// + /// MakeChannelLogic with irrelevant properties. + public static IBusinessLogic MakeChannelLogic() + { + var logger = Logger(); + var context = CommandContext(); + context.Message = UserMessage(context.Message.Author as FakeUser, "debug!makechannel make-me"); + var channelName = "test-channel"; + + return new MakeChannelLogic(logger, context, channelName); + } + /// + /// Instantiates a generic . + /// + /// PingLogic with irrelevant properties. + public static IBusinessLogic PingLogic() + { + var logger = Logger(); + var context = CommandContext(); + var latency = 1; + + return new PingLogic(logger, context, latency); + } + #endregion + + /// + /// Instantiates a Project with generic Name and Author. + /// + /// Project's Id. + /// Project with irrelevant Name and Author. + public static Project Project(int id) => + Project(id, $"proj{id}"); + /// + /// Instantiates a Project with custom Id and Author. + /// + /// Project's Id. + /// Project's Name. + /// Project with custom id and author and irrelevant project name. + public static Project Project(int id, string name) => + Project(id, name, User()); + /// + /// Instantiates a Project with custom Id, Name and Author. + /// + /// Project's Id. + /// Project's Name. + /// Project's Author. + /// Project with all the custom properties. + public static Project Project(int id, string name, FakeUser author) => + new Project + ( + id, + name, + author + ); + /// + /// Instantiates a ProjectManager with empty FakeProjectRepository. + /// + /// ProjectManager with empty project repository. + public static ProjectManager ProjectManager() => + new ProjectManager(ProjectRepository()); + /// + /// Instantiates a ProjectManager with custom IProjectRepository. + /// + /// ProjectManager's project repository. + /// ProjectManager with custom project repository. + public static ProjectManager ProjectManager(IProjectRepository projectRepository) => + new ProjectManager(projectRepository); + /// + /// Instantiates a empty FakeProjectRepository. + /// + /// Empty FakeProjectRepository. + public static FakeProjectRepository ProjectRepository() => + new FakeProjectRepository(); + /// + /// Intantiates a FakeUser with generic Username, DiscriminatorValue and Id. + /// + /// FakeUser with irrelevant properties. + public static FakeUser User() => + User("UnitTest", 1234, 123456789); + /// + /// Intantiates a FakeUser with especific Username, DiscriminatorValue and Id. + /// + /// FakeUser's Username. + /// FakeUser's DiscriminatorValue. + /// FakeUser's Id. + /// FakeUser with the properties informed in the parameters. + public static FakeUser User(string username, ushort discriminatorValue, ulong id) => + new FakeUser() + { + Username = username, + DiscriminatorValue = discriminatorValue, + Id = id + }; + /// + /// Intantiates a generic FakeDiscordClient. + /// + /// FakeDiscordClient with irrelevant properties. + private static FakeDiscordClient Client() => + new FakeDiscordClient(); + /// + /// Intantiates a generic FakeCommandContext. + /// + /// FakeCommandContext with irrelevant properties. + private static FakeCommandContext CommandContext() + { + var commandContext = new FakeCommandContext() + { + Channel = MessageChannel(), + Client = Client(), + Guild = Guild(), + User = User() + }; + commandContext.Message = UserMessage(commandContext.User as FakeUser); + + return commandContext; + } + /// + /// Intantiates a generic FakeGuild. + /// + /// FakeGuild with irrelevant properties. + private static FakeGuild Guild() => + new FakeGuild(); + /// + /// Instantiates a FakeLogger that logs nothing. + /// + /// A non-logger Logger. + private static FakeLogger Logger() => + new FakeLogger(); + /// + /// Instantiates a generic FakeMessageChannel. + /// + /// FakeMessageChannel with irrelevant properties. + private static FakeMessageChannel MessageChannel() => + new FakeMessageChannel(); + /// + /// Instantiates a generic FakeUserMessage with specific author. + /// + /// Message's author. + /// FakeUserMessage with a specific author and irrelevant properties. + private static FakeUserMessage UserMessage(FakeUser author) => + UserMessage(author, null); + /// + /// Instantiates a generic FakeUserMessage with specific author and content. + /// + /// Message's author. + /// Message's content. + /// FakeUserMessage with a specific author, content and irrelevant properties. + private static FakeUserMessage UserMessage(FakeUser author, string content) => + new FakeUserMessage() + { + Author = author, + Content = content + }; + } +}