1- using FluentAssertions ;
1+ using FluentAssertions ;
22using MechanicsSoftware . Application . UseCases . ServiceOrders ;
33using MechanicsSoftware . Application . UseCases . ServiceOrders . Commands ;
44using MechanicsSoftware . Application . UseCases . ServiceOrders . Handlers ;
@@ -14,11 +14,11 @@ namespace MechanicsSoftware.UnitTests.Application.ServiceOrders;
1414public class ListServiceOrdersUseCaseTests
1515{
1616 [ Fact ]
17- public async Task ExecuteAsync_NoFilter_ReturnsAllOrders ( )
17+ public async Task ExecuteAsync_NoFilter_ReturnsActiveOrders ( )
1818 {
1919 await using var db = InMemoryDbContextHelper . Create ( ) ;
20- db . ServiceOrders . Add ( ServiceOrder . Create ( Guid . NewGuid ( ) , Guid . NewGuid ( ) , Guid . NewGuid ( ) ) ) ;
21- db . ServiceOrders . Add ( ServiceOrder . Create ( Guid . NewGuid ( ) , Guid . NewGuid ( ) , Guid . NewGuid ( ) ) ) ;
20+ db . ServiceOrders . Add ( CreateOrderInState ( ServiceOrderStatus . Status . Received ) ) ;
21+ db . ServiceOrders . Add ( CreateOrderInState ( ServiceOrderStatus . Status . Received ) ) ;
2222 await db . SaveChangesAsync ( ) ;
2323
2424 var result = await new ListServiceOrdersHandler ( db ) . ExecuteAsync ( new ListServiceOrdersQuery ( ) ) ;
@@ -30,11 +30,8 @@ public async Task ExecuteAsync_NoFilter_ReturnsAllOrders()
3030 public async Task ExecuteAsync_StatusFilter_ReturnsMatchingOrders ( )
3131 {
3232 await using var db = InMemoryDbContextHelper . Create ( ) ;
33- var received = ServiceOrder . Create ( Guid . NewGuid ( ) , Guid . NewGuid ( ) , Guid . NewGuid ( ) ) ;
34- var inDiagnosis = ServiceOrder . Create ( Guid . NewGuid ( ) , Guid . NewGuid ( ) , Guid . NewGuid ( ) ) ;
35- inDiagnosis . StartDiagnosis ( ) ;
36- db . ServiceOrders . Add ( received ) ;
37- db . ServiceOrders . Add ( inDiagnosis ) ;
33+ db . ServiceOrders . Add ( CreateOrderInState ( ServiceOrderStatus . Status . Received ) ) ;
34+ db . ServiceOrders . Add ( CreateOrderInState ( ServiceOrderStatus . Status . InDiagnosis ) ) ;
3835 await db . SaveChangesAsync ( ) ;
3936
4037 var result = await new ListServiceOrdersHandler ( db ) . ExecuteAsync (
@@ -45,15 +42,68 @@ public async Task ExecuteAsync_StatusFilter_ReturnsMatchingOrders()
4542 }
4643
4744 [ Fact ]
48- public async Task ExecuteAsync_InvalidStatusFilter_ReturnsAllOrders ( )
45+ public async Task ExecuteAsync_InvalidStatusFilter_ReturnsActiveOrders ( )
4946 {
5047 await using var db = InMemoryDbContextHelper . Create ( ) ;
51- db . ServiceOrders . Add ( ServiceOrder . Create ( Guid . NewGuid ( ) , Guid . NewGuid ( ) , Guid . NewGuid ( ) ) ) ;
48+ db . ServiceOrders . Add ( CreateOrderInState ( ServiceOrderStatus . Status . Received ) ) ;
5249 await db . SaveChangesAsync ( ) ;
5350
5451 var result = await new ListServiceOrdersHandler ( db ) . ExecuteAsync (
5552 new ListServiceOrdersQuery ( Status : "UNKNOWN_STATUS" ) ) ;
5653
5754 result . Should ( ) . HaveCount ( 1 ) ;
5855 }
56+
57+ [ Fact ]
58+ public async Task ExecuteAsync_NoFilter_ExcludesCompletedAndDeliveredOrders ( )
59+ {
60+ await using var db = InMemoryDbContextHelper . Create ( ) ;
61+ db . ServiceOrders . Add ( CreateOrderInState ( ServiceOrderStatus . Status . Received ) ) ;
62+ db . ServiceOrders . Add ( CreateOrderInState ( ServiceOrderStatus . Status . InDiagnosis ) ) ;
63+ db . ServiceOrders . Add ( CreateOrderInState ( ServiceOrderStatus . Status . Completed ) ) ;
64+ db . ServiceOrders . Add ( CreateOrderInState ( ServiceOrderStatus . Status . Delivered ) ) ;
65+ await db . SaveChangesAsync ( ) ;
66+
67+ var result = await new ListServiceOrdersHandler ( db ) . ExecuteAsync ( new ListServiceOrdersQuery ( ) ) ;
68+
69+ result . Should ( ) . HaveCount ( 2 ) ;
70+ result . Should ( ) . NotContain ( o => o . Status == "COMPLETED" || o . Status == "DELIVERED" ) ;
71+ }
72+
73+ [ Fact ]
74+ public async Task ExecuteAsync_NoFilter_OrdersByStatusPriorityThenCreatedAt ( )
75+ {
76+ await using var db = InMemoryDbContextHelper . Create ( ) ;
77+ db . ServiceOrders . Add ( CreateOrderInState ( ServiceOrderStatus . Status . Received ) ) ;
78+ db . ServiceOrders . Add ( CreateOrderInState ( ServiceOrderStatus . Status . InDiagnosis ) ) ;
79+ db . ServiceOrders . Add ( CreateOrderInState ( ServiceOrderStatus . Status . AwaitingApproval ) ) ;
80+ db . ServiceOrders . Add ( CreateOrderInState ( ServiceOrderStatus . Status . InExecution ) ) ;
81+ await db . SaveChangesAsync ( ) ;
82+
83+ var result = await new ListServiceOrdersHandler ( db ) . ExecuteAsync ( new ListServiceOrdersQuery ( ) ) ;
84+
85+ result . Should ( ) . HaveCount ( 4 ) ;
86+ result [ 0 ] . Status . Should ( ) . Be ( "IN_EXECUTION" ) ;
87+ result [ 1 ] . Status . Should ( ) . Be ( "AWAITING_APPROVAL" ) ;
88+ result [ 2 ] . Status . Should ( ) . Be ( "IN_DIAGNOSIS" ) ;
89+ result [ 3 ] . Status . Should ( ) . Be ( "RECEIVED" ) ;
90+ }
91+
92+ private static ServiceOrder CreateOrderInState ( ServiceOrderStatus . Status target )
93+ {
94+ var order = ServiceOrder . Create ( Guid . NewGuid ( ) , Guid . NewGuid ( ) , Guid . NewGuid ( ) ) ;
95+ if ( target == ServiceOrderStatus . Status . Received ) return order ;
96+ order . StartDiagnosis ( ) ;
97+ if ( target == ServiceOrderStatus . Status . InDiagnosis ) return order ;
98+ order . AddServiceItem ( Guid . NewGuid ( ) , "Service" , new Money ( 1_000 ) , 1 ) ;
99+ order . GenerateBudget ( ) ;
100+ order . SendBudget ( ) ;
101+ if ( target == ServiceOrderStatus . Status . AwaitingApproval ) return order ;
102+ order . Approve ( ) ;
103+ if ( target == ServiceOrderStatus . Status . InExecution ) return order ;
104+ order . Complete ( ) ;
105+ if ( target == ServiceOrderStatus . Status . Completed ) return order ;
106+ order . Deliver ( ) ;
107+ return order ;
108+ }
59109}
0 commit comments