Skip to content

Commit

Permalink
Unit test For Legder module (neo-project#1038)
Browse files Browse the repository at this point in the history
* update test ledger

* format

* Add enter between classes

* fix ut suggestion

* Update UT_PoolItem.cs

* Update UT_MemoryPool.cs

* Reduce usings

* Reduce changes

* More details on TestReVerifyTopUnverifiedTransactionsIfNeeded and fixed comment on ReVerifyTopUnverifiedTransactionsIfNeeded

* Minor fix on generic transaction generation fee

* Applying dotnet format

* Enhance functions in TestDataCache

* dotnet format

* Cast refactor

* comment tx3 for mempool

* Removing TODO comment
  • Loading branch information
eryeer authored and Luchuan committed Jan 10, 2020
1 parent 622331b commit 29c5843
Show file tree
Hide file tree
Showing 14 changed files with 837 additions and 29 deletions.
102 changes: 102 additions & 0 deletions neo.UnitTests/Ledger/UT_Blockchain.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.IO;
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;

namespace Neo.UnitTests.Ledger
{
internal class TestBlock : Block
{
public override bool Verify(Snapshot snapshot)
{
return true;
}

public static TestBlock Cast(Block input)
{
return input.ToArray().AsSerializable<TestBlock>();
}
}

internal class TestHeader : Header
{
public override bool Verify(Snapshot snapshot)
{
return true;
}

public static TestHeader Cast(Header input)
{
return input.ToArray().AsSerializable<TestHeader>();
}
}

[TestClass]
public class UT_Blockchain
{
private NeoSystem system;
private Store store;
Transaction txSample = Blockchain.GenesisBlock.Transactions[0];

[TestInitialize]
public void Initialize()
{
system = TestBlockchain.InitializeMockNeoSystem();
store = TestBlockchain.GetStore();
Blockchain.Singleton.MemPool.TryAdd(txSample.Hash, txSample);
}

[TestMethod]
public void TestConstructor()
{
system.ActorSystem.ActorOf(Blockchain.Props(system, store)).Should().NotBeSameAs(system.Blockchain);
}

[TestMethod]
public void TestContainsBlock()
{
Blockchain.Singleton.ContainsBlock(UInt256.Zero).Should().BeFalse();
}

[TestMethod]
public void TestContainsTransaction()
{
Blockchain.Singleton.ContainsTransaction(UInt256.Zero).Should().BeFalse();
Blockchain.Singleton.ContainsTransaction(txSample.Hash).Should().BeTrue();
}

[TestMethod]
public void TestGetCurrentBlockHash()
{
Blockchain.Singleton.CurrentBlockHash.Should().Be(UInt256.Parse("5662a113d8fa9532ea9c52046a463e2e3fcfcdd6192d99cad805b376fb643ceb"));
}

[TestMethod]
public void TestGetCurrentHeaderHash()
{
Blockchain.Singleton.CurrentHeaderHash.Should().Be(UInt256.Parse("5662a113d8fa9532ea9c52046a463e2e3fcfcdd6192d99cad805b376fb643ceb"));
}

[TestMethod]
public void TestGetBlock()
{
Blockchain.Singleton.GetBlock(UInt256.Zero).Should().BeNull();
}

[TestMethod]
public void TestGetBlockHash()
{
Blockchain.Singleton.GetBlockHash(0).Should().Be(UInt256.Parse("5662a113d8fa9532ea9c52046a463e2e3fcfcdd6192d99cad805b376fb643ceb"));
Blockchain.Singleton.GetBlockHash(10).Should().BeNull();
}

[TestMethod]
public void TestGetTransaction()
{
Blockchain.Singleton.GetTransaction(UInt256.Zero).Should().BeNull();
Blockchain.Singleton.GetTransaction(txSample.Hash).Should().NotBeNull();
}
}
}
99 changes: 99 additions & 0 deletions neo.UnitTests/Ledger/UT_ContractState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.IO;
using Neo.IO.Json;
using Neo.Ledger;
using Neo.SmartContract;
using Neo.SmartContract.Manifest;
using System.IO;

namespace Neo.UnitTests.Ledger
{
[TestClass]
public class UT_ContractState
{
ContractState contract;
byte[] script = { 0x01 };
ContractManifest manifest;

[TestInitialize]
public void TestSetup()
{
manifest = ContractManifest.CreateDefault(UInt160.Parse("0xa400ff00ff00ff00ff00ff00ff00ff00ff00ff01"));
contract = new ContractState
{
Script = script,
Manifest = manifest
};
}

[TestMethod]
public void TestGetHasStorage()
{
contract.HasStorage.Should().BeFalse();
}

[TestMethod]
public void TestGetPayable()
{
contract.Payable.Should().BeFalse();
}

[TestMethod]
public void TestGetScriptHash()
{
// _scriptHash == null
contract.ScriptHash.Should().Be(script.ToScriptHash());
// _scriptHash != null
contract.ScriptHash.Should().Be(script.ToScriptHash());
}

[TestMethod]
public void TestClone()
{
ICloneable<ContractState> cloneable = contract;
ContractState clone = cloneable.Clone();
clone.ToJson().ToString().Should().Be(contract.ToJson().ToString());
}

[TestMethod]
public void TestFromReplica()
{
ICloneable<ContractState> cloneable = new ContractState();
cloneable.FromReplica(contract);
((ContractState)cloneable).ToJson().ToString().Should().Be(contract.ToJson().ToString());
}

[TestMethod]
public void TestDeserialize()
{
ISerializable newContract = new ContractState();
using (MemoryStream ms = new MemoryStream(1024))
using (BinaryWriter writer = new BinaryWriter(ms))
using (BinaryReader reader = new BinaryReader(ms))
{
((ISerializable)contract).Serialize(writer);
ms.Seek(0, SeekOrigin.Begin);
newContract.Deserialize(reader);
}
((ContractState)newContract).Manifest.ToJson().ToString().Should().Be(contract.Manifest.ToJson().ToString());
((ContractState)newContract).Script.Should().BeEquivalentTo(contract.Script);
}

[TestMethod]
public void TestGetSize()
{
ISerializable newContract = contract;
newContract.Size.Should().Be(355);
}

[TestMethod]
public void TestToJson()
{
JObject json = contract.ToJson();
json["hash"].AsString().Should().Be("0x820944cfdc70976602d71b0091445eedbc661bc5");
json["script"].AsString().Should().Be("01");
json["manifest"].AsString().Should().Be(manifest.ToJson().AsString());
}
}
}
63 changes: 63 additions & 0 deletions neo.UnitTests/Ledger/UT_HashIndexState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.IO;
using Neo.Ledger;
using System.IO;

namespace Neo.UnitTests.Ledger
{
[TestClass]
public class UT_HashIndexState
{
HashIndexState origin;

[TestInitialize]
public void Initialize()
{
origin = new HashIndexState
{
Hash = UInt256.Zero,
Index = 10
};
}

[TestMethod]
public void TestClone()
{
HashIndexState dest = ((ICloneable<HashIndexState>)origin).Clone();
dest.Hash.Should().Be(origin.Hash);
dest.Index.Should().Be(origin.Index);
}

[TestMethod]
public void TestFromReplica()
{
HashIndexState dest = new HashIndexState();
((ICloneable<HashIndexState>)dest).FromReplica(origin);
dest.Hash.Should().Be(origin.Hash);
dest.Index.Should().Be(origin.Index);
}

[TestMethod]
public void TestGetSize()
{
((ISerializable)origin).Size.Should().Be(36);
}

[TestMethod]
public void TestDeserialize()
{
using (MemoryStream ms = new MemoryStream(1024))
using (BinaryWriter writer = new BinaryWriter(ms))
using (BinaryReader reader = new BinaryReader(ms))
{
((ISerializable)origin).Serialize(writer);
ms.Seek(0, SeekOrigin.Begin);
HashIndexState dest = new HashIndexState();
((ISerializable)dest).Deserialize(reader);
dest.Hash.Should().Be(origin.Hash);
dest.Index.Should().Be(origin.Index);
}
}
}
}
59 changes: 59 additions & 0 deletions neo.UnitTests/Ledger/UT_HeaderHashList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.IO;
using Neo.Ledger;
using System.IO;

namespace Neo.UnitTests.Ledger
{
[TestClass]
public class UT_HeaderHashList
{
HeaderHashList origin;

[TestInitialize]
public void Initialize()
{
origin = new HeaderHashList
{
Hashes = new UInt256[] { UInt256.Zero }
};
}

[TestMethod]
public void TestClone()
{
HeaderHashList dest = ((ICloneable<HeaderHashList>)origin).Clone();
dest.Hashes.Should().BeEquivalentTo(origin.Hashes);
}

[TestMethod]
public void TestFromReplica()
{
HeaderHashList dest = new HeaderHashList();
((ICloneable<HeaderHashList>)dest).FromReplica(origin);
dest.Hashes.Should().BeEquivalentTo(origin.Hashes);
}

[TestMethod]
public void TestDeserialize()
{
using (MemoryStream ms = new MemoryStream(1024))
using (BinaryWriter writer = new BinaryWriter(ms))
using (BinaryReader reader = new BinaryReader(ms))
{
((ISerializable)origin).Serialize(writer);
ms.Seek(0, SeekOrigin.Begin);
HeaderHashList dest = new HeaderHashList();
((ISerializable)dest).Deserialize(reader);
dest.Hashes.Should().BeEquivalentTo(origin.Hashes);
}
}

[TestMethod]
public void TestGetSize()
{
((ISerializable)origin).Size.Should().Be(33);
}
}
}
Loading

0 comments on commit 29c5843

Please sign in to comment.