forked from slavaxom9k/PIBD-23_Fomichev_V.S._MagicCarpet
342 lines
13 KiB
C#
342 lines
13 KiB
C#
using MagicCarpetContracts.BindingModels;
|
|
using MagicCarpetContracts.Enums;
|
|
using MagicCarpetContracts.ViewModels;
|
|
using MagicCarpetDatabase.Models;
|
|
using MagicCarpetTests.Infrastructure;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MagicCarpetTests.WebApiControllersTests;
|
|
|
|
[TestFixture]
|
|
internal class TourControllerTests : BaseWebApiControllerTest
|
|
{
|
|
[TearDown]
|
|
public void TearDown()
|
|
{
|
|
MagicCarpetDbContext.RemoveToursFromDatabase();
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetList_WhenHaveRecords_ShouldSuccess_Test()
|
|
{
|
|
//Arrange
|
|
var tour = MagicCarpetDbContext.InsertTourToDatabaseAndReturn(tourName: "name 1");
|
|
MagicCarpetDbContext.InsertTourToDatabaseAndReturn(tourName: "name 2");
|
|
MagicCarpetDbContext.InsertTourToDatabaseAndReturn(tourName: "name 3");
|
|
//Act
|
|
var response = await HttpClient.GetAsync("/api/tours/getrecords");
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
|
var data = await GetModelFromResponseAsync<List<TourViewModel>>(response);
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(data, Is.Not.Null);
|
|
Assert.That(data, Has.Count.EqualTo(3));
|
|
});
|
|
AssertElement(data.First(x => x.Id == tour.Id), tour);
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetList_WhenNoRecords_ShouldSuccess_Test()
|
|
{
|
|
//Act
|
|
var response = await HttpClient.GetAsync("/api/tours/getrecords");
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
|
var data = await GetModelFromResponseAsync<List<TourViewModel>>(response);
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(data, Is.Not.Null);
|
|
Assert.That(data, Has.Count.EqualTo(0));
|
|
});
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetHistoryByTourId_WhenHaveRecords_ShouldSuccess_Test()
|
|
{
|
|
//Arrange
|
|
var tour = MagicCarpetDbContext.InsertTourToDatabaseAndReturn();
|
|
MagicCarpetDbContext.InsertTourHistoryToDatabaseAndReturn(tour.Id, 20, DateTime.UtcNow.AddDays(-1));
|
|
MagicCarpetDbContext.InsertTourHistoryToDatabaseAndReturn(tour.Id, 30, DateTime.UtcNow.AddMinutes(-10));
|
|
var history = MagicCarpetDbContext.InsertTourHistoryToDatabaseAndReturn(tour.Id, 40, DateTime.UtcNow.AddDays(1));
|
|
//Act
|
|
var response = await HttpClient.GetAsync($"/api/tours/gethistory?id={tour.Id}");
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
|
var data = await GetModelFromResponseAsync<List<TourHistoryViewModel>>(response);
|
|
Assert.That(data, Is.Not.Null);
|
|
Assert.That(data, Has.Count.EqualTo(3));
|
|
AssertElement(data[0], history);
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetHistoryByTourId_WhenNoRecords_ShouldSuccess_Test()
|
|
{
|
|
//Arrange
|
|
var tour = MagicCarpetDbContext.InsertTourToDatabaseAndReturn();
|
|
MagicCarpetDbContext.InsertTourHistoryToDatabaseAndReturn(tour.Id, 20, DateTime.UtcNow.AddDays(-1));
|
|
MagicCarpetDbContext.InsertTourHistoryToDatabaseAndReturn(tour.Id, 30, DateTime.UtcNow.AddMinutes(-10));
|
|
MagicCarpetDbContext.InsertTourHistoryToDatabaseAndReturn(tour.Id, 40, DateTime.UtcNow.AddDays(1));
|
|
//Act
|
|
var response = await HttpClient.GetAsync($"/api/tours/gethistory?id={Guid.NewGuid()}");
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
|
var data = await GetModelFromResponseAsync<List<TourViewModel>>(response);
|
|
Assert.That(data, Is.Not.Null);
|
|
Assert.That(data, Has.Count.EqualTo(0));
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetHistoryByTourId_WhenTourIdIsNotGuid_ShouldBadRequest_Test()
|
|
{
|
|
//Act
|
|
var response = await HttpClient.GetAsync($"/api/tours/gethistory?id=id");
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetElement_ById_WhenHaveRecord_ShouldSuccess_Test()
|
|
{
|
|
//Arrange
|
|
var tour = MagicCarpetDbContext.InsertTourToDatabaseAndReturn();
|
|
//Act
|
|
var response = await HttpClient.GetAsync($"/api/tours/getrecord/{tour.Id}");
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
|
AssertElement(await GetModelFromResponseAsync<TourViewModel>(response), tour);
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetElement_ById_WhenNoRecord_ShouldNotFound_Test()
|
|
{
|
|
//Arrange
|
|
MagicCarpetDbContext.InsertTourToDatabaseAndReturn();
|
|
//Act
|
|
var response = await HttpClient.GetAsync($"/api/tours/getrecord/{Guid.NewGuid()}");
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetElement_ByName_WhenHaveRecord_ShouldSuccess_Test()
|
|
{
|
|
//Arrange
|
|
var tour = MagicCarpetDbContext.InsertTourToDatabaseAndReturn();
|
|
//Act
|
|
var response = await HttpClient.GetAsync($"/api/tours/getrecord/{tour.TourName}");
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
|
AssertElement(await GetModelFromResponseAsync<TourViewModel>(response), tour);
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetElement_ByName_WhenNoRecord_ShouldNotFound_Test()
|
|
{
|
|
//Arrange
|
|
MagicCarpetDbContext.InsertTourToDatabaseAndReturn();
|
|
//Act
|
|
var response = await HttpClient.GetAsync($"/api/tours/getrecord/New%20Name");
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
|
}
|
|
|
|
|
|
[Test]
|
|
public async Task Post_ShouldSuccess_Test()
|
|
{
|
|
//Arrange
|
|
MagicCarpetDbContext.InsertTourToDatabaseAndReturn();
|
|
var tourModel = CreateModel();
|
|
//Act
|
|
var response = await HttpClient.PostAsync($"/api/tours/register", MakeContent(tourModel));
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
|
AssertElement(MagicCarpetDbContext.GetTourFromDatabaseById(tourModel.Id!), tourModel);
|
|
}
|
|
|
|
[Test]
|
|
public async Task Post_WhenHaveRecordWithSameId_ShouldBadRequest_Test()
|
|
{
|
|
//Arrange
|
|
var tourModel = CreateModel();
|
|
MagicCarpetDbContext.InsertTourToDatabaseAndReturn(tourModel.Id);
|
|
//Act
|
|
var response = await HttpClient.PostAsync($"/api/tours/register", MakeContent(tourModel));
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Post_WhenHaveRecordWithSameName_ShouldBadRequest_Test()
|
|
{
|
|
//Arrange
|
|
var tourModel = CreateModel(tourName: "unique name");
|
|
MagicCarpetDbContext.InsertTourToDatabaseAndReturn(tourName: tourModel.TourName!);
|
|
//Act
|
|
var response = await HttpClient.PostAsync($"/api/tours/register", MakeContent(tourModel));
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Post_WhenSendEmptyData_ShouldBadRequest_Test()
|
|
{
|
|
//Act
|
|
var response = await HttpClient.PostAsync($"/api/tours/register", MakeContent(string.Empty));
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Post_WhenSendWrongFormatData_ShouldBadRequest_Test()
|
|
{
|
|
//Act
|
|
var response = await HttpClient.PostAsync($"/api/tours/register", MakeContent(new { Data = "test", Position = 10 }));
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Put_ShouldSuccess_Test()
|
|
{
|
|
//Arrange
|
|
var tourModel = CreateModel();
|
|
MagicCarpetDbContext.InsertTourToDatabaseAndReturn(tourModel.Id);
|
|
//Act
|
|
var response = await HttpClient.PutAsync($"/api/tours/changeinfo", MakeContent(tourModel));
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
|
MagicCarpetDbContext.ChangeTracker.Clear();
|
|
AssertElement(MagicCarpetDbContext.GetTourFromDatabaseById(tourModel.Id!), tourModel);
|
|
}
|
|
|
|
[Test]
|
|
public async Task Put_WhenNoFoundRecord_ShouldBadRequest_Test()
|
|
{
|
|
//Arrange
|
|
var tourModel = CreateModel();
|
|
MagicCarpetDbContext.InsertTourToDatabaseAndReturn();
|
|
//Act
|
|
var response = await HttpClient.PutAsync($"/api/tours/changeinfo", MakeContent(tourModel));
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Put_WhenHaveRecordWithSameName_ShouldBadRequest_Test()
|
|
{
|
|
//Arrange
|
|
var tourModel = CreateModel(tourName: "unique name");
|
|
MagicCarpetDbContext.InsertTourToDatabaseAndReturn(tourModel.Id);
|
|
MagicCarpetDbContext.InsertTourToDatabaseAndReturn(tourName: tourModel.TourName!);
|
|
//Act
|
|
var response = await HttpClient.PutAsync($"/api/tours/changeinfo", MakeContent(tourModel));
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Put_WhenSendEmptyData_ShouldBadRequest_Test()
|
|
{
|
|
//Act
|
|
var response = await HttpClient.PutAsync($"/api/tours/changeinfo", MakeContent(string.Empty));
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Put_WhenSendWrongFormatData_ShouldBadRequest_Test()
|
|
{
|
|
//Act
|
|
var response = await HttpClient.PutAsync($"/api/tours/changeinfo", MakeContent(new { Data = "test", Position = 10 }));
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Delete_ShouldSuccess_Test()
|
|
{
|
|
//Arrange
|
|
var tourId = Guid.NewGuid().ToString();
|
|
MagicCarpetDbContext.InsertTourToDatabaseAndReturn(tourId);
|
|
//Act
|
|
var response = await HttpClient.DeleteAsync($"/api/tours/delete/{tourId}");
|
|
MagicCarpetDbContext.ChangeTracker.Clear();
|
|
//Assert
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
|
});
|
|
}
|
|
|
|
[Test]
|
|
public async Task Delete_WhenNoFoundRecord_ShouldBadRequest_Test()
|
|
{
|
|
//Arrange
|
|
MagicCarpetDbContext.InsertTourToDatabaseAndReturn();
|
|
//Act
|
|
var response = await HttpClient.DeleteAsync($"/api/tours/delete/{Guid.NewGuid()}");
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Delete_WhenSendWrongFormatData_ShouldBadRequest_Test()
|
|
{
|
|
//Act
|
|
var response = await HttpClient.DeleteAsync($"/api/tours/delete/id");
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
private static void AssertElement(TourViewModel? actual, Tour expected)
|
|
{
|
|
Assert.That(actual, Is.Not.Null);
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
|
Assert.That(actual.TourName, Is.EqualTo(expected.TourName));
|
|
Assert.That(actual.TourType, Is.EqualTo(expected.TourType.ToString()));
|
|
Assert.That(actual.Price, Is.EqualTo(expected.Price));
|
|
});
|
|
}
|
|
|
|
private static void AssertElement(TourHistoryViewModel? actual, TourHistory expected)
|
|
{
|
|
Assert.That(actual, Is.Not.Null);
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(actual.OldPrice, Is.EqualTo(expected.OldPrice));
|
|
Assert.That(actual.ChangeDate.ToString(), Is.EqualTo(expected.ChangeDate.ToString()));
|
|
});
|
|
}
|
|
|
|
private static TourBindingModel CreateModel(string? id = null, string tourName = "name", string tourCountry = "country", TourType tourType = TourType.Beach, double price = 1)
|
|
=> new()
|
|
{
|
|
Id = id ?? Guid.NewGuid().ToString(),
|
|
TourName = tourName,
|
|
TourCountry = tourCountry,
|
|
TourType = tourType.ToString(),
|
|
Price = price
|
|
};
|
|
|
|
private static void AssertElement(Tour? actual, TourBindingModel expected)
|
|
{
|
|
Assert.That(actual, Is.Not.Null);
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
|
Assert.That(actual.TourName, Is.EqualTo(expected.TourName));
|
|
Assert.That(actual.TourType.ToString(), Is.EqualTo(expected.TourType));
|
|
Assert.That(actual.Price, Is.EqualTo(expected.Price));
|
|
});
|
|
}
|
|
}
|