Click here to Skip to main content
15,884,472 members
Articles / Database Development / SQL Server / SQL Server 2008

Simple Data Access in C#

Rate me:
Please Sign up or sign in to vote.
4.11/5 (15 votes)
11 Jan 2009CPOL5 min read 83.6K   1.4K   58  
Fast and easy to use data access class library.
using System;
using System.Data;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using Yap.Data.Client;


namespace Yap.Data.UnitTest
{
	[TestClass]
	public class CommandTestFixture
	{
		[TestMethod]
		public void Constructor_Default_PropertiesShouldBeInitializedWithDefaultValues()
		{
			Command command = new DummyCommand();
			Assert.AreEqual(String.Empty, command.Text, "Command.Text should be String.Empty by default.");
			Assert.AreEqual(String.Empty, command.Description, "Command.Description should be String.Empty by default.");
			Assert.IsNotNull(command.Parameters, "Command.Parameters collection should be initialized by default.");
		}

		[TestMethod]
		[ExpectedException(typeof(InvalidOperationException))]
		public void Execute_TextIsEmpty_ThrowInvalidOperationException()
		{
			Command command = new DummyCommand {Text = String.Empty};
			command.Execute();
		}

		[TestMethod]
		public void Equals_OtherIsNull_ReturnFalse()
		{
			Command command = new DummyCommand();
			Assert.IsFalse(command.Equals(null), "Equals() with null should return false.");
		}

		[TestMethod]
		public void Equals_FullTest()
		{
			Command one = new DummyCommand();
			Command another = new DummyCommand();
			Assert.AreEqual(one, another, "Equals() with equal command should return true.");
			one.Text = "differs";
			Assert.AreNotEqual(one, another, "Equals() with not equal command should return false.");
			one.Text = another.Text = null;
			Assert.AreEqual(one, another, "Equals() with equal command should return true.");
			one.Text = another.Text = "SELECT * FROM dual";
			Assert.AreEqual(one, another, "Equals() with equal command should return true.");
			one.Description = "differs";
			Assert.AreNotEqual(one, another, "Equals() with not equal command should return false.");
			one.Description = another.Description = null;
			Assert.AreEqual(one, another, "Equals() with equal command should return true.");
			one.Description = another.Description = "description";
			Assert.AreEqual(one, another, "Equals() with equal command should return true.");
			one.Parameters.Add("differs", null);
			Assert.AreNotEqual(one, another, "Equals() with not equal command should return false.");
			one.Parameters.Clear();
			Assert.AreEqual(one, another, "Equals() with equal command should return true.");
			one.Parameters.Add("differs", null);
			another.Parameters.Add("differs", "not null");
			Assert.AreNotEqual(one, another, "Equals() with not equal command should return false.");
			one.Parameters.Clear();
			another.Parameters.Clear();
			Assert.AreEqual(one, another, "Equals() with equal command should return true.");
			one.Parameters.Add("first", "first value");
			one.Parameters.Add("second", "second value");
			another.Parameters.Add("second", "second value");
			another.Parameters.Add("first", "first value");
			Assert.AreEqual(one, another, "Equals() with equal command should return true.");
		}

		[TestMethod]
		public void ToString__ShouldBeEqualToText()
		{
			Command command = new DummyCommand();
			Assert.AreEqual(command.Text, command.ToString(), "ToString() should be equal to Command.Text.");
			command.Text = "SELECT * FROM dual";
			Assert.AreEqual(command.Text, command.ToString(), "ToString() should be equal to Command.Text.");
			command.Text = null;
			Assert.AreEqual(command.Text, command.ToString(), "ToString() should be equal to Command.Text.");
		}

		[TestMethod]
		public void GetHashCode_TextIsEmpty_ShouldBeEqualTo0()
		{
			Command command = new DummyCommand();
			Assert.AreEqual(0, command.GetHashCode(), "GetHashCode() should be 0 if Command.Text is empty.");
		}

		[TestMethod]
		public void GetHashCode_TextIsNotEmpty_ShouldBeEqualToTextGetHashCode()
		{
			Command command = new DummyCommand {Text = "SELECT * FROM dual"};
			Assert.AreEqual(command.Text.GetHashCode(), command.GetHashCode(), "GetHashCode() should be equal to Command.Text.GetHashCode() if Command.Text is not empty.");
		}
	}

	public class DummyCommand : Command
	{
		protected override void Execute(IDbCommand command, IDatabaseProvider provider)
		{
			throw new NotImplementedException();
		}
	}

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions