3

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.