Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 344 lines (278 sloc) 11.143 kB
64754ec @emiliotorrens Eliminados metodos de busca por expreciones + Refactor
authored
1 using System;
2 using System.Collections.Generic;
3 using System.Diagnostics;
4 using EtoolTech.MongoDB.Mapper.Exceptions;
5 using MongoDB.Driver.Builders;
6 using NUnit.Framework;
7
8 namespace EtoolTech.MongoDB.Mapper.Test.NUnit
9fff1e4 Refactor
emiliotorrens authored
9 {
d5c024f @emiliotorrens Test Code Cleanup
authored
10 [TestFixture]
9fff1e4 Refactor
emiliotorrens authored
11 public class ExtensionTest
a528a3d @emiliotorrens Refactor Tests
authored
12 {
c9c946b @emiliotorrens Implemento mi MongoTestServer
authored
13
64754ec @emiliotorrens Eliminados metodos de busca por expreciones + Refactor
authored
14 public void TestPerfFillByKeyNormalVsExtensionMethod()
15 {
16 Helper.DropAllCollections();
17
18 //Insert de Paises
19 var c = new Country {Code = "ES", Name = "España"};
20 c.Save();
21
22 Stopwatch timer = Stopwatch.StartNew();
23
24 for (int i = 0; i < 1000000; i++)
25 {
26 var country = new Country();
27 country.FillByKey("ES");
28 }
29 timer.Stop();
30 Console.WriteLine(string.Format("Elapsed para ExtensionMethod: {0}", timer.Elapsed));
31 //Elapsed para ExtensionMethod: 00:04:38.5479462
32
33 timer = Stopwatch.StartNew();
34
35 for (int i = 0; i < 1000000; i++)
36 {
37 MongoMapper.FindByKey<Country>("ES");
38 }
39
40 timer.Stop();
41 Console.WriteLine(string.Format("Elapsed para StaticMethod: {0}", timer.Elapsed));
42 //Elapsed para StaticMethod: 00:04:27.1441065
43 }
44
45 public void TestPerfMongoFindNormalVsExtensionMethods()
46 {
47 Helper.DropAllCollections();
48
49 //Insert de Paises
50 var c = new Country {Code = "ES", Name = "España"};
51 c.Save();
52 c = new Country {Code = "UK", Name = "Reino Unido"};
53 c.Save();
54 c = new Country {Code = "US", Name = "Estados Unidos"};
55 c.Save();
56
57 Stopwatch timer = Stopwatch.StartNew();
58
59 for (int i = 0; i < 1000000; i++)
60 {
61 var countries = new List<Country>();
62 countries.MongoFind(
e5e504d @emiliotorrens MongoQuery<T>
authored
63 Query.Or(MongoQuery<Country>.Eq(co => co.Code, "ES"), MongoQuery<Country>.Eq(co => co.Code, "UK")));
64754ec @emiliotorrens Eliminados metodos de busca por expreciones + Refactor
authored
64 }
65 timer.Stop();
66 Console.WriteLine(string.Format("Elapsed para ExtensionMethod: {0}", timer.Elapsed));
67 //Elapsed para ExtensionMethod: 00:04:29.8042031
68
69 timer = Stopwatch.StartNew();
70
71 for (int i = 0; i < 1000000; i++)
72 {
0d231c9 @emiliotorrens MongoMapperCollection
authored
73 CountryCollection mongoCol = new CountryCollection();
6ea92d6 @emiliotorrens Actualizados todos los Test con la nueva busqueda
authored
74 mongoCol.Find(
e5e504d @emiliotorrens MongoQuery<T>
authored
75 Query.Or(MongoQuery<Country>.Eq(co=>co.Code, "ES"), MongoQuery<Country>.Eq(co => co.Code, "UK")));
6ea92d6 @emiliotorrens Actualizados todos los Test con la nueva busqueda
authored
76 mongoCol.ToList();
64754ec @emiliotorrens Eliminados metodos de busca por expreciones + Refactor
authored
77 }
78
79 timer.Stop();
80 Console.WriteLine(string.Format("Elapsed para StaticMethod: {0}", timer.Elapsed));
81 //Elapsed para StaticMethod: 00:04:10.1821050
82 }
d5c024f @emiliotorrens Test Code Cleanup
authored
83
84 [Test]
9fff1e4 Refactor
emiliotorrens authored
85 public void TestCollectionExtensions()
86 {
87 Helper.DropAllCollections();
88
89 //Insert de Paises
64754ec @emiliotorrens Eliminados metodos de busca por expreciones + Refactor
authored
90 var c = new Country {Code = "ES", Name = "España"};
6b66424 @emiliotorrens Extension Test filtrar por valores de Child
authored
91 c.Save();
64754ec @emiliotorrens Eliminados metodos de busca por expreciones + Refactor
authored
92 c = new Country {Code = "UK", Name = "Reino Unido"};
6b66424 @emiliotorrens Extension Test filtrar por valores de Child
authored
93 c.Save();
64754ec @emiliotorrens Eliminados metodos de busca por expreciones + Refactor
authored
94 c = new Country {Code = "US", Name = "Estados Unidos"};
6b66424 @emiliotorrens Extension Test filtrar por valores de Child
authored
95 c.Save();
9fff1e4 Refactor
emiliotorrens authored
96
64754ec @emiliotorrens Eliminados metodos de busca por expreciones + Refactor
authored
97 var countries = new List<Country>();
9fff1e4 Refactor
emiliotorrens authored
98 countries.MongoFind();
99 Assert.AreEqual(countries.Count, 3);
100
e5e504d @emiliotorrens MongoQuery<T>
authored
101 countries.MongoFind(MongoQuery<Country>.Eq(co => co.Code, "ES"));
9fff1e4 Refactor
emiliotorrens authored
102 Assert.AreEqual(countries.Count, 1);
103 Assert.AreEqual(countries[0].Code, "ES");
104
105 countries.MongoFind(
e5e504d @emiliotorrens MongoQuery<T>
authored
106 Query.Or(MongoQuery<Country>.Eq(co => co.Code, "ES"), MongoQuery<Country>.Eq(co => co.Code, "UK")));
9fff1e4 Refactor
emiliotorrens authored
107 Assert.AreEqual(countries.Count, 2);
d5c024f @emiliotorrens Test Code Cleanup
authored
108 }
109
110 [Test]
111 public void TestDelete()
112 {
113 Helper.DropAllCollections();
9fff1e4 Refactor
emiliotorrens authored
114
d5c024f @emiliotorrens Test Code Cleanup
authored
115 //Insert de Paises
64754ec @emiliotorrens Eliminados metodos de busca por expreciones + Refactor
authored
116 var c = new Country {Code = "ES", Name = "España"};
d5c024f @emiliotorrens Test Code Cleanup
authored
117 c.Save();
118
119 c.FillByKey("ES");
120 c.Delete();
121
122 //TODO: Pruebas Replica Set
123 //System.Threading.Thread.Sleep(5000);
124
64754ec @emiliotorrens Eliminados metodos de busca por expreciones + Refactor
authored
125 var country = new List<Country>();
d5c024f @emiliotorrens Test Code Cleanup
authored
126 country.MongoFind();
127 Assert.AreEqual(0, country.Count);
9fff1e4 Refactor
emiliotorrens authored
128 }
129
d5c024f @emiliotorrens Test Code Cleanup
authored
130 [Test]
9fff1e4 Refactor
emiliotorrens authored
131 public void TestFillByKeyExtension()
132 {
133 Helper.DropAllCollections();
d5c024f @emiliotorrens Test Code Cleanup
authored
134
9fff1e4 Refactor
emiliotorrens authored
135 //Insert de Paises
64754ec @emiliotorrens Eliminados metodos de busca por expreciones + Refactor
authored
136 var c = new Country {Code = "ES", Name = "España"};
bed7ead @emiliotorrens Eliminado el <T> en los metodos no estaticos de la clase Base
authored
137 c.Save();
d5c024f @emiliotorrens Test Code Cleanup
authored
138
139 var country = new Country();
9fff1e4 Refactor
emiliotorrens authored
140 country.FillByKey("ES");
141 Assert.AreEqual(country.Code, "ES");
142
143 //Insert de personas
d5c024f @emiliotorrens Test Code Cleanup
authored
144 var p = new Person
145 {
146 Name = "Pepito Perez",
147 Age = 35,
148 BirthDate = DateTime.Now.AddDays(57).AddYears(-35),
149 Married = true,
150 Country = "ES",
151 BankBalance = decimal.Parse("3500,00")
152 };
153
154 p.Childs.Add(
64754ec @emiliotorrens Eliminados metodos de busca por expreciones + Refactor
authored
155 new Child {ID = 1, Age = 10, BirthDate = DateTime.Now.AddDays(57).AddYears(-10), Name = "Juan Perez"});
d5c024f @emiliotorrens Test Code Cleanup
authored
156 p.Childs.Add(
64754ec @emiliotorrens Eliminados metodos de busca por expreciones + Refactor
authored
157 new Child {ID = 2, Age = 7, BirthDate = DateTime.Now.AddDays(57).AddYears(-7), Name = "Ana Perez"});
9fff1e4 Refactor
emiliotorrens authored
158
bed7ead @emiliotorrens Eliminado el <T> en los metodos no estaticos de la clase Base
authored
159 p.Save();
9fff1e4 Refactor
emiliotorrens authored
160
b86c9c3 @emiliotorrens Compatibilidad rota: Cambio nombres de campos (MongoMapper_Id es m_id…
authored
161 long id = p.m_id;
9fff1e4 Refactor
emiliotorrens authored
162
163 p = new Person();
164 p.FillByKey(id);
165 }
166
d5c024f @emiliotorrens Test Code Cleanup
authored
167 [Test]
9fff1e4 Refactor
emiliotorrens authored
168 public void TestInsert()
169 {
170 Helper.DropAllCollections();
171
172 //Insert de Paises
64754ec @emiliotorrens Eliminados metodos de busca por expreciones + Refactor
authored
173 var c = new Country {Code = "es", Name = "España"};
9fff1e4 Refactor
emiliotorrens authored
174 try
175 {
176 c.Save();
177 }
178 catch (Exception ex)
179 {
64754ec @emiliotorrens Eliminados metodos de busca por expreciones + Refactor
authored
180 Assert.AreEqual(ex.GetBaseException().GetType(), typeof (ValidatePropertyException));
9fff1e4 Refactor
emiliotorrens authored
181 c.Code = "ES";
182 c.Save();
183 }
184
64754ec @emiliotorrens Eliminados metodos de busca por expreciones + Refactor
authored
185 c = new Country {Code = "UK", Name = "Reino Unido"};
9fff1e4 Refactor
emiliotorrens authored
186 c.Save();
187
64754ec @emiliotorrens Eliminados metodos de busca por expreciones + Refactor
authored
188 c = new Country {Code = "UK", Name = "Reino Unido"};
9fff1e4 Refactor
emiliotorrens authored
189 try
190 {
191 c.Save();
192 }
193 catch (Exception ex)
194 {
64754ec @emiliotorrens Eliminados metodos de busca por expreciones + Refactor
authored
195 Assert.AreEqual(ex.GetBaseException().GetType(), typeof (DuplicateKeyException));
9fff1e4 Refactor
emiliotorrens authored
196 }
197
64754ec @emiliotorrens Eliminados metodos de busca por expreciones + Refactor
authored
198 c = new Country {Code = "US", Name = "Estados Unidos"};
9fff1e4 Refactor
emiliotorrens authored
199 c.Save();
200
64754ec @emiliotorrens Eliminados metodos de busca por expreciones + Refactor
authored
201 var Countries = new List<Country>();
b91f1c7 @emiliotorrens INTERNALTOOLS-60 Control de filtros para DefaultValues e IgnoreIfDefault
authored
202 Countries.MongoFind(C=>C.Code, "ES");
9fff1e4 Refactor
emiliotorrens authored
203 Assert.AreEqual(Countries.Count, 1);
204
b91f1c7 @emiliotorrens INTERNALTOOLS-60 Control de filtros para DefaultValues e IgnoreIfDefault
authored
205 Countries.MongoFind(C=>C.Code, "UK");
9fff1e4 Refactor
emiliotorrens authored
206 Assert.AreEqual(Countries.Count, 1);
207
b91f1c7 @emiliotorrens INTERNALTOOLS-60 Control de filtros para DefaultValues e IgnoreIfDefault
authored
208 Countries.MongoFind(C=>C.Code, "US");
9fff1e4 Refactor
emiliotorrens authored
209 Assert.AreEqual(Countries.Count, 1);
210
211 Countries.MongoFind();
212 Assert.AreEqual(Countries.Count, 3);
213
214 //Insert de personas
d5c024f @emiliotorrens Test Code Cleanup
authored
215 var p = new Person
216 {
217 Name = "Pepito Perez",
218 Age = 35,
219 BirthDate = DateTime.Now.AddDays(57).AddYears(-35),
220 Married = true,
221 Country = "ES",
222 BankBalance = decimal.Parse("3500,00")
223 };
224
225 p.Childs.Add(
64754ec @emiliotorrens Eliminados metodos de busca por expreciones + Refactor
authored
226 new Child {ID = 1, Age = 10, BirthDate = DateTime.Now.AddDays(57).AddYears(-10), Name = "Juan Perez"});
d5c024f @emiliotorrens Test Code Cleanup
authored
227 p.Childs.Add(
64754ec @emiliotorrens Eliminados metodos de busca por expreciones + Refactor
authored
228 new Child {ID = 2, Age = 7, BirthDate = DateTime.Now.AddDays(57).AddYears(-7), Name = "Ana Perez"});
9fff1e4 Refactor
emiliotorrens authored
229
230 p.Save();
231
232 p = new Person
d5c024f @emiliotorrens Test Code Cleanup
authored
233 {
234 Name = "Juanito Sanchez",
235 Age = 25,
236 BirthDate = DateTime.Now.AddDays(52).AddYears(-38),
237 Married = true,
238 Country = "ES",
239 BankBalance = decimal.Parse("1500,00")
240 };
241
242 p.Childs.Add(
64754ec @emiliotorrens Eliminados metodos de busca por expreciones + Refactor
authored
243 new Child {ID = 1, Age = 5, BirthDate = DateTime.Now.AddDays(7).AddYears(-5), Name = "Toni Sanchez"});
9fff1e4 Refactor
emiliotorrens authored
244
245 p.Save();
246
247 p = new Person
d5c024f @emiliotorrens Test Code Cleanup
authored
248 {
249 Name = "Andres Perez",
250 Age = 25,
251 BirthDate = DateTime.Now.AddDays(25).AddYears(-25),
252 Married = false,
253 Country = "ES",
254 BankBalance = decimal.Parse("500,00")
255 };
9fff1e4 Refactor
emiliotorrens authored
256
257 p.Save();
258
259 p = new Person
d5c024f @emiliotorrens Test Code Cleanup
authored
260 {
261 Name = "Marta Serrano",
262 Age = 28,
263 BirthDate = DateTime.Now.AddDays(28).AddYears(-28),
264 Married = false,
265 Country = "ES",
266 BankBalance = decimal.Parse("9500,00")
267 };
268
269 p.Childs.Add(
64754ec @emiliotorrens Eliminados metodos de busca por expreciones + Refactor
authored
270 new Child {ID = 1, Age = 2, BirthDate = DateTime.Now.AddDays(2).AddYears(-2), Name = "Toni Serrano"});
9fff1e4 Refactor
emiliotorrens authored
271 p.Save();
272
273 p = new Person
d5c024f @emiliotorrens Test Code Cleanup
authored
274 {
275 Name = "Jonh Smith",
276 Age = 21,
277 BirthDate = DateTime.Now.AddDays(21).AddYears(-21),
278 Married = false,
279 Country = "US",
280 BankBalance = decimal.Parse("100,00")
281 };
9fff1e4 Refactor
emiliotorrens authored
282
283 p.Save();
284
64754ec @emiliotorrens Eliminados metodos de busca por expreciones + Refactor
authored
285 var persons = new List<Person>();
d5c024f @emiliotorrens Test Code Cleanup
authored
286 persons.MongoFind();
9fff1e4 Refactor
emiliotorrens authored
287 Assert.AreEqual(persons.Count, 5);
288
6b66424 @emiliotorrens Extension Test filtrar por valores de Child
authored
289 persons.MongoFind("Childs.Age", 2);
290 Assert.AreEqual(persons.Count, 1);
9fff1e4 Refactor
emiliotorrens authored
291 }
f00f6d3 @emiliotorrens Añado ServerUpdate a los Test de Extension Methods
authored
292
d5c024f @emiliotorrens Test Code Cleanup
authored
293 [Test]
294 public void TestSave()
295 {
296 Helper.DropAllCollections();
297
298 //Insert de Paises
64754ec @emiliotorrens Eliminados metodos de busca por expreciones + Refactor
authored
299 var c = new Country {Code = "ES", Name = "España"};
d5c024f @emiliotorrens Test Code Cleanup
authored
300 c.Save();
301
302 var country = new Country();
303 country.FillByKey("ES");
304 Assert.AreEqual(country.Code, "ES");
305 }
306
307 [Test]
308 public void TestServerUdpate()
309 {
310 Helper.DropAllCollections();
311
312 //Insert de Paises
64754ec @emiliotorrens Eliminados metodos de busca por expreciones + Refactor
authored
313 var c = new Country {Code = "ES", Name = "España"};
d5c024f @emiliotorrens Test Code Cleanup
authored
314 c.Save();
bac3bad @emiliotorrens INTERNALTOOLS-60 Control de filtros para DefaultValues e IgnoreIfDefault
authored
315 c.ServerUpdate(Update.Set(MongoMapperHelper.ConvertFieldName("Country","Name"), "España 22"));
d5c024f @emiliotorrens Test Code Cleanup
authored
316
317 Assert.AreEqual(c.Name, "España 22");
318
319 //Insert de personas
320 var p = new Person
321 {
322 Name = "Pepito Perez",
323 Age = 35,
324 BirthDate = DateTime.Now.AddDays(57).AddYears(-35),
325 Married = true,
326 Country = "ES",
327 BankBalance = decimal.Parse("3500,00")
328 };
329
330 p.Childs.Add(
64754ec @emiliotorrens Eliminados metodos de busca por expreciones + Refactor
authored
331 new Child {ID = 1, Age = 10, BirthDate = DateTime.Now.AddDays(57).AddYears(-10), Name = "Juan Perez"});
d5c024f @emiliotorrens Test Code Cleanup
authored
332 p.Save();
333
334 p.ServerUpdate(
335 Update.PushWrapped(
bac3bad @emiliotorrens INTERNALTOOLS-60 Control de filtros para DefaultValues e IgnoreIfDefault
authored
336 MongoMapperHelper.ConvertFieldName("Person","Childs"),
d5c024f @emiliotorrens Test Code Cleanup
authored
337 new Child
64754ec @emiliotorrens Eliminados metodos de busca por expreciones + Refactor
authored
338 {ID = 2, Age = 3, BirthDate = DateTime.Now.AddDays(57).AddYears(-17), Name = "Laura Perez"}));
d5c024f @emiliotorrens Test Code Cleanup
authored
339
340 Assert.AreEqual(p.Childs.Count, 2);
341 Assert.AreEqual(p.Childs[1].Name, "Laura Perez");
9fff1e4 Refactor
emiliotorrens authored
342 }
343 }
d5c024f @emiliotorrens Test Code Cleanup
authored
344 }
Something went wrong with that request. Please try again.