Skip to content

Commit

Permalink
test: add unit test for newly added feature
Browse files Browse the repository at this point in the history
  • Loading branch information
limebell committed Dec 30, 2024
1 parent 75cbe15 commit 439d3da
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions sdk/node/Libplanet.Node.Tests/Services/BlockChainServiceTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
using Bencodex.Types;
using Libplanet.Common;
using Libplanet.Crypto;
using Libplanet.Node.Extensions;
using Libplanet.Node.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;

namespace Libplanet.Node.Tests.Services;

Expand All @@ -16,4 +21,69 @@ public void Create_Test()

Assert.Equal(1, blockChain.Count);
}

[Fact]
public void Create_Using_Genesis_Configuration_Test()
{
var genesisKey = new PrivateKey();
string tempDirectory = Path.GetTempPath();
string tempFilePath = Path.Combine(tempDirectory, Guid.NewGuid().ToString() + ".json");
Address accountA = new("0000000000000000000000000000000000000000");
Address accountB = new("0000000000000000000000000000000000000001");
Address addressA = new("0000000000000000000000000000000000000000");
Address addressB = new("0000000000000000000000000000000000000001");

try
{
string jsonContent = $@"
{{
""{accountA}"": {{
""{addressA}"": ""A"",
""{addressB}"": 123
}},
""{accountB}"": {{
""{addressA}"": ""B"",
""{addressB}"": 456
}}
}}";
File.WriteAllText(tempFilePath, jsonContent);
var configDict = new Dictionary<string, string>
{
{ "Genesis:GenesisConfigurationPath", tempFilePath },
{ "Genesis:GenesisKey", ByteUtil.Hex(genesisKey.ToByteArray()) },
};

var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(configDict!)
.Build();

var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory, NullLoggerFactory>();
services.AddLogging();
services.AddLibplanetNode(configuration);
var serviceProvider = services.BuildServiceProvider();
var blockChainService = serviceProvider.GetRequiredService<IBlockChainService>();
var blockChain = blockChainService.BlockChain;

Assert.Equal(
(Text)"A",
blockChain.GetNextWorldState()?.GetAccountState(accountA).GetState(addressA));
Assert.Equal(
(Integer)123,
blockChain.GetNextWorldState()?.GetAccountState(accountA).GetState(addressB));
Assert.Equal(
(Text)"B",
blockChain.GetNextWorldState()?.GetAccountState(accountB).GetState(addressA));
Assert.Equal(
(Integer)456,
blockChain.GetNextWorldState()?.GetAccountState(accountB).GetState(addressB));
}
finally
{
if (File.Exists(tempFilePath))
{
File.Delete(tempFilePath);
}
}
}
}

0 comments on commit 439d3da

Please sign in to comment.