Extensions

Backup and Restore

On ITestsFixture and IApplicationFactory is extension BackupMsSqlDatabaseAsync which creates backup of whole database. To restore state, use RestoreMsSqlDatabasesAsync.

Do not use master

To make this whole work, ensure that you are not using master as db. This can be achieved by defining database name in UseSql settings as shown bellow:

.UseSql(SqlSeed, (configuration, sqlSettings) =>
{
    configuration.AddConnectionString("Web", sqlSettings.ConnectionString);
}, opts =>
{
    opts.Database = "TestDb"; //specify name, default is master
})
[Fact]
public async Task SomeUpdatingOrCreatingtestScenario()
{
    //Create backup
    await fixture.BackupMsSqlDatabasesAsync();

    var res = await fixture.Client.PutAsync("sql", someObject);

    res.AssertStatusCode(System.Net.HttpStatusCode.OK);

    //Restore data
    await fixture.RestoreMsSqlDatabasesAsync();
}

Obtain data from sql for futher use

To Get data there are extension on ITestsFixture and IApplicationFactory named GetMsSqlObjectAsync and GetRawMsSqlObjectAsync or for collection you can use GetMsSqlCollectionAsync

GetMsSqlObjectAsync

This one is trying to map data for you from sql response - for mapping purposes is used JSON, so you should reflect naming etc or use native .NET JSON attributes to achieve so.

var obj = await fixture.GetMsSqlObjectAsync<SomeTable, int>("SomeTable", 1, "Id");

obj?.SomeString.Should().Be("MyString");

No enumerables

Do not try to obtain collection of data with this, since this method contains TOP(1) and even will throw exception if object is assignable to IEnumerable

GetRawMsSqlObjectAsync

Some error may occure in serialization proces, so there is even GetRawMsSqlObjectAsync which returns raw data

Raw string

This extension returns response from SQL server running inside container, so it is just string which needs some work, but as was foretold, some problems in serialization may occure, so to not let you without this functionality this one comes to play

var data = await fixture.GetRawMsSqlObjectAsync("SomeTable", 1, "Id");
Example of string data representation returned from container
Id          SomeInt     SomeString                    
----------- ----------- ------------------------------
1           0           kokos                         

(1 rows affected)

GetMsSqlCollectionAsync

This one is trying to map collection of data for you from sql response - for mapping purposes is used JSON, so you should reflect naming etc or use native .NET JSON attributes to achieve so.

var collectionOfAll = await fixture.GetMsSqlCollectionAsync<SomeTable>("SomeTable");

collectionOfAll.Should().HaveCount(4);