Is it possible to create an In-memory database using SQLite.Net-PCL?
Asked Answered
R

1

6

Currently I am working on unit tests for a Xamarin MvvmCross Application using SQLite.Net-PCL 3.1.1. The unit test project is a normal .NET project.

So now I am mocking the MvxSqliteConnectionFactoryBase

public class MockMvxSqliteConnectionFactoryBase :  MvxSqliteConnectionFactoryBase
{
    #region implemented abstract members of MvxSqliteConnectionFactoryBase

    public override string GetPlattformDatabasePath(string databaseName)
    {
        return "Data Source=:memory:";
    }

    public override ISQLitePlatform GetCurrentPlatform()
    {
        return new SQLite.Net.Platform.Generic.SQLitePlatformGeneric(); 
    }
    public override ISQLitePlatform GetCurrentPlatform(string key)
    {
        return new  SQLite.Net.Platform.Generic.SQLitePlatformGeneric();
    }

    #endregion
}

But the database is not created in memory rather than in the bin folder of the project.

I tried to initialize SQLiteConnection like this example but there is not a constructor that accepts only one string.

Richey answered 24/5, 2016 at 8:54 Comment(9)
@Aron actually, it can work with or without a backing file. Using it as memory-only is a very common scenario. That's what this question is about.Bosch
This is a perfectly valid question. Any downvoters should give a valid explanationBosch
@Aron sqlite.org/inmemorydb.htmlTelegraphese
@Aron then you are looking at the wrong page. Using SQLite as a memory-only database is a common scenarioBosch
@Telegraphese I stand corrected.Aklog
@Richey try to create an in-memory database directly instead of mocking. If you can, the problem is in the MvxSqliteConnectionFactoryBase class. It may be changing the connection string.Bosch
I tried something like this SQLiteConnection connection = new SQLiteConnection(new SQLite.Net.Platform.Generic.SQLitePlatformGeneric(), "Data Source=:memory:"); but it creates the db inside the bin folderRichey
I don't know Mvvm, but should Data Source= really be part of the path?Microstructure
@Microstructure I think you are right!Richey
R
4

Following the hint that @CL. gave me I changed the method GetPlattformDatabasePath:

public override string GetPlattformDatabasePath(string databaseName)
    {
        return ":memory:";
    }

Now I don't see something to be created in the bin folder and there is no need for the extisting data to be deleted after every unit test run, so the database must be in-memory.

Richey answered 24/5, 2016 at 15:48 Comment(1)
that works. But do you know how to use more than one in-memory database so that tests in paralell can run without stepping on each other's toes?Delacourt

© 2022 - 2024 — McMap. All rights reserved.