Better handling of complex primary key params.
[castle.git] / ActiveRecord / Castle.ActiveRecord.Tests / CountQueryTestCase.cs
blob0504953dad12cb3d1373e1a3859dc3709ee99ddb
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.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;
29 [TestFixture]
30 public class CountQueryTestCase : AbstractActiveRecordTest
32 // overrides the setup in the base class
33 public void Prepare()
35 ActiveRecordStarter.ResetInitializationFlag();
36 ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Blog), typeof(Post));
38 ActiveRecordStarter.CreateSchema();
40 Post.DeleteAll();
41 Blog.DeleteAll();
43 Blog blog = new Blog();
44 blog.Name = "hammett's blog";
45 blog.Author = "hamilton verissimo";
47 blog.Save();
49 Post p;
51 p = new Post(blog, "a", "b", "c");
52 p.Save();
54 p = new Post(blog, "d", "e", "c");
55 p.Save();
57 p = new Post(blog, "f", "g", "h");
58 p.Save();
61 // Test CountQuery(type)
62 [Test]
63 public void CountQuery_ByType()
65 Prepare();
66 CountQuery cq = new CountQuery(typeof(Post));
67 int recordCount = (int)ActiveRecordMediator.ExecuteQuery(cq);
68 Assert.AreEqual(3, recordCount);
71 // Test CountQuery(type, criteria)
72 [Test]
73 public void CountQuery_ByTypeAndCriteria()
75 Prepare();
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)
86 [Test]
87 public void CountQuery_PositionalParameters()
89 Prepare();
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)
97 [Test]
98 public void CountQuery_DetachedCriteria()
100 Prepare();
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()
112 Prepare();
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);