Tests: Add in-memory database for testing
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@ -3,3 +3,4 @@ bin/
|
|||||||
*.user
|
*.user
|
||||||
.vs
|
.vs
|
||||||
.idea
|
.idea
|
||||||
|
Tests/Resources/Create.sql
|
||||||
|
@ -64,7 +64,8 @@ namespace Elwig.Helpers {
|
|||||||
public DateTime SavedLastWriteTime { get; private set; }
|
public DateTime SavedLastWriteTime { get; private set; }
|
||||||
public bool HasBackendChanged => SavedLastWriteTime != LastWriteTime;
|
public bool HasBackendChanged => SavedLastWriteTime != LastWriteTime;
|
||||||
|
|
||||||
public static string ConnectionString => $"Data Source=\"{App.Config.DatabaseFile}\"; Foreign Keys=True; Mode=ReadWrite; Cache=Default";
|
public static string? ConnectionStringOverride { get; set; } = null;
|
||||||
|
public static string ConnectionString => ConnectionStringOverride ?? $"Data Source=\"{App.Config.DatabaseFile}\"; Mode=ReadWrite; Foreign Keys=True; Cache=Default";
|
||||||
|
|
||||||
private readonly Dictionary<int, Dictionary<int, Dictionary<string, AreaComBucket>>> _memberAreaCommitmentBuckets = [];
|
private readonly Dictionary<int, Dictionary<int, Dictionary<string, AreaComBucket>>> _memberAreaCommitmentBuckets = [];
|
||||||
private readonly Dictionary<int, Dictionary<int, Dictionary<string, int>>> _memberDeliveryBuckets = [];
|
private readonly Dictionary<int, Dictionary<int, Dictionary<string, int>>> _memberDeliveryBuckets = [];
|
||||||
|
@ -8,6 +8,7 @@ using System.Threading.Tasks;
|
|||||||
namespace Elwig.Helpers {
|
namespace Elwig.Helpers {
|
||||||
public static class AppDbUpdater {
|
public static class AppDbUpdater {
|
||||||
|
|
||||||
|
// Don't forget to update value in Tests/fetch-resources.bat!
|
||||||
public static readonly int RequiredSchemaVersion = 12;
|
public static readonly int RequiredSchemaVersion = 12;
|
||||||
|
|
||||||
private static int VersionOffset = 0;
|
private static int VersionOffset = 0;
|
||||||
|
27
Tests/DatabaseSetup.cs
Normal file
27
Tests/DatabaseSetup.cs
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
using Elwig.Helpers;
|
||||||
|
using Microsoft.Data.Sqlite;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
|
namespace Tests {
|
||||||
|
[SetUpFixture]
|
||||||
|
public class DatabaseSetup {
|
||||||
|
|
||||||
|
private SqliteConnection? Connection;
|
||||||
|
|
||||||
|
[OneTimeSetUp]
|
||||||
|
public async Task SetupDatabase() {
|
||||||
|
AppDbContext.ConnectionStringOverride = $"Data Source=ElwigTestDB; Mode=Memory; Foreign Keys=True; Cache=Default";
|
||||||
|
Connection = await AppDbContext.ConnectAsync();
|
||||||
|
await AppDbContext.ExecuteEmbeddedScript(Connection, Assembly.GetExecutingAssembly(), "Tests.Resources.Create.sql");
|
||||||
|
}
|
||||||
|
|
||||||
|
[OneTimeTearDown]
|
||||||
|
public async Task TeardownDatabase() {
|
||||||
|
AppDbContext.ConnectionStringOverride = null;
|
||||||
|
if (Connection == null) return;
|
||||||
|
await Connection.DisposeAsync();
|
||||||
|
Connection = null;
|
||||||
|
// The in-memory database will be dropped if all connections to it are closed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
30
Tests/HelpersBillingTest.cs
Normal file
30
Tests/HelpersBillingTest.cs
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
using Elwig.Helpers;
|
||||||
|
using Microsoft.Data.Sqlite;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
|
namespace Tests {
|
||||||
|
[TestFixture]
|
||||||
|
public class HelpersBillingTest {
|
||||||
|
|
||||||
|
private SqliteConnection? Connection;
|
||||||
|
|
||||||
|
[OneTimeSetUp]
|
||||||
|
public async Task SetupDatabase() {
|
||||||
|
Connection = await AppDbContext.ConnectAsync();
|
||||||
|
await AppDbContext.ExecuteEmbeddedScript(Connection, Assembly.GetExecutingAssembly(), "Tests.Resources.BillingInsert.sql");
|
||||||
|
}
|
||||||
|
|
||||||
|
[OneTimeTearDown]
|
||||||
|
public async Task TeardownDatabase() {
|
||||||
|
if (Connection == null) return;
|
||||||
|
await AppDbContext.ExecuteEmbeddedScript(Connection, Assembly.GetExecutingAssembly(), "Tests.Resources.BillingDelete.sql");
|
||||||
|
await Connection.DisposeAsync();
|
||||||
|
Connection = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Test() {
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
1
Tests/Resources/BillingDelete.sql
Normal file
1
Tests/Resources/BillingDelete.sql
Normal file
@ -0,0 +1 @@
|
|||||||
|
-- deletes for HelpersBillingTest
|
1
Tests/Resources/BillingInsert.sql
Normal file
1
Tests/Resources/BillingInsert.sql
Normal file
@ -0,0 +1 @@
|
|||||||
|
-- inserts for HelpersBillingTest
|
@ -9,6 +9,14 @@
|
|||||||
<IsTestProject>true</IsTestProject>
|
<IsTestProject>true</IsTestProject>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<EmbeddedResource Include="Resources\*.sql" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
|
||||||
|
<Exec Command="call fetch-resources.bat" />
|
||||||
|
</Target>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
|
||||||
<PackageReference Include="NUnit" Version="3.13.3" />
|
<PackageReference Include="NUnit" Version="3.13.3" />
|
||||||
|
1
Tests/fetch-resources.bat
Normal file
1
Tests/fetch-resources.bat
Normal file
@ -0,0 +1 @@
|
|||||||
|
curl -s "https://www.necronda.net/elwig/files/create.sql?v=12" -u "elwig:ganzGeheim123!" -o "Resources\Create.sql"
|
Reference in New Issue
Block a user