mirror of
https://github.com/2dust/v2rayN.git
synced 2026-11-14 08:24:59 +03:00
fix(tests): initialize shared SQLite schema once (#10596)
This commit is contained in:
committed by
GitHub
parent
a189c659c6
commit
f2f336d97c
@@ -98,7 +98,7 @@ public class CoreConfigContextBuilderTests
|
|||||||
|
|
||||||
private static async Task UpsertProfilesAsync(params ProfileItem[] profiles)
|
private static async Task UpsertProfilesAsync(params ProfileItem[] profiles)
|
||||||
{
|
{
|
||||||
SQLiteHelper.Instance.CreateTable<ProfileItem>();
|
TestDatabase.Initialize();
|
||||||
foreach (var profile in profiles)
|
foreach (var profile in profiles)
|
||||||
{
|
{
|
||||||
await SQLiteHelper.Instance.ReplaceAsync(profile);
|
await SQLiteHelper.Instance.ReplaceAsync(profile);
|
||||||
|
|||||||
@@ -16,8 +16,7 @@ public class ConfigHandlerCustomSubscriptionTests
|
|||||||
{
|
{
|
||||||
var config = CoreConfigTestFactory.CreateConfig();
|
var config = CoreConfigTestFactory.CreateConfig();
|
||||||
CoreConfigTestFactory.BindAppManagerConfig(config);
|
CoreConfigTestFactory.BindAppManagerConfig(config);
|
||||||
SQLiteHelper.Instance.CreateTable<SubItem>();
|
TestDatabase.Initialize();
|
||||||
SQLiteHelper.Instance.CreateTable<ProfileItem>();
|
|
||||||
|
|
||||||
var subId = $"sub-{Guid.NewGuid():N}";
|
var subId = $"sub-{Guid.NewGuid():N}";
|
||||||
await SQLiteHelper.Instance.ReplaceAsync(new SubItem
|
await SQLiteHelper.Instance.ReplaceAsync(new SubItem
|
||||||
@@ -50,8 +49,7 @@ public class ConfigHandlerCustomSubscriptionTests
|
|||||||
{
|
{
|
||||||
var config = CoreConfigTestFactory.CreateConfig();
|
var config = CoreConfigTestFactory.CreateConfig();
|
||||||
CoreConfigTestFactory.BindAppManagerConfig(config);
|
CoreConfigTestFactory.BindAppManagerConfig(config);
|
||||||
SQLiteHelper.Instance.CreateTable<SubItem>();
|
TestDatabase.Initialize();
|
||||||
SQLiteHelper.Instance.CreateTable<ProfileItem>();
|
|
||||||
|
|
||||||
var subId = $"sub-failed-{Guid.NewGuid():N}";
|
var subId = $"sub-failed-{Guid.NewGuid():N}";
|
||||||
await SQLiteHelper.Instance.ReplaceAsync(new SubItem
|
await SQLiteHelper.Instance.ReplaceAsync(new SubItem
|
||||||
|
|||||||
@@ -13,6 +13,33 @@ public class SQLiteHelperTests
|
|||||||
return database;
|
return database;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task RepeatedSharedSchemaInitialization_ShouldSucceedDuringAnExclusiveTransaction()
|
||||||
|
{
|
||||||
|
TestDatabase.Initialize();
|
||||||
|
var database = Database.Value;
|
||||||
|
var item = new SubItem { Id = Guid.NewGuid().ToString("N"), Remarks = "schema-init", Url = "http://127.0.0.1/sub" };
|
||||||
|
await database.InsertAsync(item);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await database.ExecuteAsync("BEGIN EXCLUSIVE");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
TestDatabase.Initialize();
|
||||||
|
var saved = await database.TableAsync<SubItem>().Where(row => row.Id == item.Id).FirstAsync();
|
||||||
|
await saved.Id.Should().BeEqualTo(item.Id);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
await database.ExecuteAsync("ROLLBACK");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
await database.DeleteAsync(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public async Task SingleWrites_ShouldPreserveValuesAndDeleteByPrimaryKey()
|
public async Task SingleWrites_ShouldPreserveValuesAndDeleteByPrimaryKey()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ public class ProfileExManagerPersistenceTests
|
|||||||
[Test]
|
[Test]
|
||||||
public async Task SaveTo_NewProfilesWithRepeatedResults_ShouldPersistEveryProfile()
|
public async Task SaveTo_NewProfilesWithRepeatedResults_ShouldPersistEveryProfile()
|
||||||
{
|
{
|
||||||
SQLiteHelper.Instance.CreateTable<ProfileExItem>();
|
TestDatabase.Initialize();
|
||||||
var manager = new ProfileExManager();
|
var manager = new ProfileExManager();
|
||||||
var ids = Enumerable.Range(0, 3).Select(_ => Guid.NewGuid().ToString("N")).ToArray();
|
var ids = Enumerable.Range(0, 3).Select(_ => Guid.NewGuid().ToString("N")).ToArray();
|
||||||
|
|
||||||
@@ -46,7 +46,7 @@ public class ProfileExManagerPersistenceTests
|
|||||||
[Test]
|
[Test]
|
||||||
public async Task SaveTo_MixedNewAndExistingProfiles_ShouldInsertAndUpdateResults()
|
public async Task SaveTo_MixedNewAndExistingProfiles_ShouldInsertAndUpdateResults()
|
||||||
{
|
{
|
||||||
SQLiteHelper.Instance.CreateTable<ProfileExItem>();
|
TestDatabase.Initialize();
|
||||||
var manager = new ProfileExManager();
|
var manager = new ProfileExManager();
|
||||||
var existingId = Guid.NewGuid().ToString("N");
|
var existingId = Guid.NewGuid().ToString("N");
|
||||||
var newId = Guid.NewGuid().ToString("N");
|
var newId = Guid.NewGuid().ToString("N");
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
namespace ServiceLib.Tests;
|
||||||
|
|
||||||
|
internal static class TestDatabase
|
||||||
|
{
|
||||||
|
private static readonly Lazy<SQLiteHelper> Database = new(() =>
|
||||||
|
{
|
||||||
|
var database = SQLiteHelper.Instance;
|
||||||
|
database.CreateTable<SubItem>();
|
||||||
|
database.CreateTable<ProfileItem>();
|
||||||
|
database.CreateTable<ProfileExItem>();
|
||||||
|
return database;
|
||||||
|
});
|
||||||
|
|
||||||
|
public static void Initialize() => _ = Database.Value;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user