Skip to content

Commit

Permalink
Unit Test For RPC Module (#1111)
Browse files Browse the repository at this point in the history
  • Loading branch information
eryeer authored and erikzhang committed Oct 24, 2019
1 parent ae7b319 commit cd85039
Show file tree
Hide file tree
Showing 12 changed files with 459 additions and 3 deletions.
29 changes: 29 additions & 0 deletions neo.UnitTests/Network/RPC/Models/UT_RpcBlock.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.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 _, out UInt160 _, out ulong _, out uint _, out Witness _, out Transaction[] _, 1);
rpcBlock.Block = block;
var json = rpcBlock.ToJson();
json["previousblockhash"].AsString().Should().Be("0x0000000000000000000000000000000000000000000000000000000000000000");
json.Should().NotBeNull();

rpcBlock.Confirmations = 1;
rpcBlock.NextBlockHash = UInt256.Zero;
json = rpcBlock.ToJson();
json["confirmations"].AsNumber().Should().Be(1);
}
}
}
29 changes: 29 additions & 0 deletions neo.UnitTests/Network/RPC/Models/UT_RpcBlockHeader.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.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 _, out UInt160 _, out ulong _, out uint _, out Witness _);
rpcBlockHeader.Header = header;
var json = rpcBlockHeader.ToJson();
json["previousblockhash"].AsString().Should().Be("0x0000000000000000000000000000000000000000000000000000000000000000");
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);
}
}
}
66 changes: 66 additions & 0 deletions neo.UnitTests/Network/RPC/Models/UT_RpcNep5Balances.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_RpcNep5Balances
{
private RpcNep5Balances balances;

[TestInitialize]
public void Setup()
{
balances = new RpcNep5Balances()
{
Address = "abc",
Balances = new RpcNep5Balance[] {
new RpcNep5Balance()
{
AssetHash = UInt160.Zero,
Amount = BigInteger.Zero,
LastUpdatedBlock = 0
},
new RpcNep5Balance()
{
AssetHash = UInt160.Parse("0xa400ff00ff00ff00ff00ff00ff00ff00ff00ff01"),
Amount = new BigInteger(1),
LastUpdatedBlock = 1
}
}
};
}

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

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

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

[TestMethod]
public void TestFromJson()
{
var json = balances.ToJson();
var rpcNep5Balances = RpcNep5Balances.FromJson(json);
rpcNep5Balances.Address.Should().Be("abc");
rpcNep5Balances.Balances.Length.Should().Be(2);
}
}
}
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

0 comments on commit cd85039

Please sign in to comment.