From 4417b86ccd570f1f5637afc7460ef70fcf1c47c2 Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Thu, 21 Jan 2021 19:30:25 +0200 Subject: Refactored technology layer tests to be async --- .../TechnologyRepository.Tests.cs | 147 ++++++++------------- 1 file changed, 57 insertions(+), 90 deletions(-) (limited to 'src/DevHive.Tests/DevHive.Data.Tests/TechnologyRepository.Tests.cs') diff --git a/src/DevHive.Tests/DevHive.Data.Tests/TechnologyRepository.Tests.cs b/src/DevHive.Tests/DevHive.Data.Tests/TechnologyRepository.Tests.cs index 98f2b2f..9c95c6d 100644 --- a/src/DevHive.Tests/DevHive.Data.Tests/TechnologyRepository.Tests.cs +++ b/src/DevHive.Tests/DevHive.Data.Tests/TechnologyRepository.Tests.cs @@ -4,7 +4,6 @@ using Microsoft.EntityFrameworkCore; using NUnit.Framework; using System; using System.Linq; -using System.Threading.Tasks; namespace DevHive.Data.Tests { @@ -35,7 +34,6 @@ namespace DevHive.Data.Tests } #region AddAsync - [Test] public void AddAsync_AddsTheGivenTechnologyToTheDatabase() { @@ -50,153 +48,122 @@ namespace DevHive.Data.Tests #region GetByIdAsync [Test] - public void GetByIdAsync_ReturnsTheCorrectTechnology_IfIdExists() + public async void GetByIdAsync_ReturnsTheCorrectTechnology_IfIdExists() { - Task.Run(async () => - { - AddEntity(); - Technology technology = this.Context.Technologies.Where(x => x.Name == TECHNOLOGY_NAME).ToList().FirstOrDefault(); - Guid id = technology.Id; + 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"); - }).GetAwaiter().GetResult(); + Assert.AreEqual(TECHNOLOGY_NAME, technologyReturned.Name, "GetByIdAsync does not return the correct Technology when id is valid"); } [Test] - public void GetByIdAsync_ReturnsNull_IfIdDoesNotExists() + public async void GetByIdAsync_ReturnsNull_IfIdDoesNotExists() { - Task.Run(async () => - { - Guid id = Guid.NewGuid(); - Technology technologyReturned = await this.TechnologyRepository.GetByIdAsync(id); + Guid id = Guid.NewGuid(); + + 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 void DoesTechnologyExist_ReturnsTrue_IfIdExists() + public async void DoesTechnologyExist_ReturnsTrue_IfIdExists() { - Task.Run(async () => - { - AddEntity(); - Technology technology = this.Context.Technologies.Where(x => x.Name == TECHNOLOGY_NAME).ToList().FirstOrDefault(); - Guid id = technology.Id; + 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"); - }).GetAwaiter().GetResult(); + Assert.IsTrue(result, "DoesTechnologyExistAsync returns flase hwen technology exists"); } [Test] - public void DoesTechnologyExist_ReturnsFalse_IfIdDoesNotExists() + public async void DoesTechnologyExist_ReturnsFalse_IfIdDoesNotExists() { - Task.Run(async () => - { - Guid id = Guid.NewGuid(); + 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"); - }).GetAwaiter().GetResult(); + Assert.IsFalse(result, "DoesTechnologyExistAsync returns true when technology does not exist"); } #endregion #region DoesTechnologyNameExistAsync [Test] - public void DoesTechnologyNameExist_ReturnsTrue_IfTechnologyExists() + public async void DoesTechnologyNameExist_ReturnsTrue_IfTechnologyExists() { - Task.Run(async () => - { - AddEntity(); + 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"); - }).GetAwaiter().GetResult(); + Assert.IsTrue(result, "DoesTechnologyNameExists returns true when technology name does not exist"); } [Test] - public void DoesTechnologyNameExist_ReturnsFalse_IfTechnologyDoesNotExists() + public async void DoesTechnologyNameExist_ReturnsFalse_IfTechnologyDoesNotExists() { - Task.Run(async () => - { - bool result = await this.TechnologyRepository.DoesTechnologyNameExistAsync(TECHNOLOGY_NAME); + bool result = await this.TechnologyRepository.DoesTechnologyNameExistAsync(TECHNOLOGY_NAME); - Assert.False(result, "DoesTechnologyNameExistAsync returns true when technology name does not exist"); - }).GetAwaiter().GetResult(); + Assert.False(result, "DoesTechnologyNameExistAsync returns true when technology name does not exist"); } #endregion #region EditAsync - //TO DO fix + //TO DO fix: check UserRepo [Test] - public void EditAsync_UpdatesEntity() + public async void EditAsync_UpdatesEntity() { - Task.Run(async () => + string newName = "New name"; + Guid id = Guid.NewGuid(); + Technology technology = new Technology + { + Name = TECHNOLOGY_NAME, + Id = id + }; Technology newTechnology = new Technology { - string newName = "New name"; - Guid id = Guid.NewGuid(); - Technology technology = new Technology - { - Name = TECHNOLOGY_NAME, - Id = id - }; Technology newTechnology = new Technology - { - Name = newName, - Id = id - }; - - await this.TechnologyRepository.AddAsync(technology); - - bool result = await this.TechnologyRepository.EditAsync(newTechnology); - - Assert.IsTrue(result); - }).GetAwaiter().GetResult(); + Name = newName, + Id = id + }; + + await this.TechnologyRepository.AddAsync(technology); + + bool result = await this.TechnologyRepository.EditAsync(newTechnology); + + Assert.IsTrue(result); } #endregion #region DeleteAsync [Test] - public void DeleteAsync_ReturnsTrue_IfDeletionIsSuccesfull() + public async void DeleteAsync_ReturnsTrue_IfDeletionIsSuccesfull() { - Task.Run(async () => - { - AddEntity(); - Technology technology = this.Context.Technologies.Where(x => x.Name == TECHNOLOGY_NAME).ToList().FirstOrDefault(); - - bool result = await this.TechnologyRepository.DeleteAsync(technology); + AddEntity(); + Technology technology = this.Context.Technologies.Where(x => x.Name == TECHNOLOGY_NAME).ToList().FirstOrDefault(); - Assert.IsTrue(result, "DeleteAsync returns false when deletion is successfull"); + bool result = await this.TechnologyRepository.DeleteAsync(technology); - }).GetAwaiter().GetResult(); + Assert.IsTrue(result, "DeleteAsync returns false when deletion is successfull"); } #endregion #region HelperMethods - private void AddEntity(string name = TECHNOLOGY_NAME) + private async void AddEntity(string name = TECHNOLOGY_NAME) { - Task.Run(async () => + Technology technology = new Technology { - Technology technology = new Technology - { - Name = name - }; + Name = name + }; - await this.TechnologyRepository.AddAsync(technology); - }).GetAwaiter().GetResult(); + await this.TechnologyRepository.AddAsync(technology); } #endregion - - //Task.Run(async () => - //{ - // - //}).GetAwaiter().GetResult(); } } -- cgit v1.2.3