Skip to content

Commit

Permalink
feat(net): add Demo Client tests (#681)
Browse files Browse the repository at this point in the history
Co-authored-by: Dennis Diatlov <[email protected]>
  • Loading branch information
vobradovich and DennisInSky authored Nov 26, 2024
1 parent 7a3ebd2 commit 442b945
Show file tree
Hide file tree
Showing 10 changed files with 250 additions and 13 deletions.
4 changes: 2 additions & 2 deletions net/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<PackageVersion Include="MessagePack" Version="2.5.192" />
<PackageVersion Include="System.Text.Json" Version="9.0.0" />
<!-- Examples -->
<PackageVersion Include="Sails.ClientGenerator" Version="0.0.1-beta.1" />
<PackageVersion Include="Sails.Net" Version="0.0.1-beta.1" />
<PackageVersion Include="Sails.ClientGenerator" Version="0.0.1-beta.2" />
<PackageVersion Include="Sails.Net" Version="0.0.1-beta.2" />
</ItemGroup>
</Project>
4 changes: 1 addition & 3 deletions net/tests/Sails.Client.Tests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
global using System;
global using System.Collections.Generic;
global using System.Linq;
global using System.Collections.Generic;
global using System.Threading;
global using System.Threading.Tasks;
global using NSubstitute;
Expand Down
2 changes: 2 additions & 0 deletions net/tests/Sails.DemoClient.Tests/AssemblyAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@

[assembly: AssemblyFixture(
typeof(Sails.DemoClient.Tests._Infra.XUnit.Fixtures.SailsFixture))]

[assembly: CollectionBehavior(DisableTestParallelization = true)]
107 changes: 107 additions & 0 deletions net/tests/Sails.DemoClient.Tests/CounterTests.cs
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);
}
}
46 changes: 38 additions & 8 deletions net/tests/Sails.DemoClient.Tests/DemoFactoryTests.cs
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);
}
}
2 changes: 2 additions & 0 deletions net/tests/Sails.DemoClient.Tests/GlobalUsings.cs
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;
31 changes: 31 additions & 0 deletions net/tests/Sails.DemoClient.Tests/PingPongTests.cs
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"));
}
}
33 changes: 33 additions & 0 deletions net/tests/Sails.DemoClient.Tests/RemotingTestsBase.cs
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<ItemGroup>
<PackageReference Include="coverlet.collector" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" />
Expand Down
33 changes: 33 additions & 0 deletions net/tests/Sails.DemoClient.Tests/ValueFeeTests.cs
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
}
}

0 comments on commit 442b945

Please sign in to comment.