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

Unit Test For RPC Module #1111

Merged
merged 13 commits into from
Oct 24, 2019
30 changes: 30 additions & 0 deletions neo.UnitTests/Network/RPC/Models/UT_RpcBlock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Network.P2P.Payloads;
using Neo.Network.RPC.Models;
using System;

namespace Neo.UnitTests.Network.RPC.Models
{
[TestClass]
public class UT_RpcBlock
{
[TestMethod]
public void TestToJson()
{
var rpcBlock = new RpcBlock();
var block = new Block();
TestUtils.SetupBlockWithValues(block, UInt256.Zero, out UInt256 merkRootVal, out UInt160 val160, out ulong timestampVal, out uint indexVal, out Witness scriptVal, out Transaction[] transactionsVal, 1);
rpcBlock.Block = block;
var json = rpcBlock.ToJson();
json["previousblockhash"].AsString().Should().Be("0x0000000000000000000000000000000000000000000000000000000000000000");
Console.WriteLine(json.AsString());
shargon marked this conversation as resolved.
Show resolved Hide resolved
json.Should().NotBeNull();

rpcBlock.Confirmations = 1;
rpcBlock.NextBlockHash = UInt256.Zero;
json = rpcBlock.ToJson();
json["confirmations"].AsNumber().Should().Be(1);
}
}
}
30 changes: 30 additions & 0 deletions neo.UnitTests/Network/RPC/Models/UT_RpcBlockHeader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Network.P2P.Payloads;
using Neo.Network.RPC.Models;
using System;

namespace Neo.UnitTests.Network.RPC.Models
{
[TestClass]
public class UT_RpcBlockHeader
{
[TestMethod]
public void TestToJson()
{
var rpcBlockHeader = new RpcBlockHeader();
var header = new Header();
TestUtils.SetupHeaderWithValues(header, UInt256.Zero, out UInt256 merkRootVal, out UInt160 val160, out ulong timestampVal, out uint indexVal, out Witness scriptVal);
eryeer marked this conversation as resolved.
Show resolved Hide resolved
rpcBlockHeader.Header = header;
var json = rpcBlockHeader.ToJson();
json["previousblockhash"].AsString().Should().Be("0x0000000000000000000000000000000000000000000000000000000000000000");
Console.WriteLine(json.AsString());
shargon marked this conversation as resolved.
Show resolved Hide resolved
json.Should().NotBeNull();

rpcBlockHeader.Confirmations = 1;
rpcBlockHeader.NextBlockHash = UInt256.Zero;
json = rpcBlockHeader.ToJson();
json["confirmations"].AsNumber().Should().Be(1);
}
}
}
66 changes: 66 additions & 0 deletions neo.UnitTests/Network/RPC/Models/UT_RpcNep5Balance.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.IO.Json;
using Neo.Network.RPC.Models;
using System.Numerics;

namespace Neo.UnitTests.Network.RPC.Models
{
[TestClass]
public class UT_RpcNep5Balance
{
private RpcNep5Balance balance;

[TestInitialize]
public void Setup()
{
balance = new RpcNep5Balance();
}

[TestMethod]
public void TestAssetHash()
{
balance.AssetHash = UInt160.Zero;
balance.AssetHash.Should().Be(UInt160.Zero);
}

[TestMethod]
public void TestAmount()
{
balance.Amount = BigInteger.Zero;
balance.Amount.Should().Be(BigInteger.Zero);
}

[TestMethod]
public void TestLastUpdatedBlock()
{
balance.LastUpdatedBlock = 0;
balance.LastUpdatedBlock.Should().Be(0);
}

[TestMethod]
public void TestToJson()
{
balance.AssetHash = UInt160.Zero;
balance.Amount = BigInteger.Zero;
balance.LastUpdatedBlock = 0;
var json = balance.ToJson();
json["asset_hash"].AsString().Should().Be("0000000000000000000000000000000000000000");
json["amount"].AsNumber().Should().Be(0);
json["last_updated_block"].AsNumber().Should().Be(0);
}

[TestMethod]
public void TestFromJson()
{
var json = new JObject();
json["asset_hash"] = "0000000000000000000000000000000000000000";
json["amount"] = "0";
json["last_updated_block"] = "0";
var rpcNep5Balance = RpcNep5Balance.FromJson(json);
rpcNep5Balance.AssetHash.Should().Be(UInt160.Zero);
rpcNep5Balance.Amount.Should().Be(BigInteger.Zero);
rpcNep5Balance.LastUpdatedBlock.Should().Be(0);
}
}
}
60 changes: 60 additions & 0 deletions neo.UnitTests/Network/RPC/Models/UT_RpcNep5Balances.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.IO.Json;
using Neo.Network.RPC.Models;
using System.Numerics;

namespace Neo.UnitTests.Network.RPC.Models
{
[TestClass]
public class UT_RpcNep5Balances
{
private RpcNep5Balances balances;

[TestInitialize]
public void Setup()
{
balances = new RpcNep5Balances()
{
Address = "abc",
Balances = new RpcNep5Balance[] {
new RpcNep5Balance()
eryeer marked this conversation as resolved.
Show resolved Hide resolved
{
AssetHash = UInt160.Zero,
Amount = BigInteger.Zero,
LastUpdatedBlock = 0
}
}
};
}

[TestMethod]
public void TestAddress()
{
balances.Address.Should().Be("abc");
}

[TestMethod]
public void TestBalances()
{
balances.Balances.Length.Should().Be(1);
}

[TestMethod]
public void TestToJson()
{
var json = balances.ToJson();
json["address"].AsString().Should().Be("abc");
((JArray)json["balance"]).Count.Should().Be(1);
}

[TestMethod]
public void TestFromJson()
{
var json = balances.ToJson();
var rpcNep5Balances = RpcNep5Balances.FromJson(json);
rpcNep5Balances.Address.Should().Be("abc");
rpcNep5Balances.Balances.Length.Should().Be(1);
}
}
}
23 changes: 23 additions & 0 deletions neo.UnitTests/Network/RPC/Models/UT_RpcPeer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Network.RPC.Models;

namespace Neo.UnitTests.Network.RPC.Models
{
[TestClass]
public class UT_RpcPeer
{
[TestMethod]
public void TestToJson()
{
var rpcPeer = new RpcPeer()
{
Address = "abc",
Port = 800
};
var json = rpcPeer.ToJson();
json["address"].AsString().Should().Be("abc");
json["port"].AsNumber().Should().Be(800);
}
}
}
44 changes: 44 additions & 0 deletions neo.UnitTests/Network/RPC/Models/UT_RpcPeers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.IO.Json;
using Neo.Network.RPC.Models;

namespace Neo.UnitTests.Network.RPC.Models
{
[TestClass]
public class UT_RpcPeers
{
[TestMethod]
public void TestToJson()
{
var rpcPeers = new RpcPeers()
{
Unconnected = new RpcPeer[] {
new RpcPeer()
{
Address = "Unconnected",
Port = 600
}
},
Bad = new RpcPeer[] {
new RpcPeer()
{
Address = "Bad",
Port = 700
}
},
Connected = new RpcPeer[] {
new RpcPeer()
{
Address = "Connected",
Port = 800
}
}
};
var json = rpcPeers.ToJson();
((JArray)json["unconnected"]).Count.Should().Be(1);
((JArray)json["bad"]).Count.Should().Be(1);
((JArray)json["connected"]).Count.Should().Be(1);
}
}
}
29 changes: 29 additions & 0 deletions neo.UnitTests/Network/RPC/Models/UT_RpcRawMemPool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Network.RPC.Models;

namespace Neo.UnitTests.Network.RPC.Models
{
[TestClass]
public class UT_RpcRawMemPool
{
[TestMethod]
public void TestToJson()
{
var pool = new RpcRawMemPool
{
Height = 1,
Verified = new string[] {
"a", "b"
},
UnVerified = new string[] {
"c", "d"
}
};
var json = pool.ToJson();
json["height"].AsNumber().Should().Be(1);
json["verified"].AsString().Should().Be("a,b");
json["unverified"].AsString().Should().Be("c,d");
}
}
}
31 changes: 31 additions & 0 deletions neo.UnitTests/Network/RPC/Models/UT_RpcRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.IO.Json;
using Neo.Network.RPC.Models;

namespace Neo.UnitTests.Network.RPC.Models
{
[TestClass]
public class UT_RpcRequest
{
[TestMethod]
public void TestFromJson()
{
var req = new RpcRequest()
{
Id = 1,
Jsonrpc = "myrpc",
Method = "get",
Params = new JObject[] {
new JBoolean(true)
}
};
var json = req.ToJson();
var rpcRequest = RpcRequest.FromJson(json);
rpcRequest.Jsonrpc.Should().Be("myrpc");
rpcRequest.Method.Should().Be("get");
rpcRequest.Id.Should().Be(1);
rpcRequest.Params.Length.Should().Be(1);
}
}
}
34 changes: 34 additions & 0 deletions neo.UnitTests/Network/RPC/Models/UT_RpcResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.IO.Json;
using Neo.Network.RPC.Models;

namespace Neo.UnitTests.Network.RPC.Models
{
[TestClass]
public class UT_RpcResponse
{
[TestMethod]
public void TestToJson()
{
var error = new RpcResponseError()
{
Code = 0,
Message = "msg",
Data = new JBoolean(true)
};
var rep = new RpcResponse()
{
Id = 1,
Jsonrpc = "rpc",
Error = error,
Result = new JBoolean(true)
};
var json = rep.ToJson();
json["id"].AsNumber().Should().Be(1);
json["jsonrpc"].AsString().Should().Be("rpc");
json["error"].AsString().Should().Be(error.ToJson().AsString());
json["result"].AsBoolean().Should().BeTrue();
}
}
}
27 changes: 27 additions & 0 deletions neo.UnitTests/Network/RPC/Models/UT_RpcVersion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Network.RPC.Models;

namespace Neo.UnitTests.Network.RPC.Models
{
[TestClass]
public class UT_RpcVersion
{
[TestMethod]
public void TestToJson()
{
var version = new RpcVersion()
{
TcpPort = 800,
WsPort = 900,
Nonce = 1,
UserAgent = "agent"
};
var json = version.ToJson();
json["topPort"].AsNumber().Should().Be(800);
json["wsPort"].AsNumber().Should().Be(900);
json["nonce"].AsNumber().Should().Be(1);
json["useragent"].AsString().Should().Be("agent");
}
}
}
Loading