Fixing an issue with output parameters that are of type IntPtr
[castle.git] / Experiments / Attic / Generator / Castle.ActiveRecord.Generator.Components.Tests / RelationshipBuilderTestCase.cs
blobf0dfba5af40e2f9cfbc1409220038f23d6e60be5
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.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 [TestFixture]
26 public class RelationshipBuilderTestCase : AbstractContainerTestCase
28 [Test]
29 public void BelongsTo()
31 InitKernel();
32 IRelationshipBuilder relService = ObtainService();
34 TableDefinition blogTable;
35 TableDefinition postTable;
37 BuildBlogPostsStructure(out blogTable, out postTable);
39 ActiveRecordDescriptor desc = new ActiveRecordDescriptor("Post");
40 ActiveRecordDescriptor targetDesc = new ActiveRecordDescriptor("Blog");
42 RelationshipInfo info = new RelationshipInfo( AssociationEnum.BelongsTo, desc, targetDesc );
43 info.ParentCol = new ColumnDefinition("blog_id", false, true, true, false, OleDbType.Numeric);
45 ActiveRecordPropertyRelationDescriptor propDesc = relService.Build( info );
46 Assert.IsNotNull(propDesc);
47 Assert.IsNotNull(propDesc as ActiveRecordBelongsToDescriptor);
48 Assert.AreEqual( "Blog", propDesc.PropertyName );
49 Assert.AreEqual( targetDesc, propDesc.TargetType );
50 Assert.AreEqual( "blog_id", propDesc.ColumnName );
53 [Test]
54 public void HasMany()
56 InitKernel();
57 IRelationshipBuilder relService = ObtainService();
59 TableDefinition blogTable;
60 TableDefinition postTable;
62 BuildBlogPostsStructure(out blogTable, out postTable);
64 ActiveRecordDescriptor desc = new ActiveRecordDescriptor("Blog");
65 ActiveRecordDescriptor targetDesc = new ActiveRecordDescriptor("Post");
67 RelationshipInfo info = new RelationshipInfo( AssociationEnum.HasMany, desc, targetDesc );
68 info.ChildCol = new ColumnDefinition("blog_id", false, true, true, false, OleDbType.Numeric);
70 ActiveRecordPropertyRelationDescriptor propDesc = relService.Build( info );
71 Assert.IsNotNull(propDesc);
72 Assert.IsNotNull(propDesc as ActiveRecordHasManyDescriptor);
73 Assert.AreEqual( "Posts", propDesc.PropertyName );
74 Assert.AreEqual( targetDesc, propDesc.TargetType );
75 Assert.AreEqual( "blog_id", propDesc.ColumnName );
78 [Test]
79 public void HasÁndBelongsToMany()
81 InitKernel();
82 IRelationshipBuilder relService = ObtainService();
84 DatabaseDefinition dbdef = new DatabaseDefinition("alias");
86 TableDefinition compTable = new TableDefinition("companies", dbdef );
87 compTable.AddColumn( new ColumnDefinition("id", true, false, true, false, OleDbType.Integer) );
88 compTable.AddColumn( new ColumnDefinition("name", false, false, false, false, OleDbType.VarChar) );
90 TableDefinition peopleTable = new TableDefinition("people", dbdef );
91 peopleTable.AddColumn( new ColumnDefinition("id", true, false, true, false, OleDbType.Integer) );
92 peopleTable.AddColumn( new ColumnDefinition("name", false, false, false, false, OleDbType.VarChar) );
94 TableDefinition assocTable = new TableDefinition("companiespeople", dbdef );
95 assocTable.AddColumn( new ColumnDefinition("comp_id", true, true, true, false, OleDbType.Integer) );
96 assocTable.AddColumn( new ColumnDefinition("person_id", true, true, false, false, OleDbType.Integer) );
98 ActiveRecordDescriptor desc = new ActiveRecordDescriptor("Company");
99 ActiveRecordDescriptor targetDesc = new ActiveRecordDescriptor("Person");
101 RelationshipInfo info = new RelationshipInfo( AssociationEnum.HasAndBelongsToMany, desc, targetDesc );
102 info.AssociationTable = assocTable;
103 info.ParentCol = new ColumnDefinition("comp_id", false, true, false, true, OleDbType.Integer);
104 info.ChildCol = new ColumnDefinition("person_id", false, true, false, true, OleDbType.Integer);
106 ActiveRecordPropertyRelationDescriptor propDesc = relService.Build( info );
107 Assert.IsNotNull(propDesc as ActiveRecordHasAndBelongsToManyDescriptor);
108 Assert.IsNotNull(propDesc);
109 Assert.AreEqual( "People", propDesc.PropertyName );
110 Assert.AreEqual( targetDesc, propDesc.TargetType );
111 Assert.AreEqual( "person_id", propDesc.ColumnName );
112 Assert.AreEqual( "comp_id", (propDesc as ActiveRecordHasAndBelongsToManyDescriptor).ColumnKey);
115 private void InitKernel()
117 Kernel.AddComponent( "relationsService", typeof(IRelationshipInferenceService), typeof(RelationshipInferenceService) );
118 Kernel.AddComponent( "relationBuilder", typeof(IRelationshipBuilder), typeof(RelationshipBuilder) );
119 Kernel.AddComponent( "nameService", typeof(INamingService), typeof(NamingService) );
120 Kernel.AddComponent( "typeinf", typeof(ITypeInferenceService), typeof(TypeInferenceService) );
123 private IRelationshipBuilder ObtainService()
125 return Kernel[ typeof(IRelationshipBuilder) ] as IRelationshipBuilder;
128 private void BuildBlogPostsStructure(out TableDefinition blogTable, out TableDefinition postTable)
130 DatabaseDefinition dbdef = new DatabaseDefinition("alias");
132 blogTable = new TableDefinition("blogs", dbdef );
133 blogTable.AddColumn( new ColumnDefinition("id", true, false, true, false, OleDbType.Integer) );
134 blogTable.AddColumn( new ColumnDefinition("name", false, false, false, false, OleDbType.VarChar) );
136 postTable = new TableDefinition("posts", dbdef );
137 postTable.AddColumn( new ColumnDefinition("id", true, false, true, false, OleDbType.Integer) );
138 postTable.AddColumn( new ColumnDefinition("name", false, false, false, false, OleDbType.VarChar) );
139 postTable.AddColumn( new ColumnDefinition("blog_id", false, true, false, false, OleDbType.VarChar, blogTable) );
141 blogTable.AddManyRelation(postTable);