forked from urfnet/URF.Core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrackableRepositoryTest.cs
140 lines (116 loc) · 5.11 KB
/
TrackableRepositoryTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using TrackableEntities.Common.Core;
using URF.Core.Abstractions.Trackable;
using URF.Core.EF.Tests.Contexts;
using URF.Core.EF.Tests.Models;
using URF.Core.EF.Trackable;
using Xunit;
namespace URF.Core.EF.Tests
{
[Collection(nameof(NorthwindDbContext))]
public class TrackableRepositoryTest
{
private readonly NorthwindDbContextFixture _fixture;
private readonly UnitOfWork _unitOfWork;
public TrackableRepositoryTest(NorthwindDbContextFixture fixture)
{
_fixture = fixture;
_fixture.Initialize(true, () =>
{
_fixture.Context.Categories.AddRange(Factory.Categories());
_fixture.Context.Products.AddRange(Factory.Products());
_fixture.Context.Customers.AddRange(Factory.Customers());
_fixture.Context.Orders.AddRange(Factory.Orders());
_fixture.Context.OrderDetails.AddRange(Factory.OrderDetails());
_fixture.Context.SaveChanges();
});
_unitOfWork = new UnitOfWork(_fixture.Context);
}
[Fact]
public void Insert_Should_Set_Entity_State_to_Added()
{
// Arrange
ITrackableRepository<Product> productsRepo = new TrackableRepository<Product>(_fixture.Context);
var product = new Product {ProductId = 80, ProductName = "Product 80", UnitPrice = 40, CategoryId = 1};
// Act
productsRepo.Insert(product);
// Assert
Assert.Equal(TrackingState.Added, product.TrackingState);
}
[Fact]
public void Update_Should_Set_Entity_State_to_Modified()
{
// Arrange
ITrackableRepository<Product> productsRepo = new TrackableRepository<Product>(_fixture.Context);
var product = new Product { ProductId = 81, ProductName = "Product 81", UnitPrice = 40, CategoryId = 1 };
// Act
productsRepo.Update(product);
// Assert
Assert.Equal(TrackingState.Modified, product.TrackingState);
}
[Fact]
public void Delete_Should_Set_Entity_State_to_Deleted()
{
// Arrange
ITrackableRepository<Product> productsRepo = new TrackableRepository<Product>(_fixture.Context);
var product = new Product { ProductId = 4, ProductName = "Product 4", UnitPrice = 40, CategoryId = 1 };
// Act
productsRepo.Delete(product);
// Assert
Assert.Equal(TrackingState.Deleted, product.TrackingState);
}
[Fact]
public async Task DeleteAsync_Should_Set_Entity_State_to_Deleted()
{
// Arrange
ITrackableRepository<Product> productsRepo = new TrackableRepository<Product>(_fixture.Context);
var product = await productsRepo.FindAsync(1);
// Act
await productsRepo.DeleteAsync(1);
// Assert
Assert.Equal(TrackingState.Deleted, product.TrackingState);
}
[Fact]
public async Task ApplyChanges_Should_Set_Graph_States()
{
// Arrange
ITrackableRepository<Order> ordersRepo = new TrackableRepository<Order>(_fixture.Context);
var order = await ordersRepo.FindAsync(10248);
ordersRepo.DetachEntities(order);
// updating object in complex object graph
order.OrderDetails[0].UnitPrice = 17;
order.OrderDetails[0].TrackingState = TrackingState.Modified;
// updating object in complex object graph
order.OrderDetails[1].Quantity = 2;
order.OrderDetails[1].TrackingState = TrackingState.Modified;
// deleting object in complex object graph
order.OrderDetails[2].TrackingState = TrackingState.Deleted;
// adding object in complex object graph
var addedDetail = new OrderDetail
{
//OrderDetailId = 4,
OrderId = 1,
ProductId = 3,
Product = order.OrderDetails[2].Product,
UnitPrice = 40,
Quantity = 400,
TrackingState = TrackingState.Added
};
// Act
order.OrderDetails.Add(addedDetail);
// Act
ordersRepo.ApplyChanges(order);
// Assert
Assert.Equal(EntityState.Unchanged, _fixture.Context.Entry(order).State);
Assert.Equal(EntityState.Modified, _fixture.Context.Entry(order.OrderDetails[0]).State);
Assert.Equal(EntityState.Modified, _fixture.Context.Entry(order.OrderDetails[1]).State);
Assert.Equal(EntityState.Deleted, _fixture.Context.Entry(order.OrderDetails[2]).State);
Assert.Equal(EntityState.Added, _fixture.Context.Entry(order.OrderDetails[3]).State);
// Save changes to object graph with different TrackingStates throughout the object graph
await _unitOfWork.SaveChangesAsync();
}
}
}