Skip to content

Commit

Permalink
Fix - Correction de la récupération des deliveries et ajout de TU
Browse files Browse the repository at this point in the history
  • Loading branch information
noelmugnier committed Nov 12, 2020
1 parent c134f78 commit 26c6577
Show file tree
Hide file tree
Showing 11 changed files with 308 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ public async Task<Result<Guid>> Handle(CreateDeliveryModeCommand request, Cancel
}
}
var entity = new DeliveryMode(Guid.NewGuid(), request.Kind, producer, request.LockOrderHoursBeforeDelivery, deliveryModeAddress, openingHours, request.Name, request.Description);
entity.SetAvailability(request.Available);
var entity = new DeliveryMode(Guid.NewGuid(), request.Kind, producer, request.Available, request.LockOrderHoursBeforeDelivery, deliveryModeAddress, openingHours, request.Name, request.Description);
if (request.Kind == DeliveryKind.Collective || request.Kind == DeliveryKind.Farm || request.Kind == DeliveryKind.Market)
producer.CanDirectSell = true;
Expand Down
214 changes: 214 additions & 0 deletions Sheaft.Application.Queries.Tests/GetProducerDeliveriesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sheaft.Application.Interop;
using Sheaft.Application.Models;
using Sheaft.Application.Queries;
using Sheaft.Domain.Enums;
using Sheaft.Domain.Models;
using Sheaft.Tests.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace Queries.Delivery.Tests
{
[TestClass]
public class GetProducerDeliveries
{
private IAppDbContext _context;
private IDeliveryQueries _queries;

[TestInitialize]
public void Initialize()
{
_context = ContextHelper.GetInMemoryContext();
_queries = new DeliveryQueries(_context, null);
}

[TestMethod]
[DataRow("3b5c008bb6a24f5c8cc8258b9e33105f", false, DeliveryKind.Farm)]
[DataRow("6a209232b2184ae5bf8a1c26a7a74e8b", true, DeliveryKind.Farm)]
[DataRow("3b5c008bb6a24f5c8cc8258b9e33105f", true, DeliveryKind.ProducerToStore)]
public async Task Should_Return_No_Deliveries(string producerId, bool available, DeliveryKind kind)
{
var token = CancellationToken.None;

await _context.AddAsync(new DeliveryMode(
Guid.NewGuid(),
kind,
new Producer(Guid.Parse("3b5c008bb6a24f5c8cc8258b9e33105f"), "prod1", "fa", "la", "[email protected]", new UserAddress("x", null, "x", "x", CountryIsoCode.FR, null)),
available,
0,
new DeliveryAddress("x", null, "x", "x", CountryIsoCode.FR, null, null),
new List<TimeSlotHour>
{
new TimeSlotHour(DayOfWeek.Wednesday, TimeSpan.FromHours(8), TimeSpan.FromHours(12))
},
"delivery1"), token);

await _context.SaveChangesAsync(token);

//test
var results = await _queries.GetProducersDeliveriesAsync(
new List<Guid> { Guid.Parse(producerId) },
new List<DeliveryKind> { DeliveryKind.Farm },
DateTime.UtcNow,
null,
token);

//assert
results.Should().NotBeNull().And.BeEmpty();
}

[TestMethod]
[DataRow(0, 2020, 1, 1, 7, 59, 59, 1, 8)]
[DataRow(0, 2020, 1, 1, 8, 0, 0, 8, 15)]
[DataRow(24, 2019, 12, 31, 8, 0, 0, 8, 15)]
[DataRow(24, 2019, 12, 31, 7, 0, 0, 1, 8)]
public async Task Should_Return_Wednesday_DeliveryHours(int orderLockInHours, int year, int month, int day, int hour, int minute, int second, int expectedFirstDay, int expectedLastDay)
{
var token = CancellationToken.None;
var currentDate = new DateTime(year, month, day, hour, minute, second);
var producerId = Guid.NewGuid();

await _context.AddAsync(new DeliveryMode(
Guid.NewGuid(),
DeliveryKind.Farm,
new Producer(producerId, "prod1", "fa", "la", "[email protected]", new UserAddress("x", null, "x", "x", CountryIsoCode.FR, null)),
true,
orderLockInHours,
new DeliveryAddress("x", null, "x", "x", CountryIsoCode.FR, null, null),
new List<TimeSlotHour> { new TimeSlotHour(DayOfWeek.Wednesday, TimeSpan.FromHours(8), TimeSpan.FromHours(12)) },
"delivery1"), token);

await _context.SaveChangesAsync(token);

//test
var results = await _queries.GetProducersDeliveriesAsync(
new List<Guid> { producerId },
new List<DeliveryKind> { DeliveryKind.Farm },
currentDate,
null,
token);

//assert
var deliveryHoursResults = results
.Should().NotBeNull().And.ContainSingle()
.And.Subject.First().Deliveries.Should().HaveCount(1)
.And.Subject.First().DeliveryHours.Should().HaveCount(2)
.And.Subject.Should().OnlyContain(c => c.Day == DayOfWeek.Wednesday);

deliveryHoursResults.And.Subject.First().ExpectedDeliveryDate.Day.Should().Be(expectedFirstDay);
deliveryHoursResults.And.Subject.Last().ExpectedDeliveryDate.Day.Should().Be(expectedLastDay);
}

[TestMethod]
[DataRow(0, 2020, 1, 1, 7, 59, 59, 1, 8, 4, 11)]
[DataRow(0, 2020, 1, 1, 8, 0, 0, 8, 15, 4, 11)]
[DataRow(24, 2019, 12, 31, 8, 0, 0, 8, 15, 4, 11)]
[DataRow(24, 2019, 12, 31, 7, 0, 0, 1, 8, 4, 11)]
[DataRow(0, 2020, 1, 8, 8, 0, 0, 15, 22, 11, 18)]
public async Task Should_Return_Multiple_Days_DeliveryHours(int orderLockInHours, int year, int month, int day, int hour, int minute, int second, int expectedFirstDay, int expectedSecondDay, int expectedThirdDay, int expectedLastDay)
{
var token = CancellationToken.None;
var currentDate = new DateTime(year, month, day, hour, minute, second);
var producerId = Guid.NewGuid();

await _context.AddAsync(new DeliveryMode(
Guid.NewGuid(),
DeliveryKind.Farm,
new Producer(producerId, "prod1", "fa", "la", "[email protected]", new UserAddress("x", null, "x", "x", CountryIsoCode.FR, null)),
true,
orderLockInHours,
new DeliveryAddress("x", null, "x", "x", CountryIsoCode.FR, null, null),
new List<TimeSlotHour> {
new TimeSlotHour(DayOfWeek.Wednesday, TimeSpan.FromHours(8), TimeSpan.FromHours(12)),
new TimeSlotHour(DayOfWeek.Saturday, TimeSpan.FromHours(10), TimeSpan.FromHours(16))
},
"delivery1"), token);

await _context.SaveChangesAsync(token);

//test
var results = await _queries.GetProducersDeliveriesAsync(
new List<Guid> { producerId },
new List<DeliveryKind> { DeliveryKind.Farm },
currentDate,
null,
token);

//assert
var deliveryHoursResults = results
.Should().NotBeNull().And.ContainSingle()
.And.Subject.First().Deliveries.Should().HaveCount(1)
.And.Subject.First().DeliveryHours.Should().HaveCount(4)
.And.Subject.Should().OnlyContain(c => c.Day == DayOfWeek.Wednesday || c.Day == DayOfWeek.Saturday);

deliveryHoursResults.And.Subject.ElementAt(0).ExpectedDeliveryDate.Day.Should().Be(expectedFirstDay);
deliveryHoursResults.And.Subject.ElementAt(1).ExpectedDeliveryDate.Day.Should().Be(expectedSecondDay);
deliveryHoursResults.And.Subject.ElementAt(2).ExpectedDeliveryDate.Day.Should().Be(expectedThirdDay);
deliveryHoursResults.And.Subject.ElementAt(3).ExpectedDeliveryDate.Day.Should().Be(expectedLastDay);
}

[TestMethod]
[DataRow(0, 2020, 1, 1, 7, 59, 59, 1, 8, 3, 10)]
[DataRow(0, 2020, 1, 1, 8, 0, 0, 8, 15, 3, 10)]
[DataRow(24, 2019, 12, 31, 8, 0, 0, 8, 15, 3, 10)]
[DataRow(24, 2019, 12, 31, 7, 0, 0, 1, 8, 3, 10)]
[DataRow(0, 2020, 1, 8, 8, 0, 0, 15, 22, 10, 17)]
public async Task Should_Return_Multiple_Deliveries(int orderLockInHours, int year, int month, int day, int hour, int minute, int second, int expectedFarmDelivery_FirstDay, int expectedFarmDelivery_SecondDay, int expectedMarketDelivery_FirstDay, int expectedMarketDelivery_SecondDay)
{
var token = CancellationToken.None;
var currentDate = new DateTime(year, month, day, hour, minute, second);
var producerId = Guid.NewGuid();

await _context.AddAsync(new DeliveryMode(
Guid.NewGuid(),
DeliveryKind.Farm,
new Producer(producerId, "prod1", "fa", "la", "[email protected]", new UserAddress("x", null, "x", "x", CountryIsoCode.FR, null)),
true,
orderLockInHours,
new DeliveryAddress("x", null, "x", "x", CountryIsoCode.FR, null, null),
new List<TimeSlotHour> { new TimeSlotHour(DayOfWeek.Wednesday, TimeSpan.FromHours(8), TimeSpan.FromHours(12)) },
"delivery1"), token);

await _context.AddAsync(new DeliveryMode(
Guid.NewGuid(),
DeliveryKind.Market,
new Producer(producerId, "prod1", "fa", "la", "[email protected]", new UserAddress("x", null, "x", "x", CountryIsoCode.FR, null)),
true,
orderLockInHours,
new DeliveryAddress("x", null, "x", "x", CountryIsoCode.FR, null, null),
new List<TimeSlotHour> { new TimeSlotHour(DayOfWeek.Friday, TimeSpan.FromHours(16), TimeSpan.FromHours(18)) },
"delivery2"), token);

await _context.SaveChangesAsync(token);

//test
var results = await _queries.GetProducersDeliveriesAsync(
new List<Guid> { producerId },
new List<DeliveryKind> { DeliveryKind.Farm, DeliveryKind.Market },
currentDate,
null,
token);

//assert
var deliveriesResults = results
.Should().NotBeNull().And.ContainSingle()
.And.Subject.First().Deliveries.Should().HaveCount(2);

var marketDelivery = deliveriesResults.And.Subject.First(c => c.Kind == DeliveryKind.Market);
var farmDelivery = deliveriesResults.And.Subject.First(c => c.Kind == DeliveryKind.Farm);

marketDelivery.DeliveryHours.Should().OnlyContain(c => c.Day == DayOfWeek.Friday);
marketDelivery.DeliveryHours.ElementAt(0).ExpectedDeliveryDate.Day.Should().Be(expectedMarketDelivery_FirstDay);
marketDelivery.DeliveryHours.ElementAt(1).ExpectedDeliveryDate.Day.Should().Be(expectedMarketDelivery_SecondDay);

farmDelivery.DeliveryHours.Should().OnlyContain(c => c.Day == DayOfWeek.Wednesday);
farmDelivery.DeliveryHours.ElementAt(0).ExpectedDeliveryDate.Day.Should().Be(expectedFarmDelivery_FirstDay);
farmDelivery.DeliveryHours.ElementAt(1).ExpectedDeliveryDate.Day.Should().Be(expectedFarmDelivery_SecondDay);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.2" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.2" />
<PackageReference Include="coverlet.collector" Version="1.3.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Sheaft.Application.Interop\Sheaft.Application.Interop.csproj" />
<ProjectReference Include="..\Sheaft.Application.Queries\Sheaft.Application.Queries.csproj" />
<ProjectReference Include="..\Sheaft.Tests.Common\Sheaft.Tests.Common.csproj" />
</ItemGroup>

</Project>
28 changes: 11 additions & 17 deletions Sheaft.Application.Queries/DeliveryQueries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,35 +152,29 @@ private IEnumerable<DeliveryHourDto> GetAvailableDeliveryHours(IEnumerable<TimeS
var list = new List<DeliveryHourDto>();
foreach (var openingHour in openingHours.OrderBy(oh => oh.Day))
{
var diff = (int)openingHour.Day >= (int)currentDate.DayOfWeek ? (int)openingHour.Day - (int)currentDate.DayOfWeek : 0;
var result = GetDeliveryHourIfMatch(currentDate, openingHour, diff, lockOrderHoursBeforeDelivery);
if (result != null)
list.Add(result);

diff = (int)openingHour.Day + 7 - (int)currentDate.DayOfWeek;
result = GetDeliveryHourIfMatch(currentDate, openingHour, diff, lockOrderHoursBeforeDelivery);
if (result != null)
list.Add(result);

var increment = 14;
while (result == null && increment < 31)
var results = new List<DeliveryHourDto>();
var increment = 0;
while (results.Count < 2 && increment < 31)
{
diff = (int)openingHour.Day + increment - (int)currentDate.DayOfWeek;
result = GetDeliveryHourIfMatch(currentDate, openingHour, diff, lockOrderHoursBeforeDelivery);
var diff = (int)openingHour.Day + increment - (int)currentDate.DayOfWeek;
var result = GetDeliveryHourIfMatch(currentDate, openingHour, diff, lockOrderHoursBeforeDelivery);
if (result != null)
list.Add(result);
results.Add(result);

increment += 7;
}

if (results.Any())
list.AddRange(results);
}

return list;
}

private DeliveryHourDto GetDeliveryHourIfMatch(DateTimeOffset currentDate, TimeSlotHour openingHour, int diff, int lockOrderHoursBeforeDelivery)
{
var targetDate = new DateTime(currentDate.Year, currentDate.Month, currentDate.Day, openingHour.To.Hours, openingHour.To.Minutes, openingHour.To.Seconds).AddDays(diff);
if (currentDate.AddHours(lockOrderHoursBeforeDelivery) > targetDate)
var targetDate = new DateTime(currentDate.Year, currentDate.Month, currentDate.Day, openingHour.From.Hours, openingHour.From.Minutes, openingHour.From.Seconds).AddDays(diff);
if (currentDate.AddHours(lockOrderHoursBeforeDelivery) >= targetDate)
return null;

return new DeliveryHourDto
Expand Down
18 changes: 0 additions & 18 deletions Sheaft.Application.Tests/Sheaft.Application.Tests.csproj

This file was deleted.

18 changes: 0 additions & 18 deletions Sheaft.Application.Tests/UnitTest1.cs

This file was deleted.

18 changes: 0 additions & 18 deletions Sheaft.Domain.Tests/UnitTest1.cs

This file was deleted.

3 changes: 2 additions & 1 deletion Sheaft.Domain/DeliveryMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ protected DeliveryMode()
{
}

public DeliveryMode(Guid id, DeliveryKind kind, Producer producer, int lockOrderHoursBeforeDelivery, DeliveryAddress address, IEnumerable<TimeSlotHour> openingHours, string name, string description = null)
public DeliveryMode(Guid id, DeliveryKind kind, Producer producer, bool available, int lockOrderHoursBeforeDelivery, DeliveryAddress address, IEnumerable<TimeSlotHour> openingHours, string name, string description = null)
{
Id = id;
Name = name;
Expand All @@ -27,6 +27,7 @@ public DeliveryMode(Guid id, DeliveryKind kind, Producer producer, int lockOrder
Producer = producer;

SetOpeningHours(openingHours);
SetAvailability(available);
}

public Guid Id { get; private set; }
Expand Down
22 changes: 22 additions & 0 deletions Sheaft.Tests.Common/ContextHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Localization;
using Moq;
using Sheaft.Application.Interop;
using Sheaft.Infrastructure.Persistence;
using Sheaft.Localization;
using System;

namespace Sheaft.Tests.Common
{
public static class ContextHelper
{
public static IAppDbContext GetInMemoryContext()
{
var options = new DbContextOptionsBuilder<AppDbContext>()
.UseInMemoryDatabase(Guid.NewGuid().ToString("N"))
.Options;

return new AppDbContext(options, Mock.Of<IStringLocalizer<MessageResources>>());
}
}
}
Loading

0 comments on commit 26c6577

Please sign in to comment.