Files
speckle-sharp-sdk/tests/Speckle.Sdk.Tests.Unit/SQLite/SQLiteJsonCacheManagerTests.cs
T
Adam Hathcock ed5bdc91ed Sqlite pooling for connections and commands (#193)
* add ServerObjectManagerFactory

* add usage of a command pool

* add more disposal

* save saving increase

* fix tests

* fixes

* push out concurrency and disposablity

* Add a custom task scheduler

* Better usage, don't wait to enqueue to save to channels

* Completely pre-cal batch size to avoid spinning issues

* Try to fix cache counting

* properly dispose things

* format

* clean up

* adjust count and save on current thread

* move batch it's own file

* update a few packages

* fix build and add batch tests

* revert and format

* Revert "save saving increase"

This reverts commit 3b50c857fb.

* revert change

* adjust and add tests

* Dispose sqlite manager properly

* Make Batch a IMemoryOwner to allow for pooling

* Fix tests

* Upgrade some deps

* try to make tests more explicit

* remove return value

* Use named tuple for all objects
2025-01-08 11:04:32 +00:00

92 lines
2.3 KiB
C#

using Microsoft.Data.Sqlite;
using NUnit.Framework;
using Shouldly;
using Speckle.Sdk.Common;
using Speckle.Sdk.SQLite;
namespace Speckle.Sdk.Tests.Unit.SQLite;
[TestFixture]
public class SQLiteJsonCacheManagerTests
{
private readonly string _basePath = $"{Guid.NewGuid()}.db";
private string? _connectionString;
[SetUp]
public void Setup() => _connectionString = $"Data Source={_basePath};";
[TearDown]
public void TearDown()
{
if (File.Exists(_basePath))
{
SqliteConnection.ClearAllPools();
GC.Collect();
GC.WaitForPendingFinalizers();
File.Delete(_basePath);
}
}
[Test]
public void TestGetAll()
{
var data = new List<(string id, string json)>() { ("id1", "1"), ("id2", "2") };
using var manager = new SqLiteJsonCacheManager(_connectionString.NotNull(), 2);
manager.SaveObjects(data);
var items = manager.GetAllObjects();
items.Count.ShouldBe(data.Count);
var i = items.ToDictionary();
foreach (var (id, json) in data)
{
i.TryGetValue(id, out var j).ShouldBeTrue();
j.ShouldBe(json);
}
}
[Test]
public void TestGet()
{
var data = new List<(string id, string json)>() { ("id1", "1"), ("id2", "2") };
using var manager = new SqLiteJsonCacheManager(_connectionString.NotNull(), 2);
foreach (var d in data)
{
manager.SaveObject(d.id, d.json);
}
foreach (var d in data)
{
manager.SaveObject(d.id, d.json);
}
var items = manager.GetAllObjects();
items.Count.ShouldBe(data.Count);
var id1 = data[0].id;
var json1 = manager.GetObject(id1);
json1.ShouldBe(data[0].json);
manager.HasObject(id1).ShouldBeTrue();
manager.UpdateObject(id1, "3");
json1 = manager.GetObject(id1);
json1.ShouldBe("3");
manager.HasObject(id1).ShouldBeTrue();
manager.DeleteObject(id1);
json1 = manager.GetObject(id1);
json1.ShouldBeNull();
manager.HasObject(id1).ShouldBeFalse();
manager.UpdateObject(id1, "3");
json1 = manager.GetObject(id1);
json1.ShouldBe("3");
manager.HasObject(id1).ShouldBeTrue();
var id2 = data[1].id;
var json2 = manager.GetObject(id2);
json2.ShouldBe(data[1].json);
manager.HasObject(id2).ShouldBeTrue();
manager.DeleteObject(id2);
json2 = manager.GetObject(id2);
json2.ShouldBeNull();
manager.HasObject(id2).ShouldBeFalse();
}
}