Test project for location service updated
This commit is contained in:
parent
96ccb96bf1
commit
cd261a5556
@ -13,131 +13,144 @@ namespace DamageAssesment.Api.Locations.Test
|
|||||||
{
|
{
|
||||||
public class LocationsServiceTest
|
public class LocationsServiceTest
|
||||||
{
|
{
|
||||||
|
//Test for locations
|
||||||
[Fact(DisplayName = "Get Location using Location ID")]
|
[Fact(DisplayName = "Get Locations - Ok case")]
|
||||||
public async Task GetLocationsUsingLocationID()
|
public async Task GetLocationsAsync_ShouldReturnStatusCode200()
|
||||||
{
|
{
|
||||||
var options = new DbContextOptionsBuilder<LocationDbContext>()
|
var mockLocationService = new Mock<ILocationsProvider>();
|
||||||
.UseInMemoryDatabase(nameof(GetLocationsUsingLocationID))
|
var mockResponse = await MockData.getOkResponseLocation();
|
||||||
.Options;
|
mockLocationService.Setup(service => service.GetLocationsAsync()).ReturnsAsync(mockResponse);
|
||||||
|
|
||||||
var dbContext = new LocationDbContext(options);
|
var locationProvider = new LocationsController(mockLocationService.Object);
|
||||||
CreateLocations(dbContext);
|
var result = (OkObjectResult)await locationProvider.GetLocationsAsync();
|
||||||
//Mapping
|
|
||||||
var LocationsProfile = new LocationProfile();
|
|
||||||
var configuration = new MapperConfiguration(cfg => cfg.AddProfile(LocationsProfile));
|
|
||||||
var mapper = new Mapper(configuration);
|
|
||||||
|
|
||||||
var LocationsProvider = new LocationsProvider(dbContext, null, mapper);
|
Assert.Equal(200, result.StatusCode);
|
||||||
//Testmethode
|
|
||||||
var Location = await LocationsProvider.GetLocationByIdAsync("Loc3");
|
|
||||||
|
|
||||||
Assert.True(Location.IsSuccess);
|
|
||||||
Assert.Null(Location.ErrorMessage);
|
|
||||||
}
|
|
||||||
[Fact(DisplayName = "Get Locations")]
|
|
||||||
public async Task GetAllLocationsTest()
|
|
||||||
{
|
|
||||||
var options = new DbContextOptionsBuilder<LocationDbContext>()
|
|
||||||
.UseInMemoryDatabase(nameof(GetLocationsUsingLocationID))
|
|
||||||
.Options;
|
|
||||||
|
|
||||||
var dbContext = new LocationDbContext(options);
|
|
||||||
CreateLocations(dbContext);
|
|
||||||
//Mapping
|
|
||||||
var LocationsProfile = new LocationProfile();
|
|
||||||
var configuration = new MapperConfiguration(cfg => cfg.AddProfile(LocationsProfile));
|
|
||||||
var mapper = new Mapper(configuration);
|
|
||||||
|
|
||||||
var LocationsProvider = new LocationsProvider(dbContext, null, mapper);
|
|
||||||
//Testmethode
|
|
||||||
var Location = await LocationsProvider.GetLocationsAsync();
|
|
||||||
|
|
||||||
Assert.True(Location.IsSuccess);
|
|
||||||
Assert.Null(Location.ErrorMessage);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact(DisplayName = "Delete Location by Id")]
|
|
||||||
public async Task DeleteLocationTest()
|
[Fact(DisplayName = "Get Locations - NoContent Case")]
|
||||||
|
public async Task GetLocationsAsync_ShouldReturnStatusCode204()
|
||||||
{
|
{
|
||||||
var options = new DbContextOptionsBuilder<LocationDbContext>()
|
var mockLocationService = new Mock<ILocationsProvider>();
|
||||||
.UseInMemoryDatabase(nameof(GetLocationsUsingLocationID))
|
var mockResponse = await MockData.getNotFoundResponseLocation();
|
||||||
.Options;
|
mockLocationService.Setup(service => service.GetLocationsAsync()).ReturnsAsync(mockResponse);
|
||||||
|
|
||||||
var dbContext = new LocationDbContext(options);
|
var locationProvider = new LocationsController(mockLocationService.Object);
|
||||||
CreateLocations(dbContext);
|
var result = (NoContentResult)await locationProvider.GetLocationsAsync();
|
||||||
//Mapping
|
|
||||||
var LocationsProfile = new LocationProfile();
|
|
||||||
var configuration = new MapperConfiguration(cfg => cfg.AddProfile(LocationsProfile));
|
|
||||||
var mapper = new Mapper(configuration);
|
|
||||||
|
|
||||||
var LocationsProvider = new LocationsProvider(dbContext, null, mapper);
|
Assert.Equal(204, result.StatusCode);
|
||||||
//Testmethode
|
|
||||||
var Location = await LocationsProvider.DeleteLocationAsync("Loc2");
|
|
||||||
|
|
||||||
Assert.True(Location.IsSuccess);
|
|
||||||
Assert.NotNull(Location.ErrorMessage);
|
|
||||||
}
|
}
|
||||||
[Fact(DisplayName = "Add Location")]
|
|
||||||
public async Task AddLocationTest()
|
|
||||||
{
|
|
||||||
var options = new DbContextOptionsBuilder<LocationDbContext>()
|
|
||||||
.UseInMemoryDatabase(nameof(GetLocationsUsingLocationID))
|
|
||||||
.Options;
|
|
||||||
|
|
||||||
var dbContext = new LocationDbContext(options);
|
[Fact(DisplayName = "Get Locations by Id- Ok case")]
|
||||||
CreateLocations(dbContext);
|
public async Task GetLocationsByIdAsync_ShouldReturnStatusCode200()
|
||||||
//Mapping
|
{
|
||||||
var LocationsProfile = new LocationProfile();
|
var mockLocationService = new Mock<ILocationsProvider>();
|
||||||
var configuration = new MapperConfiguration(cfg => cfg.AddProfile(LocationsProfile));
|
var mockResponse = await MockData.getOkResponseLocation("Loc1");
|
||||||
var mapper = new Mapper(configuration);
|
mockLocationService.Setup(service => service.GetLocationByIdAsync("Loc1")).ReturnsAsync(mockResponse);
|
||||||
var LocationsProvider = new LocationsProvider(dbContext, null, mapper);
|
|
||||||
//Testmethode
|
|
||||||
Models.Location newLocation = new Models.Location() { Id = "Loc9", RegionId = "1", Name = "Test 1", MaintenanceCenter = "1", SchoolType = "US" };
|
|
||||||
var Location = await LocationsProvider.PostLocationAsync(newLocation);
|
|
||||||
|
|
||||||
Assert.True(Location.IsSuccess);
|
var locationProvider = new LocationsController(mockLocationService.Object);
|
||||||
Assert.Null(Location.ErrorMessage);
|
var result = (OkObjectResult)await locationProvider.GetLocationByIdAsync("Loc1");
|
||||||
}
|
|
||||||
[Fact(DisplayName = "Update Location")]
|
|
||||||
public async Task UpdateLocationTest()
|
|
||||||
{
|
|
||||||
var options = new DbContextOptionsBuilder<LocationDbContext>()
|
|
||||||
.UseInMemoryDatabase(nameof(GetLocationsUsingLocationID))
|
|
||||||
.Options;
|
|
||||||
var dbContext = new LocationDbContext(options);
|
|
||||||
CreateLocations(dbContext);
|
|
||||||
//Mapping
|
|
||||||
var LocationsProfile = new LocationProfile();
|
|
||||||
var configuration = new MapperConfiguration(cfg => cfg.AddProfile(LocationsProfile));
|
|
||||||
var mapper = new Mapper(configuration);
|
|
||||||
var LocationsProvider = new LocationsProvider(dbContext, null, mapper);
|
|
||||||
//Testmethode
|
|
||||||
Models.Location updateLocation = new Models.Location() { Id = "Loc1", RegionId = "1", Name = "Tampa", MaintenanceCenter = "1", SchoolType = "NA" };
|
|
||||||
var Location = await LocationsProvider.UpdateLocationAsync(updateLocation);
|
|
||||||
var modified = dbContext.Locations.FirstOrDefault(a => a.Id == updateLocation.Id);
|
|
||||||
Assert.True(Location.IsSuccess);
|
|
||||||
Assert.NotNull(Location.ErrorMessage);
|
|
||||||
}
|
|
||||||
private static void CreateLocations(LocationDbContext dbContext)
|
|
||||||
{
|
|
||||||
//Create sample data for testing
|
|
||||||
if (dbContext.Locations.Count() == 0)
|
|
||||||
{
|
|
||||||
for (int i = 1; i < 6; i++)
|
|
||||||
{
|
|
||||||
dbContext.Locations.Add(new Db.Location()
|
|
||||||
{
|
|
||||||
Id = "Loc"+i.ToString(),
|
|
||||||
RegionId = i.ToString(),
|
|
||||||
Name = "Test Location" + Guid.NewGuid().ToString(),
|
|
||||||
MaintenanceCenter = i.ToString(),
|
|
||||||
SchoolType = "US"
|
|
||||||
|
|
||||||
});
|
Assert.Equal(200, result.StatusCode);
|
||||||
}
|
}
|
||||||
dbContext.SaveChanges();
|
|
||||||
|
|
||||||
|
[Fact(DisplayName = "Get Locations By Id - NoFound Case")]
|
||||||
|
public async Task GetLocationsByIdAsync_ShouldReturnStatusCode404()
|
||||||
|
{
|
||||||
|
var mockLocationService = new Mock<ILocationsProvider>();
|
||||||
|
var mockResponse = await MockData.getLocationNotFoundResponse();
|
||||||
|
mockLocationService.Setup(service => service.GetLocationByIdAsync("Loc1")).ReturnsAsync(mockResponse);
|
||||||
|
|
||||||
|
var locationProvider = new LocationsController(mockLocationService.Object);
|
||||||
|
var result = (NotFoundResult)await locationProvider.GetLocationByIdAsync("Loc1");
|
||||||
|
|
||||||
|
Assert.Equal(404, result.StatusCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Fact(DisplayName = "Post Location - Ok case")]
|
||||||
|
public async Task PostLocationAsync_ShouldReturnStatusCode200()
|
||||||
|
{
|
||||||
|
var mockLocationService = new Mock<ILocationsProvider>();
|
||||||
|
var mockResponse = await MockData.getOkResponseLocation("Loc1");
|
||||||
|
var mockInputLocation = new Models.Location { Id = "Loc1", RegionId = "R1", Name = "Location 1", SchoolType = "US", MaintenanceCenter = "1" };
|
||||||
|
mockLocationService.Setup(service => service.PostLocationAsync(mockInputLocation)).ReturnsAsync(mockResponse);
|
||||||
|
|
||||||
|
var locationProvider = new LocationsController(mockLocationService.Object);
|
||||||
|
var result = (OkObjectResult)await locationProvider.CreateLocation(mockInputLocation);
|
||||||
|
|
||||||
|
Assert.Equal(200, result.StatusCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact(DisplayName = "Post Location - BadRequest case")]
|
||||||
|
public async Task PostLocationAsync_ShouldReturnStatusCode400()
|
||||||
|
{
|
||||||
|
var mockLocationService = new Mock<ILocationsProvider>();
|
||||||
|
var mockResponse = await MockData.getLocationNotFoundResponse();
|
||||||
|
var mockInputLocation = new Models.Location { Id = "Loc1", RegionId = "R1", Name = "Location 1", SchoolType = "US", MaintenanceCenter = "1" };
|
||||||
|
mockLocationService.Setup(service => service.PostLocationAsync(mockInputLocation)).ReturnsAsync(mockResponse);
|
||||||
|
|
||||||
|
var locationProvider = new LocationsController(mockLocationService.Object);
|
||||||
|
var result = (BadRequestResult)await locationProvider.CreateLocation(mockInputLocation);
|
||||||
|
|
||||||
|
Assert.Equal(400, result.StatusCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Fact(DisplayName = "Put Location - Ok case")]
|
||||||
|
public async Task PutLocationAsync_ShouldReturnStatusCode200()
|
||||||
|
{
|
||||||
|
var mockLocationService = new Mock<ILocationsProvider>();
|
||||||
|
var mockResponse = await MockData.getLocation(true, "update success");
|
||||||
|
var mockInputLocation = new Models.Location { Id = "Loc1", RegionId = "R1", Name = "Location 1", SchoolType = "US", MaintenanceCenter = "1" };
|
||||||
|
mockLocationService.Setup(service => service.UpdateLocationAsync(mockInputLocation)).ReturnsAsync(mockResponse);
|
||||||
|
|
||||||
|
var locationProvider = new LocationsController(mockLocationService.Object);
|
||||||
|
var result = (OkObjectResult)await locationProvider.UpdateLocation(mockInputLocation);
|
||||||
|
|
||||||
|
Assert.Equal(200, result.StatusCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact(DisplayName = "Put Location - NotFound case")]
|
||||||
|
public async Task PutLocationAsync_ShouldReturnStatusCode404()
|
||||||
|
{
|
||||||
|
var mockLocationService = new Mock<ILocationsProvider>();
|
||||||
|
var mockResponse = await MockData.getLocation(false, null);
|
||||||
|
var mockInputLocation = new Models.Location { Id = "Loc1", RegionId = "R1", Name = "Location 1", SchoolType = "US", MaintenanceCenter = "1" };
|
||||||
|
mockLocationService.Setup(service => service.UpdateLocationAsync(mockInputLocation)).ReturnsAsync(mockResponse);
|
||||||
|
|
||||||
|
var locationProvider = new LocationsController(mockLocationService.Object);
|
||||||
|
var result = (NotFoundResult)await locationProvider.UpdateLocation(mockInputLocation);
|
||||||
|
|
||||||
|
Assert.Equal(404, result.StatusCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Fact(DisplayName = "Delete Location - Ok case")]
|
||||||
|
public async Task DeleteLocationAsync_ShouldReturnStatusCode200()
|
||||||
|
{
|
||||||
|
var mockLocationService = new Mock<ILocationsProvider>();
|
||||||
|
var mockResponse = await MockData.getLocation(true, "delete success");
|
||||||
|
mockLocationService.Setup(service => service.DeleteLocationAsync("Loc1")).ReturnsAsync(mockResponse);
|
||||||
|
|
||||||
|
var locationProvider = new LocationsController(mockLocationService.Object);
|
||||||
|
var result = (OkObjectResult)await locationProvider.DeleteLocation("Loc1");
|
||||||
|
|
||||||
|
Assert.Equal(200, result.StatusCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact(DisplayName = "Delete Location - NotFound case")]
|
||||||
|
public async Task DeleteLocationAsync_ShouldReturnStatusCode404()
|
||||||
|
{
|
||||||
|
var mockLocationService = new Mock<ILocationsProvider>();
|
||||||
|
var mockResponse = await MockData.getLocation(false, null);
|
||||||
|
mockLocationService.Setup(service => service.DeleteLocationAsync("Loc1")).ReturnsAsync(mockResponse);
|
||||||
|
|
||||||
|
var locationProvider = new LocationsController(mockLocationService.Object);
|
||||||
|
var result = (NotFoundResult)await locationProvider.DeleteLocation("Loc1");
|
||||||
|
|
||||||
|
Assert.Equal(404, result.StatusCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Tests for regions
|
//Tests for regions
|
||||||
@ -194,7 +207,7 @@ namespace DamageAssesment.Api.Locations.Test
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact(DisplayName = "Post Region - Ok case")]
|
[Fact(DisplayName = "Post Region - Ok case")]
|
||||||
public async Task PostSurveyAsync_ShouldReturnStatusCode200()
|
public async Task PostRegionAsync_ShouldReturnStatusCode200()
|
||||||
{
|
{
|
||||||
var mockRegionService = new Mock<IRegionsProvider>();
|
var mockRegionService = new Mock<IRegionsProvider>();
|
||||||
var mockResponse = await MockData.getOkResponse("1");
|
var mockResponse = await MockData.getOkResponse("1");
|
||||||
@ -208,7 +221,7 @@ namespace DamageAssesment.Api.Locations.Test
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact(DisplayName = "Post Region - BadRequest case")]
|
[Fact(DisplayName = "Post Region - BadRequest case")]
|
||||||
public async Task PostSurveyAsync_ShouldReturnStatusCode400()
|
public async Task PostRegionAsync_ShouldReturnStatusCode400()
|
||||||
{
|
{
|
||||||
var mockRegionService = new Mock<IRegionsProvider>();
|
var mockRegionService = new Mock<IRegionsProvider>();
|
||||||
var mockResponse = await MockData.getBadRequestResponse();
|
var mockResponse = await MockData.getBadRequestResponse();
|
||||||
@ -236,7 +249,7 @@ namespace DamageAssesment.Api.Locations.Test
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact(DisplayName = "Put Region - NotFound case")]
|
[Fact(DisplayName = "Put Region - NotFound case")]
|
||||||
public async Task PutSurveyAsync_ShouldReturnStatusCode404()
|
public async Task PutRegionAsync_ShouldReturnStatusCode404()
|
||||||
{
|
{
|
||||||
var mockRegionService = new Mock<IRegionsProvider>();
|
var mockRegionService = new Mock<IRegionsProvider>();
|
||||||
var mockResponse = await MockData.getNotFoundResponse();
|
var mockResponse = await MockData.getNotFoundResponse();
|
||||||
@ -250,7 +263,7 @@ namespace DamageAssesment.Api.Locations.Test
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact(DisplayName = "Put Region - BadRequest case")]
|
[Fact(DisplayName = "Put Region - BadRequest case")]
|
||||||
public async Task PutSurveyAsync_ShouldReturnStatusCode400()
|
public async Task PutRegionyAsync_ShouldReturnStatusCode400()
|
||||||
{
|
{
|
||||||
var mockRegionService = new Mock<IRegionsProvider>();
|
var mockRegionService = new Mock<IRegionsProvider>();
|
||||||
var mockResponse = await MockData.getBadRequestResponse();
|
var mockResponse = await MockData.getBadRequestResponse();
|
||||||
@ -264,7 +277,7 @@ namespace DamageAssesment.Api.Locations.Test
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact(DisplayName = "Delete Region - Ok case")]
|
[Fact(DisplayName = "Delete Region - Ok case")]
|
||||||
public async Task DeleteSurveyAsync_ShouldReturnStatusCode200()
|
public async Task DeleteRegionAsync_ShouldReturnStatusCode200()
|
||||||
{
|
{
|
||||||
var mockRegionService = new Mock<IRegionsProvider>();
|
var mockRegionService = new Mock<IRegionsProvider>();
|
||||||
var mockResponse = await MockData.getOkResponse("1");
|
var mockResponse = await MockData.getOkResponse("1");
|
||||||
@ -278,7 +291,7 @@ namespace DamageAssesment.Api.Locations.Test
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact(DisplayName = "Delete Region - NotFound case")]
|
[Fact(DisplayName = "Delete Region - NotFound case")]
|
||||||
public async Task DeleteSurveyAsync_ShouldReturnStatusCode404()
|
public async Task DeleteRegionAsync_ShouldReturnStatusCode404()
|
||||||
{
|
{
|
||||||
var mockRegionService = new Mock<IRegionsProvider>();
|
var mockRegionService = new Mock<IRegionsProvider>();
|
||||||
var mockResponse = await MockData.getNotFoundResponse();
|
var mockResponse = await MockData.getNotFoundResponse();
|
||||||
|
@ -17,9 +17,43 @@ namespace DamageAssesment.Api.Locations.Test
|
|||||||
|
|
||||||
public static async Task<(bool, Locations.Models.Region, string)> getOkResponse(string Id)
|
public static async Task<(bool, Locations.Models.Region, string)> getOkResponse(string Id)
|
||||||
{
|
{
|
||||||
var surveys = await getOkResponse();
|
var regions = await getOkResponse();
|
||||||
var survey = surveys.Item2.FirstOrDefault(s => s.Id == Id);
|
var region = regions.Item2.FirstOrDefault(s => s.Id == Id);
|
||||||
return (true, survey, null);
|
return (true, region, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static async Task<(bool, IEnumerable<Models.Location>, string)> getOkResponseLocation()
|
||||||
|
{
|
||||||
|
IEnumerable<Locations.Models.Location> list = new List<Models.Location>();
|
||||||
|
|
||||||
|
for (int i = 0; i < 10; i++)
|
||||||
|
{
|
||||||
|
list.Append(new Locations.Models.Location { Id = "Loc"+ i, RegionId = "R"+i, Name ="Location"});
|
||||||
|
}
|
||||||
|
return (true, list, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<(bool, IEnumerable<Models.Location>, string)> getNotFoundResponseLocation()
|
||||||
|
{
|
||||||
|
return (false, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<(bool, Models.Location, string)> getOkResponseLocation(string Id)
|
||||||
|
{
|
||||||
|
var locations = await getOkResponseLocation();
|
||||||
|
var location = locations.Item2.FirstOrDefault(s => s.Id == Id);
|
||||||
|
return (true, location, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<(bool, string)> getLocation(bool value,string message)
|
||||||
|
{
|
||||||
|
return (value,message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<(bool, Locations.Models.Location, string)> getLocationNotFoundResponse()
|
||||||
|
{
|
||||||
|
return (false, null, "Not Found");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task<(bool, Locations.Models.Region, string)> getBadRequestResponse()
|
public static async Task<(bool, Locations.Models.Region, string)> getBadRequestResponse()
|
||||||
@ -41,6 +75,5 @@ namespace DamageAssesment.Api.Locations.Test
|
|||||||
{
|
{
|
||||||
return new Locations.Models.Region { Id = "R99", Name = "Region 99", Abbreviation = "A99" };
|
return new Locations.Models.Region { Id = "R99", Name = "Region 99", Abbreviation = "A99" };
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user