I am trying to create Unit Test (NUnit) for the old code that we have.
We using DbProviderFactory to check if table exist in the database before doing anything with that table.
I know that this might look more like integration testing, but regardless of what you call it I need to have a test for it. I would prefer to use something self contained and not dependent on the database.
I tried to use excel as my data source but to select table in excel it should have $ after the table name which wouldn't work in my case because I don't want to modify my code to accommodate Unit Test.
How can I unit test following code?
static bool TableDoesNotExist(string tableName, string connectionString, string providerName = "System.Data.OleDb")
{
try
{
DbProviderFactory providerFactory = DbProviderFactories.GetFactory(providerName);
using (DbConnection conn = providerFactory.CreateConnection())
{
conn.ConnectionString = connectionString;
conn.Open();
DbCommand cmd = providerFactory.CreateCommand();
cmd.Connection = conn;
string tblQuery = "";
if (providerName == "System.Data.Odbc")
tblQuery = string.Format("SELECT COUNT(*) FROM SYSTABLE WHERE TABLE_NAME = '{0}'", tableName);
else
tblQuery = string.Format("SELECT COUNT(*) FROM [INFORMATION_SCHEMA.TABLES$] WHERE TABLE_NAME = '{0}'", tableName);
cmd.CommandText = tblQuery;
Console.WriteLine(cmd.CommandText);
DbDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
if (dt.Rows.Count == 1 && Convert.ToInt32(dt.Rows[0][0]) == 0)
{
return true;
}
}
return false;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
return false;
}
Any help would be appreciated.
Aucun commentaire:
Enregistrer un commentaire