-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
133 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using FoodDiary.API.Dtos; | ||
using FoodDiary.Domain.Entities; | ||
using FoodDiary.Domain.Utils; | ||
|
||
namespace FoodDiary.API.Mapping; | ||
|
||
public static class PagesMapper | ||
{ | ||
public static PageItemDto ToPageItemDto(this Page page, ICaloriesCalculator caloriesCalculator) => new() | ||
{ | ||
Id = page.Id, | ||
Date = page.Date.ToString("yyyy-MM-dd"), | ||
CountNotes = page.Notes.Count, | ||
CountCalories = caloriesCalculator.Calculate(page.Notes) | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/backend/tests/FoodDiary.ComponentTests/Dsl/PagesListBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using FoodDiary.Domain.Entities; | ||
|
||
namespace FoodDiary.ComponentTests.Dsl; | ||
|
||
public class PagesListBuilder | ||
{ | ||
private readonly int _count; | ||
private DateOnly _startDate = DateOnly.FromDateTime(DateTime.UtcNow); | ||
private int _intervalInDays = 1; | ||
|
||
public PagesListBuilder(int count) | ||
{ | ||
_count = count; | ||
} | ||
|
||
public Page[] Please() | ||
{ | ||
var pages = new List<Page>(_count); | ||
var currentDate = _startDate; | ||
|
||
for (var i = 0; i < _count; i++) | ||
{ | ||
var page = Create.Page(currentDate.ToString("O")).Please(); | ||
pages.Add(page); | ||
currentDate = currentDate.AddDays(_intervalInDays); | ||
} | ||
|
||
return pages.ToArray(); | ||
} | ||
|
||
public PagesListBuilder StartingFrom(string date) | ||
{ | ||
_startDate = DateOnly.Parse(date); | ||
return this; | ||
} | ||
|
||
public PagesListBuilder WithOneDayInterval() | ||
{ | ||
_intervalInDays = 1; | ||
return this; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/backend/tests/FoodDiary.ComponentTests/Scenarios/Pages/PagesApiContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System.Net.Http.Json; | ||
using FoodDiary.API.Dtos; | ||
using FoodDiary.API.Mapping; | ||
using FoodDiary.ComponentTests.Infrastructure; | ||
using FoodDiary.Domain.Entities; | ||
using FoodDiary.Domain.Utils; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace FoodDiary.ComponentTests.Scenarios.Pages; | ||
|
||
public class PagesApiContext : BaseContext | ||
{ | ||
private PagesSearchResultDto? _pagesSearchResult; | ||
|
||
public PagesApiContext(FoodDiaryWebApplicationFactory factory) : base(factory) | ||
{ | ||
} | ||
|
||
public Task Given_pages(params Page[] pages) | ||
{ | ||
return Factory.SeedDataAsync(pages); | ||
} | ||
|
||
public async Task When_user_retieves_pages_list_from_to(string from, string to) | ||
{ | ||
_pagesSearchResult = await ApiClient.GetFromJsonAsync<PagesSearchResultDto>( | ||
$"/api/v1/pages?startDate={from}&endDate={to}"); | ||
} | ||
|
||
public Task Then_pages_list_contains_total_items_count(int count) | ||
{ | ||
_pagesSearchResult?.TotalPagesCount.Should().Be(count); | ||
return Task.CompletedTask; | ||
} | ||
|
||
public Task Then_pages_list_contains_items(params Page[] items) | ||
{ | ||
var caloriesCalculator = Factory.Services.GetRequiredService<ICaloriesCalculator>(); | ||
var expectedPageItems = items.Select(p => p.ToPageItemDto(caloriesCalculator)); | ||
_pagesSearchResult?.PageItems.Should().BeEquivalentTo(expectedPageItems); | ||
return Task.CompletedTask; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/backend/tests/FoodDiary.ComponentTests/Scenarios/Pages/PagesApiTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using FoodDiary.ComponentTests.Dsl; | ||
using FoodDiary.ComponentTests.Infrastructure; | ||
|
||
namespace FoodDiary.ComponentTests.Scenarios.Pages; | ||
|
||
public class PagesApiTests : ScenarioBase<PagesApiContext> | ||
{ | ||
public PagesApiTests(FoodDiaryWebApplicationFactory factory) : base(factory, () => new PagesApiContext(factory)) | ||
{ | ||
} | ||
|
||
[Scenario] | ||
public Task I_can_retrieve_pages_list() | ||
{ | ||
var pages = Create.PagesList(5) | ||
.StartingFrom("2024-01-01") | ||
.WithOneDayInterval() | ||
.Please(); | ||
|
||
var expectedPages = pages[1..4]; | ||
|
||
return Run( | ||
c => c.Given_authenticated_user(), | ||
c => c.Given_pages(pages), | ||
c => c.When_user_retieves_pages_list_from_to("2024-01-02", "2024-01-04"), | ||
c => c.Then_pages_list_contains_total_items_count(3), | ||
c => c.Then_pages_list_contains_items(expectedPages)); | ||
} | ||
} |