I have a class named Database that implements following interface ;
public interface IDatabase
{
int ExecuteNonQuery(string command, CommandType commandType, Dictionary<string, object> parameters);
}
As the name indicates ExecuteNonQuery function executes non-query database commands in a provider agnostic way.
I need to write integration test for this class, and so far I've written an integration test for Sql Server Compact Edition which passes the test.
Here is the integration test code ;
public class DatabaseIntegrationTests
{
private readonly string CurrentDir;
private readonly string SqlCeDbPath;
public DatabaseIntegrationTests()
{
CurrentDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
SqlCeDbPath = CurrentDir + @"\TestDb.sdf";
}
[Fact]
public void ExecuteNonQuery_SqlServerCE_Should_Execute()
{
var dbConfFunc = this.InitializeSqlServerCeInstance();
Database database = new Database(dbConfFunc);
database.ExecuteNonQuery("CREATE TABLE Test([ID] [bigint] IDENTITY(1,1) NOT NULL,[Message] [nvarchar](255) NULL)",
CommandType.Text, null);
var parameters = new Dictionary<string, object>();
parameters.Add("@message", "Hello World!");
database.ExecuteNonQuery("INSERT INTO Test(Message) VALUES(@message)",
CommandType.Text, parameters);
CleanUpSqlServerCe();
}
private void CleanUpSqlServerCe()
{
if (File.Exists(SqlCeDbPath))
File.Delete(SqlCeDbPath);
}
private Func<DbConnectionConfiguration> InitializeSqlServerCeInstance()
{
string connectionString = "Data Source=" + CurrentDir + @"\ASTest.sdf;Persist Security Info=False";
CleanUpSqlServerCe();
SqlCeEngine engine = new SqlCeEngine(connectionString);
engine.CreateDatabase();
return () =>
{
return new DbConnectionConfiguration()
{
DataProvider = "System.Data.SqlServerCe.4.0",
ConnectionString = connectionString
};
};
}
}
My question is , how can write integration test for this class for MySql and Sql Server database providers? What is the most practical way to write integration test for this class to test it for both MySql and Sql Server databases?
Aucun commentaire:
Enregistrer un commentaire