Better handling of complex primary key params.
[castle.git] / ActiveRecord / Castle.ActiveRecord.Tests / TableHierarchyTestCase.cs
blobcf8f229fe6f334d50fdbaa638d738cda67ee1034
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.Collections;
20 using NUnit.Framework;
22 using Castle.ActiveRecord.Tests.Model;
23 using Iesi.Collections;
25 [TestFixture]
26 public class TableHierarchyTestCase : AbstractActiveRecordTest
28 [Test]
29 public void CompanyFirmAndClient()
31 ActiveRecordStarter.Initialize( GetConfigSource(),
32 typeof(Company), typeof(Client), typeof(Firm), typeof(Person) );
33 Recreate();
35 Client.DeleteAll();
36 Firm.DeleteAll();
37 Company.DeleteAll();
38 Person.DeleteAll();
40 Firm firm = new Firm("keldor");
41 firm.Save();
43 Client client = new Client("castle", firm);
44 client.Save();
46 Client[] clients = Client.FindAll();
47 Assert.AreEqual( 1, clients.Length );
49 Firm[] firms = Firm.FindAll();
50 Assert.AreEqual( 1, firms.Length );
52 Assert.AreEqual( firm.Id, firms[0].Id );
53 Assert.AreEqual( client.Id, clients[0].Id );
55 Assert.IsNotNull( clients[0].Firm );
56 Assert.AreEqual( firm.Id, clients[0].Firm.Id );
59 [Test]
60 public void ManyToMany()
62 ActiveRecordStarter.Initialize( GetConfigSource(),
63 typeof(Company), typeof(Client), typeof(Firm), typeof(Person) );
64 Recreate();
66 Company.DeleteAll();
67 Client.DeleteAll();
68 Firm.DeleteAll();
69 Person.DeleteAll();
71 Firm firm = new Firm("keldor");
72 Client client = new Client("castle", firm);
73 Company company = new Company("vs");
75 using(new SessionScope())
77 firm.Save();
78 client.Save();
79 company.Save();
81 Person person = new Person();
82 person.Name = "hammett";
84 person.Companies.Add( firm );
85 person.Companies.Add( client );
86 person.Companies.Add( company );
87 person.Save();
90 company = Company.Find( company.Id );
91 Assert.AreEqual(1, company.People.Count );
94 [Test]
95 public void ManyToManyUsingSet()
97 ActiveRecordStarter.Initialize(GetConfigSource(),
98 typeof(Order), typeof(Product)/*, typeof(LineItem)*/);
99 Recreate();
101 Order.DeleteAll();
102 Product.DeleteAll();
104 Order myOrder = new Order();
105 myOrder.OrderedDate = DateTime.Parse("05/09/2004");
106 Product coolGadget = new Product();
107 coolGadget.Name = "PSP";
108 coolGadget.Price = 250.39f;
110 using (new SessionScope())
112 coolGadget.Save();
113 ISet products = new ListSet();
114 products.Add(coolGadget);
115 myOrder.Products = products;
116 myOrder.Save();
119 Order secondRef2Order = Order.Find(myOrder.ID);
120 Assert.IsFalse(secondRef2Order.Products.IsEmpty);
122 Product secondRef2Product = Product.Find(coolGadget.ID);
123 Assert.AreEqual(1, secondRef2Product.Orders.Count);
126 [Test]
127 [Ignore("Jira issue for NH team")]
128 public void InvalidSessionCache()
130 ActiveRecordStarter.Initialize( GetConfigSource(),
131 typeof(Company), typeof(Client), typeof(Firm), typeof(Person) );
132 Recreate();
134 Company.DeleteAll();
135 Client.DeleteAll();
136 Firm.DeleteAll();
137 Person.DeleteAll();
139 Firm firm = new Firm("keldor");
140 Client client = new Client("castle", firm);
141 Company company = new Company("vs");
143 using(new SessionScope())
145 firm.Save();
146 client.Save();
147 company.Save();
150 using(new SessionScope())
154 Client c = Client.Find( firm.Id );
156 Assert.Fail("Exception was expected");
158 catch(Exception)
160 // Phew!!
163 Firm firm2 = Firm.Find( firm.Id );
165 Assert.IsNotNull(firm2);
169 [Test]
170 [Ignore("Create schema does not create all necessary tables for this test case")]
171 public void ManyToManyUsingIDBag()
173 ActiveRecordStarter.Initialize(GetConfigSource(),
174 typeof(OrderWithIDBag), typeof(ProductWithIDBag));
175 Recreate();
177 OrderWithIDBag.DeleteAll();
178 ProductWithIDBag.DeleteAll();
180 OrderWithIDBag myOrder = new OrderWithIDBag();
181 myOrder.OrderedDate = new DateTime(2006, 12, 25);
182 ProductWithIDBag coolGadget = new ProductWithIDBag();
183 coolGadget.Name = "Xbox 2";
184 coolGadget.Price = 330.23f;
186 using (new SessionScope())
188 coolGadget.Save();
189 IList products = new ArrayList();
190 products.Add(coolGadget);
191 myOrder.Products = products;
192 myOrder.Save();
195 OrderWithIDBag secondRef2Order = OrderWithIDBag.Find(myOrder.ID);
196 Assert.IsTrue(secondRef2Order.Products.Count > 0);
198 ProductWithIDBag secondRef2Product = ProductWithIDBag.Find(coolGadget.ID);
199 Assert.AreEqual(1, secondRef2Product.Orders.Count);