Added RedirectUsingNamedRoute
[castle.git] / ActiveRecord / Castle.ActiveRecord.Tests / ActiveRecordGenericsTestCase.cs
blobf36d7842e998f47205e9867209c2e22bd31bd8a2
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.ActiveRecord.Tests
17 using System.Collections.Generic;
18 using System.Threading;
19 using Castle.ActiveRecord.Queries;
20 using Castle.ActiveRecord.Tests.Model.GenericModel;
21 using NHibernate.Expressions;
22 using NUnit.Framework;
24 [TestFixture]
25 public class ActiveRecordGenericsTestCase : AbstractActiveRecordTest
27 [SetUp]
28 public void Setup()
30 ActiveRecordStarter.ResetInitializationFlag();
32 ActiveRecordStarter.Initialize(GetConfigSource(),
33 typeof(Blog),
34 typeof(Post),
35 typeof(Company),
36 typeof(Award),
37 typeof(Employee),
38 typeof(Person));
39 Recreate();
41 Post.DeleteAll();
42 Blog.DeleteAll();
43 Company.DeleteAll();
44 Award.DeleteAll();
45 Employee.DeleteAll();
48 [Test]
49 public void SimpleOperations()
51 Blog[] blogs = Blog.FindAll();
53 Assert.IsNotNull(blogs);
54 Assert.AreEqual(0, blogs.Length);
56 Blog blog = new Blog();
57 blog.Name = "hammett's blog";
58 blog.Author = "hamilton verissimo";
59 blog.Save();
61 blogs = Blog.FindAll();
63 Assert.IsNotNull(blogs);
64 Assert.AreEqual(1, blogs.Length);
66 Blog retrieved = blogs[0];
67 Assert.IsNotNull(retrieved);
69 Assert.AreEqual(blog.Name, retrieved.Name);
70 Assert.AreEqual(blog.Author, retrieved.Author);
73 [Test]
74 public void ExistsTest()
76 Blog[] blogs = Blog.FindAll();
78 Assert.IsNotNull(blogs);
79 Assert.AreEqual(0, blogs.Length);
81 Blog blog = new Blog();
82 blog.Name = "hammett's blog";
83 blog.Author = "hamilton verissimo";
84 blog.Save();
86 Assert.IsTrue(blog.Id > 0);
87 Assert.IsTrue(Blog.Exists(blog.Id));
89 blog = new Blog();
90 blog.Name = "chad's blog";
91 blog.Author = "chad humphries";
92 blog.Save();
94 Assert.IsTrue(Blog.Exists(blog.Id));
96 Assert.IsFalse(Blog.Exists(1000));
99 [Test]
100 public void ExistsByCriterion()
102 Blog[] blogs = Blog.FindAll();
104 Assert.IsNotNull(blogs);
105 Assert.AreEqual(0, blogs.Length);
107 Blog blog = new Blog();
108 blog.Name = "hammett's blog";
109 blog.Author = "hamilton verissimo";
110 blog.Save();
112 Assert.IsTrue(blog.Id > 0);
113 Assert.IsTrue(Blog.Exists(
114 Expression.Eq("Name", blog.Name),
115 Expression.Eq("Author", blog.Author)));
117 blog = new Blog();
118 blog.Name = "chad's blog";
119 blog.Author = "chad humphries";
120 blog.Save();
122 Assert.IsTrue(Blog.Exists(
123 Expression.Eq("Name", blog.Name),
124 Expression.Eq("Author", blog.Author)));
126 Assert.IsFalse(Blog.Exists(
127 Expression.Eq("Name", "/\ndrew's Blog"),
128 Expression.Eq("Author", "Andrew Peters")));
131 [Test]
132 public void SlicedOperation()
134 Blog blog = new Blog();
135 blog.Name = "hammett's blog";
136 blog.Author = "hamilton verissimo";
137 blog.Save();
139 Post post1 = new Post(blog, "title1", "contents", "category1");
140 Post post2 = new Post(blog, "title2", "contents", "category2");
141 Post post3 = new Post(blog, "title3", "contents", "category3");
143 post1.Save();
144 post2.Save();
145 post3.Published = true;
146 post3.Save();
148 Post[] posts = Post.SlicedFindAll(1, 2, Expression.Eq("Blog", blog));
149 Assert.AreEqual(2, posts.Length);
152 [Test]
153 public void SimpleOperations2()
155 Blog[] blogs = Blog.FindAll();
157 Assert.IsNotNull(blogs);
158 Assert.AreEqual(0, blogs.Length);
160 Blog blog = new Blog();
161 blog.Name = "hammett's blog";
162 blog.Author = "hamilton verissimo";
163 blog.Create();
165 blogs = Blog.FindAll();
166 Assert.AreEqual(blog.Name, blogs[0].Name);
167 Assert.AreEqual(blog.Author, blogs[0].Author);
169 Assert.IsNotNull(blogs);
170 Assert.AreEqual(1, blogs.Length);
172 blog.Name = "something else1";
173 blog.Author = "something else2";
174 blog.Update();
176 blogs = Blog.FindAll();
178 Assert.IsNotNull(blogs);
179 Assert.AreEqual(1, blogs.Length);
180 Assert.AreEqual(blog.Name, blogs[0].Name);
181 Assert.AreEqual(blog.Author, blogs[0].Author);
185 [Test]
186 public void ComponentAttribute()
188 Company company = new Company("Castle Corp.");
189 company.Address = new PostalAddress(
190 "Embau St., 102", "Sao Paulo", "SP", "040390-060");
191 company.Save();
193 Company[] companies = Company.FindAll();
194 Assert.IsNotNull(companies);
195 Assert.AreEqual(1, companies.Length);
197 Company corp = companies[0];
198 Assert.IsNotNull(corp.Address);
199 Assert.AreEqual(corp.Address.Address, company.Address.Address);
200 Assert.AreEqual(corp.Address.City, company.Address.City);
201 Assert.AreEqual(corp.Address.State, company.Address.State);
202 Assert.AreEqual(corp.Address.ZipCode, company.Address.ZipCode);
205 [Test]
206 public void RelationsOneToMany()
208 Blog blog = new Blog();
209 blog.Name = "hammett's blog";
210 blog.Author = "hamilton verissimo";
211 blog.Save();
213 Post post1 = new Post(blog, "title1", "contents", "category1");
214 Post post2 = new Post(blog, "title2", "contents", "category2");
216 post1.Save();
217 post2.Save();
219 blog = Blog.Find(blog.Id);
221 Assert.IsNotNull(blog);
222 Assert.IsNotNull(blog.Posts, "posts collection is null");
223 Assert.AreEqual(2, blog.Posts.Count);
225 foreach(Post post in blog.Posts)
227 Assert.AreEqual(blog.Id, post.Blog.Id);
231 [Test]
232 public void RelationsOneToManyWithWhereAndOrder()
234 Blog blog = new Blog();
235 blog.Name = "hammett's blog";
236 blog.Author = "hamilton verissimo";
237 blog.Save();
239 Post post1 = new Post(blog, "title1", "contents", "category1");
240 Post post2 = new Post(blog, "title2", "contents", "category2");
241 Post post3 = new Post(blog, "title3", "contents", "category3");
243 post1.Save();
244 Thread.Sleep(1000); // Its a smalldatetime (small precision)
245 post2.Save();
246 Thread.Sleep(1000); // Its a smalldatetime (small precision)
247 post3.Published = true;
248 post3.Save();
250 blog = Blog.Find(blog.Id);
252 Assert.IsNotNull(blog);
253 Assert.AreEqual(2, blog.UnPublishedPosts.Count);
254 Assert.AreEqual(1, blog.PublishedPosts.Count);
256 Assert.AreEqual(3, blog.RecentPosts.Count);
257 Assert.AreEqual(post3.Id, ((Post) blog.RecentPosts[0]).Id);
258 Assert.AreEqual(post2.Id, ((Post) blog.RecentPosts[1]).Id);
259 Assert.AreEqual(post1.Id, ((Post) blog.RecentPosts[2]).Id);
263 [Test]
264 public void TestExpressionQuerySubProperty()
266 Blog blog = new Blog();
267 blog.Name = "hammett's blog";
268 blog.Author = "hamilton verissimo";
269 blog.Save();
271 Blog blog2 = new Blog();
272 blog2.Name = "hammett's blog other blog";
273 blog2.Author = "hamilton verissimo 2";
274 blog2.Save();
276 Post post1 = new Post(blog, "title1", "contents", "category1");
277 Post post2 = new Post(blog, "title2", "contents", "category2");
278 Post post3 = new Post(blog, "title3", "contents", "category3");
280 Post post21 = new Post(blog2, "title21", "contents", "category21");
281 Post post22 = new Post(blog2, "title22", "contents", "category22");
282 Post post23 = new Post(blog2, "title23", "contents", "category23");
284 post1.Save();
285 post2.Save();
286 post3.Save();
288 post21.Save();
289 post22.Save();
290 post23.Save();
292 //no idea how to make this style of query work.
293 //Post[] posts = Post.FindAll(
294 // Expression.Eq("Blog.Name", blog2.Name)
295 // );
296 //Assert.IsTrue(posts.Length > 0);
298 SimpleQuery<Post> q =
299 new SimpleQuery<Post>(typeof(Post), "from Post p where p.Id = ? or p.Blog.Name = ?", 1, "hammett's blog other blog");
301 Post[] p = q.Execute();
303 Assert.IsTrue(p.Length > 0);
306 [Test]
307 public void RelationsOneToOne()
309 Employee emp = new Employee();
310 emp.FirstName = "john";
311 emp.LastName = "doe";
312 emp.Save();
314 Assert.AreEqual(1, Employee.FindAll().Length);
316 Award award = new Award(emp);
317 award.Description = "Invisible employee";
318 award.Save();
320 Assert.AreEqual(1, Award.FindAll().Length);
322 Employee emp2 = Employee.Find(emp.ID);
323 Assert.IsNotNull(emp2);
324 Assert.IsNotNull(emp2.Award);
325 Assert.AreEqual(emp.FirstName, emp2.FirstName);
326 Assert.AreEqual(emp.LastName, emp2.LastName);
327 Assert.AreEqual(award.Description, emp2.Award.Description);
330 [Test]
331 [ExpectedException(typeof(NotFoundException))]
332 public void FindLoad()
334 Blog.Find(1);
337 [Test]
338 public void SaveUpdate()
340 Blog[] blogs = Blog.FindAll();
342 Assert.IsNotNull(blogs);
343 Assert.AreEqual(0, blogs.Length);
345 Blog blog = new Blog();
346 blog.Name = "hammett's blog";
347 blog.Author = "hamilton verissimo";
348 blog.Save();
350 blogs = Blog.FindAll();
352 Assert.IsNotNull(blogs);
353 Assert.AreEqual(1, blogs.Length);
355 blog.Name = "Something else";
356 blog.Author = "changed too";
357 blog.Save();
359 blogs = Blog.FindAll();
361 Assert.IsNotNull(blogs);
362 Assert.AreEqual(1, blogs.Length);
364 Assert.AreEqual(blog.Name, blogs[0].Name);
365 Assert.AreEqual(blog.Author, blogs[0].Author);
368 [Test]
369 public void Delete()
371 Blog[] blogs = Blog.FindAll();
373 Assert.IsNotNull(blogs);
374 Assert.AreEqual(0, blogs.Length);
376 Blog blog = new Blog();
377 blog.Name = "hammett's blog";
378 blog.Author = "hamilton verissimo";
379 blog.Save();
381 blogs = Blog.FindAll();
383 Assert.IsNotNull(blogs);
384 Assert.AreEqual(1, blogs.Length);
386 blog.Delete();
388 blogs = Blog.FindAll();
390 Assert.IsNotNull(blogs);
391 Assert.AreEqual(0, blogs.Length);
394 [Test]
395 public void ScalarProjectionQueryTest()
397 Blog blog = new Blog();
398 blog.Name = "hammett's blog";
399 blog.Author = "hamilton verissimo";
400 blog.Save();
402 ScalarProjectionQuery<Blog, int> proj = new ScalarProjectionQuery<Blog, int>(Projections.RowCount());
403 int rowCount = proj.Execute();
404 Assert.AreEqual(1, rowCount);
408 [Test]
409 public void UnTypedProjectionQueryTest()
411 Blog blog = new Blog();
412 blog.Name = "hammett's blog";
413 blog.Author = "hamilton verissimo";
414 blog.Save();
416 ProjectionQuery<Blog> proj = new ProjectionQuery<Blog>(
417 Projections.ProjectionList()
418 .Add(Projections.Property("Name"))
419 .Add(Projections.Property("Author")));
420 IList<object[]> results = proj.Execute();
421 Assert.AreEqual(blog.Name, results[0][0]);
422 Assert.AreEqual(blog.Author, results[0][1]);
426 [Test]
427 public void TypedProjectionQueryTest()
429 Blog blog = new Blog();
430 blog.Name = "hammett's blog";
431 blog.Author = "hamilton verissimo";
432 blog.Save();
434 ProjectionQuery<Blog, KeyValuePair<string, string>> proj = new ProjectionQuery<Blog, KeyValuePair<string, string>>(
435 Projections.ProjectionList()
436 .Add(Projections.Property("Name"))
437 .Add(Projections.Property("Author")));
438 IList<KeyValuePair<string, string>> results = proj.Execute();
439 Assert.AreEqual(blog.Name, results[0].Key);
440 Assert.AreEqual(blog.Author, results[0].Value);
444 [Test]
445 public void UseBlogWithGenericPostCollection()
447 Blog blog = new Blog();
448 blog.Name = "hammett's blog";
449 blog.Author = "hamilton verissimo";
451 blog.Save();
453 Post p = new Post(blog, "a", "b", "c");
454 blog.Posts.Add(p);
456 p.Save();
458 Blog fromDB = Blog.Find(blog.Id);
459 Assert.AreEqual(1, fromDB.Posts.Count);
462 [Test]
463 public void AbstractClassTableName()
465 ActiveRecordStarter.ResetInitializationFlag();
467 ActiveRecordStarter.Initialize(GetConfigSource(),
468 typeof(AbstractClass<>),
469 typeof(ConcreteClass));
470 Recreate();
472 ConcreteClass c = new ConcreteClass();
474 c.Save();
476 Assert.IsTrue(ConcreteClass.FindAll().Length == 1);