Skip to content

Commit

Permalink
Delete page tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pkirilin committed Jan 5, 2024
1 parent 9d86fdb commit ef86fcc
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Net;
using System.Net.Http.Json;
using System.Text;
using System.Text.Json;
using FoodDiary.API.Dtos;
using FoodDiary.API.Mapping;
using FoodDiary.API.Requests;
Expand All @@ -17,6 +19,8 @@ public class PagesApiContext : BaseContext
private string? _dateForNewPage;
private HttpResponseMessage _createPageResponse = null!;
private HttpResponseMessage _updatePageResponse = null!;
private HttpResponseMessage _deletePageResponse = null!;
private HttpResponseMessage _deleteMultiplePagesResponse = null!;

public PagesApiContext(FoodDiaryWebApplicationFactory factory) : base(factory)
{
Expand Down Expand Up @@ -50,6 +54,23 @@ public async Task When_user_updates_page(Page page)
_updatePageResponse = await ApiClient.PutAsJsonAsync($"/api/v1/pages/{page.Id}", request);
}

public async Task When_user_deletes_page(Page page)
{
_deletePageResponse = await ApiClient.DeleteAsync($"/api/v1/pages/{page.Id}");
}

public async Task When_user_deletes_multiple_pages(params Page[] pages)
{
var pageIds = pages.Select(p => p.Id);

var request = new HttpRequestMessage(HttpMethod.Delete, "/api/v1/pages/batch")
{
Content = new StringContent(JsonSerializer.Serialize(pageIds), Encoding.Unicode, "application/json")
};

_deleteMultiplePagesResponse = await ApiClient.SendAsync(request);
}

public async Task When_user_retieves_date_for_new_page()
{
var response = await ApiClient.GetAsync("/api/v1/pages/date");
Expand All @@ -73,6 +94,12 @@ public Task Then_pages_list_contains_items(params Page[] items)

return Task.CompletedTask;
}

public Task Then_pages_list_is_empty()
{
_pagesSearchResult?.PageItems.Should().BeEmpty();
return Task.CompletedTask;
}

public Task Then_page_is_successfully_created()
{
Expand All @@ -85,6 +112,18 @@ public Task Then_page_is_successfully_updated()
_updatePageResponse.StatusCode.Should().Be(HttpStatusCode.OK);
return Task.CompletedTask;
}

public Task Then_page_is_successfully_deleted()
{
_deletePageResponse.StatusCode.Should().Be(HttpStatusCode.OK);
return Task.CompletedTask;
}

public Task Then_multiple_pages_are_successfully_deleted()
{
_deleteMultiplePagesResponse.StatusCode.Should().Be(HttpStatusCode.OK);
return Task.CompletedTask;
}

public Task Then_date_for_new_page_contains_value(string value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,37 @@ public Task I_can_update_page()
c => c.Then_pages_list_contains_items(page));
}

[Scenario]
public Task I_can_delete_page()
{
var page = Create.Page("2024-01-02").Please();

return Run(
c => c.Given_authenticated_user(),
c => c.Given_pages(page),
c => c.When_user_deletes_page(page),
c => c.Then_page_is_successfully_deleted(),
c => c.When_user_retieves_pages_list(),
c => c.Then_pages_list_is_empty());
}

[Scenario]
public Task I_can_delete_many_pages()
{
var pages = Create.PagesList(3)
.StartingFrom("2024-01-01")
.WithOneDayInterval()
.Please();

return Run(
c => c.Given_authenticated_user(),
c => c.Given_pages(pages),
c => c.When_user_deletes_multiple_pages(pages),
c => c.Then_multiple_pages_are_successfully_deleted(),
c => c.When_user_retieves_pages_list(),
c => c.Then_pages_list_is_empty());
}

[Scenario]
public Task I_receive_next_day_date_for_new_page_when_I_have_some_pages()
{
Expand Down

0 comments on commit ef86fcc

Please sign in to comment.