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

add tests #6

Merged
merged 22 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
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
19 changes: 16 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,41 @@ jobs:
uses: actions/checkout@v4
with:
path: DSIronPython3
repository: DynamoDS/DSIronPython3
ref: ${{ github.ref }}

- name: Setup nuget
uses: nuget/[email protected]
- name: Setup msbuild
uses: microsoft/[email protected]
- name: Nuget Restore DSIronPython3 solution
if: ${{ true }}
mjkkirschner marked this conversation as resolved.
Show resolved Hide resolved
run: nuget restore ${{ github.workspace }}\DSIronPython3\IronPython3Extension.sln
- name: Build DSIronPython3 with MSBuild
if: ${{ true }}
run: |
Write-Output "***Continue with the build, Good luck developer!***"
msbuild ${{ github.workspace }}\DSIronPython3\IronPython3Extension.sln
msbuild ${{ github.workspace }}\DSIronPython3\IronPython3Extension.sln -p:Configuration=Release
- name: Look for package
if: ${{ true }}
run: |
Write-Output "***Locating iron python package!***"
if (Test-Path -Path "${{ github.workspace }}\DSIronPython3\package_output\DSIronPython3\bin\python3eval.dll") {
Write-Output "python node dll exists!"
} else {
Write-Error "python node dll was not found!"
}
- name: Get DynamoRuntime from s3
run: |
curl -o DynamoRuntime.zip https://dyn-builds-data.s3-us-west-2.amazonaws.com/DynamoCoreRuntime3.0.2.zip
ls
- name: Extract DynamoRuntime
run: |
7z x DynamoRuntime.zip -o${{ github.workspace }}\DSIronPython3\IronPython3Tests\bin\Release\net8.0\DynamoRuntime


- name: Run test with the dotnet CLI
run: |
dotnet test ${{ github.workspace }}\DSIronPython3 --logger "trx;LogFileName=results.trx" --results-directory ${{ github.workspace }}\DSIronPython3\TestResults
dotnet test ${{ github.workspace }}\DSIronPython3 -p:Configuration=Release --filter "TestCategory!=Failure" --logger "trx;LogFileName=results.trx" --results-directory ${{ github.workspace }}\DSIronPython3\TestResults
- name: Upload build artifact
uses: actions/[email protected]
with:
Expand Down
8 changes: 7 additions & 1 deletion IronPython3Extension.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ VisualStudioVersion = 17.5.33424.131
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IronPython3Extension", "IronPython3Extension\IronPython3Extension.csproj", "{7A44B553-ED7E-4C01-A91A-70C7AD5897A8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "python3eval", "python3eval\python3eval.csproj", "{2B256D7E-4864-4086-A481-6E5B24CBA95E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "python3eval", "python3eval\python3eval.csproj", "{2B256D7E-4864-4086-A481-6E5B24CBA95E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IronPython3Tests", "IronPython3Tests\IronPython3Tests.csproj", "{CD76A408-05CF-45E6-A508-6A6BA6A95E75}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -21,6 +23,10 @@ Global
{2B256D7E-4864-4086-A481-6E5B24CBA95E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2B256D7E-4864-4086-A481-6E5B24CBA95E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2B256D7E-4864-4086-A481-6E5B24CBA95E}.Release|Any CPU.Build.0 = Release|Any CPU
{CD76A408-05CF-45E6-A508-6A6BA6A95E75}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CD76A408-05CF-45E6-A508-6A6BA6A95E75}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CD76A408-05CF-45E6-A508-6A6BA6A95E75}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CD76A408-05CF-45E6-A508-6A6BA6A95E75}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 1 addition & 1 deletion IronPython3Extension/IronPython3Extension.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
<OutputPath>$(SolutionDir)\package_output\DSIronPython3\bin\</OutputPath>
Expand Down
34 changes: 34 additions & 0 deletions IronPython3Tests/IronPython3Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IsTestProject is set automatically by consuming the microsoft test.sdk nuget
Not sure why you need to specify IsPackable false

Copy link
Member Author

@mjkkirschner mjkkirschner Jan 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these were set by VS when creating a test project from nunit template. Do you want me to remove them?

</PropertyGroup>

<ItemGroup>
<PackageReference Include="DynamoVisualProgramming.Tests" Version="3.1.0-beta3874">
<PrivateAssets>all</PrivateAssets>
mjkkirschner marked this conversation as resolved.
Show resolved Hide resolved
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.6.1" />
mjkkirschner marked this conversation as resolved.
Show resolved Hide resolved
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\python3eval\python3eval.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="TestServices.dll.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
84 changes: 84 additions & 0 deletions IronPython3Tests/Setup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.IO;
using System.Reflection;
using NUnit.Framework;


[SetUpFixture]
public class Setup
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code is stolen from @pinzart90's example in DynamoSamples repo.

{
private string moduleRootFolder;
List<string> resolutionPaths;

[OneTimeSetUp]
public void RunBeforeAllTests()
{
var thisDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var configPath = Path.Combine(thisDir, "TestServices.dll.config");

// Adjust the config file map because the config file
// might not always be in the same directory as the dll.
var map = new ExeConfigurationFileMap { ExeConfigFilename = configPath };
var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);

var element = config.AppSettings.Settings["DynamoBasePath"];
moduleRootFolder = element?.Value ?? string.Empty;

if (string.IsNullOrEmpty(moduleRootFolder))
{
throw new Exception($"Missing DynamoBasePath in TestServices.dll.config. Please set the DynamoBasePath to a valid Dynamo bin folder. DynamoBasePath is set to {moduleRootFolder}");
}
else if (!File.Exists(Path.Combine(moduleRootFolder, "DynamoCore.dll")))
{
throw new Exception($"Invalid DynamoBasePath in TestServices.dll.config. Please set the DynamoBasePath to a valid Dynamo bin folder. DynamoBasePath is set to {moduleRootFolder}");
}

resolutionPaths = new List<string>
{
// Search for nodes
Path.Combine(moduleRootFolder, "nodes")
};
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; ;
}

private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
try
{
var targetAssemblyName = new AssemblyName(args.Name).Name + ".dll";

// First check the core path
string assemblyPath = Path.Combine(moduleRootFolder, targetAssemblyName);
if (File.Exists(assemblyPath))
{
return Assembly.LoadFrom(assemblyPath);
}

// Then check all additional resolution paths
foreach (var resolutionPath in resolutionPaths)
{
assemblyPath = Path.Combine(resolutionPath, targetAssemblyName);
if (File.Exists(assemblyPath))
{
return Assembly.LoadFrom(assemblyPath);
}
}

return null;
}
catch (Exception ex)
{
throw new Exception(string.Format("There location of the assembly, " +
"{0} could not be resolved for loading.", args.Name), ex);
}
}

[OneTimeTearDown]
public void RunAfterAllTests()
{
AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve;
}
}
7 changes: 7 additions & 0 deletions IronPython3Tests/TestServices.dll.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="DynamoBasePath" value=".\DynamoRuntime"/>
<add key="RequestedLibraryVersion2" value=""/>
</appSettings>
</configuration>
156 changes: 156 additions & 0 deletions IronPython3Tests/pythonEvalTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
using Dynamo;
using NUnit.Framework;
using System.Collections;


namespace IronPython3Tests
{
public class PythonEvalTests : UnitTestBase
{
public delegate object PythonEvaluatorDelegate(string code, IList bindingNames, IList bindingValues);

public IEnumerable<PythonEvaluatorDelegate> Evaluators = new List<PythonEvaluatorDelegate> {
IronPython3.Evaluator.IronPython3Evaluator.EvaluateIronPythonScript,
};

[Test]
[Category("UnitTests")]
public void EvaluatorWorks()
{
foreach (var pythonEvaluator in Evaluators)
{
var empty = new ArrayList();
var output = pythonEvaluator("OUT = 0", empty, empty);
Assert.AreEqual(0, output);
}
}

[Test]
[Category("UnitTests")]
public void BindingsWork()
{
const string expected = "Hi!";

var names = new ArrayList { "test" };
var vals = new ArrayList { expected };

foreach (var pythonEvaluator in Evaluators)
{
var output = pythonEvaluator(
"OUT = test",
names,
vals
);

Assert.AreEqual(expected, output);
}
}

[Test]
[Category("UnitTests")]
public void DataMarshaling_Output()
{
var marshaler = IronPython3.Evaluator.IronPython3Evaluator.OutputMarshaler;
marshaler.RegisterMarshaler((string s) => s.Length);

const string script = "OUT = ['', ' ', ' ']";

object output = IronPython3.Evaluator.IronPython3Evaluator.EvaluateIronPythonScript(
script,
new ArrayList(),
new ArrayList());

Assert.AreEqual(new[] { 0, 1, 2 }, output);

marshaler.UnregisterMarshalerOfType<string>();
}

[Test]
[Category("UnitTests")]
public void DataMarshaling_Input()
{
var marshaler = IronPython3.Evaluator.IronPython3Evaluator.InputMarshaler;
marshaler.RegisterMarshaler((string s) => s.Length);

const string script = "OUT = sum(IN)";

object output = IronPython3.Evaluator.IronPython3Evaluator.EvaluateIronPythonScript(
script,
new ArrayList { "IN" },
new ArrayList { new ArrayList { " ", " " } });

Assert.AreEqual(3, output);

marshaler.UnregisterMarshalerOfType<string>();
}


[Test]
public void SliceOperator_Output()
{
var names = new ArrayList { "indx" };
var vals = new ArrayList { 3 };

foreach (var pythonEvaluator in Evaluators)
{
var output = pythonEvaluator(
"OUT = [1,2,3,4,5,6,7][indx:indx+2]",
names,
vals);

var expected = new ArrayList { 4, 5 };

Assert.AreEqual(expected, output);
}
}

[Test]
public void IronPythonGivesCorrectErrorLineNumberAndLoadsStdLib()
{
var code = @"
from xml.dom.minidom import parseString
my_xml = parseString('invalid XML!')
";
try
{
IronPython3.Evaluator.IronPython3Evaluator.EvaluateIronPythonScript(code, new ArrayList(), new ArrayList());
Assert.Fail("An exception was expected");
}
catch (Exception exc)
{
StringAssert.StartsWith(@"Traceback (most recent call last):
File ""<string>"", line 3, in <module>", exc.Message);
StringAssert.EndsWith("Data at the root level is invalid. Line 1, position 1.", exc.Message);
}
}

[Test]
public void NonListIterablesCanBeOutput()
{
var code = @"
s = { 'hello' }
fs = frozenset({ 'world' })
d = { 'one': 1 }
dk = d.keys()
dv = d.values()
di = d.items()

OUT = s,fs,dk,dv,di
";
var expected = new ArrayList
{
new ArrayList { "hello" },
new ArrayList { "world" },
new ArrayList { "one" },
new ArrayList { 1 },
new ArrayList { new ArrayList { "one", 1 } }
};
var empty = new ArrayList();
foreach (var pythonEvaluator in Evaluators)
{
var output = pythonEvaluator(code, empty, empty);
Assert.AreEqual(expected, output);
}
}
}
}
Loading
Loading