Skip to content
This repository has been archived by the owner on Nov 20, 2022. It is now read-only.

Commit

Permalink
Simplify setup of a SQL database for unit tests
Browse files Browse the repository at this point in the history
Using the LocalDb NuGet package makes it very easy to setup a test database. With the connection string from the App.config file I would get an exception:
> System.Data.SqlClient.SqlException
> A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Cannot create an automatic instance. See the Windows Application event log for error details.)
  • Loading branch information
0xced committed Sep 17, 2021
1 parent 20473fe commit 25a9bfb
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 47 deletions.
8 changes: 0 additions & 8 deletions test/Dapper.NodaTime.Tests/App.config

This file was deleted.

16 changes: 2 additions & 14 deletions test/Dapper.NodaTime.Tests/Dapper.NodaTime.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,13 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="LocalDb" Version="10.7.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
<PackageReference Include="xunit.assemblyfixture" Version="2.0.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="Dapper" Version="1.50.2" />
<PackageReference Include="NodaTime" Version="2.2.2" />
</ItemGroup>

<ItemGroup>
<Content Include="TestDB.mdf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="TestDB_log.ldf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<DependentUpon>TestDB.mdf</DependentUpon>
</Content>
</ItemGroup>

<ItemGroup>
<Reference Include="System.Configuration" />
</ItemGroup>

</Project>
26 changes: 26 additions & 0 deletions test/Dapper.NodaTime.Tests/DatabaseFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Threading.Tasks;
using LocalDb;

namespace Dapper.NodaTime.Tests
{
public class DatabaseFixture : IDisposable
{
private readonly SqlInstance _sqlInstance;
private readonly SqlDatabase _database;

public DatabaseFixture()
{
_sqlInstance = new SqlInstance(name: "Dapper.NodaTime.Tests", buildTemplate: _ => Task.CompletedTask);
_database = _sqlInstance.Build("DapperNodaTimeTests").Result;
}

public void Dispose()
{
_database.Dispose();
_sqlInstance.Cleanup();
}

public string ConnectionString => _database.ConnectionString;
}
}
9 changes: 4 additions & 5 deletions test/Dapper.NodaTime.Tests/InstantTests.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System.Configuration;
using System.Data.SqlClient;
using System.Data.SqlClient;
using System.Linq;
using NodaTime;
using Xunit;

namespace Dapper.NodaTime.Tests
{
[Collection("DBTests")]
public class InstantTests
public class InstantTests : IAssemblyFixture<DatabaseFixture>
{
private readonly string _connectionString;

Expand All @@ -16,9 +15,9 @@ private class TestObject
public Instant? Value { get; set; }
}

public InstantTests()
public InstantTests(DatabaseFixture databaseFixture)
{
_connectionString = ConfigurationManager.ConnectionStrings["TestDB"].ConnectionString;
_connectionString = databaseFixture.ConnectionString;
SqlMapper.AddTypeHandler(InstantHandler.Default);
}

Expand Down
9 changes: 4 additions & 5 deletions test/Dapper.NodaTime.Tests/LocalDateTests.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System.Configuration;
using System.Data.SqlClient;
using System.Data.SqlClient;
using System.Linq;
using NodaTime;
using Xunit;

namespace Dapper.NodaTime.Tests
{
[Collection("DBTests")]
public class LocalDateTests
public class LocalDateTests : IAssemblyFixture<DatabaseFixture>
{
private readonly string _connectionString;

Expand All @@ -16,9 +15,9 @@ private class TestObject
public LocalDate? Value { get; set; }
}

public LocalDateTests()
public LocalDateTests(DatabaseFixture databaseFixture)
{
_connectionString = ConfigurationManager.ConnectionStrings["TestDB"].ConnectionString;
_connectionString = databaseFixture.ConnectionString;
SqlMapper.AddTypeHandler(LocalDateHandler.Default);
}

Expand Down
9 changes: 4 additions & 5 deletions test/Dapper.NodaTime.Tests/LocalDateTimeTests.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System.Configuration;
using System.Data.SqlClient;
using System.Data.SqlClient;
using System.Linq;
using NodaTime;
using Xunit;

namespace Dapper.NodaTime.Tests
{
[Collection("DBTests")]
public class LocalDateTimeTests
public class LocalDateTimeTests : IAssemblyFixture<DatabaseFixture>
{
private readonly string _connectionString;

Expand All @@ -16,9 +15,9 @@ private class TestObject
public LocalDateTime? Value { get; set; }
}

public LocalDateTimeTests()
public LocalDateTimeTests(DatabaseFixture databaseFixture)
{
_connectionString = ConfigurationManager.ConnectionStrings["TestDB"].ConnectionString;
_connectionString = databaseFixture.ConnectionString;
SqlMapper.AddTypeHandler(LocalDateTimeHandler.Default);
}

Expand Down
9 changes: 4 additions & 5 deletions test/Dapper.NodaTime.Tests/LocalTimeTests.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System.Configuration;
using System.Data.SqlClient;
using System.Data.SqlClient;
using System.Linq;
using NodaTime;
using Xunit;

namespace Dapper.NodaTime.Tests
{
[Collection("DBTests")]
public class LocalTimeTests
public class LocalTimeTests : IAssemblyFixture<DatabaseFixture>
{
private readonly string _connectionString;

Expand All @@ -16,9 +15,9 @@ private class TestObject
public LocalTime? Value { get; set; }
}

public LocalTimeTests()
public LocalTimeTests(DatabaseFixture databaseFixture)
{
_connectionString = ConfigurationManager.ConnectionStrings["TestDB"].ConnectionString;
_connectionString = databaseFixture.ConnectionString;
SqlMapper.AddTypeHandler(LocalTimeHandler.Default);
}

Expand Down
9 changes: 4 additions & 5 deletions test/Dapper.NodaTime.Tests/OffsetDateTimeTests.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System.Configuration;
using System.Data.SqlClient;
using System.Data.SqlClient;
using System.Linq;
using NodaTime;
using Xunit;

namespace Dapper.NodaTime.Tests
{
[Collection("DBTests")]
public class OffsetDateTimeTests
public class OffsetDateTimeTests : IAssemblyFixture<DatabaseFixture>
{
private readonly string _connectionString;

Expand All @@ -16,9 +15,9 @@ public class TestObject
public OffsetDateTime? Value { get; set; }
}

public OffsetDateTimeTests()
public OffsetDateTimeTests(DatabaseFixture databaseFixture)
{
_connectionString = ConfigurationManager.ConnectionStrings["TestDB"].ConnectionString;
_connectionString = databaseFixture.ConnectionString;
SqlMapper.AddTypeHandler(OffsetDateTimeHandler.Default);
}

Expand Down
Binary file removed test/Dapper.NodaTime.Tests/TestDB.mdf
Binary file not shown.
Binary file removed test/Dapper.NodaTime.Tests/TestDB_log.ldf
Binary file not shown.

0 comments on commit 25a9bfb

Please sign in to comment.