1 // Copyright 2004-2008 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
.Tests
18 using System
.Diagnostics
;
20 using Castle
.ActiveRecord
.Framework
;
21 using Castle
.ActiveRecord
.Queries
;
23 using Castle
.ActiveRecord
.Tests
.Model
;
25 using NHibernate
.Expression
;
27 using NUnit
.Framework
;
30 public class CountQueryTestCase
: AbstractActiveRecordTest
32 // overrides the setup in the base class
35 ActiveRecordStarter
.ResetInitializationFlag();
36 ActiveRecordStarter
.Initialize(GetConfigSource(), typeof(Blog
), typeof(Post
));
38 ActiveRecordStarter
.CreateSchema();
43 Blog blog
= new Blog();
44 blog
.Name
= "hammett's blog";
45 blog
.Author
= "hamilton verissimo";
51 p
= new Post(blog
, "a", "b", "c");
54 p
= new Post(blog
, "d", "e", "c");
57 p
= new Post(blog
, "f", "g", "h");
61 // Test CountQuery(type)
63 public void CountQuery_ByType()
66 CountQuery cq
= new CountQuery(typeof(Post
));
67 int recordCount
= (int)ActiveRecordMediator
.ExecuteQuery(cq
);
68 Assert
.AreEqual(3, recordCount
);
71 // Test CountQuery(type, criteria)
73 public void CountQuery_ByTypeAndCriteria()
76 ICriterion
[] criterionArray
= new ICriterion
[]
78 Expression
.Eq("Category", "c")
80 CountQuery cq
= new CountQuery(typeof(Post
), criterionArray
);
81 int recordCount
= (int)ActiveRecordMediator
.ExecuteQuery(cq
);
82 Assert
.AreEqual(2, recordCount
);
85 // Test CountQuery(type, filter, positional_parameters)
87 public void CountQuery_PositionalParameters()
90 object[] parameters
= new object[] { "c" }
;
91 CountQuery cq
= new CountQuery(typeof(Post
), "Category = ?", parameters
);
92 int recordCount
= (int)ActiveRecordMediator
.ExecuteQuery(cq
);
93 Assert
.AreEqual(2, recordCount
);
96 // Test CountQuery(type, Detached Criteria)
98 public void CountQuery_DetachedCriteria()
101 DetachedCriteria detachedCriteria
= DetachedCriteria
.For(typeof(Post
));
102 detachedCriteria
.Add(Expression
.Eq("Category", "c"));
103 CountQuery cq
= new CountQuery(typeof(Post
), detachedCriteria
);
104 int recordCount
= (int)ActiveRecordMediator
.ExecuteQuery(cq
);
105 Assert
.AreEqual(2, recordCount
);
108 // Test CountQuery(type, criteria)
109 [Test
, ExpectedException(typeof(Castle
.ActiveRecord
.Framework
.ActiveRecordException
))]
110 public void CountQuery_ByTypeAndBadCriteria()
113 ICriterion
[] criterionArray
= new ICriterion
[]
115 // misspelling on purpose
116 Expression
.Eq("Category;", "c")
118 CountQuery cq
= new CountQuery(typeof(Post
), criterionArray
);
119 int recordCount
= (int)ActiveRecordMediator
.ExecuteQuery(cq
);
120 Assert
.AreEqual(2, recordCount
);