Skip to content

Commit

Permalink
Fix sorting notes (#128)
Browse files Browse the repository at this point in the history
* Fix sorting notes

* Correct sort by MealType + DisplayOrder

* Refactor NoteBuilder to be reusable
  • Loading branch information
pkirilin authored Nov 4, 2024
1 parent 47000ff commit 99aeef3
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public async Task<IReadOnlyCollection<Note>> FindByDate(DateOnly date, Cancellat
.AsNoTracking()
.Where(n => n.Date == date)
.Include(n => n.Product)
.OrderBy(n => n.MealType)
.ThenBy(n => n.DisplayOrder)
.ToListAsync(cancellationToken);
}

Expand Down
60 changes: 40 additions & 20 deletions src/backend/tests/FoodDiary.ComponentTests/Dsl/NoteBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,66 @@ namespace FoodDiary.ComponentTests.Dsl;

public class NoteBuilder
{
private readonly Note _note = new()
private int? _id;
private DateOnly _date = DateOnly.Parse(FakeDateTimeProvider.CurrentFakeDateAsString);
private MealType _mealType = MealType.Breakfast;
private Product _product = Create.Product().Please();
private int _productId;
private int _productQuantity = 100;
private int _displayOrder = 1;

public Note Please() => new()
{
Id = Random.Shared.Next(),
Date = DateOnly.Parse(FakeDateTimeProvider.CurrentFakeDateAsString),
MealType = MealType.Breakfast,
Product = Create.Product().Please(),
ProductQuantity = 100
Id = _id ?? Random.Shared.Next(),
Date = _date,
MealType = _mealType,
Product = _product,
ProductId = _productId,
ProductQuantity = _productQuantity,
DisplayOrder = _displayOrder
};

public Note Please() => _note;

public NoteBuilder WithDate(string date)
{
_note.Date = DateOnly.Parse(date);
_date = DateOnly.Parse(date);
return this;
}

public NoteBuilder WithProduct(Product product, int quantity)
{
_note.Product = product;
_note.ProductQuantity = quantity;
_product = product;
_productQuantity = quantity;
return this;
}

public NoteBuilder WithProduct(string productName, int quantity)
{
_note.Product = Create.Product(productName).Please();
_note.ProductQuantity = quantity;
_product = Create.Product(productName).Please();
_productQuantity = quantity;
return this;
}

public NoteBuilder WithMealType(MealType mealType)
{
_mealType = mealType;
return this;
}

public NoteBuilder WithDisplayOrder(int displayOrder)
{
_displayOrder = displayOrder;
return this;
}

public NoteBuilder From(Note note)
{
_note.Id = note.Id;
_note.Date = note.Date;
_note.MealType = note.MealType;
_note.ProductId = note.ProductId;
_note.ProductQuantity = note.ProductQuantity;
_note.DisplayOrder = note.DisplayOrder;
_note.Product = note.Product;
_id = note.Id;
_date = note.Date;
_mealType = note.MealType;
_product = note.Product;
_productId = note.ProductId;
_productQuantity = note.ProductQuantity;
_displayOrder = note.DisplayOrder;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ public Task Then_notes_list_contains_items(params Note[] items)
.Excluding(note => note.Id)
.Excluding(note => note.ProductId)
.Excluding(note => note.Calories))
.And.AllSatisfy(note => { note.Calories.Should().BePositive(); });
.And.AllSatisfy(note => { note.Calories.Should().BePositive(); })
.And.BeInAscendingOrder(note => note.MealType)
.And.ThenBeInAscendingOrder(note => note.DisplayOrder);

return Task.CompletedTask;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Net;
using FoodDiary.ComponentTests.Dsl;
using FoodDiary.ComponentTests.Infrastructure;
using FoodDiary.Domain.Enums;

namespace FoodDiary.ComponentTests.Scenarios.Notes;

Expand All @@ -14,17 +15,21 @@ protected override NotesApiContext CreateContext(
[Scenario]
public Task I_can_retrieve_notes_list()
{
var breakfast = Create.Note().WithDate("2024-01-04").WithMealType(MealType.Breakfast);
var lunch = Create.Note().WithDate("2024-01-04").WithMealType(MealType.Lunch);
var notes = new
{
Chicken = Create.Note().WithDate("2024-01-04").WithProduct("Chicken", 150).Please(),
Rice = Create.Note().WithDate("2024-01-04").WithProduct("Rice", 100).Please()
Chicken = breakfast.WithProduct("Chicken", 150).WithDisplayOrder(0).Please(),
Rice = breakfast.WithProduct("Rice", 100).WithDisplayOrder(1).Please(),
Cheese = lunch.WithProduct("Cheese", 350).WithDisplayOrder(0).Please(),
Bread = lunch.WithProduct("Bread", 250).WithDisplayOrder(1).Please(),
};

return Run(
c => c.Given_authenticated_user(),
c => c.Given_notes(notes.Chicken, notes.Rice),
c => c.Given_notes(notes.Rice, notes.Bread, notes.Chicken, notes.Cheese),
c => c.When_user_retrieves_notes_list_for_date("2024-01-04"),
c => c.Then_notes_list_contains_items(notes.Chicken, notes.Rice));
c => c.Then_notes_list_contains_items(notes.Chicken, notes.Rice, notes.Cheese, notes.Bread));
}

[Scenario]
Expand Down

0 comments on commit 99aeef3

Please sign in to comment.