1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Linq ;
4
+ using System . Text . Json ;
4
5
using System . Threading . Tasks ;
5
6
using Microsoft . Extensions . DependencyInjection ;
6
7
using Microsoft . Extensions . DependencyInjection . Extensions ;
@@ -512,7 +513,9 @@ public virtual async Task Should_Write_AuditLog_For_Navigation_Changes()
512
513
x . EntityChanges [ 1 ] . EntityTypeFullName == typeof ( AppEntityWithNavigations ) . FullName &&
513
514
x . EntityChanges [ 1 ] . PropertyChanges . Count == 1 &&
514
515
x . EntityChanges [ 1 ] . PropertyChanges [ 0 ] . PropertyName == nameof ( AppEntityWithNavigations . OneToOne ) &&
515
- x . EntityChanges [ 1 ] . PropertyChanges [ 0 ] . PropertyTypeFullName == typeof ( AppEntityWithNavigationChildOneToOne ) . FullName ) ) ;
516
+ x . EntityChanges [ 1 ] . PropertyChanges [ 0 ] . PropertyTypeFullName == typeof ( AppEntityWithNavigationChildOneToOne ) . FullName &&
517
+ x . EntityChanges [ 1 ] . PropertyChanges [ 0 ] . OriginalValue == null &&
518
+ x . EntityChanges [ 1 ] . PropertyChanges [ 0 ] . NewValue == entityId . ToString ( ) ) ) ;
516
519
AuditingStore . ClearReceivedCalls ( ) ;
517
520
#pragma warning restore 4014
518
521
@@ -539,10 +542,13 @@ public virtual async Task Should_Write_AuditLog_For_Navigation_Changes()
539
542
x . EntityChanges [ 1 ] . EntityTypeFullName == typeof ( AppEntityWithNavigations ) . FullName &&
540
543
x . EntityChanges [ 1 ] . PropertyChanges . Count == 1 &&
541
544
x . EntityChanges [ 1 ] . PropertyChanges [ 0 ] . PropertyName == nameof ( AppEntityWithNavigations . OneToOne ) &&
542
- x . EntityChanges [ 1 ] . PropertyChanges [ 0 ] . PropertyTypeFullName == typeof ( AppEntityWithNavigationChildOneToOne ) . FullName ) ) ;
545
+ x . EntityChanges [ 1 ] . PropertyChanges [ 0 ] . PropertyTypeFullName == typeof ( AppEntityWithNavigationChildOneToOne ) . FullName &&
546
+ x . EntityChanges [ 1 ] . PropertyChanges [ 0 ] . OriginalValue == entityId . ToString ( ) &&
547
+ x . EntityChanges [ 1 ] . PropertyChanges [ 0 ] . NewValue == null ) ) ;
543
548
AuditingStore . ClearReceivedCalls ( ) ;
544
549
#pragma warning restore 4014
545
550
551
+ var oneToManyId = "" ;
546
552
using ( var scope = _auditingManager . BeginScope ( ) )
547
553
{
548
554
using ( var uow = _unitOfWorkManager . Begin ( ) )
@@ -561,6 +567,8 @@ public virtual async Task Should_Write_AuditLog_For_Navigation_Changes()
561
567
await repository . UpdateAsync ( entity ) ;
562
568
await uow . CompleteAsync ( ) ;
563
569
await scope . SaveAsync ( ) ;
570
+
571
+ oneToManyId = entity . OneToMany . First ( ) . Id . ToString ( ) ;
564
572
}
565
573
}
566
574
@@ -572,36 +580,80 @@ public virtual async Task Should_Write_AuditLog_For_Navigation_Changes()
572
580
x . EntityChanges [ 1 ] . EntityTypeFullName == typeof ( AppEntityWithNavigations ) . FullName &&
573
581
x . EntityChanges [ 1 ] . PropertyChanges . Count == 1 &&
574
582
x . EntityChanges [ 1 ] . PropertyChanges [ 0 ] . PropertyName == nameof ( AppEntityWithNavigations . OneToMany ) &&
575
- x . EntityChanges [ 1 ] . PropertyChanges [ 0 ] . PropertyTypeFullName == typeof ( List < AppEntityWithNavigationChildOneToMany > ) . FullName ) ) ;
583
+ x . EntityChanges [ 1 ] . PropertyChanges [ 0 ] . PropertyTypeFullName == typeof ( List < AppEntityWithNavigationChildOneToMany > ) . FullName &&
584
+ x . EntityChanges [ 1 ] . PropertyChanges [ 0 ] . OriginalValue == null &&
585
+ x . EntityChanges [ 1 ] . PropertyChanges [ 0 ] . NewValue == $ "[\" { oneToManyId } \" ]") ) ;
576
586
AuditingStore . ClearReceivedCalls ( ) ;
577
587
#pragma warning restore 4014
578
588
589
+ var newOneToManyId = "" ;
579
590
using ( var scope = _auditingManager . BeginScope ( ) )
580
591
{
581
592
using ( var uow = _unitOfWorkManager . Begin ( ) )
582
593
{
583
594
var entity = await repository . GetAsync ( entityId ) ;
584
595
585
- entity . OneToMany = null ;
596
+ entity . OneToMany . Add ( new AppEntityWithNavigationChildOneToMany
597
+ {
598
+ AppEntityWithNavigationId = entity . Id ,
599
+ ChildName = "ChildName2"
600
+ } ) ;
586
601
587
602
await repository . UpdateAsync ( entity ) ;
588
603
await uow . CompleteAsync ( ) ;
589
604
await scope . SaveAsync ( ) ;
605
+
606
+ newOneToManyId = JsonSerializer . Serialize ( entity . OneToMany . Select ( x => x . Id ) . ToList ( ) ) ;
590
607
}
591
608
}
592
609
593
610
#pragma warning disable 4014
594
611
AuditingStore . Received ( ) . SaveAsync ( Arg . Is < AuditLogInfo > ( x => x . EntityChanges . Count == 2 &&
595
- x . EntityChanges [ 0 ] . ChangeType == EntityChangeType . Deleted &&
612
+ x . EntityChanges [ 0 ] . ChangeType == EntityChangeType . Created &&
596
613
x . EntityChanges [ 0 ] . EntityTypeFullName == typeof ( AppEntityWithNavigationChildOneToMany ) . FullName &&
597
614
x . EntityChanges [ 1 ] . ChangeType == EntityChangeType . Updated &&
598
615
x . EntityChanges [ 1 ] . EntityTypeFullName == typeof ( AppEntityWithNavigations ) . FullName &&
599
616
x . EntityChanges [ 1 ] . PropertyChanges . Count == 1 &&
600
617
x . EntityChanges [ 1 ] . PropertyChanges [ 0 ] . PropertyName == nameof ( AppEntityWithNavigations . OneToMany ) &&
601
- x . EntityChanges [ 1 ] . PropertyChanges [ 0 ] . PropertyTypeFullName == typeof ( List < AppEntityWithNavigationChildOneToMany > ) . FullName ) ) ;
618
+ x . EntityChanges [ 1 ] . PropertyChanges [ 0 ] . PropertyTypeFullName == typeof ( List < AppEntityWithNavigationChildOneToMany > ) . FullName &&
619
+ x . EntityChanges [ 1 ] . PropertyChanges [ 0 ] . OriginalValue == $ "[\" { oneToManyId } \" ]" &&
620
+ x . EntityChanges [ 1 ] . PropertyChanges [ 0 ] . NewValue == newOneToManyId ) ) ;
621
+ AuditingStore . ClearReceivedCalls ( ) ;
622
+ #pragma warning restore 4014
623
+
624
+ using ( var scope = _auditingManager . BeginScope ( ) )
625
+ {
626
+ using ( var uow = _unitOfWorkManager . Begin ( ) )
627
+ {
628
+ var entity = await repository . GetAsync ( entityId ) ;
629
+
630
+ newOneToManyId = JsonSerializer . Serialize ( entity . OneToMany . Select ( x => x . Id ) . ToList ( ) ) ;
631
+
632
+ entity . OneToMany = null ;
633
+
634
+ await repository . UpdateAsync ( entity ) ;
635
+ await uow . CompleteAsync ( ) ;
636
+ await scope . SaveAsync ( ) ;
637
+ }
638
+ }
639
+
640
+ #pragma warning disable 4014
641
+ AuditingStore . Received ( ) . SaveAsync ( Arg . Is < AuditLogInfo > ( x => x . EntityChanges . Count == 3 &&
642
+ x . EntityChanges [ 0 ] . ChangeType == EntityChangeType . Deleted &&
643
+ x . EntityChanges [ 0 ] . EntityTypeFullName == typeof ( AppEntityWithNavigationChildOneToMany ) . FullName &&
644
+ x . EntityChanges [ 1 ] . ChangeType == EntityChangeType . Deleted &&
645
+ x . EntityChanges [ 1 ] . EntityTypeFullName == typeof ( AppEntityWithNavigationChildOneToMany ) . FullName &&
646
+ x . EntityChanges [ 2 ] . ChangeType == EntityChangeType . Updated &&
647
+ x . EntityChanges [ 2 ] . EntityTypeFullName == typeof ( AppEntityWithNavigations ) . FullName &&
648
+ x . EntityChanges [ 2 ] . PropertyChanges . Count == 1 &&
649
+ x . EntityChanges [ 2 ] . PropertyChanges [ 0 ] . PropertyName == nameof ( AppEntityWithNavigations . OneToMany ) &&
650
+ x . EntityChanges [ 2 ] . PropertyChanges [ 0 ] . PropertyTypeFullName == typeof ( List < AppEntityWithNavigationChildOneToMany > ) . FullName &&
651
+ x . EntityChanges [ 2 ] . PropertyChanges [ 0 ] . OriginalValue == newOneToManyId &&
652
+ x . EntityChanges [ 2 ] . PropertyChanges [ 0 ] . NewValue == null ) ) ;
602
653
AuditingStore . ClearReceivedCalls ( ) ;
603
654
#pragma warning restore 4014
604
655
656
+ var manyToManyId = "" ;
605
657
using ( var scope = _auditingManager . BeginScope ( ) )
606
658
{
607
659
using ( var uow = _unitOfWorkManager . Begin ( ) )
@@ -619,6 +671,8 @@ public virtual async Task Should_Write_AuditLog_For_Navigation_Changes()
619
671
await repository . UpdateAsync ( entity ) ;
620
672
await uow . CompleteAsync ( ) ;
621
673
await scope . SaveAsync ( ) ;
674
+
675
+ manyToManyId = entity . ManyToMany . First ( ) . Id . ToString ( ) ;
622
676
}
623
677
}
624
678
@@ -630,7 +684,9 @@ public virtual async Task Should_Write_AuditLog_For_Navigation_Changes()
630
684
x . EntityChanges [ 1 ] . EntityTypeFullName == typeof ( AppEntityWithNavigations ) . FullName &&
631
685
x . EntityChanges [ 1 ] . PropertyChanges . Count == 1 &&
632
686
x . EntityChanges [ 1 ] . PropertyChanges [ 0 ] . PropertyName == nameof ( AppEntityWithNavigations . ManyToMany ) &&
633
- x . EntityChanges [ 1 ] . PropertyChanges [ 0 ] . PropertyTypeFullName == typeof ( List < AppEntityWithNavigationChildManyToMany > ) . FullName ) ) ;
687
+ x . EntityChanges [ 1 ] . PropertyChanges [ 0 ] . PropertyTypeFullName == typeof ( List < AppEntityWithNavigationChildManyToMany > ) . FullName &&
688
+ x . EntityChanges [ 1 ] . PropertyChanges [ 0 ] . OriginalValue == null &&
689
+ x . EntityChanges [ 1 ] . PropertyChanges [ 0 ] . NewValue == $ "[\" { manyToManyId } \" ]") ) ;
634
690
635
691
#pragma warning restore 4014
636
692
@@ -655,6 +711,8 @@ public virtual async Task Should_Write_AuditLog_For_Navigation_Changes()
655
711
x . EntityChanges [ 0 ] . PropertyChanges . Count == 1 &&
656
712
x . EntityChanges [ 0 ] . PropertyChanges [ 0 ] . PropertyName == nameof ( AppEntityWithNavigations . ManyToMany ) &&
657
713
x . EntityChanges [ 0 ] . PropertyChanges [ 0 ] . PropertyTypeFullName == typeof ( List < AppEntityWithNavigationChildManyToMany > ) . FullName &&
714
+ x . EntityChanges [ 0 ] . PropertyChanges [ 0 ] . OriginalValue == $ "[\" { manyToManyId } \" ]" &&
715
+ x . EntityChanges [ 0 ] . PropertyChanges [ 0 ] . NewValue == null &&
658
716
659
717
x . EntityChanges [ 1 ] . ChangeType == EntityChangeType . Updated &&
660
718
x . EntityChanges [ 1 ] . EntityTypeFullName == typeof ( AppEntityWithNavigationChildManyToMany ) . FullName &&
0 commit comments