-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
218 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
test/Libplanet.Explorer.Tests/GraphTypes/EvidenceIdTypeTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System; | ||
using GraphQL.Language.AST; | ||
using Libplanet.Common; | ||
using Libplanet.Explorer.GraphTypes; | ||
using Libplanet.Types.Evidence; | ||
using Xunit; | ||
|
||
namespace Libplanet.Explorer.Tests.GraphTypes | ||
{ | ||
public class EvidenceIdTypeTest : ScalarGraphTypeTestBase<EvidenceIdType> | ||
{ | ||
[Fact] | ||
public void ParseLiteral() | ||
{ | ||
Assert.Null(_type.ParseLiteral(new NullValue())); | ||
|
||
var bytes = TestUtils.GetRandomBytes(EvidenceId.Size); | ||
var evidenceId = new EvidenceId(bytes); | ||
var hex = ByteUtil.Hex(bytes); | ||
Assert.Equal( | ||
evidenceId, | ||
Assert.IsType<EvidenceId>(_type.ParseLiteral(new StringValue(hex)))); | ||
|
||
Assert.Throws<InvalidOperationException>( | ||
() => _type.ParseLiteral(new LongValue(1234))); | ||
Assert.Throws<InvalidOperationException>( | ||
() => _type.ParseValue(new StringValue("evidenceId"))); | ||
} | ||
|
||
[Fact] | ||
public void ParseValue() | ||
{ | ||
Assert.Null(_type.ParseValue(null)); | ||
|
||
var bytes = TestUtils.GetRandomBytes(EvidenceId.Size); | ||
var evidenceId = new EvidenceId(bytes); | ||
var hex = ByteUtil.Hex(bytes); | ||
Assert.Equal(evidenceId, _type.ParseValue(hex)); | ||
|
||
Assert.Throws<InvalidOperationException>(() => _type.ParseValue(0)); | ||
Assert.Throws<InvalidOperationException>(() => _type.ParseValue(new EvidenceId())); | ||
Assert.Throws<InvalidOperationException>(() => _type.ParseValue(new object())); | ||
} | ||
|
||
[Fact] | ||
public void Serialize() | ||
{ | ||
var bytes = TestUtils.GetRandomBytes(EvidenceId.Size); | ||
var evidenceId = new EvidenceId(bytes); | ||
var hex = ByteUtil.Hex(bytes); | ||
Assert.Equal(hex, _type.Serialize(evidenceId)); | ||
|
||
Assert.Throws<InvalidOperationException>(() => _type.Serialize(0)); | ||
Assert.Throws<InvalidOperationException>(() => _type.Serialize("")); | ||
Assert.Throws<InvalidOperationException>(() => _type.Serialize(new object())); | ||
} | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
test/Libplanet.Explorer.Tests/Queries/EvidenceQueryTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Bencodex.Types; | ||
using GraphQL; | ||
using GraphQL.Execution; | ||
using GraphQL.Types; | ||
using Libplanet.Action; | ||
using Libplanet.Action.Sys; | ||
using Libplanet.Blockchain; | ||
using Libplanet.Blockchain.Policies; | ||
using Libplanet.Common; | ||
using Libplanet.Crypto; | ||
using Libplanet.Types.Assets; | ||
using Libplanet.Types.Consensus; | ||
using Libplanet.Types.Tx; | ||
using Libplanet.Explorer.Queries; | ||
using Libplanet.Store; | ||
using Libplanet.Store.Trie; | ||
using Xunit; | ||
using static Libplanet.Explorer.Tests.GraphQLTestUtils; | ||
using Libplanet.Action.Loader; | ||
using Libplanet.Types.Evidence; | ||
using Libplanet.Types.Blocks; | ||
using System.Globalization; | ||
|
||
namespace Libplanet.Explorer.Tests.Queries; | ||
|
||
public class EvidenceQueryTest | ||
{ | ||
private readonly GeneratedBlockChainFixture _fixture; | ||
private readonly MockBlockChainContext _source; | ||
private readonly EvidenceQuery _queryGraph; | ||
|
||
public EvidenceQueryTest() | ||
{ | ||
_fixture = new GeneratedBlockChainFixture(seed: 0); | ||
_source = new MockBlockChainContext(_fixture.Chain); | ||
_queryGraph = new EvidenceQuery(); | ||
var _ = new ExplorerQuery(_source); | ||
} | ||
|
||
[Fact] | ||
public async Task ExecuteAsync() | ||
{ | ||
var blocks = GetBlocks().ToArray(); | ||
var block = blocks[System.Random.Shared.Next(blocks.Length)]; | ||
|
||
var result = await ExecuteQueryAsync(@$" | ||
{{ | ||
committedEvidence( | ||
index: {block.Index} | ||
) {{ | ||
id | ||
type | ||
height | ||
targetAddress | ||
timestamp | ||
}} | ||
}} | ||
", _queryGraph, source: _source); | ||
Assert.Null(result.Errors); | ||
var resultData = Assert.IsAssignableFrom<ExecutionNode>(result.Data); | ||
var resultDict = | ||
Assert.IsAssignableFrom<IDictionary<string, object>>(resultData!.ToValue()); | ||
var evidenceData = (object[])resultDict["committedEvidence"]; | ||
Assert.Equal(block.Evidence.Count, evidenceData.Length); | ||
|
||
for (var i = 0; i < block.Evidence.Count; i++) | ||
{ | ||
var evidence = block.Evidence[i]; | ||
var data = (IDictionary<string, object>)evidenceData[i]; | ||
Assert.Equal(evidence.Id.ToString(), data["id"]); | ||
Assert.Equal(evidence.GetType().FullName, data["type"]); | ||
Assert.Equal(evidence.Height, data["height"]); | ||
Assert.Equal(evidence.TargetAddress.ToString(), data["targetAddress"]); | ||
Assert.Equal( | ||
new DateTimeOffsetGraphType().Serialize(evidence.Timestamp), | ||
data["timestamp"]); | ||
// Assert.Equal(block.Evidence[i].Id.ToString(), ((IList<IDictionary<string, object>>)resultDict["committedEvidence"])[i]["id"]); | ||
} | ||
} | ||
|
||
private IEnumerable<Block> GetBlocks() | ||
{ | ||
for (var i = 0; i < _fixture.Chain.Count; i++) | ||
{ | ||
var block = _fixture.Chain[i]; | ||
if (block.Evidence.Count > 0) | ||
{ | ||
yield return block; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters