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 Legder module #1038

Merged
merged 36 commits into from
Sep 23, 2019
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
ba0d2d3
update test ledger
eryeer Aug 19, 2019
d3573f9
format
eryeer Aug 19, 2019
678e52c
Add enter between classes
shargon Aug 19, 2019
5cc4f29
fix ut suggestion
eryeer Aug 20, 2019
0f0ff55
Merge branch 'utledger' of github.com:eryeer/neoUT into utledger
eryeer Aug 20, 2019
8cf5f80
Merge branch 'master' into utledger
eryeer Aug 20, 2019
f9c1350
Merge branch 'master' into utledger
eryeer Aug 21, 2019
c2c4e23
Merge branch 'master' into utledger
eryeer Aug 22, 2019
9bdf201
Merge branch 'master' into utledger
vncoelho Aug 24, 2019
6f3b67d
Merge branch 'master' into utledger
vncoelho Aug 25, 2019
2457cc4
Merge branch 'master' into utledger
eryeer Aug 27, 2019
7506e12
Merge branch 'master' into utledger
shargon Aug 27, 2019
c9af847
Merge branch 'master' into utledger
vncoelho Aug 27, 2019
d35d8b5
Update UT_PoolItem.cs
shargon Aug 27, 2019
8fd75d8
Update UT_MemoryPool.cs
shargon Aug 27, 2019
0601178
Reduce usings
shargon Aug 27, 2019
67056e9
Reduce changes
shargon Aug 27, 2019
0913340
Merge branch 'master' into utledger
vncoelho Aug 28, 2019
89e4d34
More details on TestReVerifyTopUnverifiedTransactionsIfNeeded and fix…
vncoelho Aug 28, 2019
d5040d5
Minor fix on generic transaction generation fee
vncoelho Aug 28, 2019
2db010f
Applying dotnet format
vncoelho Aug 28, 2019
70c0e77
Merge branch 'master' into utledger
eryeer Aug 29, 2019
355c40a
Merge branch 'master' into utledger
vncoelho Aug 29, 2019
c049544
Merge branch 'master' into utledger
igormcoelho Aug 29, 2019
374745d
Enhance functions in TestDataCache
eryeer Aug 30, 2019
6a65143
Merge branch 'utledger' of github.com:eryeer/neoUT into utledger
eryeer Aug 30, 2019
d56b834
dotnet format
eryeer Aug 30, 2019
feb6c8b
Merge branch 'master' into utledger
eryeer Sep 9, 2019
19c50cf
Cast refactor
shargon Sep 9, 2019
cd74cde
Merge branch 'master' into utledger
lock9 Sep 11, 2019
2a76a37
comment tx3 for mempool
eryeer Sep 12, 2019
8843077
Merge branch 'utledger' of github.com:eryeer/neoUT into utledger
eryeer Sep 12, 2019
0be52d5
Merge branch 'master' into utledger
eryeer Sep 12, 2019
59b964d
Merge branch 'master' into utledger
eryeer Sep 18, 2019
158337c
Removing TODO comment
vncoelho Sep 18, 2019
c917de7
Merge branch 'master' into utledger
eryeer Sep 23, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions neo.UnitTests/Ledger/UT_Blockchain.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using System.IO;

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

public static TestBlock Cast(Block input)
{
TestBlock block = new TestBlock();
MemoryStream stream = new MemoryStream();
BinaryWriter writer = new BinaryWriter(stream);
BinaryReader reader = new BinaryReader(stream);
input.Serialize(writer);
stream.Seek(0, SeekOrigin.Begin);
block.Deserialize(reader);
return block;
}
}

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

public static TestHeader Cast(Header input)
{
TestHeader header = new TestHeader();
MemoryStream stream = new MemoryStream();
BinaryWriter writer = new BinaryWriter(stream);
BinaryReader reader = new BinaryReader(stream);
input.Serialize(writer);
stream.Seek(0, SeekOrigin.Begin);
header.Deserialize(reader);
return header;
}
}

[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().BeTrue();
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().NotBeNull();
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