Skip to content

Commit

Permalink
Merge pull request #3549 from greymistcube/feature/new-query-types
Browse files Browse the repository at this point in the history
✨ New query types
  • Loading branch information
greymistcube authored Dec 14, 2023
2 parents 0a2a1be + 65bb38b commit b37461e
Show file tree
Hide file tree
Showing 20 changed files with 582 additions and 244 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ Version 3.9.2

To be released.

- (Libplanet.Explorer) Added `BlockHashType` and `TxIdType`. [[#3549]]
- (Libplanet.Explorer) Changed `HashDigestSHA256Type` to `HashDigestType<T>`.
[[#3549]]

[#3549]: https://github.com/planetarium/libplanet/pull/3549


Version 3.9.1
-------------
Expand Down
61 changes: 43 additions & 18 deletions Libplanet.Explorer.Tests/GraphTypes/AddressTypeTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using GraphQL.Language.AST;
using Libplanet.Common;
using Libplanet.Crypto;
using Libplanet.Explorer.GraphTypes;
using Xunit;
Expand All @@ -8,36 +10,59 @@ namespace Libplanet.Explorer.Tests.GraphTypes
public class AddressTypeTest : ScalarGraphTypeTestBase<AddressType>
{
[Fact]
public void Serialize()
public void ParseLiteral()
{
var randomBytes = TestUtils.GetRandomBytes(Address.Size);
var randomAddress = new Address(randomBytes);
object serialized = _type.Serialize(randomAddress);
Assert.Equal(randomAddress.ToString(), serialized);
}
Assert.Null(_type.ParseLiteral(new NullValue()));

[Theory]
[InlineData(null)]
[InlineData(0)]
[InlineData("")]
public void Serialize_ReturnsItselfIfNotAddressType(object value)
{
Assert.Equal(value, _type.Serialize(value));
var bytes = TestUtils.GetRandomBytes(Address.Size);
var address = new Address(bytes);
var hex = ByteUtil.Hex(bytes);
var prefixedHex = address.ToString();
Assert.Equal(
address,
Assert.IsType<Address>(_type.ParseLiteral(new StringValue(prefixedHex))));
Assert.Equal(
address,
Assert.IsType<Address>(_type.ParseLiteral(new StringValue(hex))));

Assert.Throws<InvalidOperationException>(
() => _type.ParseLiteral(new LongValue(1234)));
Assert.Throws<InvalidOperationException>(
() => _type.ParseValue(new StringValue("address")));
}

[Fact]
public void ParseValue()
{
Assert.Null(_type.ParseValue(null));
Assert.Equal(new Address(), _type.ParseValue("0x0000000000000000000000000000000000000000"));

var bytes = TestUtils.GetRandomBytes(Address.Size);
var address = new Address(bytes);
var hex = ByteUtil.Hex(bytes);
var prefixedHex = address.ToString();
Assert.Equal(address, _type.ParseValue(prefixedHex));
Assert.Equal(address, _type.ParseValue(hex));

Assert.Throws<InvalidOperationException>(() => _type.ParseValue(0));
Assert.Throws<InvalidOperationException>(() => _type.ParseValue(new Address()));
Assert.Throws<InvalidOperationException>(() => _type.ParseValue(new object()));
}

[Fact]
public void ParseValue_ThrowsArgumentException()
public void Serialize()
{
Assert.Throws<ArgumentException>(() => _type.ParseValue(0));
Assert.Throws<ArgumentException>(() => _type.ParseValue(new Address()));
Assert.Throws<ArgumentException>(() => _type.ParseValue(new object()));
Assert.Null(_type.Serialize(null));

var bytes = TestUtils.GetRandomBytes(Address.Size);
var address = new Address(bytes);
var hex = ByteUtil.Hex(bytes);
var prefixedHex = address.ToString();
Assert.Equal(prefixedHex, _type.Serialize(address));
Assert.NotEqual(hex, _type.Serialize(address));

Assert.Throws<InvalidOperationException>(() => _type.Serialize(0));
Assert.Throws<InvalidOperationException>(() => _type.Serialize(""));
Assert.Throws<InvalidOperationException>(() => _type.Serialize(new object()));
}
}
}
58 changes: 58 additions & 0 deletions Libplanet.Explorer.Tests/GraphTypes/BlockHashTypeTest.cs
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.Blocks;
using Xunit;

namespace Libplanet.Explorer.Tests.GraphTypes
{
public class BlockHashTypeTest : ScalarGraphTypeTestBase<BlockHashType>
{
[Fact]
public void ParseLiteral()
{
Assert.Null(_type.ParseLiteral(new NullValue()));

var bytes = TestUtils.GetRandomBytes(BlockHash.Size);
var blockHash = new BlockHash(bytes);
var hex = ByteUtil.Hex(bytes);
Assert.Equal(
blockHash,
Assert.IsType<BlockHash>(_type.ParseLiteral(new StringValue(hex))));

Assert.Throws<InvalidOperationException>(
() => _type.ParseLiteral(new LongValue(1234)));
Assert.Throws<InvalidOperationException>(
() => _type.ParseValue(new StringValue("blockHash")));
}

[Fact]
public void ParseValue()
{
Assert.Null(_type.ParseValue(null));

var bytes = TestUtils.GetRandomBytes(BlockHash.Size);
var blockHash = new BlockHash(bytes);
var hex = ByteUtil.Hex(bytes);
Assert.Equal(blockHash, _type.ParseValue(hex));

Assert.Throws<InvalidOperationException>(() => _type.ParseValue(0));
Assert.Throws<InvalidOperationException>(() => _type.ParseValue(new BlockHash()));
Assert.Throws<InvalidOperationException>(() => _type.ParseValue(new object()));
}

[Fact]
public void Serialize()
{
var bytes = TestUtils.GetRandomBytes(BlockHash.Size);
var blockHash = new BlockHash(bytes);
var hex = ByteUtil.Hex(bytes);
Assert.Equal(hex, _type.Serialize(blockHash));

Assert.Throws<InvalidOperationException>(() => _type.Serialize(0));
Assert.Throws<InvalidOperationException>(() => _type.Serialize(""));
Assert.Throws<InvalidOperationException>(() => _type.Serialize(new object()));
}
}
}
66 changes: 66 additions & 0 deletions Libplanet.Explorer.Tests/GraphTypes/HashDigestTypeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;
using System.Security.Cryptography;
using GraphQL.Language.AST;
using Libplanet.Common;
using Libplanet.Crypto;
using Libplanet.Explorer.GraphTypes;
using Xunit;

namespace Libplanet.Explorer.Tests.GraphTypes
{
public class HashDigestTypeTest : ScalarGraphTypeTestBase<HashDigestType<SHA256>>
{
[Fact]
public void ParseLiteral()
{
Assert.Null(_type.ParseLiteral(new NullValue()));

var bytes = TestUtils.GetRandomBytes(HashDigest<SHA256>.Size);
var hashDigestSHA256 = new HashDigest<SHA256>(bytes);
var hex = ByteUtil.Hex(bytes);
Assert.Equal(
hashDigestSHA256,
Assert.IsType<HashDigest<SHA256>>(_type.ParseLiteral(new StringValue(hex))));

bytes = TestUtils.GetRandomBytes(HashDigest<SHA1>.Size);
hex = ByteUtil.Hex(bytes);
Assert.Throws<ArgumentOutOfRangeException>(
() => _type.ParseLiteral(new StringValue(hex)));
Assert.Throws<InvalidOperationException>(
() => _type.ParseLiteral(new LongValue(1234)));
Assert.Throws<InvalidOperationException>(
() => _type.ParseValue(new StringValue("hashDigest")));
}

[Fact]
public void ParseValue()
{
Assert.Null(_type.ParseValue(null));

var bytes = TestUtils.GetRandomBytes(HashDigest<SHA256>.Size);
var hashDigest = new HashDigest<SHA256>(bytes);
var hex = ByteUtil.Hex(bytes);
Assert.Equal(hashDigest, _type.ParseValue(hex));

Assert.Throws<InvalidOperationException>(
() => _type.ParseValue(0));
Assert.Throws<InvalidOperationException>(
() => _type.ParseValue(new HashDigest<SHA256>()));
Assert.Throws<InvalidOperationException>(
() => _type.ParseValue(new object()));
}

[Fact]
public void Serialize()
{
var bytes = TestUtils.GetRandomBytes(HashDigest<SHA256>.Size);
var hashDigest = new HashDigest<SHA256>(bytes);
var hex = ByteUtil.Hex(bytes);
Assert.Equal(hex, _type.Serialize(hashDigest));

Assert.Throws<InvalidOperationException>(() => _type.Serialize(0));
Assert.Throws<InvalidOperationException>(() => _type.Serialize(""));
Assert.Throws<InvalidOperationException>(() => _type.Serialize(new object()));
}
}
}
59 changes: 41 additions & 18 deletions Libplanet.Explorer.Tests/GraphTypes/PublicKeyTypeTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using GraphQL.Language.AST;
using Libplanet.Crypto;
using Libplanet.Explorer.GraphTypes;
using Xunit;
Expand All @@ -8,35 +9,57 @@ namespace Libplanet.Explorer.Tests.GraphTypes
public class PublicKeyTypeTest : ScalarGraphTypeTestBase<PublicKeyType>
{
[Fact]
public void Serialize()
public void ParseLiteral()
{
var randomPublicKey = new PrivateKey().PublicKey;
object serialized = _type.Serialize(randomPublicKey);
Assert.Equal(randomPublicKey.ToString(), serialized);
}
Assert.Null(_type.ParseLiteral(new NullValue()));

[Theory]
[InlineData(null)]
[InlineData(0)]
[InlineData("")]
public void Serialize_ReturnsItselfIfNotPublicKeyType(object value)
{
Assert.Equal(value, _type.Serialize(value));
var publicKey = new PrivateKey().PublicKey;
var compressed = publicKey.ToHex(true);
var uncompressed = publicKey.ToHex(false);
Assert.Equal(
publicKey,
Assert.IsType<PublicKey>(_type.ParseLiteral(new StringValue(compressed))));
Assert.Equal(
publicKey,
Assert.IsType<PublicKey>(_type.ParseLiteral(new StringValue(uncompressed))));

Assert.Throws<InvalidOperationException>(
() => _type.ParseLiteral(new LongValue(1234)));
Assert.Throws<InvalidOperationException>(
() => _type.ParseValue(new StringValue("publicKey")));
}

[Fact]
public void ParseValue()
{
var expected = new PrivateKey().PublicKey;
Assert.Equal(expected, _type.ParseValue(expected.ToString()));
Assert.Null(_type.ParseValue(null));

var publicKey = new PrivateKey().PublicKey;
var compressed = publicKey.ToHex(true);
var uncompressed = publicKey.ToHex(false);
Assert.Equal(publicKey, _type.ParseValue(compressed));
Assert.Equal(publicKey, _type.ParseValue(uncompressed));

Assert.Throws<InvalidOperationException>(() => _type.ParseValue(0));
Assert.Throws<InvalidOperationException>(
() => _type.ParseValue(new PrivateKey().PublicKey));
Assert.Throws<InvalidOperationException>(() => _type.ParseValue(new object()));
}

[Fact]
public void ParseValue_ThrowsArgumentException()
public void Serialize()
{
Assert.Throws<ArgumentException>(() => _type.ParseValue(0));
Assert.Throws<ArgumentException>(() => _type.ParseValue(new PrivateKey().PublicKey));
Assert.Throws<ArgumentException>(() => _type.ParseValue(new object()));
Assert.Null(_type.Serialize(null));

var publicKey = new PrivateKey().PublicKey;
var compressed = publicKey.ToHex(true);
var uncompressed = publicKey.ToHex(false);
Assert.Equal(compressed, _type.Serialize(publicKey));
Assert.NotEqual(uncompressed, _type.Serialize(publicKey));

Assert.Throws<InvalidOperationException>(() => _type.Serialize(0));
Assert.Throws<InvalidOperationException>(() => _type.Serialize(""));
Assert.Throws<InvalidOperationException>(() => _type.Serialize(new object()));
}
}
}
58 changes: 58 additions & 0 deletions Libplanet.Explorer.Tests/GraphTypes/TxIdTypeTest.cs
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.Tx;
using Xunit;

namespace Libplanet.Explorer.Tests.GraphTypes
{
public class TxIdTypeTest : ScalarGraphTypeTestBase<TxIdType>
{
[Fact]
public void ParseLiteral()
{
Assert.Null(_type.ParseLiteral(new NullValue()));

var bytes = TestUtils.GetRandomBytes(TxId.Size);
var txId = new TxId(bytes);
var hex = ByteUtil.Hex(bytes);
Assert.Equal(
txId,
Assert.IsType<TxId>(_type.ParseLiteral(new StringValue(hex))));

Assert.Throws<InvalidOperationException>(
() => _type.ParseLiteral(new LongValue(1234)));
Assert.Throws<InvalidOperationException>(
() => _type.ParseValue(new StringValue("txId")));
}

[Fact]
public void ParseValue()
{
Assert.Null(_type.ParseValue(null));

var bytes = TestUtils.GetRandomBytes(TxId.Size);
var txId = new TxId(bytes);
var hex = ByteUtil.Hex(bytes);
Assert.Equal(txId, _type.ParseValue(hex));

Assert.Throws<InvalidOperationException>(() => _type.ParseValue(0));
Assert.Throws<InvalidOperationException>(() => _type.ParseValue(new TxId()));
Assert.Throws<InvalidOperationException>(() => _type.ParseValue(new object()));
}

[Fact]
public void Serialize()
{
var bytes = TestUtils.GetRandomBytes(TxId.Size);
var txId = new TxId(bytes);
var hex = ByteUtil.Hex(bytes);
Assert.Equal(hex, _type.Serialize(txId));

Assert.Throws<InvalidOperationException>(() => _type.Serialize(0));
Assert.Throws<InvalidOperationException>(() => _type.Serialize(""));
Assert.Throws<InvalidOperationException>(() => _type.Serialize(new object()));
}
}
}
Loading

0 comments on commit b37461e

Please sign in to comment.