Updates Castle to the latest NHibernate Trunk libs. This required namespace changes...
[castle.git] / ActiveRecord / Castle.ActiveRecord.Tests / ActiveRecordTestCase.cs
blobae1076260a1fc358c686080af2e527d1f6dd00a5
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.Threading;
19 using Castle.ActiveRecord.Framework;
20 using Castle.ActiveRecord.Tests.Model;
21 using Castle.ActiveRecord.Tests.Model.CompositeModel;
22 using NHibernate.Criterion;
23 using NUnit.Framework;
25 [TestFixture]
26 public class ActiveRecordTestCase : AbstractActiveRecordTest
28 [Test,
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));
38 [Test]
39 public void SimpleOperations()
41 ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Post), typeof(Blog));
42 Recreate();
44 Post.DeleteAll();
45 Blog.DeleteAll();
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";
55 blog.Save();
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);
68 [Test]
69 public void SimpleOperations2()
71 ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Post), typeof(Blog));
72 Recreate();
74 Post.DeleteAll();
75 Blog.DeleteAll();
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";
85 blog.Create();
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";
96 blog.Update();
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);
106 [Test]
107 public void ComponentAttribute()
109 ActiveRecordStarter.Initialize(GetConfigSource(),
110 typeof(Company), typeof(Client), typeof(Firm), typeof(Person));
111 Recreate();
113 Company.DeleteAll();
115 Company company = new Company("Castle Corp.");
116 company.Address = new PostalAddress(
117 "Embau St., 102", "Sao Paulo", "SP", "040390-060");
118 company.Save();
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);
132 [Test]
133 public void RelationsOneToMany()
135 ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Post), typeof(Blog));
136 Recreate();
138 Post.DeleteAll();
139 Blog.DeleteAll();
141 Blog blog = new Blog();
142 blog.Name = "hammett's blog";
143 blog.Author = "hamilton verissimo";
144 blog.Save();
146 Post post1 = new Post(blog, "title1", "contents", "category1");
147 Post post2 = new Post(blog, "title2", "contents", "category2");
149 post1.Save();
150 post2.Save();
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);
164 [Test]
165 public void RelationsWithCompositeKey()
167 // User HasAndBelongsToMany Groups
168 // Groups HasAndBelongsToMany Users
169 // User HasMany Groups
171 ActiveRecordStarter.Initialize(GetConfigSource(),
172 typeof(Group),
173 typeof(Agent),
174 typeof(Org));
175 Recreate();
177 Agent.DeleteAll();
178 Org.DeleteAll();
179 Group.DeleteAll();
181 Org org = new Org("org1", "Test Org.");
182 org.Save();
184 Group group1 = new Group();
185 group1.Name = "Group1";
186 group1.Save();
188 Group group2 = new Group();
189 group2.Name = "Group2";
190 group2.Save();
192 AgentKey agentKey1 = new AgentKey("org1", "rbellamy");
193 Agent agent1 = new Agent(agentKey1);
194 agent1.Save();
195 agent1.Groups.Add(group1);
196 group1.Agents.Add(agent1);
197 agent1.Save();
198 agent1.Groups.Add(group2);
199 group2.Agents.Add(agent1);
200 agent1.Save();
202 AgentKey agentKey2 = new AgentKey("org1", "hammett");
203 Agent agent2 = new Agent(agentKey2);
204 agent2.Groups.Add(group1);
205 agent2.Save();
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));
235 [Test]
236 public void RelationsOneToManyWithWhereAndOrder()
238 ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Post), typeof(Blog));
239 Recreate();
241 Post.DeleteAll();
242 Blog.DeleteAll();
244 Blog blog = new Blog();
245 blog.Name = "hammett's blog";
246 blog.Author = "hamilton verissimo";
247 blog.Save();
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");
253 post1.Save();
254 Thread.Sleep(1000); // Its a smalldatetime (small precision)
255 post2.Save();
256 Thread.Sleep(1000); // Its a smalldatetime (small precision)
257 post3.Published = true;
258 post3.Save();
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);
272 [Test]
273 public void RelationsOneToOne()
275 ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Employee), typeof(Award));
276 Recreate();
278 Award.DeleteAll();
279 Employee.DeleteAll();
281 Award award;
282 Employee emp = new Employee();
284 using(new SessionScope())
286 emp.FirstName = "john";
287 emp.LastName = "doe";
288 emp.Save();
290 Assert.AreEqual(1, Employee.FindAll().Length);
291 Assert.AreEqual(0, Award.FindAll().Length);
293 award = new Award(emp);
294 award.Description = "Invisible employee";
295 award.Save();
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);
309 [Test]
310 [ExpectedException(typeof(NotFoundException))]
311 public void FindLoad()
313 ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Post), typeof(Blog));
314 Recreate();
316 Post.DeleteAll();
317 Blog.DeleteAll();
319 Blog blog = Blog.Find(0);
320 Assert.IsNull(blog);
323 [Test]
324 public void SaveUpdate()
326 ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Post), typeof(Blog));
327 Recreate();
330 Post.DeleteAll();
331 Blog.DeleteAll();
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";
341 blog.Save();
343 blogs = Blog.FindAll();
345 Assert.IsNotNull(blogs);
346 Assert.AreEqual(1, blogs.Length);
348 blog.Name = "Something else";
349 blog.Author = "changed too";
350 blog.Save();
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);
361 [Test]
362 public void Delete()
364 ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Post), typeof(Blog));
365 Recreate();
367 Post.DeleteAll();
368 Blog.DeleteAll();
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";
378 blog.Save();
380 blogs = Blog.FindAll();
382 Assert.IsNotNull(blogs);
383 Assert.AreEqual(1, blogs.Length);
385 blog.Delete();
387 blogs = Blog.FindAll();
389 Assert.IsNotNull(blogs);
390 Assert.AreEqual(0, blogs.Length);
393 [Test]
394 public void DeleteAll()
396 ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Post), typeof(Blog));
397 Recreate();
399 Post.DeleteAll();
400 Blog.DeleteAll();
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";
410 blog1.Save();
412 Blog blog2 = new Blog();
413 blog2.Name = "richard's blog";
414 blog2.Author = "g. richard bellamy";
415 blog2.Save();
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);
430 blog1.Delete();
432 blogs = Blog.FindAll();
434 Assert.IsNotNull(blogs);
435 Assert.AreEqual(0, blogs.Length);
438 [Test]
439 public void ExecuteAndCallback()
441 ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Post), typeof(Blog));
442 Recreate();
444 Post.DeleteAll();
445 Blog.DeleteAll();
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";
455 blog.Save();
457 blogs = Blog.FindAll();
459 Assert.IsNotNull(blogs);
460 Assert.AreEqual(1, blogs.Length);
462 blog.CustomAction();
464 blogs = Blog.FindAll();
466 Assert.IsNotNull(blogs);
467 Assert.AreEqual(0, blogs.Length);
470 [Test]
471 [Ignore("Need to complete this test case!")]
472 public void RelationMap()
474 ActiveRecordStarter.Initialize(GetConfigSource(), typeof(IntlName), typeof(Snippet));
475 Recreate();
477 IntlName n1 = new IntlName();
478 n1.AddSnippet("pt-br", "bom dia");
479 n1.AddSnippet("en-us", "good morning");
480 n1.Save();
483 [Test]
484 public void TestTimestampedClass()
486 ActiveRecordStarter.Initialize(GetConfigSource(), typeof(TimeStamped));
487 Recreate();
489 TimeStamped ts = new TimeStamped();
490 ts.name = "a timestamped record";
492 Assert.IsTrue(ts.LastSaved == DateTime.MinValue);
493 ts.Save();
494 Assert.IsFalse(ts.LastSaved == DateTime.MinValue);
496 //DateTime origional_lastsaved = ts.LastSaved;
498 ts.name = "another name";
499 ts.Save();
501 // Assert.IsFalse(ts.LastSaved == origional_lastsaved);
504 [Test]
505 public void FetchCount()
507 ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Post), typeof(Blog));
508 Recreate();
510 Post.DeleteAll();
511 Blog.DeleteAll();
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";
525 blog.Save();
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";
537 blog2.Save();
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")));
558 [Test]
559 public void LifecycleMethods()
561 ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Post), typeof(Blog));
562 Recreate();
564 Post.DeleteAll();
565 Blog.DeleteAll();
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";
573 blog.Save();
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";
582 blog.Save();
583 Assert.IsTrue(blog.OnUpdateCalled);
585 blog = Blog.Find(blog.Id);
586 Assert.IsTrue(blog.OnLoadCalled);
588 blog.Delete();
589 Assert.IsTrue(blog.OnDeleteCalled);
592 [Test]
593 public void RegisterTypeTest()
595 ActiveRecordStarter.Initialize(GetConfigSource());
596 ActiveRecordStarter.RegisterTypes(typeof(Blog), typeof(Post));
597 Recreate();
599 Post.DeleteAll();
600 Blog.DeleteAll();
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";
610 blog.Save();
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);
623 [Test]
624 public void TestName()
626 ActiveRecordStarter.Initialize(GetConfigSource());
627 ActiveRecordStarter.RegisterTypes(typeof(Blog), typeof(Post));
628 Recreate();
630 Blog blog = new Blog();
631 blog.Name = null;
632 blog.Author = "hamilton verissimo";
633 blog.Save();
635 Blog[] blogs = Blog.FindByProperty("Name", null);
637 Assert.IsTrue(blogs.Length == 1);
639 using(new SessionScope())
641 blog.Name = "Hammetts blog";
642 blog.Save();
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);
654 [Test]
655 public void ExistsTest()
657 ActiveRecordStarter.Initialize(GetConfigSource());
658 ActiveRecordStarter.RegisterTypes(typeof(Blog), typeof(Post));
659 Recreate();
661 Blog blog = new Blog();
662 blog.Name = "hammett's blog";
663 blog.Author = "hamilton verissimo";
664 blog.Save();
666 Assert.IsTrue(blog.Id > 0);
667 Assert.IsTrue(Blog.Exists(blog.Id));
669 blog = new Blog();
670 blog.Name = "chad's blog";
671 blog.Author = "chad humphries";
672 blog.Save();
674 Assert.IsTrue(Blog.Exists(blog.Id));
676 Assert.IsFalse(Blog.Exists(1000));
679 [Test]
680 public void ExistsByCriterion()
682 ActiveRecordStarter.Initialize(GetConfigSource());
683 ActiveRecordStarter.RegisterTypes(typeof(Blog), typeof(Post));
684 Recreate();
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";
694 blog.Save();
696 Assert.IsTrue(blog.Id > 0);
697 Assert.IsTrue(Blog.Exists(
698 Expression.Eq("Name", blog.Name),
699 Expression.Eq("Author", blog.Author)));
701 blog = new Blog();
702 blog.Name = "chad's blog";
703 blog.Author = "chad humphries";
704 blog.Save();
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));
719 Recreate();
721 ActiveRecordStarter.ResetInitializationFlag();
722 ActiveRecordStarter.Initialize(GetConfigSource(), typeof(ModelClassWithBrokenField));
724 ModelClassWithBrokenField brokenClass = new ModelClassWithBrokenField();
725 brokenClass.Broken = true;
726 brokenClass.Save();