Updates Castle to the latest NHibernate Trunk libs. This required namespace changes...
[castle.git] / ActiveRecord / Castle.ActiveRecord.Tests / ActiveRecordBaseGenericsTestCase.cs
blob92cd71f639a8c999eb79e2f03d807eb4c7ddd605
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;
18 using System.Text;
19 using System.Reflection;
20 using System.Collections.Generic;
22 using NUnit.Framework;
24 using Castle.ActiveRecord.Tests.Model.GenericModel;
26 using NHibernate.Criterion;
28 [TestFixture]
29 public class ActiveRecordBaseGenericsTestCase : AbstractActiveRecordTest
31 [SetUp]
32 public void Setup()
34 ActiveRecordStarter.ResetInitializationFlag();
36 ActiveRecordStarter.Initialize(GetConfigSource(),
37 typeof(Blog),
38 typeof(Post),
39 typeof(Company),
40 typeof(Award),
41 typeof(Employee),
42 typeof(Person));
44 Recreate();
46 Post.DeleteAll();
47 Blog.DeleteAll();
48 Company.DeleteAll();
49 Award.DeleteAll();
50 Employee.DeleteAll();
53 [Test]
54 public void SimpleOperations()
56 Blog[] blogs = Blog.FindAll();
58 Assert.IsNotNull(blogs);
59 Assert.AreEqual(0, blogs.Length);
61 Blog blog = new Blog();
62 blog.Name = "hammett's blog";
63 blog.Author = "hamilton verissimo";
64 blog.Save();
66 blogs = Blog.FindAll();
68 Assert.IsNotNull(blogs);
69 Assert.AreEqual(1, blogs.Length);
71 Blog retrieved = blogs[0];
72 Assert.IsNotNull(retrieved);
74 Assert.AreEqual(blog.Name, retrieved.Name);
75 Assert.AreEqual(blog.Author, retrieved.Author);
78 [Test]
79 public void SlicedOperation()
81 Blog blog = new Blog();
82 blog.Name = "hammett's blog";
83 blog.Author = "hamilton verissimo";
84 blog.Save();
86 Post post1 = new Post(blog, "title1", "contents", "category1");
87 Post post2 = new Post(blog, "title2", "contents", "category2");
88 Post post3 = new Post(blog, "title3", "contents", "category3");
90 post1.Save();
91 post2.Save();
92 post3.Published = true;
93 post3.Save();
94 Post[] posts = Post.CustomSlicedFind(Expression.Eq("Blog", blog), 1, 2);
95 Assert.AreEqual(2, posts.Length);
98 [Test]
99 public void SimpleOperations2()
101 Blog[] blogs = Blog.FindAll();
103 Assert.IsNotNull(blogs);
104 Assert.AreEqual(0, blogs.Length);
106 Blog blog = new Blog();
107 blog.Name = "hammett's blog";
108 blog.Author = "hamilton verissimo";
109 blog.Create();
111 blogs = Blog.FindAll();
112 Assert.AreEqual(blog.Name, blogs[0].Name);
113 Assert.AreEqual(blog.Author, blogs[0].Author);
115 Assert.IsNotNull(blogs);
116 Assert.AreEqual(1, blogs.Length);
118 blog.Name = "something else1";
119 blog.Author = "something else2";
120 blog.Update();
122 blogs = Blog.FindAll();
124 Assert.IsNotNull(blogs);
125 Assert.AreEqual(1, blogs.Length);
126 Assert.AreEqual(blog.Name, blogs[0].Name);
127 Assert.AreEqual(blog.Author, blogs[0].Author);
130 [Test]
131 public void HasManyAndBelongsToMany()
133 Company company = new Company("Castle Corp.");
134 company.Address = new PostalAddress(
135 "Embau St., 102", "Sao Paulo", "SP", "040390-060");
136 company.Save();
138 Person person = new Person();
139 person.Name = "ayende";
140 person.Companies.Add(company);
141 company.People.Add(person);
143 person.Save();
145 Company fromDB = Company.FindFirst();
146 Assert.AreEqual(1, fromDB.People.Count);
148 Assert.AreEqual("ayende", new List<Person>(fromDB.People)[0].Name);
151 [Test]
152 public void ComponentAttribute()
154 Company company = new Company("Castle Corp.");
155 company.Address = new PostalAddress(
156 "Embau St., 102", "Sao Paulo", "SP", "040390-060");
157 company.Save();
159 Company[] companies = Company.FindAll();
160 Assert.IsNotNull(companies);
161 Assert.AreEqual(1, companies.Length);
163 Company corp = companies[0];
164 Assert.IsNotNull(corp.Address);
165 Assert.AreEqual(corp.Address.Address, company.Address.Address);
166 Assert.AreEqual(corp.Address.City, company.Address.City);
167 Assert.AreEqual(corp.Address.State, company.Address.State);
168 Assert.AreEqual(corp.Address.ZipCode, company.Address.ZipCode);
171 [Test]
172 public void RelationsOneToMany()
174 Blog blog = new Blog();
175 blog.Name = "hammett's blog";
176 blog.Author = "hamilton verissimo";
177 blog.Save();
179 Post post1 = new Post(blog, "title1", "contents", "category1");
180 Post post2 = new Post(blog, "title2", "contents", "category2");
182 post1.Save();
183 post2.Save();
185 blog = Blog.Find(blog.Id);
187 Assert.IsNotNull(blog);
188 Assert.IsNotNull(blog.Posts, "posts collection is null");
189 Assert.AreEqual(2, blog.Posts.Count);
191 foreach (Post post in blog.Posts)
193 Assert.AreEqual(blog.Id, post.Blog.Id);
197 [Test]
198 public void RelationsOneToManyWithWhereAndOrder()
200 Blog blog = new Blog();
201 blog.Name = "hammett's blog";
202 blog.Author = "hamilton verissimo";
203 blog.Save();
205 Post post1 = new Post(blog, "title1", "contents", "category1");
206 Post post2 = new Post(blog, "title2", "contents", "category2");
207 Post post3 = new Post(blog, "title3", "contents", "category3");
209 post1.Save();
210 System.Threading.Thread.Sleep(1000); // Its a smalldatetime (small precision)
211 post2.Save();
212 System.Threading.Thread.Sleep(1000); // Its a smalldatetime (small precision)
213 post3.Published = true;
214 post3.Save();
216 blog = Blog.Find(blog.Id);
218 Assert.IsNotNull(blog);
219 Assert.AreEqual(2, blog.UnPublishedPosts.Count);
220 Assert.AreEqual(1, blog.PublishedPosts.Count);
222 Assert.AreEqual(3, blog.RecentPosts.Count);
223 Assert.AreEqual(post3.Id, (blog.RecentPosts[0] as Post).Id);
224 Assert.AreEqual(post2.Id, (blog.RecentPosts[1] as Post).Id);
225 Assert.AreEqual(post1.Id, (blog.RecentPosts[2] as Post).Id);
228 [Test]
229 public void RelationsOneToOne()
231 Employee emp = new Employee();
232 emp.FirstName = "john";
233 emp.LastName = "doe";
234 emp.Save();
236 Assert.AreEqual(1, Employee.FindAll().Length);
238 Award award = new Award(emp);
239 award.Description = "Invisible employee";
240 award.Save();
242 Assert.AreEqual(1, Award.FindAll().Length);
244 Employee emp2 = Employee.Find(emp.ID);
245 Assert.IsNotNull(emp2);
246 Assert.IsNotNull(emp2.Award);
247 Assert.AreEqual(emp.FirstName, emp2.FirstName);
248 Assert.AreEqual(emp.LastName, emp2.LastName);
249 Assert.AreEqual(award.Description, emp2.Award.Description);
252 [Test]
253 [ExpectedException(typeof(NotFoundException))]
254 public void FindLoad()
256 Blog.Find(1);
259 [Test]
260 public void SaveUpdate()
262 Blog[] blogs = Blog.FindAll();
264 Assert.IsNotNull(blogs);
265 Assert.AreEqual(0, blogs.Length);
267 Blog blog = new Blog();
268 blog.Name = "hammett's blog";
269 blog.Author = "hamilton verissimo";
270 blog.Save();
272 blogs = Blog.FindAll();
274 Assert.IsNotNull(blogs);
275 Assert.AreEqual(1, blogs.Length);
277 blog.Name = "Something else";
278 blog.Author = "changed too";
279 blog.Save();
281 blogs = Blog.FindAll();
283 Assert.IsNotNull(blogs);
284 Assert.AreEqual(1, blogs.Length);
286 Assert.AreEqual(blog.Name, blogs[0].Name);
287 Assert.AreEqual(blog.Author, blogs[0].Author);
290 [Test]
291 public void Delete()
293 Blog[] blogs = Blog.FindAll();
295 Assert.IsNotNull(blogs);
296 Assert.AreEqual(0, blogs.Length);
298 Blog blog = new Blog();
299 blog.Name = "hammett's blog";
300 blog.Author = "hamilton verissimo";
301 blog.Save();
303 blogs = Blog.FindAll();
305 Assert.IsNotNull(blogs);
306 Assert.AreEqual(1, blogs.Length);
308 blog.Delete();
310 blogs = Blog.FindAll();
312 Assert.IsNotNull(blogs);
313 Assert.AreEqual(0, blogs.Length);