-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(net): add Demo Client tests (#681)
Co-authored-by: Dennis Diatlov <[email protected]>
- Loading branch information
1 parent
7a3ebd2
commit 442b945
Showing
10 changed files
with
250 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
using System; | ||
using System.Threading; | ||
using Sails.DemoClient.Tests._Infra.XUnit.Fixtures; | ||
using Sails.Remoting.Abstractions; | ||
using Substrate.NetApi.Model.Types.Base; | ||
using Substrate.NetApi.Model.Types.Primitive; | ||
|
||
namespace Sails.DemoClient.Tests; | ||
|
||
public class CounterTests(SailsFixture sailsFixture) : RemotingTestsBase(sailsFixture) | ||
{ | ||
[Fact] | ||
public async Task Counter_Add_Works() | ||
{ | ||
// arrange | ||
var codeId = await this.SailsFixture.GetDemoContractCodeIdAsync(); | ||
|
||
var demoFactory = new Demo.DemoFactory(this.Remoting); | ||
var counterClient = new Demo.Counter(this.Remoting); | ||
// TODO add listener | ||
//var counterClient = new Demo.CounterListener(this.remoting); | ||
|
||
// act | ||
var dogPosition = new BaseOpt<BaseTuple<I32, I32>>(new BaseTuple<I32, I32>(new I32(0), new I32(0))); | ||
var programId = await demoFactory | ||
.New(counter: new U32(42), dogPosition: dogPosition) | ||
.SendReceiveAsync(codeId, BitConverter.GetBytes(Random.NextInt64()), CancellationToken.None); | ||
|
||
var result = await counterClient.Add(new U32(10)).SendReceiveAsync(programId, CancellationToken.None); | ||
|
||
// assert | ||
Assert.NotNull(result); | ||
Assert.Equal(52u, result.Value); | ||
// TODO add event assert | ||
} | ||
|
||
[Fact] | ||
public async Task Counter_Sub_Works() | ||
{ | ||
// arrange | ||
var codeId = await this.SailsFixture.GetDemoContractCodeIdAsync(); | ||
|
||
var demoFactory = new Demo.DemoFactory(this.Remoting); | ||
var counterClient = new Demo.Counter(this.Remoting); | ||
// TODO add listener | ||
//var counterClient = new Demo.CounterListener(this.remoting); | ||
|
||
// act | ||
var dogPosition = new BaseOpt<BaseTuple<I32, I32>>(new BaseTuple<I32, I32>(new I32(0), new I32(0))); | ||
var programId = await demoFactory | ||
.New(counter: new U32(42), dogPosition: dogPosition) | ||
.SendReceiveAsync(codeId, BitConverter.GetBytes(Random.NextInt64()), CancellationToken.None); | ||
|
||
var result = await counterClient.Sub(new U32(10)).SendReceiveAsync(programId, CancellationToken.None); | ||
|
||
// assert | ||
Assert.NotNull(result); | ||
Assert.Equal(32u, result.Value); | ||
// TODO add event assert | ||
} | ||
|
||
[Fact] | ||
public async Task Counter_Query_Works() | ||
{ | ||
// arrange | ||
var codeId = await this.SailsFixture.GetDemoContractCodeIdAsync(); | ||
|
||
var demoFactory = new Demo.DemoFactory(this.Remoting); | ||
var counterClient = new Demo.Counter(this.Remoting); | ||
|
||
// act | ||
var dogPosition = new BaseOpt<BaseTuple<I32, I32>>(new BaseTuple<I32, I32>(new I32(0), new I32(0))); | ||
var programId = await demoFactory | ||
.New(counter: new U32(42), dogPosition: dogPosition) | ||
.SendReceiveAsync(codeId, BitConverter.GetBytes(Random.NextInt64()), CancellationToken.None); | ||
|
||
var result = await counterClient.Value().QueryAsync(programId, CancellationToken.None); | ||
|
||
// assert | ||
Assert.NotNull(result); | ||
Assert.Equal(42u, result.Value); | ||
} | ||
|
||
[Fact] | ||
public async Task Counter_Query_Throws_NotEnoughGas() | ||
{ | ||
// arrange | ||
var codeId = await this.SailsFixture.GetDemoContractCodeIdAsync(); | ||
|
||
var demoFactory = new Demo.DemoFactory(this.Remoting); | ||
var counterClient = new Demo.Counter(this.Remoting); | ||
|
||
// act | ||
var dogPosition = new BaseOpt<BaseTuple<I32, I32>>(new BaseTuple<I32, I32>(new I32(0), new I32(0))); | ||
var programId = await demoFactory | ||
.New(counter: new U32(42), dogPosition: dogPosition) | ||
.SendReceiveAsync(codeId, BitConverter.GetBytes(Random.NextInt64()), CancellationToken.None); | ||
|
||
var ex = await Assert.ThrowsAsync<ArgumentException>(() => counterClient.Value() | ||
.WithGasLimit(new GasUnit(0)) | ||
.QueryAsync(programId, CancellationToken.None) | ||
); | ||
|
||
// assert | ||
Assert.NotNull(ex); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,51 @@ | ||
using System; | ||
using System.Threading; | ||
using Sails.DemoClient.Tests._Infra.XUnit.Fixtures; | ||
|
||
namespace Sails.DemoClient.Tests; | ||
|
||
public sealed class DemoFactoryTests : IAssemblyFixture<SailsFixture> | ||
public sealed class DemoFactoryTests(SailsFixture fixture) : RemotingTestsBase(fixture) | ||
{ | ||
public DemoFactoryTests(SailsFixture fixture) | ||
[Fact] | ||
public async Task Test1() | ||
{ | ||
this.sailsFixture = fixture; | ||
// Assert that IDL file from the Sails.DemoClient project is the same as the one | ||
// from the SailsFixture | ||
var demoContractCodeId = await this.SailsFixture.GetDemoContractCodeIdAsync(); | ||
} | ||
|
||
private readonly SailsFixture sailsFixture; | ||
[Fact] | ||
public async Task Demo_DefaultConstructor_Works() | ||
{ | ||
// arrange | ||
var codeId = await this.SailsFixture.GetDemoContractCodeIdAsync(); | ||
|
||
// act | ||
var demoFactory = new Demo.DemoFactory(this.Remoting); | ||
var activate = await demoFactory | ||
.Default() | ||
.ActivateAsync(codeId, BitConverter.GetBytes(Random.NextInt64()), CancellationToken.None); | ||
var programId = await activate.ReceiveAsync(CancellationToken.None); | ||
|
||
// assert | ||
Assert.NotNull(programId); | ||
} | ||
|
||
[Fact] | ||
public async Task Test1() | ||
public async Task Demo_Activation_Throws_NotEnoughGas() | ||
{ | ||
var demoContractCodeId = await this.sailsFixture.GetDemoContractCodeIdAsync(); | ||
// arrange | ||
var codeId = await this.SailsFixture.GetDemoContractCodeIdAsync(); | ||
|
||
// act | ||
var demoFactory = new Demo.DemoFactory(this.Remoting); | ||
var activate = await demoFactory | ||
.Default() | ||
.WithGasLimit(new GasUnit(0)) | ||
.ActivateAsync(codeId, BitConverter.GetBytes(Random.NextInt64()), CancellationToken.None); | ||
// throws on ReceiveAsync | ||
var ex = await Assert.ThrowsAsync<ArgumentException>(() => activate.ReceiveAsync(CancellationToken.None)); | ||
|
||
// assert | ||
// TODO assert custom exception | ||
Assert.NotNull(ex); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
global using System.Threading.Tasks; | ||
global using Sails.Tests.Shared.XUnit; | ||
global using Xunit; | ||
global using GasUnit = Substrate.NetApi.Model.Types.Primitive.U64; | ||
global using ValueUnit = Substrate.NetApi.Model.Types.Primitive.U128; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System; | ||
using System.Threading; | ||
using Sails.DemoClient.Tests._Infra.XUnit.Fixtures; | ||
using Sails.Remoting.Abstractions; | ||
using Substrate.Gear.Client.NetApi.Model.Types.Base; | ||
using Substrate.NetApi.Model.Types.Primitive; | ||
|
||
namespace Sails.DemoClient.Tests; | ||
|
||
public class PingPongTests(SailsFixture sailsFixture) : RemotingTestsBase(sailsFixture) | ||
{ | ||
[Fact] | ||
public async Task PingPong_Works() | ||
{ | ||
// arrange | ||
var codeId = await this.SailsFixture.GetDemoContractCodeIdAsync(); | ||
|
||
var demoFactory = new Demo.DemoFactory(this.Remoting); | ||
var pingPongClient = new Demo.PingPong(this.Remoting); | ||
|
||
// act | ||
var programId = await demoFactory | ||
.Default() | ||
.SendReceiveAsync(codeId, BitConverter.GetBytes(Random.NextInt64()), CancellationToken.None); | ||
|
||
var result = await pingPongClient.Ping(new Str("ping")).SendReceiveAsync(programId, CancellationToken.None); | ||
|
||
// assert | ||
Assert.True(result.Matches<BaseResultEnum, Str>(BaseResultEnum.Ok, s => s == "pong")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Sails.DemoClient.Tests._Infra.XUnit.Fixtures; | ||
using Sails.Remoting.Abstractions.Core; | ||
using Sails.Remoting.DependencyInjection; | ||
using Sails.Remoting.Options; | ||
|
||
namespace Sails.DemoClient.Tests; | ||
|
||
public class RemotingTestsBase : IAssemblyFixture<SailsFixture> | ||
{ | ||
public RemotingTestsBase(SailsFixture fixture) | ||
{ | ||
this.SailsFixture = fixture; | ||
// Assert that IDL file from the Sails.DemoClient project is the same as the one | ||
// from the SailsFixture | ||
var serviceCollection = new ServiceCollection(); | ||
serviceCollection.AddRemotingViaNodeClient( | ||
new NodeClientOptions | ||
{ | ||
GearNodeUri = this.SailsFixture.GearNodeWsUrl, | ||
}); | ||
var serviceProvider = serviceCollection.BuildServiceProvider(); | ||
this.RemotingProvider = serviceProvider.GetRequiredService<IRemotingProvider>(); | ||
this.Remoting = this.RemotingProvider.CreateRemoting(SailsFixture.AliceAccount); | ||
} | ||
|
||
protected static readonly Random Random = new((int)DateTime.UtcNow.Ticks); | ||
|
||
protected readonly SailsFixture SailsFixture; | ||
protected readonly IRemotingProvider RemotingProvider; | ||
protected readonly IRemoting Remoting; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using System.Threading; | ||
using Sails.DemoClient.Tests._Infra.XUnit.Fixtures; | ||
using Sails.Remoting.Abstractions; | ||
|
||
namespace Sails.DemoClient.Tests; | ||
|
||
public class ValueFeeTests(SailsFixture sailsFixture) : RemotingTestsBase(sailsFixture) | ||
{ | ||
[Fact] | ||
public async Task ValueFee_Works() | ||
{ | ||
// arrange | ||
var codeId = await this.SailsFixture.GetDemoContractCodeIdAsync(); | ||
|
||
var demoFactory = new Demo.DemoFactory(this.Remoting); | ||
var valueFeeClient = new Demo.ValueFee(this.Remoting); | ||
|
||
// act | ||
var programId = await demoFactory | ||
.Default() | ||
.SendReceiveAsync(codeId, BitConverter.GetBytes(Random.NextInt64()), CancellationToken.None); | ||
|
||
var result = await valueFeeClient | ||
.DoSomethingAndTakeFee() | ||
.WithValue(new ValueUnit(15_000_000_000_000)) | ||
.SendReceiveAsync(programId, CancellationToken.None); | ||
|
||
// assert | ||
Assert.True(result); | ||
// TODO assert balances | ||
} | ||
} |