1 // Copyright 2004-2007 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.
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
;
27 public class ActiveRecordGenericsTestCase
: AbstractActiveRecordTest
32 ActiveRecordStarter
.ResetInitializationFlag();
34 ActiveRecordStarter
.Initialize(GetConfigSource(),
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";
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
);
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";
88 Assert
.IsTrue(blog
.Id
> 0);
89 Assert
.IsTrue(Blog
.Exists(blog
.Id
));
92 blog
.Name
= "chad's blog";
93 blog
.Author
= "chad humphries";
96 Assert
.IsTrue(Blog
.Exists(blog
.Id
));
98 Assert
.IsFalse(Blog
.Exists(1000));
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";
114 Assert
.IsTrue(blog
.Id
> 0);
115 Assert
.IsTrue(Blog
.Exists(
116 Expression
.Eq("Name", blog
.Name
),
117 Expression
.Eq("Author", blog
.Author
)));
120 blog
.Name
= "chad's blog";
121 blog
.Author
= "chad humphries";
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")));
134 public void SlicedOperation()
136 Blog blog
= new Blog();
137 blog
.Name
= "hammett's blog";
138 blog
.Author
= "hamilton verissimo";
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");
147 post3
.Published
= true;
150 Post
[] posts
= Post
.SlicedFindAll(1, 2, Expression
.Eq("Blog", blog
));
151 Assert
.AreEqual(2, posts
.Length
);
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";
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";
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
);
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");
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
);
208 public void RelationsOneToMany()
210 Blog blog
= new Blog();
211 blog
.Name
= "hammett's blog";
212 blog
.Author
= "hamilton verissimo";
215 Post post1
= new Post(blog
, "title1", "contents", "category1");
216 Post post2
= new Post(blog
, "title2", "contents", "category2");
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
);
234 public void RelationsOneToManyWithWhereAndOrder()
236 Blog blog
= new Blog();
237 blog
.Name
= "hammett's blog";
238 blog
.Author
= "hamilton verissimo";
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");
246 Thread
.Sleep(1000); // Its a smalldatetime (small precision)
248 Thread
.Sleep(1000); // Its a smalldatetime (small precision)
249 post3
.Published
= true;
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
);
266 public void TestExpressionQuerySubProperty()
268 Blog blog
= new Blog();
269 blog
.Name
= "hammett's blog";
270 blog
.Author
= "hamilton verissimo";
273 Blog blog2
= new Blog();
274 blog2
.Name
= "hammett's blog other blog";
275 blog2
.Author
= "hamilton verissimo 2";
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");
294 //no idea how to make this style of query work.
295 //Post[] posts = Post.FindAll(
296 // Expression.Eq("Blog.Name", blog2.Name)
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);
309 public void RelationsOneToOne()
311 Employee emp
= new Employee();
312 emp
.FirstName
= "john";
313 emp
.LastName
= "doe";
316 Assert
.AreEqual(1, Employee
.FindAll().Length
);
318 Award award
= new Award(emp
);
319 award
.Description
= "Invisible employee";
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
);
333 [ExpectedException(typeof(NotFoundException
))]
334 public void FindLoad()
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";
352 blogs
= Blog
.FindAll();
354 Assert
.IsNotNull(blogs
);
355 Assert
.AreEqual(1, blogs
.Length
);
357 blog
.Name
= "Something else";
358 blog
.Author
= "changed too";
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
);
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";
383 blogs
= Blog
.FindAll();
385 Assert
.IsNotNull(blogs
);
386 Assert
.AreEqual(1, blogs
.Length
);
390 blogs
= Blog
.FindAll();
392 Assert
.IsNotNull(blogs
);
393 Assert
.AreEqual(0, blogs
.Length
);
397 public void ScalarProjectionQueryTest()
399 Blog blog
= new Blog();
400 blog
.Name
= "hammett's blog";
401 blog
.Author
= "hamilton verissimo";
404 ScalarProjectionQuery
<Blog
, int> proj
= new ScalarProjectionQuery
<Blog
, int>(Projections
.RowCount());
405 int rowCount
= proj
.Execute();
406 Assert
.AreEqual(1, rowCount
);
411 public void UnTypedProjectionQueryTest()
413 Blog blog
= new Blog();
414 blog
.Name
= "hammett's blog";
415 blog
.Author
= "hamilton verissimo";
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]);
429 public void TypedProjectionQueryTest()
431 Blog blog
= new Blog();
432 blog
.Name
= "hammett's blog";
433 blog
.Author
= "hamilton verissimo";
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
);
447 public void UseBlogWithGenericPostCollection()
449 Blog blog
= new Blog();
450 blog
.Name
= "hammett's blog";
451 blog
.Author
= "hamilton verissimo";
455 Post p
= new Post(blog
, "a", "b", "c");
460 Blog fromDB
= Blog
.Find(blog
.Id
);
461 Assert
.AreEqual(1, fromDB
.Posts
.Count
);
465 public void AbstractClassTableName()
467 ActiveRecordStarter
.ResetInitializationFlag();
469 ActiveRecordStarter
.Initialize(GetConfigSource(),
470 typeof(AbstractClass
<>),
471 typeof(ConcreteClass
));
474 ConcreteClass c
= new ConcreteClass();
478 Assert
.IsTrue(ConcreteClass
.FindAll().Length
== 1);