소스 검색

Added tests for ads controller & service

feature/write_unit_tests_be^2
Ermin Bronja 3 년 전
부모
커밋
07deb4a121

+ 12
- 8
Diligent.WebAPI.Business/Extensions/AdExtensions.cs 파일 보기

@@ -8,22 +8,26 @@ namespace Diligent.WebAPI.Business.Extensions
{
public static class AdExtensions
{
public static IQueryable<Ad> FilterByExperience(this IQueryable<Ad> query, int minExperience, int maxExperience) =>
minExperience >= maxExperience ? query.Where(x => x.MinimumExperience >= minExperience && x.MinimumExperience <= maxExperience)
: query.Where(x => x.MinimumExperience >= minExperience && x.MinimumExperience <= maxExperience);
public static IQueryable<Ad> FilterByWorkType(this IQueryable<Ad> query, string workHour) =>
workHour.ToLower() == "parttime" ? query.Where(x => x.WorkHour == WorkHours.PartTime) : query.Where(x => x.WorkHour == WorkHours.FullTime);
public static IQueryable<Ad> FilterByEmploymentType(this IQueryable<Ad> query, string employmentType) =>
employmentType.ToLower() == "intership" ? query.Where(x => x.EmploymentType == EmploymentTypes.Intership) : query.Where(x => x.EmploymentType == EmploymentTypes.Work);
public static List<Ad> Filter(this List<Ad> query, AdFilterDto filters) =>
query.FilterByExperience(filters.MinExperience, filters.MaxExperience).FilterByWorkType(filters.WorkHour).FilterByEmploymentType(filters.EmploymentType).ToList().FilterByTechnologies(filters.Technologies);

public static List<Ad> FilterByExperience(this List<Ad> query, int minExperience, int maxExperience) =>
minExperience >= maxExperience ? query.Where(x => x.MinimumExperience >= minExperience && x.MinimumExperience <= maxExperience).ToList()
: query.Where(x => x.MinimumExperience >= minExperience && x.MinimumExperience <= maxExperience).ToList();
public static List<Ad> FilterByWorkType(this List<Ad> query, string workHour) =>
workHour.ToLower() == "parttime" ? query.Where(x => x.WorkHour == WorkHours.PartTime).ToList() : query.Where(x => x.WorkHour == WorkHours.FullTime).ToList();
public static List<Ad> FilterByEmploymentType(this List<Ad> query, string employmentType) =>
employmentType.ToLower() == "intership" ? query.Where(x => x.EmploymentType == EmploymentTypes.Intership).ToList() : query.Where(x => x.EmploymentType == EmploymentTypes.Work).ToList();

public static List<Ad> FilterByTechnologies(this List<Ad> query, string[] technologies)
{
if (technologies.Length == 0)
if (technologies == null || technologies.Length == 0)
{
return query;
}

List<Ad> filteredAds = new List<Ad>();

for (int i = 0; i < query.Count(); i++)
{

+ 2
- 8
Diligent.WebAPI.Business/Services/AdService.cs 파일 보기

@@ -52,15 +52,9 @@ namespace Diligent.WebAPI.Business.Services

public async Task<List<AdResponseDto>> GetFilteredAdsAsync(AdFilterDto filters)
{
var filteredAds = await _context.Ads.Include(x => x.Technologies)
.FilterByExperience(filters.MinExperience, filters.MaxExperience)
.FilterByWorkType(filters.WorkHour)
.FilterByEmploymentType(filters.EmploymentType)
.ToListAsync();
var filteredAds = await _context.Ads.Include(x => x.Technologies).ToListAsync();

filteredAds = filteredAds.FilterByTechnologies(filters.Technologies);

return _mapper.Map<List<AdResponseDto>>(filteredAds);
return _mapper.Map<List<AdResponseDto>>(filteredAds.Filter(filters));
}

public async Task CreateAsync(AdCreateDto adCreateDto)

+ 6
- 2
Diligent.WebAPI.Contracts/DTOs/Ad/AdUpdateDto.cs 파일 보기

@@ -16,10 +16,14 @@ namespace Diligent.WebAPI.Contracts.DTOs.Ad

public DateTime ExpiredAt { get; set; }

public string MainLiabilities { get; set; }
public string KeyResponsibilities { get; set; }

public string Conditions { get; set; }
public string Requirements { get; set; }

public string Offer { get; set; }

public string WorkHour { get; set; }

public string EmploymentType { get; set; }
}
}

+ 1
- 1
Diligent.WebAPI.Data/Entities/Technology.cs 파일 보기

@@ -7,7 +7,7 @@
public int TechnologyId { get; set; }
public string Name { get; set; }
public TechnologyTypes TechnologyType { get; set; }
public List<TechnologyApplicant> TechnologyApplicants { get; set; }
public List<TechnologyApplicant> TechnologyApplicants { get; set; } = new();
public List<Ad> Ads { get; set; } = new();

}

+ 1
- 1
Diligent.WebAPI.Host/Controllers/V1/AdsController.cs 파일 보기

@@ -48,7 +48,7 @@ namespace Diligent.WebAPI.Host.Controllers.V1
}

[HttpDelete("{id}")]
public async Task<IActionResult> DeleteInsurer([FromRoute]int id)
public async Task<IActionResult> DeleteAd([FromRoute]int id)
{
await _adService.DeleteAsync(id);
return NoContent();

+ 1
- 1
Diligent.WebAPI.Host/appsettings.Development.json 파일 보기

@@ -1,6 +1,6 @@
{
"ConnectionStrings": {
"WebApi": "Server=192.168.88.105;Database=HRCenter;User Id=hrcentar;Password="
"WebApi": "Server=192.168.88.105;Database=HRCenter;User Id=hrcentar;Password=administrator#2021;"
},
"Authorization": {
"JwtExpiredTime": "5",

+ 1
- 1
Diligent.WebAPI.Host/appsettings.json 파일 보기

@@ -1,6 +1,6 @@
{
"ConnectionStrings": {
"WebApi": "Server=192.168.88.105;Database=HRCenter;User Id=hrcentar;Password="
"WebApi": "Server=192.168.88.105;Database=HRCenter;User Id=hrcentar;Password=administrator#2021;"
},
"Authorization": {
"JwtExpiredTime": "5",

+ 223
- 0
Diligent.WebAPI.Tests/Controllers/AdsControllerTests.cs 파일 보기

@@ -0,0 +1,223 @@
using Diligent.WebAPI.Contracts.DTOs.Ad;
using Diligent.WebAPI.Contracts.Exceptions;
using Diligent.WebAPI.Data.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Diligent.WebAPI.Tests.Controllers
{
public class AdsControllerTests
{
private IAdService _adService = Substitute.For<IAdService>();
private List<AdResponseDto> _ads = new List<AdResponseDto>();
private AdResponseDto _ad;

public AdsControllerTests()
{
_ad = new AdResponseDto
{
Id = 1,
Title = "React Developer",
MinimumExperience = 0,
CreatedAt = DateTime.UtcNow,
ExpiredAt = DateTime.UtcNow.AddDays(30),
KeyResponsibilities = "KR|KR",
Requirements = "R|R|R",
Offer = "O|O",
WorkHour = nameof(WorkHours.FullTime),
EmploymentType = nameof(EmploymentTypes.Intership)
};

_ads.Add(_ad);
}

[Fact]
public async Task GetAll_ShouldReturn200OK_WhenCalled()
{
_adService.GetAllAsync().Returns(_ads);
AdsController adsController = new(_adService);
var result = await adsController.GetAll();
(result as OkObjectResult).StatusCode.Should().Be(200);
}

[Fact]
public async Task GetById_ShouldReturn200OK_WhenAdExists()
{
_adService.GetByIdAsync(Arg.Any<int>()).Returns(_ad);

AdsController adsController = new(_adService);

var result = await adsController.GetById(1);

(result as OkObjectResult).StatusCode.Should().Be(200);
}

[Fact]
public async Task GetById_ShouldThrowEntityNotFoundException_WhenAdDontExist()
{
_adService.When(x => x.GetByIdAsync(Arg.Any<int>())).Do(x => { throw new EntityNotFoundException(); });

AdsController adsController = new(_adService);

await Assert.ThrowsAsync<EntityNotFoundException>(() => adsController.GetById(1000));
}

[Fact]
public async Task GetAdDetailsById_ShouldReturn200OK_WhenAdExists()
{
_adService.GetAdDetailsByIdAsync(Arg.Any<int>()).Returns(new AdDetailsResponseDto
{
Id = 1,
Title = "React Developer",
MinimumExperience = 0,
CreatedAt = DateTime.UtcNow,
ExpiredAt = DateTime.UtcNow.AddDays(30),
KeyResponsibilities = "KR|KR",
Requirements = "R|R|R",
Offer = "O|O"
});

AdsController adsController = new(_adService);

var result = await adsController.GetAdDetailsById(1);

(result as OkObjectResult).StatusCode.Should().Be(200);
}

[Fact]
public async Task GetAdDetailsById_ShouldThrowEntityNotFoundException_WhenAdDontExist()
{
_adService.When(x => x.GetAdDetailsByIdAsync(Arg.Any<int>())).Do(x => { throw new EntityNotFoundException(); });

AdsController adsController = new(_adService);

await Assert.ThrowsAsync<EntityNotFoundException>(() => adsController.GetAdDetailsById(1000));
}

[Fact]
public async Task GetArchiveAds_ShouldReturn200OK_WhenCalled()
{
_adService.GetArchiveAds().Returns(_ads);

AdsController adsController = new(_adService);

var result = await adsController.GetArchiveAds();

(result as OkObjectResult).StatusCode.Should().Be(200);
}

[Fact]
public async Task GetFilteredAds_ShouldReturn200OK_WhenCalled()
{
var filters = new AdFilterDto
{
MinExperience = 0,
MaxExperience = 10,
EmploymentType = "Work",
WorkHour = "FullTime",
Technologies = new string[] { ".NET" }
};

_adService.GetFilteredAdsAsync(filters).Returns(_ads);

AdsController adsController = new(_adService);

var result = await adsController.GetFilteredAds(filters);

(result as OkObjectResult).StatusCode.Should().Be(200);
}

[Fact]
public async Task CreateAd_ShouldReturn201Created_WhenAdCreated()
{
var adCreateDto = new AdCreateDto
{
Title = "React Developer",
MinimumExperience = 0,
CreatedAt = DateTime.UtcNow,
ExpiredAt = DateTime.UtcNow.AddDays(30),
KeyResponsibilities = "KR|KR",
Requirements = "R|R|R",
Offer = "O|O"
};

_adService.CreateAsync(adCreateDto);

AdsController adsController = new(_adService);

var result = await adsController.Create(adCreateDto);

(result as StatusCodeResult).StatusCode.Should().Be(201);
}

[Fact]
public async Task UpdateAd_ShouldReturn200OK_WhenAdUpdated()
{
var adUpdateDto = new AdUpdateDto
{
Title = "React Developer",
MinimumExperience = 0,
CreatedAt = DateTime.UtcNow,
ExpiredAt = DateTime.UtcNow.AddDays(30),
KeyResponsibilities = "KR|KR",
Requirements = "R|R|R",
Offer = "O|O"
};

_adService.UpdateAsync(1, adUpdateDto);

AdsController adsController = new(_adService);

var result = await adsController.Update(adUpdateDto, 1);

(result as StatusCodeResult).StatusCode.Should().Be(200);
}

[Fact]
public async Task UpdateAd_ShouldThrowEntityNotFoundException_WhenAdDontExist()
{
var adUpdateDto = new AdUpdateDto
{
Title = "React Developer",
MinimumExperience = 0,
CreatedAt = DateTime.UtcNow,
ExpiredAt = DateTime.UtcNow.AddDays(30),
KeyResponsibilities = "KR|KR",
Requirements = "R|R|R",
Offer = "O|O"
};

_adService.When(x => x.UpdateAsync(Arg.Any<int>(), adUpdateDto)).Do(x => { throw new EntityNotFoundException(); });

AdsController adsController = new(_adService);

await Assert.ThrowsAsync<EntityNotFoundException>(() => adsController.Update(adUpdateDto, 1000));
}


[Fact]
public async Task DeleteAd_ShouldReturn204NoContent_WhenAdDeleted()
{
_adService.DeleteAsync(1);

AdsController adsController = new(_adService);

var result = await adsController.DeleteAd(1);

(result as StatusCodeResult).StatusCode.Should().Be(204);
}

[Fact]
public async Task DeleteAd_ShouldThrowEntityNotFoundException_WhenAdDontExist()
{
_adService.When(x => x.DeleteAsync(Arg.Any<int>())).Do(x => { throw new EntityNotFoundException(); });

AdsController adsController = new(_adService);

await Assert.ThrowsAsync<EntityNotFoundException>(() => adsController.DeleteAd(1000));
}
}
}

+ 263
- 0
Diligent.WebAPI.Tests/Services/AdServiceTests.cs 파일 보기

@@ -0,0 +1,263 @@
using AutoMapper;
using Diligent.WebAPI.Business.MappingProfiles;
using Diligent.WebAPI.Business.Services;
using Diligent.WebAPI.Contracts.DTOs.Ad;
using Diligent.WebAPI.Contracts.Exceptions;
using Diligent.WebAPI.Data.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Diligent.WebAPI.Tests.Services
{
public class AdServiceTests
{
private readonly IMapper _mapper;
private readonly List<Ad> _ads = new List<Ad>();
private readonly Ad _ad;
private ITechnologyService _technologyService = Substitute.For<ITechnologyService>();
private readonly List<Technology> _technologies = new List<Technology>();

public AdServiceTests()
{
var configuration = new MapperConfiguration(cfg => cfg.AddProfiles(
new List<Profile>
{
new AdMappingProfile(),
new TechnologyMappingProfile()
}));
_mapper = new Mapper(configuration);

_ad = new Ad
{

Id = 1,
Title = "React Developer",
MinimumExperience = 0,
CreatedAt = DateTime.UtcNow,
ExpiredAt = DateTime.UtcNow.AddDays(30),
KeyResponsibilities = "KR|KR",
Requirements = "R|R|R",
Offer = "O|O",
WorkHour = WorkHours.FullTime,
EmploymentType = EmploymentTypes.Intership,
Technologies = new List<Technology>
{
new Technology { TechnologyId = 1, Name = "React", TechnologyType = TechnologyTypes.Backend}
}
};

_ads.Add(_ad);
_ads.Add(new Ad
{
Id = 2,
Title = ".NET Developer",
MinimumExperience = 0,
CreatedAt = DateTime.UtcNow.AddDays(-2),
ExpiredAt = DateTime.UtcNow.AddDays(-1),
KeyResponsibilities = "KR|KR",
Requirements = "R|R|R",
Offer = "O|O",
WorkHour = WorkHours.FullTime,
EmploymentType = EmploymentTypes.Intership,
Technologies = new List<Technology>
{
new Technology { TechnologyId = 2, Name = ".NET", TechnologyType = TechnologyTypes.Frontend}
}
});

_technologies.Add(new Technology
{
TechnologyId = 1,
Name = ".NET"
});
}

[Fact]
public async Task GetAll_ShouldReturnListOfAds_WhenCalled()
{
var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
AdService adService = new(databaseContext, _mapper, _technologyService);

var result = await adService.GetAllAsync();

//result.Should().BeEquivalentTo(_mapper.Map<List<AdResponseDto>>(_ads));
Assert.Equal(result.Count, 1);
}

[Fact]
public async Task GetById_ShouldReturnAd_WhenAdExists()
{
var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
AdService adService = new(databaseContext, _mapper, _technologyService);

var result = await adService.GetByIdAsync(1);

result.Should().BeEquivalentTo(_mapper.Map<AdResponseDto>(_ad));
}

[Fact]
public async Task GetById_ShouldThrowEntityNotFoundException_WhenAdDontExists()
{
var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
AdService adService = new(databaseContext, _mapper, _technologyService);

await Assert.ThrowsAsync<EntityNotFoundException>(async () => await adService.GetByIdAsync(1000));
}

[Fact]
public async Task GetAdDetailsById_ShouldReturnAd_WhenAdExists()
{
var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
AdService adService = new(databaseContext, _mapper, _technologyService);

var result = await adService.GetAdDetailsByIdAsync(1);

result.Should().BeEquivalentTo(_mapper.Map<AdDetailsResponseDto>(_ad));
}

[Fact]
public async Task GetAdDetailsById_ShouldThrowEntityNotFoundException_WhenAdDontExists()
{
var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
AdService adService = new(databaseContext, _mapper, _technologyService);

await Assert.ThrowsAsync<EntityNotFoundException>(async () => await adService.GetAdDetailsByIdAsync(1000));
}

[Fact]
public async Task GetArchiveAds_ShouldReturnListOfAds_WhenCalled()
{
var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
AdService adService = new(databaseContext, _mapper, _technologyService);

var result = await adService.GetArchiveAds();

//result.Should().BeEquivalentTo(_mapper.Map<List<AdResponseDto>>(_ads));
Assert.Equal(result.Count, 1);
}

[Fact]
public async Task GetFilteredAds_ShouldReturnListOfAds_WhenCalled()
{
var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
AdService adService = new(databaseContext, _mapper, _technologyService);

var result = await adService.GetFilteredAdsAsync(new AdFilterDto
{
MinExperience = 0,
MaxExperience = 10,
EmploymentType = "Intership",
WorkHour = "FullTime",
Technologies = new string[]
{
".NET"
}
});

Assert.Equal(result.Count, 1);
}

[Fact]
public async Task CreateAd_ShouldAddAdIntoDatabase_WhenCalled()
{
var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
AdService adService = new(databaseContext, _mapper, _technologyService);
_technologyService.GetEntityByIdAsync(Arg.Any<int>()).Returns(new Technology
{
TechnologyId = 3,
Name = "Vue"
});

AdCreateDto adCreateDto = new AdCreateDto
{
Title = "Vue Developer",
MinimumExperience = 0,
CreatedAt = DateTime.UtcNow,
ExpiredAt = DateTime.UtcNow.AddDays(30),
KeyResponsibilities = "KR|KR",
Requirements = "R|R|R",
Offer = "O|O",
WorkHour = "FullTime",
EmploymentType = "Intership",
TechnologiesIds = new List<int> { 3 }
};

await adService.CreateAsync(adCreateDto);

var ads = await adService.GetAllAsync();

Assert.Equal(2, ads.Count);
}

[Fact]
public async Task UpdateAd_ShouldUpdateAd_WhenAdExists()
{
var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
AdService adService = new(databaseContext, _mapper, _technologyService);

var updateForAd = new AdUpdateDto
{
Title = "Vue Developer",
MinimumExperience = 0,
CreatedAt = DateTime.UtcNow,
ExpiredAt = DateTime.UtcNow.AddDays(30),
KeyResponsibilities = "KR|KR",
Requirements = "R|R|R",
Offer = "O|O",
WorkHour = "FullTime",
EmploymentType = "Intership",
};

await adService.UpdateAsync(1, updateForAd);

var ads = await adService.GetAllAsync();

Assert.Equal(ads[0].Title, "Vue Developer");
}

[Fact]
public async Task UpdateAd_ShouldThrowEntityNotFoundException_WhenAdDontExists()
{
var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
AdService adService = new(databaseContext, _mapper, _technologyService);

await Assert.ThrowsAsync<EntityNotFoundException>(async () => await adService.UpdateAsync(1000, new AdUpdateDto
{
Title = "Vue Developer",
MinimumExperience = 0,
CreatedAt = DateTime.UtcNow,
ExpiredAt = DateTime.UtcNow.AddDays(30),
KeyResponsibilities = "KR|KR",
Requirements = "R|R|R",
Offer = "O|O",
WorkHour = "FullTime",
EmploymentType = "Intership",
}));
}

[Fact]
public async Task DeleteAd_ShouldDeleteAdFromDatabase_WhenAdExists()
{
var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
AdService adService = new(databaseContext, _mapper, _technologyService);

await adService.DeleteAsync(1);

var ads = await adService.GetAllAsync();

Assert.Equal(0, ads.Count);
}

[Fact]
public async Task DeleteAd_ShouldThrowEntityNotFoundException_WhenAdDontExists()
{
var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
AdService adService = new(databaseContext, _mapper, _technologyService);

await Assert.ThrowsAsync<EntityNotFoundException>(async () => await adService.DeleteAsync(1000));
}
}
}

Loading…
취소
저장