From c565e345025e8c589e4b1804af43ecddea64d4ba Mon Sep 17 00:00:00 2001 From: markpollack Date: Sat, 31 May 2008 04:07:44 +0000 Subject: [PATCH] Add files overlooked in migration to svn --- .../Data/Objects/AbstractAdoQueryTests.cs | 66 +++++++++ .../Data/Objects/AdoOperationTests.cs | 112 +++++++++++++++ .../Data/Objects/AdoQueryTests.cs | 133 +++++++++--------- .../Data/Objects/StoredProcedureTests.cs | 109 ++++++++++++++ 4 files changed, 352 insertions(+), 68 deletions(-) create mode 100644 test/Spring/Spring.Data.Tests/Data/Objects/AbstractAdoQueryTests.cs create mode 100644 test/Spring/Spring.Data.Tests/Data/Objects/AdoOperationTests.cs create mode 100644 test/Spring/Spring.Data.Tests/Data/Objects/StoredProcedureTests.cs diff --git a/test/Spring/Spring.Data.Tests/Data/Objects/AbstractAdoQueryTests.cs b/test/Spring/Spring.Data.Tests/Data/Objects/AbstractAdoQueryTests.cs new file mode 100644 index 00000000..cbcc01c6 --- /dev/null +++ b/test/Spring/Spring.Data.Tests/Data/Objects/AbstractAdoQueryTests.cs @@ -0,0 +1,66 @@ +#region License + +/* + * Copyright © 2002-2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#endregion + +using System.Data; +using Rhino.Mocks; +using Spring.Data.Common; + +namespace Spring.Data.Objects +{ + /// + /// Contains shared mocking calls for performing tests on AdoQuery and subclasses. + /// + public abstract class AbstractAdoQueryTests + { + + protected MockRepository mocks; + protected IDbProvider provider; + protected IDbCommand command; + + + public void SetUpMocks() + { + mocks = new MockRepository(); + provider = (IDbProvider)mocks.DynamicMock(typeof(IDbProvider)); + + IDbConnection connection = (IDbConnection)mocks.DynamicMock(typeof(IDbConnection)); + + Expect.Call(provider.CreateConnection()).Return(connection); + + // Creating a query (setting DbProvider property) + // will call new DbParameters(IDbProvider), which is a real pain to mock. + // to store the declared parameters. + + command = (IDbCommand)mocks.DynamicMock(typeof(IDbCommand)); + //This IDbCommand is used as a container for the underlying parameter collection. + Expect.Call(provider.CreateCommand()).Return(command); + + //Create a real instance of IDbParameters to stored the declared parameters + IDbProvider realDbProvider = DbProviderFactory.GetDbProvider("System.Data.SqlClient"); + IDbParameters dbParameters = new DbParameters(realDbProvider); + + //Pass real instance into mock instance. + Expect.Call(command.Parameters).Return(dbParameters.DataParameterCollection); + Expect.Call(provider.CreateCommand()).Return(command); + + // done with init of DbParameters mock/stubbing + } + } +} \ No newline at end of file diff --git a/test/Spring/Spring.Data.Tests/Data/Objects/AdoOperationTests.cs b/test/Spring/Spring.Data.Tests/Data/Objects/AdoOperationTests.cs new file mode 100644 index 00000000..509d0a45 --- /dev/null +++ b/test/Spring/Spring.Data.Tests/Data/Objects/AdoOperationTests.cs @@ -0,0 +1,112 @@ +#region License + +/* + * Copyright © 2002-2007 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#endregion +#region License + +/* + * Copyright © 2002-2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#endregion + +#region Imports + +using System.Data; +using NUnit.Framework; +using Spring.Dao; +using Spring.Data.Common; + +#endregion + +namespace Spring.Data.Objects +{ + /// + /// Tests for AdoOperation + /// + /// Mark Pollack + /// $Id: AdoOperationTests.cs,v 1.1 2008/05/30 21:10:00 markpollack Exp $ + [TestFixture] + public class AdoOperationTests + { + [SetUp] + public void Setup() + { + } + + [Test] + [ExpectedException(typeof(InvalidDataAccessApiUsageException))] + public void EmptySql() + { + TestAdoOperation operation = new TestAdoOperation(); + operation.Compile(); + } + + [Test] + [ExpectedException(typeof(InvalidDataAccessApiUsageException))] + public void DeclareParameterAfterCompile() + { + TestAdoOperation operation = new TestAdoOperation(); + operation.DbProvider = DbProviderFactory.GetDbProvider("System.Data.SqlClient"); + operation.Sql = "select * from table"; + operation.Compile(); + IDbParameters parameters = new DbParameters(operation.DbProvider); + operation.DeclaredParameters = parameters; + } + + [Test] + [ExpectedException(typeof(InvalidDataAccessApiUsageException))] + public void TooFewParameters() + { + TestAdoOperation operation = new TestAdoOperation(); + operation.DbProvider = DbProviderFactory.GetDbProvider("System.Data.SqlClient"); + operation.Sql = "select * from table"; + IDbParameters parameters = new DbParameters(operation.DbProvider); + parameters.Add("name"); + operation.DeclaredParameters = parameters; + operation.Compile(); + operation.ValidateParams(null); + } + + } + + internal class TestAdoOperation : AdoOperation + { + protected override void CompileInternal() + { + + } + + public void ValidateParams(params object[] inParamValues) + { + ValidateParameters(inParamValues); + } + } +} \ No newline at end of file diff --git a/test/Spring/Spring.Data.Tests/Data/Objects/AdoQueryTests.cs b/test/Spring/Spring.Data.Tests/Data/Objects/AdoQueryTests.cs index 728bbf33..da25b255 100644 --- a/test/Spring/Spring.Data.Tests/Data/Objects/AdoQueryTests.cs +++ b/test/Spring/Spring.Data.Tests/Data/Objects/AdoQueryTests.cs @@ -16,15 +16,35 @@ * limitations under the License. */ +#endregion +#region License + +/* + * Copyright © 2002-2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #endregion #region Imports using System.Collections; using System.Data; -using DotNetMock.Dynamic; +using System.Data.SqlClient; using NUnit.Framework; using Rhino.Mocks; +using Spring.Dao; using Spring.Data.Common; #endregion @@ -32,43 +52,39 @@ using Spring.Data.Common; namespace Spring.Data.Objects { /// - /// This class contains tests for AdoQuery subclasses + /// Tests for AdoQuery /// /// Mark Pollack - /// $Id: AdoQueryTests.cs,v 1.3 2007/07/25 08:25:34 markpollack Exp $ + /// $Id: AdoQueryTests.cs,v 1.4 2008/05/30 21:10:00 markpollack Exp $ [TestFixture] - public class AdoQueryTests + public class AdoQueryTests : AbstractAdoQueryTests { - private MockRepository mocks; + + private static string SELECT_ID_WHERE = "select id from custmr"; + private static string[] COLUMN_NAMES = new string[] {"id", "forename"}; + private static DbType[] COLUMN_TYPES = new DbType[] {DbType.Int32, DbType.String}; [SetUp] public void Setup() { - mocks = new MockRepository(); + SetUpMocks(); + + } -/* + [Test] - public void QueryWithoutContextRhino() + public void MappingAdoQueryWithContextWithoutParams() { - IDbProvider provider = (IDbProvider) mocks.DynamicMock(typeof (IDbProvider)); - - IDbConnection connection = (IDbConnection) mocks.DynamicMock(typeof (IDbConnection)); - - Expect.Call(provider.CreateConnection()).Return(connection); - - IDbCommand command = (IDbCommand) mocks.DynamicMock(typeof (IDbCommand)); - - IDataReader reader = (IDataReader) mocks.DynamicMock(typeof (IDataReader)); + IDataReader reader = (IDataReader)mocks.DynamicMock(typeof(IDataReader)); Expect.Call(reader.Read()).Return(true); Expect.Call(reader.GetInt32(0)).Return(1); Expect.Call(reader.Read()).Return(false); Expect.Call(command.ExecuteReader()).Return(reader); - Expect.Call(provider.CreateCommand()).Return(command); - mocks.ReplayAll(); - IntMappingQueryWithNoContext queryWithNoContext = new IntMappingQueryWithNoContext(provider); + mocks.ReplayAll(); + IntMappingQueryWithContext queryWithNoContext = new IntMappingQueryWithContext(provider); queryWithNoContext.Compile(); IList list = queryWithNoContext.QueryByNamedParam(null, null); Assert.IsTrue(list.Count != 0); @@ -78,66 +94,39 @@ namespace Spring.Data.Objects } mocks.VerifyAll(); - - - } - */ + [Test] - public void QueryWithoutContext() + [ExpectedException(typeof(InvalidDataAccessApiUsageException))] + public void QueryWithoutEnoughParams() { + SqlParameter sqlParameter1 = new SqlParameter(); + Expect.Call(command.CreateParameter()).Return(sqlParameter1); + Expect.Call(provider.CreateParameterNameForCollection(COLUMN_NAMES[0])).Return("@" + COLUMN_NAMES[0]); - //Prepare provider - IDynamicMock mockProvider = new DynamicMock(typeof(IDbProvider)); - - //Prepare connection to return from provider - IDynamicMock mockConnection = new DynamicMock(typeof(IDbConnection)); - IDbConnection connection = (IDbConnection) mockConnection.Object; - mockProvider.ExpectAndReturn("CreateConnection", connection); - - //Prepare command - IDynamicMock mockCommand = new DynamicMock(typeof(IDbCommand)); - - //Prepare reader to return from command - IDynamicMock mockReader = new DynamicMock(typeof (IDataReader)); - mockReader.ExpectAndReturn("Read", true); - mockReader.ExpectAndReturn("GetInt32", 1, 0); - mockReader.ExpectAndReturn("Read", false); - IDataReader reader = (IDataReader) mockReader.Object; - mockCommand.ExpectAndReturn("ExecuteReader", reader); - - //Preppare command to return from provider - IDbCommand command = (IDbCommand)mockCommand.Object; - mockProvider.ExpectAndReturn("CreateCommand", command); - + SqlParameter sqlParameter2 = new SqlParameter(); + Expect.Call(command.CreateParameter()).Return(sqlParameter2); + Expect.Call(provider.CreateParameterNameForCollection(COLUMN_NAMES[1])).Return("@" + COLUMN_NAMES[1]); - IDbProvider dbProvider = (IDbProvider)mockProvider.Object; - - - //Test MappingAdoQueryWithContext - IntMappingQueryWithNoContext queryWithNoContext = new IntMappingQueryWithNoContext(dbProvider); - queryWithNoContext.Compile(); - //IList list = queryWithNoContext.QueryByNamedParam(null, null); - IList list = queryWithNoContext.Query(); - Assert.IsTrue(list.Count != 0); - foreach (int count in list) - { - Assert.AreEqual(1, count); - } - - - - - + mocks.ReplayAll(); + IntMappingAdoQuery query = new IntMappingAdoQuery(); + query.DbProvider = provider; + query.Sql = SELECT_ID_WHERE; + query.DeclaredParameters.Add(COLUMN_NAMES[0], COLUMN_TYPES[0]); + query.DeclaredParameters.Add(COLUMN_NAMES[1], COLUMN_TYPES[1]); + query.Compile(); + IList list = query.Query(); + mocks.VerifyAll(); } - public class IntMappingQueryWithNoContext : MappingAdoQueryWithContext + + public class IntMappingQueryWithContext : MappingAdoQueryWithContext { private static string sql = "select id from custmr"; - public IntMappingQueryWithNoContext(IDbProvider dbProvider) + public IntMappingQueryWithContext(IDbProvider dbProvider) : base(dbProvider, sql) { CommandType = CommandType.Text; @@ -152,6 +141,14 @@ namespace Spring.Data.Objects return reader.GetInt32(0); } } + + public class IntMappingAdoQuery : MappingAdoQuery + { + protected override object MapRow(IDataReader reader, int rowNum) + { + return reader.GetInt32(0); + } + } } } \ No newline at end of file diff --git a/test/Spring/Spring.Data.Tests/Data/Objects/StoredProcedureTests.cs b/test/Spring/Spring.Data.Tests/Data/Objects/StoredProcedureTests.cs new file mode 100644 index 00000000..356c8e7f --- /dev/null +++ b/test/Spring/Spring.Data.Tests/Data/Objects/StoredProcedureTests.cs @@ -0,0 +1,109 @@ +#region License + +/* + * Copyright © 2002-2007 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#endregion +#region License + +/* + * Copyright © 2002-2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#endregion + +#region Imports + +using System; +using System.Data; +using System.Data.SqlClient; +using NUnit.Framework; +using Rhino.Mocks; +using Spring.Data.Common; + +#endregion + +namespace Spring.Data.Objects +{ + /// + /// Tests for StoredProcedure + /// + /// Mark Pollack + /// $Id: StoredProcedureTests.cs,v 1.1 2008/05/30 21:10:00 markpollack Exp $ + [TestFixture] + public class StoredProcedureTests : AbstractAdoQueryTests + { + [SetUp] + public void Setup() + { + SetUpMocks(); + } + + [Test] + public void NullArg() + { + SqlParameter sqlParameter1 = new SqlParameter(); + Expect.Call(command.CreateParameter()).Return(sqlParameter1); + Expect.Call(provider.CreateParameterNameForCollection("ptest")).Return("@ptest"); + + //Create a real instance of IDbParameters to store the executable parameters + //IDbProvider realDbProvider = DbProviderFactory.GetDbProvider("System.Data.SqlClient"); + //IDbParameters dbParameters = new DbParameters(realDbProvider); + IDataParameterCollection dbParamCollection = new SqlCommand().Parameters; + Expect.Call(command.Parameters).Return(dbParamCollection); + + //provide the same instance to another call to extract output params + Expect.Call(command.Parameters).Return(dbParamCollection); + + + mocks.ReplayAll(); + NullArg na = new NullArg(provider); + na.Execute(null); + mocks.VerifyAll(); + } + + + + + } + + internal class NullArg : StoredProcedure + { + public NullArg(IDbProvider provider) + : base(provider, "takes_null") + { + DeclaredParameters.Add("ptest", DbType.String); + Compile(); + } + + public void Execute(string s) + { + ExecuteNonQuery(s); + } + } +} \ No newline at end of file