Samuel A C
Samuel A C

Reputation: 466

Using an in Memory Sqlite database as mock for Mysql connection,

I was building unit tests for Data access blocks and liked to replace a MySql db with Sqlite in memory database, which i could consider as mock and load with test values.

Sqlite is created as below

using (var connection = new SqliteConnection("" +new SqliteConnectionStringBuilder
                            {
                                DataSource = ":memory:"
                            }))
{

  connection.Open();

  using (var transaction = connection.BeginTransaction())
  {
    //code to create table and load data
    var connectionString=connection.ConnectionString
    methodToBeTested(connectionString);
    //Assert codes
  }
}

MySql code that need to be unit tested is as below

methodToBeTested(string connectionString)
{
 using (MySqlConnection cn = new MySqlConnection(connectionString))
 {
    cn.Open();//Exception->Unable to connect to any of the specified MySQL hosts
    //other codes
  }
}

The MySqlClient adapter throws exception while opening a SQLite db connection. Is there a way to achieve this?

Upvotes: 3

Views: 2894

Answers (1)

virullius
virullius

Reputation: 989

No.

At least not as you have it setup. The MySQL client does not know how to use a SQLite database. MySQL client is meant to connect to a MySQL server process while SQLite would be a code library that reads a local database file. That both are "SQL" only means they (mostly) follow the same standard for querying data once connected to the database. The connection process itself is very different.

Even if you were to build a abstraction layer for the connection process, SQLite would not be a good test for database function written for MySQL. Neither follow and SQL spec perfectly and either can offer more than standard functionality.

Upvotes: 2

Related Questions