Added ability to order the execution of dictionary adapter behaviors.
[castle.git] / Experiments / Attic / Generator / Castle.ActiveRecord.Generator.Components.Tests / RelationshipInferenceServiceTestCase.cs
bloba0111e0cae0eff8964d0a67fe2adcdedcb70ec1c
1 // Copyright 2004-2007 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.Generator.Components.Tests
17 using System;
18 using System.Collections;
19 using System.Data.OleDb;
21 using NUnit.Framework;
23 using Castle.ActiveRecord.Generator.Components.Database;
25 /// <summary>
26 /// TODO: Map HasOne association
27 /// </summary>
28 [TestFixture]
29 public class RelationshipInferenceServiceTestCase : AbstractContainerTestCase
31 [Test]
32 public void BlogHasManyPosts()
34 InitKernel();
35 IRelationshipInferenceService relService = ObtainService();
37 TableDefinition blogTable;
38 TableDefinition postTable;
40 BuildBlogPostsStructure(out blogTable, out postTable);
42 BuildContext context = new BuildContext();
44 ActiveRecordDescriptor arDesc = new ActiveRecordDescriptor();
46 ActiveRecordPropertyDescriptor[] descs = relService.InferRelations( arDesc, blogTable, context );
48 Assert.IsNotNull(descs);
49 Assert.AreEqual( 1, descs.Length );
51 ActiveRecordHasManyDescriptor desc1 = descs[0] as ActiveRecordHasManyDescriptor;
52 Assert.IsNotNull(desc1);
53 Assert.IsNotNull(desc1.TargetType);
54 Assert.IsNotNull(desc1.PropertyType);
56 Assert.AreEqual( "Posts", desc1.PropertyName );
57 Assert.AreEqual( "blog_id", desc1.ColumnName );
58 Assert.AreEqual( typeof(IList), desc1.PropertyType );
60 ActiveRecordDescriptor targetARDescriptor = context.GetNextPendent();
61 Assert.AreSame( postTable, targetARDescriptor.Table );
64 [Test]
65 public void PostBelongsToBlog()
67 InitKernel();
68 IRelationshipInferenceService relService = ObtainService();
70 TableDefinition blogTable;
71 TableDefinition postTable;
73 BuildBlogPostsStructure(out blogTable, out postTable);
75 BuildContext context = new BuildContext();
77 ActiveRecordDescriptor arDesc = new ActiveRecordDescriptor();
79 ActiveRecordPropertyDescriptor[] descs = relService.InferRelations( arDesc, postTable, context );
81 Assert.IsNotNull(descs);
82 Assert.AreEqual( 1, descs.Length );
84 ActiveRecordBelongsToDescriptor desc1 = descs[0] as ActiveRecordBelongsToDescriptor;
85 Assert.IsNotNull(desc1);
86 Assert.IsNotNull(desc1.TargetType);
87 Assert.IsNull(desc1.PropertyType);
89 Assert.AreEqual( "Blog", desc1.PropertyName );
90 Assert.AreEqual( "blog_id", desc1.ColumnName );
92 ActiveRecordDescriptor targetARDescriptor = context.GetNextPendent();
93 Assert.AreSame( blogTable, targetARDescriptor.Table );
96 [Test]
97 public void SelfReference()
99 InitKernel();
100 IRelationshipInferenceService relService = ObtainService();
102 DatabaseDefinition dbdef = new DatabaseDefinition("alias");
104 TableDefinition categoryTable = new TableDefinition("categories", dbdef );
105 categoryTable.AddColumn( new ColumnDefinition("id", true, false, true, false, OleDbType.Integer) );
106 categoryTable.AddColumn( new ColumnDefinition("name", false, false, false, false, OleDbType.VarChar) );
107 categoryTable.AddColumn( new ColumnDefinition("parent_id", false, true, false, false, OleDbType.Integer, categoryTable) );
109 categoryTable.AddManyRelation(categoryTable);
111 BuildContext context = new BuildContext();
113 ActiveRecordDescriptor arDesc = new ActiveRecordDescriptor();
115 ActiveRecordPropertyDescriptor[] descs = relService.InferRelations( arDesc, categoryTable, context );
117 Assert.IsFalse(context.HasPendents);
119 Assert.IsNotNull(descs);
120 Assert.AreEqual( 2, descs.Length );
122 ActiveRecordHasManyDescriptor desc1 = descs[0] as ActiveRecordHasManyDescriptor;
123 Assert.IsNotNull(desc1);
124 Assert.IsNotNull(desc1.TargetType);
125 Assert.IsNotNull(desc1.PropertyType);
127 Assert.AreEqual( "Categories", desc1.PropertyName );
128 Assert.AreEqual( "parent_id", desc1.ColumnName );
129 Assert.AreEqual( typeof(IList), desc1.PropertyType );
131 ActiveRecordBelongsToDescriptor desc2 = descs[1] as ActiveRecordBelongsToDescriptor;
132 Assert.IsNotNull(desc2);
133 Assert.IsNotNull(desc2.TargetType);
134 Assert.IsNull(desc2.PropertyType);
135 Assert.AreEqual( "Category", desc2.PropertyName );
136 Assert.AreEqual( "parent_id", desc2.ColumnName );
139 private void InitKernel()
141 Kernel.AddComponent( "relationsService", typeof(IRelationshipInferenceService), typeof(RelationshipInferenceService) );
142 Kernel.AddComponent( "nameService", typeof(INamingService), typeof(NamingService) );
143 Kernel.AddComponent( "typeinf", typeof(ITypeInferenceService), typeof(TypeInferenceService) );
146 private IRelationshipInferenceService ObtainService()
148 return Kernel[ typeof(IRelationshipInferenceService) ] as IRelationshipInferenceService;
151 private void BuildBlogPostsStructure(out TableDefinition blogTable, out TableDefinition postTable)
153 DatabaseDefinition dbdef = new DatabaseDefinition("alias");
155 blogTable = new TableDefinition("blogs", dbdef );
156 blogTable.AddColumn( new ColumnDefinition("id", true, false, true, false, OleDbType.Integer) );
157 blogTable.AddColumn( new ColumnDefinition("name", false, false, false, false, OleDbType.VarChar) );
159 postTable = new TableDefinition("posts", dbdef );
160 postTable.AddColumn( new ColumnDefinition("id", true, false, true, false, OleDbType.Integer) );
161 postTable.AddColumn( new ColumnDefinition("name", false, false, false, false, OleDbType.VarChar) );
162 postTable.AddColumn( new ColumnDefinition("blog_id", false, true, false, false, OleDbType.VarChar, blogTable) );
164 blogTable.AddManyRelation(postTable);