I am trying to create some (I believe these are Integration not Unit) tests for code that interacts with a database and I am not sure this is the right approach.
I would appreciate any reviews and possible improvements.
Using: VS2010 Unit Testing Framework
This is the function I want to test:
public IDbConnection OpenThirdPartyDatabase(string path, string password)
{
var provider = GetProvider(path, password);
return DBObjectFactory.GetObject().GetConnection(provider.Name, true, true);
}
Functions used by OpenThirdPartyDatabase
private static IDBProvider GetProvider(string path, string password)
{
var providers = DBProviders.GetObject();
var providerExists = providers.Cast<IDBProvider>().Any(dbProvider => dbProvider.Name == "MyProviderName");
return providerExists ? providers.GetDBProvider("MyProviderName") : CreateProvider(path, password);
}
private static IDBProvider CreateProvider(string path, string password)
{
IDBProvider provider = new DBAccessProvider("MyProviderName", path, "", password);
DBProviders.GetObject().Add(provider);
return provider;
}
public IDbConnection GetConnection(string ProviderName, bool OpenConnection, bool UseConnectionPool)
{
IDBProvider dbProvider = DBProviders.GetObject().GetDBProvider(ProviderName);
if (dbProvider == null)
throw new UndeclaredDBProviderException(ProviderName);
else
{
IDbConnection newConnection = null;
if ((connectionPool.ContainsConnection(ProviderName)) && (UseConnectionPool))
newConnection = connectionPool.GetConnection(ProviderName);
else
{
newConnection = dbProvider.GetConnection();
if (UseConnectionPool)
connectionPool.AddConnection(ProviderName, newConnection);
}
if ((OpenConnection) && (newConnection.State == ConnectionState.Closed))
newConnection.Open();
return newConnection;
}
}
Tests:
/// <summary>
/// Test Connection with valid path and password
/// </summary>
[TestMethod]
public void ThirdPartyConnection_Valid()
{
var testConnection = _thirdPartyConnManager.OpenThirdPartyDatabase("Data\\test.mdb", "pass");
Assert.AreNotEqual(testConnection, null);
}
/// <summary>
/// Test Connection With Invalid Password
/// </summary>
[TestMethod]
[ExpectedException(typeof(OleDbException),"Not a valid password.")]
public void ThirdPartyConnection_ShouldThrowOleDbException_InvalidPassword()
{
var testConnection = _thirdPartyConnManager.OpenThirdPartyDatabase("Data\\test.mdb", "invalidpassword");
}
/// <summary>
/// Test Connection with invalid path
/// </summary>
[TestMethod]
[ExpectedException(typeof(OleDbException))]
public void ThirdPartyConnection_ShouldThrowOleDbException_InvalidPath()
{
var testConnection = _thirdPartyConnManager.OpenThirdPartyDatabase("InvalidPath\\Data\\test.mdb", "pass");
}
/// <summary>
/// Test Connection String
/// </summary>
[TestMethod]
public void ThirdPartyConnection_ConnectionString()
{
var testConnection = _thirdPartyConnManager.OpenThirdPartyDatabase("Data\\test.mdb", "pass");
Assert.AreEqual(testConnection.ConnectionString,"Expected Connection String","Not expected Connection String");
}