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.
15 namespace Castle
.ActiveRecord
.Tests
18 using System
.Threading
;
19 using Castle
.ActiveRecord
.Framework
;
20 using Castle
.ActiveRecord
.Tests
.Model
;
21 using Castle
.ActiveRecord
.Tests
.Model
.CompositeModel
;
22 using NHibernate
.Expression
;
23 using NUnit
.Framework
;
26 public class ActiveRecordTestCase
: AbstractActiveRecordTest
29 ExpectedException(typeof(ActiveRecordInitializationException
),
30 "You can't invoke ActiveRecordStarter.Initialize more than once")]
31 public void InitializeCantBeInvokedMoreThanOnce()
33 ActiveRecordStarter
.Initialize(GetConfigSource(), typeof(Post
));
34 ActiveRecordStarter
.Initialize(GetConfigSource(), typeof(Blog
));
39 public void SimpleOperations()
41 ActiveRecordStarter
.Initialize(GetConfigSource(), typeof(Post
), typeof(Blog
));
47 Blog
[] blogs
= Blog
.FindAll();
49 Assert
.IsNotNull(blogs
);
50 Assert
.AreEqual(0, blogs
.Length
);
52 Blog blog
= new Blog();
53 blog
.Name
= "hammett's blog";
54 blog
.Author
= "hamilton verissimo";
57 blogs
= Blog
.FindAll();
58 Assert
.IsNotNull(blogs
);
59 Assert
.AreEqual(1, blogs
.Length
);
61 Blog retrieved
= blogs
[0];
62 Assert
.IsNotNull(retrieved
);
64 Assert
.AreEqual(blog
.Name
, retrieved
.Name
);
65 Assert
.AreEqual(blog
.Author
, retrieved
.Author
);
69 public void SimpleOperations2()
71 ActiveRecordStarter
.Initialize(GetConfigSource(), typeof(Post
), typeof(Blog
));
77 Blog
[] blogs
= Blog
.FindAll();
79 Assert
.IsNotNull(blogs
);
80 Assert
.AreEqual(0, blogs
.Length
);
82 Blog blog
= new Blog();
83 blog
.Name
= "hammett's blog";
84 blog
.Author
= "hamilton verissimo";
87 blogs
= Blog
.FindAll();
88 Assert
.AreEqual(blog
.Name
, blogs
[0].Name
);
89 Assert
.AreEqual(blog
.Author
, blogs
[0].Author
);
91 Assert
.IsNotNull(blogs
);
92 Assert
.AreEqual(1, blogs
.Length
);
94 blog
.Name
= "something else1";
95 blog
.Author
= "something else2";
98 blogs
= Blog
.FindAll();
100 Assert
.IsNotNull(blogs
);
101 Assert
.AreEqual(1, blogs
.Length
);
102 Assert
.AreEqual(blog
.Name
, blogs
[0].Name
);
103 Assert
.AreEqual(blog
.Author
, blogs
[0].Author
);
107 public void ComponentAttribute()
109 ActiveRecordStarter
.Initialize(GetConfigSource(),
110 typeof(Company
), typeof(Client
), typeof(Firm
), typeof(Person
));
115 Company company
= new Company("Castle Corp.");
116 company
.Address
= new PostalAddress(
117 "Embau St., 102", "Sao Paulo", "SP", "040390-060");
120 Company
[] companies
= Company
.FindAll();
121 Assert
.IsNotNull(companies
);
122 Assert
.AreEqual(1, companies
.Length
);
124 Company corp
= companies
[0];
125 Assert
.IsNotNull(corp
.Address
);
126 Assert
.AreEqual(corp
.Address
.Address
, company
.Address
.Address
);
127 Assert
.AreEqual(corp
.Address
.City
, company
.Address
.City
);
128 Assert
.AreEqual(corp
.Address
.State
, company
.Address
.State
);
129 Assert
.AreEqual(corp
.Address
.ZipCode
, company
.Address
.ZipCode
);
133 public void RelationsOneToMany()
135 ActiveRecordStarter
.Initialize(GetConfigSource(), typeof(Post
), typeof(Blog
));
141 Blog blog
= new Blog();
142 blog
.Name
= "hammett's blog";
143 blog
.Author
= "hamilton verissimo";
146 Post post1
= new Post(blog
, "title1", "contents", "category1");
147 Post post2
= new Post(blog
, "title2", "contents", "category2");
152 blog
= Blog
.Find(blog
.Id
);
154 Assert
.IsNotNull(blog
);
155 Assert
.IsNotNull(blog
.Posts
, "posts collection is null");
156 Assert
.AreEqual(2, blog
.Posts
.Count
);
158 foreach(Post post
in blog
.Posts
)
160 Assert
.AreEqual(blog
.Id
, post
.Blog
.Id
);
165 public void RelationsWithCompositeKey()
167 // User HasAndBelongsToMany Groups
168 // Groups HasAndBelongsToMany Users
169 // User HasMany Groups
171 ActiveRecordStarter
.Initialize(GetConfigSource(),
181 Org org
= new Org("org1", "Test Org.");
184 Group group1
= new Group();
185 group1
.Name
= "Group1";
188 Group group2
= new Group();
189 group2
.Name
= "Group2";
192 AgentKey agentKey1
= new AgentKey("org1", "rbellamy");
193 Agent agent1
= new Agent(agentKey1
);
195 agent1
.Groups
.Add(group1
);
196 group1
.Agents
.Add(agent1
);
198 agent1
.Groups
.Add(group2
);
199 group2
.Agents
.Add(agent1
);
202 AgentKey agentKey2
= new AgentKey("org1", "hammett");
203 Agent agent2
= new Agent(agentKey2
);
204 agent2
.Groups
.Add(group1
);
207 using(new SessionScope())
209 org
= Org
.Find(org
.Id
);
210 group1
= Group
.Find(group1
.Id
);
211 group2
= Group
.Find(group2
.Id
);
212 agent1
= Agent
.Find(agentKey1
);
213 agent2
= Agent
.Find(agentKey2
);
215 Assert
.IsNotNull(org
);
216 Assert
.IsNotNull(group1
);
217 Assert
.IsNotNull(group2
);
218 Assert
.IsNotNull(agent1
);
219 Assert
.IsNotNull(org
.Agents
, "Org agent collection is null.");
220 Assert
.IsNotNull(group1
.Agents
, "Group1 agent collection is null");
221 Assert
.IsNotNull(group2
.Agents
, "Group2 agent collection is null");
222 Assert
.IsNotNull(agent1
.Groups
, "Agent group collection is null.");
223 Assert
.IsNotNull(agent1
.Org
, "Agent's org is null.");
224 Assert
.AreEqual(2, group1
.Agents
.Count
);
225 Assert
.AreEqual(2, agent1
.Groups
.Count
);
226 Assert
.AreEqual(1, agent2
.Groups
.Count
);
228 foreach(Agent agentLoop
in org
.Agents
)
230 Assert
.IsTrue(agentLoop
.Groups
.Contains(group1
));
236 public void RelationsOneToManyWithWhereAndOrder()
238 ActiveRecordStarter
.Initialize(GetConfigSource(), typeof(Post
), typeof(Blog
));
244 Blog blog
= new Blog();
245 blog
.Name
= "hammett's blog";
246 blog
.Author
= "hamilton verissimo";
249 Post post1
= new Post(blog
, "title1", "contents", "category1");
250 Post post2
= new Post(blog
, "title2", "contents", "category2");
251 Post post3
= new Post(blog
, "title3", "contents", "category3");
254 Thread
.Sleep(1000); // Its a smalldatetime (small precision)
256 Thread
.Sleep(1000); // Its a smalldatetime (small precision)
257 post3
.Published
= true;
260 blog
= Blog
.Find(blog
.Id
);
262 Assert
.IsNotNull(blog
);
263 Assert
.AreEqual(2, blog
.UnPublishedPosts
.Count
);
264 Assert
.AreEqual(1, blog
.PublishedPosts
.Count
);
266 Assert
.AreEqual(3, blog
.RecentPosts
.Count
);
267 Assert
.AreEqual(post3
.Id
, ((Post
) blog
.RecentPosts
[0]).Id
);
268 Assert
.AreEqual(post2
.Id
, ((Post
) blog
.RecentPosts
[1]).Id
);
269 Assert
.AreEqual(post1
.Id
, ((Post
) blog
.RecentPosts
[2]).Id
);
273 public void RelationsOneToOne()
275 ActiveRecordStarter
.Initialize(GetConfigSource(), typeof(Employee
), typeof(Award
));
279 Employee
.DeleteAll();
282 Employee emp
= new Employee();
284 using(new SessionScope())
286 emp
.FirstName
= "john";
287 emp
.LastName
= "doe";
290 Assert
.AreEqual(1, Employee
.FindAll().Length
);
291 Assert
.AreEqual(0, Award
.FindAll().Length
);
293 award
= new Award(emp
);
294 award
.Description
= "Invisible employee";
298 Assert
.AreEqual(1, Award
.FindAll().Length
);
299 Assert
.AreEqual(1, Employee
.FindAll().Length
);
301 Employee emp2
= Employee
.Find(emp
.ID
);
302 Assert
.IsNotNull(emp2
);
303 Assert
.IsNotNull(emp2
.Award
);
304 Assert
.AreEqual(emp
.FirstName
, emp2
.FirstName
);
305 Assert
.AreEqual(emp
.LastName
, emp2
.LastName
);
306 Assert
.AreEqual(award
.Description
, emp2
.Award
.Description
);
310 [ExpectedException(typeof(NotFoundException
))]
311 public void FindLoad()
313 ActiveRecordStarter
.Initialize(GetConfigSource(), typeof(Post
), typeof(Blog
));
319 Blog blog
= Blog
.Find(0);
324 public void SaveUpdate()
326 ActiveRecordStarter
.Initialize(GetConfigSource(), typeof(Post
), typeof(Blog
));
333 Blog
[] blogs
= Blog
.FindAll();
335 Assert
.IsNotNull(blogs
);
336 Assert
.AreEqual(0, blogs
.Length
);
338 Blog blog
= new Blog();
339 blog
.Name
= "hammett's blog";
340 blog
.Author
= "hamilton verissimo";
343 blogs
= Blog
.FindAll();
345 Assert
.IsNotNull(blogs
);
346 Assert
.AreEqual(1, blogs
.Length
);
348 blog
.Name
= "Something else";
349 blog
.Author
= "changed too";
352 blogs
= Blog
.FindAll();
354 Assert
.IsNotNull(blogs
);
355 Assert
.AreEqual(1, blogs
.Length
);
357 Assert
.AreEqual(blog
.Name
, blogs
[0].Name
);
358 Assert
.AreEqual(blog
.Author
, blogs
[0].Author
);
364 ActiveRecordStarter
.Initialize(GetConfigSource(), typeof(Post
), typeof(Blog
));
370 Blog
[] blogs
= Blog
.FindAll();
372 Assert
.IsNotNull(blogs
);
373 Assert
.AreEqual(0, blogs
.Length
);
375 Blog blog
= new Blog();
376 blog
.Name
= "hammett's blog";
377 blog
.Author
= "hamilton verissimo";
380 blogs
= Blog
.FindAll();
382 Assert
.IsNotNull(blogs
);
383 Assert
.AreEqual(1, blogs
.Length
);
387 blogs
= Blog
.FindAll();
389 Assert
.IsNotNull(blogs
);
390 Assert
.AreEqual(0, blogs
.Length
);
394 public void DeleteAll()
396 ActiveRecordStarter
.Initialize(GetConfigSource(), typeof(Post
), typeof(Blog
));
402 Blog
[] blogs
= Blog
.FindAll();
404 Assert
.IsNotNull(blogs
);
405 Assert
.AreEqual(0, blogs
.Length
);
407 Blog blog1
= new Blog();
408 blog1
.Name
= "hammett's blog";
409 blog1
.Author
= "hamilton verissimo";
412 Blog blog2
= new Blog();
413 blog2
.Name
= "richard's blog";
414 blog2
.Author
= "g. richard bellamy";
417 blogs
= Blog
.FindAll();
419 Assert
.IsNotNull(blogs
);
420 Assert
.AreEqual(2, blogs
.Length
);
422 Blog
.DeleteAll("Author = 'g. richard bellamy'");
424 blogs
= Blog
.FindAll();
426 Assert
.IsNotNull(blogs
);
427 Assert
.AreEqual(1, blogs
.Length
);
428 Assert
.AreEqual("hamilton verissimo", blogs
[0].Author
);
432 blogs
= Blog
.FindAll();
434 Assert
.IsNotNull(blogs
);
435 Assert
.AreEqual(0, blogs
.Length
);
439 public void ExecuteAndCallback()
441 ActiveRecordStarter
.Initialize(GetConfigSource(), typeof(Post
), typeof(Blog
));
447 Blog
[] blogs
= Blog
.FindAll();
449 Assert
.IsNotNull(blogs
);
450 Assert
.AreEqual(0, blogs
.Length
);
452 Blog blog
= new Blog();
453 blog
.Name
= "hammett's blog";
454 blog
.Author
= "hamilton verissimo";
457 blogs
= Blog
.FindAll();
459 Assert
.IsNotNull(blogs
);
460 Assert
.AreEqual(1, blogs
.Length
);
464 blogs
= Blog
.FindAll();
466 Assert
.IsNotNull(blogs
);
467 Assert
.AreEqual(0, blogs
.Length
);
471 [Ignore("Need to complete this test case!")]
472 public void RelationMap()
474 ActiveRecordStarter
.Initialize(GetConfigSource(), typeof(IntlName
), typeof(Snippet
));
477 IntlName n1
= new IntlName();
478 n1
.AddSnippet("pt-br", "bom dia");
479 n1
.AddSnippet("en-us", "good morning");
484 public void TestTimestampedClass()
486 ActiveRecordStarter
.Initialize(GetConfigSource(), typeof(TimeStamped
));
489 TimeStamped ts
= new TimeStamped();
490 ts
.name
= "a timestamped record";
492 Assert
.IsTrue(ts
.LastSaved
== DateTime
.MinValue
);
494 Assert
.IsFalse(ts
.LastSaved
== DateTime
.MinValue
);
496 //DateTime origional_lastsaved = ts.LastSaved;
498 ts
.name
= "another name";
501 // Assert.IsFalse(ts.LastSaved == origional_lastsaved);
505 public void FetchCount()
507 ActiveRecordStarter
.Initialize(GetConfigSource(), typeof(Post
), typeof(Blog
));
513 Assert
.AreEqual(0, Post
.FetchCount());
514 Assert
.AreEqual(0, Blog
.FetchCount());
516 Blog
[] blogs
= Blog
.FindAll();
518 Assert
.IsNotNull(blogs
);
519 Assert
.AreEqual(0, blogs
.Length
);
520 Assert
.IsFalse(Blog
.Exists());
522 Blog blog
= new Blog();
523 blog
.Name
= "hammett's blog";
524 blog
.Author
= "hamilton verissimo";
527 Assert
.AreEqual(1, Blog
.FetchCount());
528 Assert
.IsTrue(Blog
.Exists());
530 blogs
= Blog
.FindAll();
531 Assert
.IsNotNull(blogs
);
532 Assert
.AreEqual(1, blogs
.Length
);
534 Blog blog2
= new Blog();
535 blog2
.Name
= "joe's blog";
536 blog2
.Author
= "joe doe";
539 Assert
.AreEqual(2, Blog
.FetchCount());
540 Assert
.AreEqual(1, Blog
.FetchCount("name=?", "hammett's blog"));
541 Assert
.IsTrue(Blog
.Exists("name=?", "hammett's blog"));
543 Blog retrieved
= blogs
[0];
544 Assert
.IsNotNull(retrieved
);
546 Assert
.AreEqual(blog
.Name
, retrieved
.Name
);
547 Assert
.AreEqual(blog
.Author
, retrieved
.Author
);
549 Assert
.AreEqual(1, Blog
.FetchCount(
550 Expression
.Eq("Name", blog
.Name
),
551 Expression
.Eq("Author", blog
.Author
)));
553 Assert
.AreEqual(0, Blog
.FetchCount(
554 Expression
.Eq("Name", "/\ndrew's Blog"),
555 Expression
.Eq("Author", "Andrew Peters")));
559 public void LifecycleMethods()
561 ActiveRecordStarter
.Initialize(GetConfigSource(), typeof(Post
), typeof(Blog
));
567 Blog blog
= new Blog();
569 Assert
.IsTrue(blog
.OnDeleteCalled
== blog
.OnLoadCalled
== blog
.OnSaveCalled
== blog
.OnUpdateCalled
);
571 blog
.Name
= "hammett's blog";
572 blog
.Author
= "hamilton verissimo";
575 Assert
.IsTrue(blog
.OnSaveCalled
);
576 Assert
.IsFalse(blog
.OnDeleteCalled
);
577 Assert
.IsFalse(blog
.OnLoadCalled
);
578 Assert
.IsFalse(blog
.OnUpdateCalled
);
580 blog
.Name
= "hammett's blog x";
581 blog
.Author
= "hamilton verissimo x";
583 Assert
.IsTrue(blog
.OnUpdateCalled
);
585 blog
= Blog
.Find(blog
.Id
);
586 Assert
.IsTrue(blog
.OnLoadCalled
);
589 Assert
.IsTrue(blog
.OnDeleteCalled
);
593 public void RegisterTypeTest()
595 ActiveRecordStarter
.Initialize(GetConfigSource());
596 ActiveRecordStarter
.RegisterTypes(typeof(Blog
), typeof(Post
));
602 Blog
[] blogs
= Blog
.FindAll();
604 Assert
.IsNotNull(blogs
);
605 Assert
.AreEqual(0, blogs
.Length
);
607 Blog blog
= new Blog();
608 blog
.Name
= "hammett's blog";
609 blog
.Author
= "hamilton verissimo";
612 blogs
= Blog
.FindAll();
613 Assert
.IsNotNull(blogs
);
614 Assert
.AreEqual(1, blogs
.Length
);
616 Blog retrieved
= blogs
[0];
617 Assert
.IsNotNull(retrieved
);
619 Assert
.AreEqual(blog
.Name
, retrieved
.Name
);
620 Assert
.AreEqual(blog
.Author
, retrieved
.Author
);
624 public void TestName()
626 ActiveRecordStarter
.Initialize(GetConfigSource());
627 ActiveRecordStarter
.RegisterTypes(typeof(Blog
), typeof(Post
));
630 Blog blog
= new Blog();
632 blog
.Author
= "hamilton verissimo";
635 Blog
[] blogs
= Blog
.FindByProperty("Name", null);
637 Assert
.IsTrue(blogs
.Length
== 1);
639 using(new SessionScope())
641 blog
.Name
= "Hammetts blog";
645 blogs
= Blog
.FindByProperty("Name", null);
647 Assert
.IsTrue(blogs
.Length
== 0);
649 blogs
= Blog
.FindByProperty("Name", "Hammetts blog");
651 Assert
.IsTrue(blogs
.Length
== 1);
655 public void ExistsTest()
657 ActiveRecordStarter
.Initialize(GetConfigSource());
658 ActiveRecordStarter
.RegisterTypes(typeof(Blog
), typeof(Post
));
661 Blog blog
= new Blog();
662 blog
.Name
= "hammett's blog";
663 blog
.Author
= "hamilton verissimo";
666 Assert
.IsTrue(blog
.Id
> 0);
667 Assert
.IsTrue(Blog
.Exists(blog
.Id
));
670 blog
.Name
= "chad's blog";
671 blog
.Author
= "chad humphries";
674 Assert
.IsTrue(Blog
.Exists(blog
.Id
));
676 Assert
.IsFalse(Blog
.Exists(1000));
680 public void ExistsByCriterion()
682 ActiveRecordStarter
.Initialize(GetConfigSource());
683 ActiveRecordStarter
.RegisterTypes(typeof(Blog
), typeof(Post
));
686 Blog
[] blogs
= Blog
.FindAll();
688 Assert
.IsNotNull(blogs
);
689 Assert
.AreEqual(0, blogs
.Length
);
691 Blog blog
= new Blog();
692 blog
.Name
= "hammett's blog";
693 blog
.Author
= "hamilton verissimo";
696 Assert
.IsTrue(blog
.Id
> 0);
697 Assert
.IsTrue(Blog
.Exists(
698 Expression
.Eq("Name", blog
.Name
),
699 Expression
.Eq("Author", blog
.Author
)));
702 blog
.Name
= "chad's blog";
703 blog
.Author
= "chad humphries";
706 Assert
.IsTrue(Blog
.Exists(
707 Expression
.Eq("Name", blog
.Name
),
708 Expression
.Eq("Author", blog
.Author
)));
710 Assert
.IsFalse(Blog
.Exists(
711 Expression
.Eq("Name", "/\ndrew's Blog"),
712 Expression
.Eq("Author", "Andrew Peters")));
715 [Test
, ExpectedExceptionAttribute(typeof(ActiveRecordException
), "Could not perform Save for ModelClassWithBrokenField")]
716 public void SaveWithBadTableSchemaThrowsException()
718 ActiveRecordStarter
.Initialize(GetConfigSource(), typeof(ModelClassUsedToCreateTableForClassWithBrokenField
));
721 ActiveRecordStarter
.ResetInitializationFlag();
722 ActiveRecordStarter
.Initialize(GetConfigSource(), typeof(ModelClassWithBrokenField
));
724 ModelClassWithBrokenField brokenClass
= new ModelClassWithBrokenField();
725 brokenClass
.Broken
= true;