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
.Generator
.Components
.Tests
18 using System
.Collections
;
19 using System
.Data
.OleDb
;
21 using NUnit
.Framework
;
23 using Castle
.ActiveRecord
.Generator
.Components
.Database
;
26 /// TODO: Map HasOne association
29 public class RelationshipInferenceServiceTestCase
: AbstractContainerTestCase
32 public void BlogHasManyPosts()
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
);
65 public void PostBelongsToBlog()
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
);
97 public void SelfReference()
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
);