Skip to content

Commit

Permalink
✅ fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jxnkwlp committed Feb 4, 2024
1 parent fa8488b commit ec14256
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 114 deletions.
Original file line number Diff line number Diff line change
@@ -1,83 +1,79 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Passingwind.CSharpScriptEngine;
using Shouldly;
using Xunit;

namespace Passingwind.Abp.ElsaModule.CSharp;

public class NuGetPackageService_Tests : ElsaModuleExtensionTestBase
public class NuGetPackageServiceTests : ElsaModuleExtensionTestBase
{
private readonly INuGetPackageService _service;

public NuGetPackageService_Tests()
public NuGetPackageServiceTests()
{
_service = GetRequiredService<INuGetPackageService>();
}

[Fact]
public async Task GetReferencesAsyncTest_01()
public async Task GetReferencesAsyncTest01()
{
var files = await _service.GetReferencesAsync("Newtonsoft.Json", "13.0.2", "net6.0", false, false);
var files = await _service.GetReferencesAsync("Newtonsoft.Json", "13.0.2", "net6.0", false);

files.ShouldNotBeEmpty();
files.Count().ShouldBe(1);
files.Count.ShouldBe(1);
}

[Fact]
public async Task GetReferencesAsyncTest_02()
public async Task GetReferencesAsyncTest02()
{
var files = await _service.GetReferencesAsync("System.Text.Json", "7.0.2", "net6.0", true, false);
var files = await _service.GetReferencesAsync("System.Text.Json", "7.0.2", "net6.0", true);

files.ShouldNotBeEmpty();
files.Count().ShouldBe(3);
files.Count.ShouldBe(3);
}

[Fact]
public async Task GetReferencesAsyncTest_03()
public async Task GetReferencesAsyncTest03()
{
var files = await _service.GetReferencesAsync("Newtonsoft.Json", "13.0.2", "net6.0", false, true);
var files = await _service.GetReferencesAsync("Newtonsoft.Json", "13.0.2", "net6.0", false);

files.ShouldNotBeEmpty();

files.ShouldAllBe(x => File.Exists(x));
}

[Fact]
public async Task GetReferencesAsyncTest_04()
public async Task GetReferencesAsyncTest04()
{
var files = await _service.GetReferencesAsync("MongoDB.Driver", "2.19.0", "net6.0", resolveDependency: true, downloadPackage: true);
var files = await _service.GetReferencesAsync("MongoDB.Driver", "2.19.0", "net6.0", true);

files.ShouldNotBeEmpty();

files.ShouldAllBe(x => File.Exists(x));
}

[Fact]
public async Task DownloadAsyncTest_01()
public async Task DownloadAsyncTest01()
{
await _service.DownloadAsync("Newtonsoft.Json", "13.0.2", "net6.0");
}

[Fact]
public async Task DownloadAsyncTest_02()
{
await _service.DownloadAsync("System.Text.Json", "7.0.2", "net6.0", true);
await _service.DownloadAsync("Newtonsoft.Json", "13.0.2");
await _service.DownloadAsync("System.Text.Json", "7.0.2");
}

[Fact]
public async Task SearchAsyncTest()
{
var packages = await _service.SearchAsync("json");

packages.Count().ShouldBe(10);
packages.Count.ShouldBeGreaterThan(0);
}

[Fact]
public async Task GetPackageVersionsAsyncTest()
{
var version = await _service.GetPackageVersionsAsync("Newtonsoft.Json");
var version = await _service.GetVersionsAsync("Newtonsoft.Json");

version.Count().ShouldBeGreaterThan(0);
version.Count.ShouldBeGreaterThan(0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
using System;
using System.Threading.Tasks;
using NSubstitute;
using Passingwind.CSharpScriptEngine;
using Shouldly;
using Xunit;

namespace Passingwind.Abp.ElsaModule;

public class CSharpScriptHostTests : ElsaModuleExtensionTestBase
{
private readonly ICSharpScriptHost _cSharpScriptHost;

public CSharpScriptHostTests()
{
_cSharpScriptHost = GetRequiredService<ICSharpScriptHost>();
}

[Fact]
public async Task CompileTest1()
{
const string code = @"using System;
Console.WriteLine(""Hello, World!"");"
;
var result = await _cSharpScriptHost.CompileAsync(new CSharpScriptCompileContext(code));

result.Success.ShouldBeTrue();
}

[Fact]
public async Task CompileTest2()
{
const string code = @"
Console.WriteLine(""Hello, World!"");"
;
var result = await _cSharpScriptHost.CompileAsync(new CSharpScriptCompileContext(code));

result.Success.ShouldBeFalse();
}

[Fact]
public async Task CompileTest3()
{
const string code = @"
#r ""nuget: Newtonsoft.Json, 13.0.3""
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(new { a = 1 }));
"
;
var result = await _cSharpScriptHost.CompileAsync(new CSharpScriptCompileContext(code));

result.Success.ShouldBeTrue();
}

[Fact]
public async Task CompileTest4()
{
const string code = @"
#r ""System.Private.Uri""
using System.IO;
using System.Net;
var url = ""https://download.microsoft.com/download/4/C/8/4C830C0C-101F-4BF2-8FCB-32D9A8BA906A/Import_User_Sample_en.csv"";
var request = WebRequest.Create(url);
var response = request.GetResponse();
var dataStream = response.GetResponseStream();
var reader = new StreamReader(dataStream);
var csv = await reader.ReadToEndAsync();
reader.Close();
dataStream.Close();
response.Close();
var users = csv.Split('\n').Skip(1)
.Select(line => line.Split(','))
.Where(values => values.Length == 15)
.Select(values => new {
firstName = values[1],
lastName = values[2],
officeNumber = int.Parse(values[6])
});
foreach (var u in users)
Console.WriteLine(u);
"
;
var result = await _cSharpScriptHost.CompileAsync(new CSharpScriptCompileContext(code));

result.Success.ShouldBeTrue();
}

[Fact]
public async Task RunTest1()
{
const string code = @"
#r ""System.Private.Uri""
using System.IO;
using System.Net;
var url = ""https://download.microsoft.com/download/4/C/8/4C830C0C-101F-4BF2-8FCB-32D9A8BA906A/Import_User_Sample_en.csv"";
var request = WebRequest.Create(url);
var response = request.GetResponse();
var dataStream = response.GetResponseStream();
var reader = new StreamReader(dataStream);
var csv = await reader.ReadToEndAsync();
reader.Close();
dataStream.Close();
response.Close();
var users = csv.Split('\n').Skip(1)
.Select(line => line.Split(','))
.Where(values => values.Length == 15)
.Select(values => new {
firstName = values[1],
lastName = values[2],
officeNumber = int.Parse(values[6])
});
foreach (var u in users)
Console.WriteLine(u);
"
;
var _ = await _cSharpScriptHost.RunAsync(new CSharpScriptCompileContext(code));
}

[Fact]
public async Task RunTest2()
{
const string code = @"
#r ""nuget: Newtonsoft.Json, 13.0.3""
return Newtonsoft.Json.JsonConvert.SerializeObject(new { a = 1 });
"
;
var result = await _cSharpScriptHost.RunAsync(new CSharpScriptCompileContext(code));

result.GetType().ShouldBe(typeof(string));
}

[Fact]
public async Task RunTest3()
{
const string code = @"
return 1;
"
;
var result = await _cSharpScriptHost.RunAsync(new CSharpScriptCompileContext(code));

result.GetType().ShouldBe(typeof(int));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Passingwind.CSharpScriptEngine;
using Volo.Abp.Modularity;

namespace Passingwind.Abp.ElsaModule;
Expand All @@ -13,4 +14,8 @@ namespace Passingwind.Abp.ElsaModule;
)]
public class ElsaModuleExtensionTestModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
context.Services.AddCSharpScriptEngine();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

<ItemGroup>
<ProjectReference Include="..\..\src\Passingwind.Abp.ElsaModule.Application\Passingwind.Abp.ElsaModule.Application.csproj" />
<ProjectReference Include="..\..\src\Passingwind.Abp.ElsaModule.ElsaExtensions\Passingwind.Abp.ElsaModule.ElsaExtensions.csproj" />
<ProjectReference Include="..\..\src\Passingwind.Abp.ElsaModule.ElsaExtensions\Passingwind.Abp.ElsaModule.ElsaExtensions.csproj" />
<ProjectReference Include="..\..\src\Passingwind.CSharpScript\Passingwind.CSharpScriptEngine.csproj" />
<ProjectReference Include="..\Passingwind.Abp.ElsaModule.Domain.Tests\Passingwind.Abp.ElsaModule.Domain.Tests.csproj" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
</ItemGroup>
Expand Down

This file was deleted.

0 comments on commit ec14256

Please sign in to comment.