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();
}
}
}