/
RepositoryTests.cs
154 lines (133 loc) · 5.03 KB
/
RepositoryTests.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using TF.ProductMicroservice;
namespace ProductModuleTests
{
[TestClass]
public class RepositoryTests
{
private static IProductRepository repository
{
get
{
return new ProductRepository();
}
}
private const int SIMPLE_PRODUCTS_COUNT = 10;
private const int COMPLEX_PRODUCT_CHILDS = 10;
[TestMethod]
public void SimpleCreateTest()
{
foreach (var product in GetSimpleProductsCollection(SIMPLE_PRODUCTS_COUNT))
{
var saveResult = repository.Save(product);
TestProductRecursive(product, saveResult);
TestProductFromDB(product, saveResult.Id);
}
}
[TestMethod]
public void CreateComplexObjectTest()
{
var complexProduct = GetComplexProduct(COMPLEX_PRODUCT_CHILDS);
var saveResult = repository.Save(complexProduct);
TestProductRecursive(complexProduct, saveResult);
TestProductFromDB(complexProduct, saveResult.Id);
}
[TestMethod]
public void UpdateTest()
{
var product = GetSimpleProduct();
var saveResult = repository.Save(product);
saveResult.Name = string.Format("UpdatedTestProduct_{0}", Guid.NewGuid());
saveResult.Type = string.Format("UpdatedType_{0}", Guid.NewGuid());
var updateResult = repository.Save(saveResult);
TestProductRecursive(saveResult, updateResult);
TestProductFromDB(saveResult, saveResult.Id);
}
[TestMethod]
public void DeleteTest()
{
var product = GetComplexProduct(1);
var saveResult = repository.Save(product);
repository.Delete(saveResult.Id);
var getResult = repository.Get(saveResult.Id);
Assert.IsNull(getResult);
}
[TestMethod]
public void CascadeDeleteTest()
{
var product = GetComplexProduct(COMPLEX_PRODUCT_CHILDS);
var saveResult = repository.Save(product);
Product getResult = null;
foreach (var child in saveResult.ChildProducts)
{
repository.Delete(child.Id);
getResult = repository.Get(child.Id);
Assert.IsNull(getResult);
}
repository.Delete(saveResult.Id);
getResult = repository.Get(saveResult.Id);
Assert.IsNull(getResult);
}
private static void TestProduct(Product expected, Product actual)
{
Assert.AreEqual(expected.Name, actual.Name);
Assert.AreEqual(expected.Type, actual.Type);
if (expected.Parent != null)
{
Assert.AreEqual(expected.Parent.Id, actual.Parent.Id);
}
Assert.AreEqual(expected.ChildProducts.Count, actual.ChildProducts.Count);
Assert.IsNotNull(actual.Id);
}
public static void TestProductRecursive(Product expected, Product actual)
{
TestProduct(expected, actual);
if (actual.ChildProducts.Count != 0)
{
var expectedChildProducts = (List<Product>)expected.ChildProducts;
var actualChildProducts = (List<Product>)actual.ChildProducts;
for (int childIdx = 0; childIdx < expectedChildProducts.Count; childIdx++)
{
TestProductRecursive(expectedChildProducts[childIdx], actualChildProducts[childIdx]);
}
}
}
private static List<Product> SortByName(IEnumerable<Product> Products)
{
return Products.OrderBy(p => p.Name).ToList();
}
public static Product GetSimpleProduct()
{
return new Product() {
Name = string.Format("SimpleTestProduct_{0}", Guid.NewGuid()),
Type =string.Format("TestType_{0}", Guid.NewGuid())
};
}
private static List<Product> GetSimpleProductsCollection(int count)
{
var products = new List<Product>();
for (var productIdx = 0; productIdx < count; productIdx++)
{
products.Add(GetSimpleProduct());
}
return products;
}
public static Product GetComplexProduct(int childCount)
{
var product = new Product() {
Name = string.Format("ComplexTestProduct_{0}", Guid.NewGuid()),
Type = string.Format("ComplexType_{0}", Guid.NewGuid())
};
product.ChildProducts = GetSimpleProductsCollection(childCount);
return product;
}
private void TestProductFromDB(Product product, Guid Id)
{
var getResult = repository.Get(Id);
TestProductRecursive(product, getResult);
}
}
}