From 5e5e2eb2edef88840edbb072597f81f8da3ae929 Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Thu, 21 Jan 2021 20:51:11 +0200 Subject: Made all Technology Layer Tests not asymc again --- .../Repositories/LanguageRepository.cs | 10 +- .../DevHive.Data.Tests/LenguageRepository.Tests.cs | 1 + .../TechnologyRepository.Tests.cs | 134 +++++++++------- .../LanguageService.Tests.cs | 100 ++++++++++++ .../TechnologyServices.Tests.cs | 176 ++++++++++++--------- 5 files changed, 286 insertions(+), 135 deletions(-) create mode 100644 src/DevHive.Tests/DevHive.Services.Tests/LanguageService.Tests.cs diff --git a/src/DevHive.Data/Repositories/LanguageRepository.cs b/src/DevHive.Data/Repositories/LanguageRepository.cs index 4c51cf3..808fa9a 100644 --- a/src/DevHive.Data/Repositories/LanguageRepository.cs +++ b/src/DevHive.Data/Repositories/LanguageRepository.cs @@ -45,11 +45,13 @@ namespace DevHive.Data.Repositories #region Update - public async Task EditAsync(Language newEntity) + public async Task EditAsync(Language entity) { - this._context - .Set() - .Update(newEntity); + Language language = await this._context.Languages + .FirstOrDefaultAsync(x => x.Id == entity.Id); + + this._context.Update(language); + this._context.Entry(entity).CurrentValues.SetValues(entity); return await this.SaveChangesAsync(this._context); } diff --git a/src/DevHive.Tests/DevHive.Data.Tests/LenguageRepository.Tests.cs b/src/DevHive.Tests/DevHive.Data.Tests/LenguageRepository.Tests.cs index 0a99bf1..aefeddd 100644 --- a/src/DevHive.Tests/DevHive.Data.Tests/LenguageRepository.Tests.cs +++ b/src/DevHive.Tests/DevHive.Data.Tests/LenguageRepository.Tests.cs @@ -6,5 +6,6 @@ namespace DevHive.Data.Tests [TestFixture] public class LenguageRepositoryTests { + // pending repo refactoring } } diff --git a/src/DevHive.Tests/DevHive.Data.Tests/TechnologyRepository.Tests.cs b/src/DevHive.Tests/DevHive.Data.Tests/TechnologyRepository.Tests.cs index 9c95c6d..c0aaa93 100644 --- a/src/DevHive.Tests/DevHive.Data.Tests/TechnologyRepository.Tests.cs +++ b/src/DevHive.Tests/DevHive.Data.Tests/TechnologyRepository.Tests.cs @@ -4,6 +4,7 @@ using Microsoft.EntityFrameworkCore; using NUnit.Framework; using System; using System.Linq; +using System.Threading.Tasks; namespace DevHive.Data.Tests { @@ -46,124 +47,151 @@ namespace DevHive.Data.Tests #endregion #region GetByIdAsync - [Test] - public async void GetByIdAsync_ReturnsTheCorrectTechnology_IfIdExists() + public void GetByIdAsync_ReturnsTheCorrectTechnology_IfIdExists() { - AddEntity(); - Technology technology = this.Context.Technologies.Where(x => x.Name == TECHNOLOGY_NAME).ToList().FirstOrDefault(); - Guid id = technology.Id; + Task.Run(async () => + { + AddEntity(); + Technology technology = this.Context.Technologies.Where(x => x.Name == TECHNOLOGY_NAME).ToList().FirstOrDefault(); + Guid id = technology.Id; - Technology technologyReturned = await this.TechnologyRepository.GetByIdAsync(id); + Technology technologyReturned = await this.TechnologyRepository.GetByIdAsync(id); - Assert.AreEqual(TECHNOLOGY_NAME, technologyReturned.Name, "GetByIdAsync does not return the correct Technology when id is valid"); + Assert.AreEqual(TECHNOLOGY_NAME, technologyReturned.Name, "GetByIdAsync does not return the correct Technology when id is valid"); + }).GetAwaiter().GetResult(); } [Test] - public async void GetByIdAsync_ReturnsNull_IfIdDoesNotExists() + public void GetByIdAsync_ReturnsNull_IfIdDoesNotExists() { + Task.Run(async () => + { + Guid id = Guid.NewGuid(); - Guid id = Guid.NewGuid(); - - Technology technologyReturned = await this.TechnologyRepository.GetByIdAsync(id); + Technology technologyReturned = await this.TechnologyRepository.GetByIdAsync(id); - Assert.IsNull(technologyReturned, "GetByIdAsync returns Technology when it should be null"); + Assert.IsNull(technologyReturned, "GetByIdAsync returns Technology when it should be null"); + }).GetAwaiter().GetResult(); } #endregion #region DoesTechnologyExistAsync [Test] - public async void DoesTechnologyExist_ReturnsTrue_IfIdExists() + public void DoesTechnologyExist_ReturnsTrue_IfIdExists() { - AddEntity(); - Technology technology = this.Context.Technologies.Where(x => x.Name == TECHNOLOGY_NAME).ToList().FirstOrDefault(); - Guid id = technology.Id; + Task.Run(async () => + { + AddEntity(); + Technology technology = this.Context.Technologies.Where(x => x.Name == TECHNOLOGY_NAME).ToList().FirstOrDefault(); + Guid id = technology.Id; - bool result = await this.TechnologyRepository.DoesTechnologyExistAsync(id); + bool result = await this.TechnologyRepository.DoesTechnologyExistAsync(id); - Assert.IsTrue(result, "DoesTechnologyExistAsync returns flase hwen technology exists"); + Assert.IsTrue(result, "DoesTechnologyExistAsync returns flase hwen technology exists"); + }).GetAwaiter().GetResult(); } [Test] - public async void DoesTechnologyExist_ReturnsFalse_IfIdDoesNotExists() + public void DoesTechnologyExist_ReturnsFalse_IfIdDoesNotExists() { - Guid id = Guid.NewGuid(); + Task.Run(async () => + { + Guid id = Guid.NewGuid(); - bool result = await this.TechnologyRepository.DoesTechnologyExistAsync(id); + bool result = await this.TechnologyRepository.DoesTechnologyExistAsync(id); - Assert.IsFalse(result, "DoesTechnologyExistAsync returns true when technology does not exist"); + Assert.IsFalse(result, "DoesTechnologyExistAsync returns true when technology does not exist"); + }).GetAwaiter().GetResult(); } #endregion #region DoesTechnologyNameExistAsync [Test] - public async void DoesTechnologyNameExist_ReturnsTrue_IfTechnologyExists() + public void DoesTechnologyNameExist_ReturnsTrue_IfTechnologyExists() { - AddEntity(); + Task.Run(async () => + { + AddEntity(); - bool result = await this.TechnologyRepository.DoesTechnologyNameExistAsync(TECHNOLOGY_NAME); + bool result = await this.TechnologyRepository.DoesTechnologyNameExistAsync(TECHNOLOGY_NAME); - Assert.IsTrue(result, "DoesTechnologyNameExists returns true when technology name does not exist"); + Assert.IsTrue(result, "DoesTechnologyNameExists returns true when technology name does not exist"); + }).GetAwaiter().GetResult(); } [Test] - public async void DoesTechnologyNameExist_ReturnsFalse_IfTechnologyDoesNotExists() + public void DoesTechnologyNameExist_ReturnsFalse_IfTechnologyDoesNotExists() { - bool result = await this.TechnologyRepository.DoesTechnologyNameExistAsync(TECHNOLOGY_NAME); + Task.Run(async () => + { + bool result = await this.TechnologyRepository.DoesTechnologyNameExistAsync(TECHNOLOGY_NAME); - Assert.False(result, "DoesTechnologyNameExistAsync returns true when technology name does not exist"); + Assert.False(result, "DoesTechnologyNameExistAsync returns true when technology name does not exist"); + }).GetAwaiter().GetResult(); } #endregion #region EditAsync //TO DO fix: check UserRepo [Test] - public async void EditAsync_UpdatesEntity() + public void EditAsync_UpdatesEntity() { - string newName = "New name"; - Guid id = Guid.NewGuid(); - Technology technology = new Technology - { - Name = TECHNOLOGY_NAME, - Id = id - }; Technology newTechnology = new Technology + Task.Run(async () => { - Name = newName, - Id = id - }; + string newName = "New name"; + Technology technology = new Technology + { + Name = TECHNOLOGY_NAME + }; Technology newTechnology = new Technology + { + Name = newName + }; - await this.TechnologyRepository.AddAsync(technology); + await this.TechnologyRepository.AddAsync(technology); - bool result = await this.TechnologyRepository.EditAsync(newTechnology); + bool result = await this.TechnologyRepository.EditAsync(newTechnology); - Assert.IsTrue(result); + Assert.IsTrue(result); + }).GetAwaiter().GetResult(); } #endregion #region DeleteAsync [Test] - public async void DeleteAsync_ReturnsTrue_IfDeletionIsSuccesfull() + public void DeleteAsync_ReturnsTrue_IfDeletionIsSuccesfull() { - AddEntity(); - Technology technology = this.Context.Technologies.Where(x => x.Name == TECHNOLOGY_NAME).ToList().FirstOrDefault(); + Task.Run(async () => + { + AddEntity(); + Technology technology = this.Context.Technologies.Where(x => x.Name == TECHNOLOGY_NAME).ToList().FirstOrDefault(); - bool result = await this.TechnologyRepository.DeleteAsync(technology); + bool result = await this.TechnologyRepository.DeleteAsync(technology); - Assert.IsTrue(result, "DeleteAsync returns false when deletion is successfull"); + Assert.IsTrue(result, "DeleteAsync returns false when deletion is successfull"); + }).GetAwaiter().GetResult(); } #endregion #region HelperMethods - private async void AddEntity(string name = TECHNOLOGY_NAME) + private void AddEntity(string name = TECHNOLOGY_NAME) { - Technology technology = new Technology + Task.Run(async () => { - Name = name - }; + Technology technology = new Technology + { + Name = name + }; - await this.TechnologyRepository.AddAsync(technology); + await this.TechnologyRepository.AddAsync(technology); + }).GetAwaiter().GetResult(); } #endregion + + //Task.Run(async () => + //{ + + //}).GetAwaiter().GetResult(); } } diff --git a/src/DevHive.Tests/DevHive.Services.Tests/LanguageService.Tests.cs b/src/DevHive.Tests/DevHive.Services.Tests/LanguageService.Tests.cs new file mode 100644 index 0000000..2ebff58 --- /dev/null +++ b/src/DevHive.Tests/DevHive.Services.Tests/LanguageService.Tests.cs @@ -0,0 +1,100 @@ +using System; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Data.Interfaces.Repositories; +using DevHive.Data.Models; +using DevHive.Services.Models.Language; +using DevHive.Services.Services; +using Moq; +using NUnit.Framework; + +namespace DevHive.Services.Tests +{ + [TestFixture] + public class LanguageServiceTests + { + private Mock LanguageRepositoryMock { get; set; } + private Mock MapperMock { get; set; } + private LanguageService LanguageService { get; set; } + + [SetUp] + public void SetUp() + { + this.LanguageRepositoryMock = new Mock(); + this.MapperMock = new Mock(); + this.LanguageService = new LanguageService(this.LanguageRepositoryMock.Object, this.MapperMock.Object); + } + + #region Create + [Test] + public async void Create_ReturnsNonEmptyGuid_WhenEntityIsAddedSuccessfully() + { + string technologyName = "Gosho Trapov"; + Guid id = Guid.NewGuid(); + CreateLanguageServiceModel createLanguageServiceModel = new() + { + Name = technologyName + }; + Language language = new() + { + Name = technologyName, + Id = id + }; + + this.LanguageRepositoryMock.Setup(p => p.DoesLanguageNameExistAsync(It.IsAny())).Returns(Task.FromResult(false)); + this.LanguageRepositoryMock.Setup(p => p.AddAsync(It.IsAny())).Returns(Task.FromResult(true)); + this.LanguageRepositoryMock.Setup(p => p.GetByNameAsync(It.IsAny())).Returns(Task.FromResult(language)); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(language); + + Guid result = await this.LanguageService.CreateLanguage(createLanguageServiceModel); + + Assert.AreEqual(id, result); + } + + [Test] + public async void Create_ReturnsEmptyGuid_WhenEntityIsNotAddedSuccessfully() + { + string languageName = "Gosho Trapov"; + + CreateLanguageServiceModel createLanguageServiceModel = new() + { + Name = languageName + }; + Language language = new() + { + Name = languageName + }; + + this.LanguageRepositoryMock.Setup(p => p.DoesLanguageNameExistAsync(It.IsAny())).Returns(Task.FromResult(false)); + this.LanguageRepositoryMock.Setup(p => p.AddAsync(It.IsAny())).Returns(Task.FromResult(false)); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(language); + + Guid result = await this.LanguageService.CreateLanguage(createLanguageServiceModel); + + Assert.IsTrue(result == Guid.Empty); + } + + [Test] + public void Create_ThrowsArgumentException_WhenEntityAlreadyExists() + { + string exceptionMessage = "Technology already exists!"; + string languageName = "Gosho Trapov"; + + CreateLanguageServiceModel createLanguageServiceModel = new() + { + Name = languageName + }; + Language language = new() + { + Name = languageName + }; + + this.LanguageRepositoryMock.Setup(p => p.DoesLanguageNameExistAsync(It.IsAny())).Returns(Task.FromResult(true)); + + Exception ex = Assert.ThrowsAsync(() => this.LanguageService.CreateLanguage(createLanguageServiceModel)); + + Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); + } + #endregion + } +} diff --git a/src/DevHive.Tests/DevHive.Services.Tests/TechnologyServices.Tests.cs b/src/DevHive.Tests/DevHive.Services.Tests/TechnologyServices.Tests.cs index 20aceb5..b04e213 100644 --- a/src/DevHive.Tests/DevHive.Services.Tests/TechnologyServices.Tests.cs +++ b/src/DevHive.Tests/DevHive.Services.Tests/TechnologyServices.Tests.cs @@ -27,51 +27,57 @@ namespace DevHive.Services.Tests #region Create [Test] - public async void Create_ReturnsNonEmptyGuid_WhenEntityIsAddedSuccessfully() + public void Create_ReturnsNonEmptyGuid_WhenEntityIsAddedSuccessfully() { - string technologyName = "Gosho Trapov"; - Guid id = Guid.NewGuid(); - CreateTechnologyServiceModel createTechnologyServiceModel = new() - { - Name = technologyName - }; - Technology technology = new() + Task.Run(async () => { - Name = technologyName, - Id = id - }; - - this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny())).Returns(Task.FromResult(false)); - this.TechnologyRepositoryMock.Setup(p => p.AddAsync(It.IsAny())).Returns(Task.FromResult(true)); - this.TechnologyRepositoryMock.Setup(p => p.GetByNameAsync(It.IsAny())).Returns(Task.FromResult(technology)); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(technology); - - Guid result = await this.TechnologyService.Create(createTechnologyServiceModel); - - Assert.AreEqual(id, result); + string technologyName = "Gosho Trapov"; + Guid id = Guid.NewGuid(); + CreateTechnologyServiceModel createTechnologyServiceModel = new() + { + Name = technologyName + }; + Technology technology = new() + { + Name = technologyName, + Id = id + }; + + this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny())).Returns(Task.FromResult(false)); + this.TechnologyRepositoryMock.Setup(p => p.AddAsync(It.IsAny())).Returns(Task.FromResult(true)); + this.TechnologyRepositoryMock.Setup(p => p.GetByNameAsync(It.IsAny())).Returns(Task.FromResult(technology)); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(technology); + + Guid result = await this.TechnologyService.Create(createTechnologyServiceModel); + + Assert.AreEqual(id, result); + }).GetAwaiter().GetResult(); } [Test] - public async void Create_ReturnsEmptyGuid_WhenEntityIsNotAddedSuccessfully() + public void Create_ReturnsEmptyGuid_WhenEntityIsNotAddedSuccessfully() { - string technologyName = "Gosho Trapov"; - - CreateTechnologyServiceModel createTechnologyServiceModel = new() - { - Name = technologyName - }; - Technology technology = new() + Task.Run(async () => { - Name = technologyName - }; + string technologyName = "Gosho Trapov"; - this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny())).Returns(Task.FromResult(false)); - this.TechnologyRepositoryMock.Setup(p => p.AddAsync(It.IsAny())).Returns(Task.FromResult(false)); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(technology); + CreateTechnologyServiceModel createTechnologyServiceModel = new() + { + Name = technologyName + }; + Technology technology = new() + { + Name = technologyName + }; - Guid result = await this.TechnologyService.Create(createTechnologyServiceModel); + this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny())).Returns(Task.FromResult(false)); + this.TechnologyRepositoryMock.Setup(p => p.AddAsync(It.IsAny())).Returns(Task.FromResult(false)); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(technology); - Assert.IsTrue(result == Guid.Empty); + Guid result = await this.TechnologyService.Create(createTechnologyServiceModel); + + Assert.IsTrue(result == Guid.Empty); + }).GetAwaiter().GetResult(); } [Test] @@ -99,25 +105,28 @@ namespace DevHive.Services.Tests #region Read [Test] - public async void GetTechnologyById_ReturnsTheTechnology_WhenItExists() + public void GetTechnologyById_ReturnsTheTechnology_WhenItExists() { - Guid id = new Guid(); - string name = "Gosho Trapov"; - Technology technology = new() + Task.Run(async () => { - Name = name - }; - CreateTechnologyServiceModel createTechnologyServiceModel = new() - { - Name = name - }; - - this.TechnologyRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(technology)); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(createTechnologyServiceModel); - - CreateTechnologyServiceModel result = await this.TechnologyService.GetTechnologyById(id); - - Assert.AreEqual(name, result.Name); + Guid id = new Guid(); + string name = "Gosho Trapov"; + Technology technology = new() + { + Name = name + }; + CreateTechnologyServiceModel createTechnologyServiceModel = new() + { + Name = name + }; + + this.TechnologyRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(technology)); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(createTechnologyServiceModel); + + CreateTechnologyServiceModel result = await this.TechnologyService.GetTechnologyById(id); + + Assert.AreEqual(name, result.Name); + }).GetAwaiter().GetResult(); } [Test] @@ -137,26 +146,29 @@ namespace DevHive.Services.Tests [Test] [TestCase(true)] [TestCase(false)] - public async void UpdateTechnology_ReturnsIfUpdateIsSuccessfull_WhenTechnologyExistsy(bool shouldPass) + public void UpdateTechnology_ReturnsIfUpdateIsSuccessfull_WhenTechnologyExistsy(bool shouldPass) { - string name = "Gosho Trapov"; - Technology technology = new Technology - { - Name = name - }; - UpdateTechnologyServiceModel updatetechnologyServiceModel = new UpdateTechnologyServiceModel + Task.Run(async () => { - Name = name, - }; - - this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny())).Returns(Task.FromResult(true)); - this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny())).Returns(Task.FromResult(false)); - this.TechnologyRepositoryMock.Setup(p => p.EditAsync(It.IsAny())).Returns(Task.FromResult(shouldPass)); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(technology); - - bool result = await this.TechnologyService.UpdateTechnology(updatetechnologyServiceModel); - - Assert.AreEqual(shouldPass, result); + string name = "Gosho Trapov"; + Technology technology = new Technology + { + Name = name + }; + UpdateTechnologyServiceModel updatetechnologyServiceModel = new UpdateTechnologyServiceModel + { + Name = name, + }; + + this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny())).Returns(Task.FromResult(true)); + this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny())).Returns(Task.FromResult(false)); + this.TechnologyRepositoryMock.Setup(p => p.EditAsync(It.IsAny())).Returns(Task.FromResult(shouldPass)); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(technology); + + bool result = await this.TechnologyService.UpdateTechnology(updatetechnologyServiceModel); + + Assert.AreEqual(shouldPass, result); + }).GetAwaiter().GetResult(); } [Test] @@ -196,18 +208,21 @@ namespace DevHive.Services.Tests [Test] [TestCase(true)] [TestCase(false)] - public async void DeleteTechnology_ShouldReturnIfDeletionIsSuccessfull_WhenTechnologyExists(bool shouldPass) + public void DeleteTechnology_ShouldReturnIfDeletionIsSuccessfull_WhenTechnologyExists(bool shouldPass) { - Guid id = new Guid(); - Technology technology = new Technology(); + Task.Run(async () => + { + Guid id = new Guid(); + Technology technology = new Technology(); - this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny())).Returns(Task.FromResult(true)); - this.TechnologyRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(technology)); - this.TechnologyRepositoryMock.Setup(p => p.DeleteAsync(It.IsAny())).Returns(Task.FromResult(shouldPass)); + this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny())).Returns(Task.FromResult(true)); + this.TechnologyRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(technology)); + this.TechnologyRepositoryMock.Setup(p => p.DeleteAsync(It.IsAny())).Returns(Task.FromResult(shouldPass)); - bool result = await this.TechnologyService.DeleteTechnology(id); + bool result = await this.TechnologyService.DeleteTechnology(id); - Assert.AreEqual(shouldPass, result); + Assert.AreEqual(shouldPass, result); + }).GetAwaiter().GetResult(); } [Test] @@ -223,5 +238,10 @@ namespace DevHive.Services.Tests Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); } #endregion + + //Task.Run(async () => + //{ + + //}).GetAwaiter().GetResult(); } } -- cgit v1.2.3