Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic transaction support #47

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>

<RootNamespace>RocksDbSharp.Tests</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2"/>
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10"/>
<PackageReference Include="MSTest.TestFramework" Version="2.2.10"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\csharp\RocksDbSharp.csproj" />
</ItemGroup>

<ItemGroup>
<None Remove="rocksdb.dll" />
<Content Include="rocksdb.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

</Project>
154 changes: 154 additions & 0 deletions Tests/TransactionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
namespace RocksDbSharp.Tests;

[TestClass]
public class TransactionTests
{
private string? _path;
private TransactionDb? _db;

[TestInitialize]
public void TestInitialize()
{
_path = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());

var dbOptions = new DbOptions().SetCreateIfMissing();
var transactionDbOptions = new TransactionDbOptions();
_db = TransactionDb.Open(dbOptions, transactionDbOptions, _path);
}

[TestCleanup]
public void TestCleanup()
{
_db?.Dispose();
_db = null;

if (Directory.Exists(_path))
Directory.Delete(_path, true);
}

[TestMethod]
public void key_should_be_visible_inside_transaction_while_uncommitted()
{
// Arrange
const string key = "key1";
const string value = "value1";

using var tran = _db!.BeginTransaction();

// Act
tran.Put(key, value);
var exists = tran.HasKey(key);

// Assert
Assert.IsTrue(exists);
}

[TestMethod]
public void key_should_not_be_visible_outside_transaction_while_uncommitted()
{
// Arrange
const string key = "key1";
const string value = "value1";

using var tran = _db!.BeginTransaction();

// Act
tran.Put(key, value);
var exists = _db.HasKey(key);

// Assert
Assert.IsFalse(exists);
}

[TestMethod]
public void key_should_be_visible_outside_transaction_after_commit()
{
// Arrange
const string key = "key1";
const string value = "value1";

using var tran = _db!.BeginTransaction();

// Act
tran.Put(key, value);
tran.Commit();
var exists = _db.HasKey(key);

// Assert
Assert.IsTrue(exists);
}

[TestMethod]
public void key_should_not_be_iterable_outside_transaction_while_uncommitted()
{
// Arrange
const string key = "key1";
const string value = "value1";

using var tran = _db!.BeginTransaction();

// Act
tran.Put(key, value);
using var iterator = _db.NewIterator().SeekToFirst();

// Assert
Assert.AreNotEqual(key, iterator.StringKey());
}

[TestMethod]
public void key_should_be_iterable_outside_transaction_after_commit()
{
// Arrange
const string key = "key1";
const string value = "value1";

using var tran = _db!.BeginTransaction();

// Act
tran.Put(key, value);
tran.Commit();
using var iterator = _db.NewIterator().SeekToFirst();

// Assert
Assert.AreEqual(key, iterator.StringKey());
}

[TestMethod]
public void old_value_should_be_visible_outside_transaction_while_uncommitted()
{
// Arrange
const string key = "key";
const string value1 = "value1";
const string value2 = "value2";
_db!.Put(key, value1);

using var tran = _db.BeginTransaction();

// Act
tran.Put(key, value2);
var result = _db.Get(key);

// Assert
Assert.AreEqual(value1, result);
}

[TestMethod]
public void new_value_should_be_visible_outside_transaction_after_commit()
{
// Arrange
const string key = "key";
const string value1 = "value1";
const string value2 = "value2";
_db!.Put(key, value1);

using var tran = _db.BeginTransaction();

// Act
tran.Put(key, value2);
tran.Commit();
var result = _db.Get(key);

// Assert
Assert.AreEqual(value2, result);
}
}
1 change: 1 addition & 0 deletions Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Microsoft.VisualStudio.TestTools.UnitTesting;
Loading