Skip to content

Commit

Permalink
[Neo Core UT] Test memory store (#3336)
Browse files Browse the repository at this point in the history
* test memory store

* Update tests/Neo.UnitTests/Neo.UnitTests.csproj

Co-authored-by: Shargon <[email protected]>

---------

Co-authored-by: Shargon <[email protected]>
Co-authored-by: NGD Admin <[email protected]>
  • Loading branch information
3 people authored Jun 20, 2024
1 parent 90468e8 commit 60bb9c6
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 3 deletions.
21 changes: 21 additions & 0 deletions tests/Neo.UnitTests/Persistence/TestMemoryStoreProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// TestMemoryStoreProvider.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.Persistence;

namespace Neo.UnitTests.Persistence;

public class TestMemoryStoreProvider(MemoryStore memoryStore) : IStoreProvider
{
public MemoryStore MemoryStore { get; set; } = memoryStore;
public string Name => nameof(MemoryStore);
public IStore GetStore(string path) => MemoryStore;
}
66 changes: 63 additions & 3 deletions tests/Neo.UnitTests/Persistence/UT_MemoryStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// modifications are permitted.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.IO;
using Neo.Persistence;
using Neo.SmartContract;
using System;
Expand All @@ -21,6 +22,22 @@ namespace Neo.UnitTests.Persistence
[TestClass]
public class UT_MemoryStore
{
private NeoSystem _neoSystem;
private MemoryStore _memoryStore;

[TestInitialize]
public void Setup()
{
_memoryStore = new MemoryStore();
_neoSystem = new NeoSystem(TestProtocolSettings.Default, new TestMemoryStoreProvider(_memoryStore));
}

[TestCleanup]
public void CleanUp()
{
_memoryStore.Reset();
}

[TestMethod]
public void LoadStoreTest()
{
Expand Down Expand Up @@ -58,16 +75,59 @@ public void StoreTest()
[TestMethod]
public void NeoSystemStoreViewTest()
{
var neoSystem = new NeoSystem(TestProtocolSettings.Default, new MemoryStoreProvider());
Assert.IsNotNull(neoSystem.StoreView);
var store = neoSystem.StoreView;
Assert.IsNotNull(_neoSystem.StoreView);
var store = _neoSystem.StoreView;
var key = new StorageKey(Encoding.UTF8.GetBytes("testKey"));
var value = new StorageItem(Encoding.UTF8.GetBytes("testValue"));
store.Add(key, value);
store.Commit();
var result = store.TryGet(key);
// The StoreView is a readonly view of the store, here it will have value in the cache
Assert.AreEqual("testValue", Encoding.UTF8.GetString(result.Value.ToArray()));
// But the value will not be written to the underlying store even its committed.
Assert.IsNull(_memoryStore.TryGet(key.ToArray()));
}

[TestMethod]
public void NeoSystemStoreAddTest()
{
var storeCache = _neoSystem.GetSnapshot();
var key = new KeyBuilder(0, 0);
storeCache.Add(key, new StorageItem(UInt256.Zero.ToArray()));
storeCache.Commit();

CollectionAssert.AreEqual(UInt256.Zero.ToArray(), storeCache.TryGet(key).ToArray());
}

[TestMethod]
public void NeoSystemStoreGetAndChange()
{
var storeView = _neoSystem.GetSnapshot();
var key = new KeyBuilder(1, 1);
var item = new StorageItem([1, 2, 3]);
storeView.Delete(key);
Assert.AreEqual(null, storeView.TryGet(key));
storeView.Add(key, item);
CollectionAssert.AreEqual(new byte[] { 1, 2, 3 }, storeView.TryGet(key).ToArray());

var key2 = new KeyBuilder(1, 2);
var item2 = new StorageItem([4, 5, 6]);
storeView.Add(key2, item2);
CollectionAssert.AreEqual(key2.ToArray(), storeView.Seek(key2.ToArray(), SeekDirection.Backward).Select(u => u.Key).First().ToArray());
CollectionAssert.AreEqual(key.ToArray(), storeView.Seek(key.ToArray(), SeekDirection.Backward).Select(u => u.Key).First().ToArray());

storeView.Delete(key);
storeView.Delete(key2);

storeView.Add(new KeyBuilder(1, 0x000000), new StorageItem([0x00]));
storeView.Add(new KeyBuilder(1, 0x000001), new StorageItem([0x01]));
storeView.Add(new KeyBuilder(1, 0x000002), new StorageItem([0x02]));
storeView.Add(new KeyBuilder(1, 0x000003), new StorageItem([0x03]));
storeView.Add(new KeyBuilder(1, 0x000004), new StorageItem([0x04]));

var entries = storeView.Seek([], SeekDirection.Backward).ToArray();
// Memory store has different seek behavior than the snapshot
Assert.AreEqual(entries.Length, 37);
}
}
}

0 comments on commit 60bb9c6

Please sign in to comment.