1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
19 using System
.Reflection
;
20 using System
.Collections
.Generic
;
22 using NUnit
.Framework
;
24 using Castle
.ActiveRecord
.Tests
.Model
.GenericModel
;
26 using NHibernate
.Expression
;
29 public class ActiveRecordBaseGenericsTestCase
: AbstractActiveRecordTest
34 ActiveRecordStarter
.ResetInitializationFlag();
36 ActiveRecordStarter
.Initialize(GetConfigSource(),
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";
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
);
79 public void SlicedOperation()
81 Blog blog
= new Blog();
82 blog
.Name
= "hammett's blog";
83 blog
.Author
= "hamilton verissimo";
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");
92 post3
.Published
= true;
94 Post
[] posts
= Post
.CustomSlicedFind(Expression
.Eq("Blog", blog
), 1, 2);
95 Assert
.AreEqual(2, posts
.Length
);
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";
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";
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
);
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");
138 Person person
= new Person();
139 person
.Name
= "ayende";
140 person
.Companies
.Add(company
);
141 company
.People
.Add(person
);
145 Company fromDB
= Company
.FindFirst();
146 Assert
.AreEqual(1, fromDB
.People
.Count
);
148 Assert
.AreEqual("ayende", new List
<Person
>(fromDB
.People
)[0].Name
);
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");
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
);
172 public void RelationsOneToMany()
174 Blog blog
= new Blog();
175 blog
.Name
= "hammett's blog";
176 blog
.Author
= "hamilton verissimo";
179 Post post1
= new Post(blog
, "title1", "contents", "category1");
180 Post post2
= new Post(blog
, "title2", "contents", "category2");
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
);
198 public void RelationsOneToManyWithWhereAndOrder()
200 Blog blog
= new Blog();
201 blog
.Name
= "hammett's blog";
202 blog
.Author
= "hamilton verissimo";
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");
210 System
.Threading
.Thread
.Sleep(1000); // Its a smalldatetime (small precision)
212 System
.Threading
.Thread
.Sleep(1000); // Its a smalldatetime (small precision)
213 post3
.Published
= true;
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
);
229 public void RelationsOneToOne()
231 Employee emp
= new Employee();
232 emp
.FirstName
= "john";
233 emp
.LastName
= "doe";
236 Assert
.AreEqual(1, Employee
.FindAll().Length
);
238 Award award
= new Award(emp
);
239 award
.Description
= "Invisible employee";
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
);
253 [ExpectedException(typeof(NotFoundException
))]
254 public void FindLoad()
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";
272 blogs
= Blog
.FindAll();
274 Assert
.IsNotNull(blogs
);
275 Assert
.AreEqual(1, blogs
.Length
);
277 blog
.Name
= "Something else";
278 blog
.Author
= "changed too";
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
);
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";
303 blogs
= Blog
.FindAll();
305 Assert
.IsNotNull(blogs
);
306 Assert
.AreEqual(1, blogs
.Length
);
310 blogs
= Blog
.FindAll();
312 Assert
.IsNotNull(blogs
);
313 Assert
.AreEqual(0, blogs
.Length
);