Code Snippet Details

<- Back to List


Language
C#.Net
Description
FakeDataProvider
Code
using System.Collections.Generic; using System.Data; namespace Alphabet.Framework.DAL { /// <summary> /// A Fake version of the DATA Provider for unit testing purposes. /// </summary> /// <remarks>Check the Actions taken and Generic commands lists after carrying out an action to verify it was as expected.</remarks> public class FakeDataProvider : IDataProvider { #region Constants and enums public enum ActionTypes : int { OpenConnection, CloseConnection, ExecuteStoredProcedureWithGenericCommand, ExecuteStoredProcedureWithSQL, GetDataTableWithGenericCommand, GetDataTableWithSQL, GetScalarValue, BeginTransaction, RollbackTransaction, CommitTransaction } public class TestValues { public const int TestScalarValue = 1; public const int RecordsAffected = 1; } #endregion #region Member variables private string _ConnectionString; private int _GenericCommandID; #endregion #region Properties private List<ActionTypes> _ActionsTaken = new List<ActionTypes>(); public List<ActionTypes> ActionsTaken { get { return _ActionsTaken; } } private Dictionary<int, GenericCommand> _GenericCommands = new Dictionary<int, GenericCommand>(); #endregion #region Constructors public FakeDataProvider(string connectionString) { _ConnectionString = connectionString; } #endregion #region Public Methods /// <summary> /// Tells you how many times a generic command was called. /// </summary> /// <param name="commandText">The generic command's command text, the name of the stored procedure or the sql.</param> /// <returns>The number of times the command was called.</returns> /// <remarks></remarks> public int GetGenericCommandTimesCalled(string commandText) { int timesCalled = 0; foreach (GenericCommand loopGenericCommand in _GenericCommands.Values) { if (loopGenericCommand.CommandText == commandText) { timesCalled += 1; } } return timesCalled; } public GenericCommand GetGenericCommand(string commandText, int occurenceToFind) { GenericCommand foundGenericCommand = null; int occurence = 0; foreach (GenericCommand loopGenericCommand in _GenericCommands.Values) { if (loopGenericCommand.CommandText == commandText) { if (occurence == occurenceToFind) { foundGenericCommand = loopGenericCommand; break; } occurence += 1; } } if (foundGenericCommand == null) { throw new KeyNotFoundException("No generic command found for command text=" + commandText + " occurence=" + occurenceToFind.ToString()); } else { return foundGenericCommand; } } public GenericCommand GetGenericCommand(string commandText) { GenericCommand foundGenericCommand = null; foreach (GenericCommand loopGenericCommand in _GenericCommands.Values) { if (loopGenericCommand.CommandText == commandText) { foundGenericCommand = loopGenericCommand; break; } } if (foundGenericCommand == null) { throw new KeyNotFoundException("No generic command found for command text=" + commandText); } else { return foundGenericCommand; } } public GenericCommand GetGenericCommand(int id) { return _GenericCommands[id]; } #endregion #region Private Methods private void AddGenericCommand(GenericCommand genericCommand) { _GenericCommands.Add(_GenericCommandID, genericCommand); _GenericCommandID += 1; } #endregion #region Fake Methods public void OpenConnection() { _ActionsTaken.Add(ActionTypes.OpenConnection); } public void CloseConnection() { _ActionsTaken.Add(ActionTypes.CloseConnection); } public int ExecuteStoredProcedure(ref GenericCommand genericCommand) { _ActionsTaken.Add(ActionTypes.ExecuteStoredProcedureWithGenericCommand); AddGenericCommand(genericCommand); return TestValues.RecordsAffected; } public int ExecuteStoredProcedure(string sql) { _ActionsTaken.Add(ActionTypes.ExecuteStoredProcedureWithSQL); GenericCommand genericCommand = new GenericCommand(sql); AddGenericCommand(genericCommand); return TestValues.RecordsAffected; } public System.Data.DataTable GetDataTable(GenericCommand genericCommand) { _ActionsTaken.Add(ActionTypes.GetDataTableWithGenericCommand); AddGenericCommand(genericCommand); return new DataTable(); } public System.Data.DataTable GetDataTable(string sql) { _ActionsTaken.Add(ActionTypes.GetDataTableWithSQL); AddGenericCommand(new GenericCommand(sql)); return new DataTable(); } public object GetScalarValue(GenericCommand genericCommand) { _ActionsTaken.Add(ActionTypes.GetScalarValue); AddGenericCommand(genericCommand); return TestValues.TestScalarValue; } public object GetScalarValue(string sql) { _ActionsTaken.Add(ActionTypes.GetScalarValue); AddGenericCommand(new GenericCommand(sql)); return TestValues.TestScalarValue; } public void BeginTransaction() { _ActionsTaken.Add(ActionTypes.BeginTransaction); } public void RollbackTransaction() { _ActionsTaken.Add(ActionTypes.RollbackTransaction); } public void CommitTransaction() { _ActionsTaken.Add(ActionTypes.CommitTransaction); } #endregion } }