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
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
;
25 public class ActiveRecordGenericsTestCase
: AbstractActiveRecordTest
30 ActiveRecordStarter
.ResetInitializationFlag();
32 ActiveRecordStarter
.Initialize(GetConfigSource(),
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";
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
);
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";
86 Assert
.IsTrue(blog
.Id
> 0);
87 Assert
.IsTrue(Blog
.Exists(blog
.Id
));
90 blog
.Name
= "chad's blog";
91 blog
.Author
= "chad humphries";
94 Assert
.IsTrue(Blog
.Exists(blog
.Id
));
96 Assert
.IsFalse(Blog
.Exists(1000));
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";
112 Assert
.IsTrue(blog
.Id
> 0);
113 Assert
.IsTrue(Blog
.Exists(
114 Expression
.Eq("Name", blog
.Name
),
115 Expression
.Eq("Author", blog
.Author
)));
118 blog
.Name
= "chad's blog";
119 blog
.Author
= "chad humphries";
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")));
132 public void SlicedOperation()
134 Blog blog
= new Blog();
135 blog
.Name
= "hammett's blog";
136 blog
.Author
= "hamilton verissimo";
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");
145 post3
.Published
= true;
148 Post
[] posts
= Post
.SlicedFindAll(1, 2, Expression
.Eq("Blog", blog
));
149 Assert
.AreEqual(2, posts
.Length
);
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";
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";
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
);
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");
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
);
206 public void RelationsOneToMany()
208 Blog blog
= new Blog();
209 blog
.Name
= "hammett's blog";
210 blog
.Author
= "hamilton verissimo";
213 Post post1
= new Post(blog
, "title1", "contents", "category1");
214 Post post2
= new Post(blog
, "title2", "contents", "category2");
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
);
232 public void RelationsOneToManyWithWhereAndOrder()
234 Blog blog
= new Blog();
235 blog
.Name
= "hammett's blog";
236 blog
.Author
= "hamilton verissimo";
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");
244 Thread
.Sleep(1000); // Its a smalldatetime (small precision)
246 Thread
.Sleep(1000); // Its a smalldatetime (small precision)
247 post3
.Published
= true;
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
);
264 public void TestExpressionQuerySubProperty()
266 Blog blog
= new Blog();
267 blog
.Name
= "hammett's blog";
268 blog
.Author
= "hamilton verissimo";
271 Blog blog2
= new Blog();
272 blog2
.Name
= "hammett's blog other blog";
273 blog2
.Author
= "hamilton verissimo 2";
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");
292 //no idea how to make this style of query work.
293 //Post[] posts = Post.FindAll(
294 // Expression.Eq("Blog.Name", blog2.Name)
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);
307 public void RelationsOneToOne()
309 Employee emp
= new Employee();
310 emp
.FirstName
= "john";
311 emp
.LastName
= "doe";
314 Assert
.AreEqual(1, Employee
.FindAll().Length
);
316 Award award
= new Award(emp
);
317 award
.Description
= "Invisible employee";
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
);
331 [ExpectedException(typeof(NotFoundException
))]
332 public void FindLoad()
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";
350 blogs
= Blog
.FindAll();
352 Assert
.IsNotNull(blogs
);
353 Assert
.AreEqual(1, blogs
.Length
);
355 blog
.Name
= "Something else";
356 blog
.Author
= "changed too";
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
);
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";
381 blogs
= Blog
.FindAll();
383 Assert
.IsNotNull(blogs
);
384 Assert
.AreEqual(1, blogs
.Length
);
388 blogs
= Blog
.FindAll();
390 Assert
.IsNotNull(blogs
);
391 Assert
.AreEqual(0, blogs
.Length
);
395 public void ScalarProjectionQueryTest()
397 Blog blog
= new Blog();
398 blog
.Name
= "hammett's blog";
399 blog
.Author
= "hamilton verissimo";
402 ScalarProjectionQuery
<Blog
, int> proj
= new ScalarProjectionQuery
<Blog
, int>(Projections
.RowCount());
403 int rowCount
= proj
.Execute();
404 Assert
.AreEqual(1, rowCount
);
409 public void UnTypedProjectionQueryTest()
411 Blog blog
= new Blog();
412 blog
.Name
= "hammett's blog";
413 blog
.Author
= "hamilton verissimo";
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]);
427 public void TypedProjectionQueryTest()
429 Blog blog
= new Blog();
430 blog
.Name
= "hammett's blog";
431 blog
.Author
= "hamilton verissimo";
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
);
445 public void UseBlogWithGenericPostCollection()
447 Blog blog
= new Blog();
448 blog
.Name
= "hammett's blog";
449 blog
.Author
= "hamilton verissimo";
453 Post p
= new Post(blog
, "a", "b", "c");
458 Blog fromDB
= Blog
.Find(blog
.Id
);
459 Assert
.AreEqual(1, fromDB
.Posts
.Count
);
463 public void AbstractClassTableName()
465 ActiveRecordStarter
.ResetInitializationFlag();
467 ActiveRecordStarter
.Initialize(GetConfigSource(),
468 typeof(AbstractClass
<>),
469 typeof(ConcreteClass
));
472 ConcreteClass c
= new ConcreteClass();
476 Assert
.IsTrue(ConcreteClass
.FindAll().Length
== 1);