Changed to use Authenticate asp.net event instead of Authorize
[castle.git] / ActiveRecord / Castle.ActiveRecord.Tests / ActiveRecordGenericsTestCase.cs
blob9feaccff3d17c619e59c23d228f876495dca1186
1 // Copyright 2004-2007 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.
17 namespace Castle.ActiveRecord.Tests
19 using System.Collections.Generic;
20 using System.Threading;
21 using Castle.ActiveRecord.Queries;
22 using Castle.ActiveRecord.Tests.Model.GenericModel;
23 using NHibernate.Expression;
24 using NUnit.Framework;
26 [TestFixture]
27 public class ActiveRecordGenericsTestCase : AbstractActiveRecordTest
29 [SetUp]
30 public void Setup()
32 ActiveRecordStarter.ResetInitializationFlag();
34 ActiveRecordStarter.Initialize(GetConfigSource(),
35 typeof(Blog),
36 typeof(Post),
37 typeof(Company),
38 typeof(Award),
39 typeof(Employee),
40 typeof(Person));
41 Recreate();
43 Post.DeleteAll();
44 Blog.DeleteAll();
45 Company.DeleteAll();
46 Award.DeleteAll();
47 Employee.DeleteAll();
50 [Test]
51 public void SimpleOperations()
53 Blog[] blogs = Blog.FindAll();
55 Assert.IsNotNull(blogs);
56 Assert.AreEqual(0, blogs.Length);
58 Blog blog = new Blog();
59 blog.Name = "hammett's blog";
60 blog.Author = "hamilton verissimo";
61 blog.Save();
63 blogs = Blog.FindAll();
65 Assert.IsNotNull(blogs);
66 Assert.AreEqual(1, blogs.Length);
68 Blog retrieved = blogs[0];
69 Assert.IsNotNull(retrieved);
71 Assert.AreEqual(blog.Name, retrieved.Name);
72 Assert.AreEqual(blog.Author, retrieved.Author);
75 [Test]
76 public void ExistsTest()
78 Blog[] blogs = Blog.FindAll();
80 Assert.IsNotNull(blogs);
81 Assert.AreEqual(0, blogs.Length);
83 Blog blog = new Blog();
84 blog.Name = "hammett's blog";
85 blog.Author = "hamilton verissimo";
86 blog.Save();
88 Assert.IsTrue(blog.Id > 0);
89 Assert.IsTrue(Blog.Exists(blog.Id));
91 blog = new Blog();
92 blog.Name = "chad's blog";
93 blog.Author = "chad humphries";
94 blog.Save();
96 Assert.IsTrue(Blog.Exists(blog.Id));
98 Assert.IsFalse(Blog.Exists(1000));
101 [Test]
102 public void ExistsByCriterion()
104 Blog[] blogs = Blog.FindAll();
106 Assert.IsNotNull(blogs);
107 Assert.AreEqual(0, blogs.Length);
109 Blog blog = new Blog();
110 blog.Name = "hammett's blog";
111 blog.Author = "hamilton verissimo";
112 blog.Save();
114 Assert.IsTrue(blog.Id > 0);
115 Assert.IsTrue(Blog.Exists(
116 Expression.Eq("Name", blog.Name),
117 Expression.Eq("Author", blog.Author)));
119 blog = new Blog();
120 blog.Name = "chad's blog";
121 blog.Author = "chad humphries";
122 blog.Save();
124 Assert.IsTrue(Blog.Exists(
125 Expression.Eq("Name", blog.Name),
126 Expression.Eq("Author", blog.Author)));
128 Assert.IsFalse(Blog.Exists(
129 Expression.Eq("Name", "/\ndrew's Blog"),
130 Expression.Eq("Author", "Andrew Peters")));
133 [Test]
134 public void SlicedOperation()
136 Blog blog = new Blog();
137 blog.Name = "hammett's blog";
138 blog.Author = "hamilton verissimo";
139 blog.Save();
141 Post post1 = new Post(blog, "title1", "contents", "category1");
142 Post post2 = new Post(blog, "title2", "contents", "category2");
143 Post post3 = new Post(blog, "title3", "contents", "category3");
145 post1.Save();
146 post2.Save();
147 post3.Published = true;
148 post3.Save();
150 Post[] posts = Post.SlicedFindAll(1, 2, Expression.Eq("Blog", blog));
151 Assert.AreEqual(2, posts.Length);
154 [Test]
155 public void SimpleOperations2()
157 Blog[] blogs = Blog.FindAll();
159 Assert.IsNotNull(blogs);
160 Assert.AreEqual(0, blogs.Length);
162 Blog blog = new Blog();
163 blog.Name = "hammett's blog";
164 blog.Author = "hamilton verissimo";
165 blog.Create();
167 blogs = Blog.FindAll();
168 Assert.AreEqual(blog.Name, blogs[0].Name);
169 Assert.AreEqual(blog.Author, blogs[0].Author);
171 Assert.IsNotNull(blogs);
172 Assert.AreEqual(1, blogs.Length);
174 blog.Name = "something else1";
175 blog.Author = "something else2";
176 blog.Update();
178 blogs = Blog.FindAll();
180 Assert.IsNotNull(blogs);
181 Assert.AreEqual(1, blogs.Length);
182 Assert.AreEqual(blog.Name, blogs[0].Name);
183 Assert.AreEqual(blog.Author, blogs[0].Author);
187 [Test]
188 public void ComponentAttribute()
190 Company company = new Company("Castle Corp.");
191 company.Address = new PostalAddress(
192 "Embau St., 102", "Sao Paulo", "SP", "040390-060");
193 company.Save();
195 Company[] companies = Company.FindAll();
196 Assert.IsNotNull(companies);
197 Assert.AreEqual(1, companies.Length);
199 Company corp = companies[0];
200 Assert.IsNotNull(corp.Address);
201 Assert.AreEqual(corp.Address.Address, company.Address.Address);
202 Assert.AreEqual(corp.Address.City, company.Address.City);
203 Assert.AreEqual(corp.Address.State, company.Address.State);
204 Assert.AreEqual(corp.Address.ZipCode, company.Address.ZipCode);
207 [Test]
208 public void RelationsOneToMany()
210 Blog blog = new Blog();
211 blog.Name = "hammett's blog";
212 blog.Author = "hamilton verissimo";
213 blog.Save();
215 Post post1 = new Post(blog, "title1", "contents", "category1");
216 Post post2 = new Post(blog, "title2", "contents", "category2");
218 post1.Save();
219 post2.Save();
221 blog = Blog.Find(blog.Id);
223 Assert.IsNotNull(blog);
224 Assert.IsNotNull(blog.Posts, "posts collection is null");
225 Assert.AreEqual(2, blog.Posts.Count);
227 foreach(Post post in blog.Posts)
229 Assert.AreEqual(blog.Id, post.Blog.Id);
233 [Test]
234 public void RelationsOneToManyWithWhereAndOrder()
236 Blog blog = new Blog();
237 blog.Name = "hammett's blog";
238 blog.Author = "hamilton verissimo";
239 blog.Save();
241 Post post1 = new Post(blog, "title1", "contents", "category1");
242 Post post2 = new Post(blog, "title2", "contents", "category2");
243 Post post3 = new Post(blog, "title3", "contents", "category3");
245 post1.Save();
246 Thread.Sleep(1000); // Its a smalldatetime (small precision)
247 post2.Save();
248 Thread.Sleep(1000); // Its a smalldatetime (small precision)
249 post3.Published = true;
250 post3.Save();
252 blog = Blog.Find(blog.Id);
254 Assert.IsNotNull(blog);
255 Assert.AreEqual(2, blog.UnPublishedPosts.Count);
256 Assert.AreEqual(1, blog.PublishedPosts.Count);
258 Assert.AreEqual(3, blog.RecentPosts.Count);
259 Assert.AreEqual(post3.Id, ((Post) blog.RecentPosts[0]).Id);
260 Assert.AreEqual(post2.Id, ((Post) blog.RecentPosts[1]).Id);
261 Assert.AreEqual(post1.Id, ((Post) blog.RecentPosts[2]).Id);
265 [Test]
266 public void TestExpressionQuerySubProperty()
268 Blog blog = new Blog();
269 blog.Name = "hammett's blog";
270 blog.Author = "hamilton verissimo";
271 blog.Save();
273 Blog blog2 = new Blog();
274 blog2.Name = "hammett's blog other blog";
275 blog2.Author = "hamilton verissimo 2";
276 blog2.Save();
278 Post post1 = new Post(blog, "title1", "contents", "category1");
279 Post post2 = new Post(blog, "title2", "contents", "category2");
280 Post post3 = new Post(blog, "title3", "contents", "category3");
282 Post post21 = new Post(blog2, "title21", "contents", "category21");
283 Post post22 = new Post(blog2, "title22", "contents", "category22");
284 Post post23 = new Post(blog2, "title23", "contents", "category23");
286 post1.Save();
287 post2.Save();
288 post3.Save();
290 post21.Save();
291 post22.Save();
292 post23.Save();
294 //no idea how to make this style of query work.
295 //Post[] posts = Post.FindAll(
296 // Expression.Eq("Blog.Name", blog2.Name)
297 // );
298 //Assert.IsTrue(posts.Length > 0);
300 SimpleQuery<Post> q =
301 new SimpleQuery<Post>(typeof(Post), "from Post p where p.Id = ? or p.Blog.Name = ?", 1, "hammett's blog other blog");
303 Post[] p = q.Execute();
305 Assert.IsTrue(p.Length > 0);
308 [Test]
309 public void RelationsOneToOne()
311 Employee emp = new Employee();
312 emp.FirstName = "john";
313 emp.LastName = "doe";
314 emp.Save();
316 Assert.AreEqual(1, Employee.FindAll().Length);
318 Award award = new Award(emp);
319 award.Description = "Invisible employee";
320 award.Save();
322 Assert.AreEqual(1, Award.FindAll().Length);
324 Employee emp2 = Employee.Find(emp.ID);
325 Assert.IsNotNull(emp2);
326 Assert.IsNotNull(emp2.Award);
327 Assert.AreEqual(emp.FirstName, emp2.FirstName);
328 Assert.AreEqual(emp.LastName, emp2.LastName);
329 Assert.AreEqual(award.Description, emp2.Award.Description);
332 [Test]
333 [ExpectedException(typeof(NotFoundException))]
334 public void FindLoad()
336 Blog.Find(1);
339 [Test]
340 public void SaveUpdate()
342 Blog[] blogs = Blog.FindAll();
344 Assert.IsNotNull(blogs);
345 Assert.AreEqual(0, blogs.Length);
347 Blog blog = new Blog();
348 blog.Name = "hammett's blog";
349 blog.Author = "hamilton verissimo";
350 blog.Save();
352 blogs = Blog.FindAll();
354 Assert.IsNotNull(blogs);
355 Assert.AreEqual(1, blogs.Length);
357 blog.Name = "Something else";
358 blog.Author = "changed too";
359 blog.Save();
361 blogs = Blog.FindAll();
363 Assert.IsNotNull(blogs);
364 Assert.AreEqual(1, blogs.Length);
366 Assert.AreEqual(blog.Name, blogs[0].Name);
367 Assert.AreEqual(blog.Author, blogs[0].Author);
370 [Test]
371 public void Delete()
373 Blog[] blogs = Blog.FindAll();
375 Assert.IsNotNull(blogs);
376 Assert.AreEqual(0, blogs.Length);
378 Blog blog = new Blog();
379 blog.Name = "hammett's blog";
380 blog.Author = "hamilton verissimo";
381 blog.Save();
383 blogs = Blog.FindAll();
385 Assert.IsNotNull(blogs);
386 Assert.AreEqual(1, blogs.Length);
388 blog.Delete();
390 blogs = Blog.FindAll();
392 Assert.IsNotNull(blogs);
393 Assert.AreEqual(0, blogs.Length);
396 [Test]
397 public void ScalarProjectionQueryTest()
399 Blog blog = new Blog();
400 blog.Name = "hammett's blog";
401 blog.Author = "hamilton verissimo";
402 blog.Save();
404 ScalarProjectionQuery<Blog, int> proj = new ScalarProjectionQuery<Blog, int>(Projections.RowCount());
405 int rowCount = proj.Execute();
406 Assert.AreEqual(1, rowCount);
410 [Test]
411 public void UnTypedProjectionQueryTest()
413 Blog blog = new Blog();
414 blog.Name = "hammett's blog";
415 blog.Author = "hamilton verissimo";
416 blog.Save();
418 ProjectionQuery<Blog> proj = new ProjectionQuery<Blog>(
419 Projections.ProjectionList()
420 .Add(Projections.Property("Name"))
421 .Add(Projections.Property("Author")));
422 IList<object[]> results = proj.Execute();
423 Assert.AreEqual(blog.Name, results[0][0]);
424 Assert.AreEqual(blog.Author, results[0][1]);
428 [Test]
429 public void TypedProjectionQueryTest()
431 Blog blog = new Blog();
432 blog.Name = "hammett's blog";
433 blog.Author = "hamilton verissimo";
434 blog.Save();
436 ProjectionQuery<Blog, KeyValuePair<string, string>> proj = new ProjectionQuery<Blog, KeyValuePair<string, string>>(
437 Projections.ProjectionList()
438 .Add(Projections.Property("Name"))
439 .Add(Projections.Property("Author")));
440 IList<KeyValuePair<string, string>> results = proj.Execute();
441 Assert.AreEqual(blog.Name, results[0].Key);
442 Assert.AreEqual(blog.Author, results[0].Value);
446 [Test]
447 public void UseBlogWithGenericPostCollection()
449 Blog blog = new Blog();
450 blog.Name = "hammett's blog";
451 blog.Author = "hamilton verissimo";
453 blog.Save();
455 Post p = new Post(blog, "a", "b", "c");
456 blog.Posts.Add(p);
458 p.Save();
460 Blog fromDB = Blog.Find(blog.Id);
461 Assert.AreEqual(1, fromDB.Posts.Count);
464 [Test]
465 public void AbstractClassTableName()
467 ActiveRecordStarter.ResetInitializationFlag();
469 ActiveRecordStarter.Initialize(GetConfigSource(),
470 typeof(AbstractClass<>),
471 typeof(ConcreteClass));
472 Recreate();
474 ConcreteClass c = new ConcreteClass();
476 c.Save();
478 Assert.IsTrue(ConcreteClass.FindAll().Length == 1);