gergo/localTesting (#18)
* feat: add test project * feat: add test project
This commit is contained in:
@@ -5,6 +5,8 @@ VisualStudioVersion = 17.5.002.0
|
|||||||
MinimumVisualStudioVersion = 10.0.40219.1
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SpeckleAutomateDotnetExample", "SpeckleAutomateDotnetExample/SpeckleAutomateDotnetExample.csproj", "{E1DE5809-1111-4817-9B7D-7ADCA087FA1C}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SpeckleAutomateDotnetExample", "SpeckleAutomateDotnetExample/SpeckleAutomateDotnetExample.csproj", "{E1DE5809-1111-4817-9B7D-7ADCA087FA1C}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestAutomateFunction", "TestAutomateFunction\TestAutomateFunction.csproj", "{8107A920-A5E2-459C-9756-04B91FB63F3F}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@@ -15,6 +17,10 @@ Global
|
|||||||
{E1DE5809-1111-4817-9B7D-7ADCA087FA1C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{E1DE5809-1111-4817-9B7D-7ADCA087FA1C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{E1DE5809-1111-4817-9B7D-7ADCA087FA1C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{E1DE5809-1111-4817-9B7D-7ADCA087FA1C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{E1DE5809-1111-4817-9B7D-7ADCA087FA1C}.Release|Any CPU.Build.0 = Release|Any CPU
|
{E1DE5809-1111-4817-9B7D-7ADCA087FA1C}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{8107A920-A5E2-459C-9756-04B91FB63F3F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{8107A920-A5E2-459C-9756-04B91FB63F3F}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{8107A920-A5E2-459C-9756-04B91FB63F3F}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{8107A920-A5E2-459C-9756-04B91FB63F3F}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ using Speckle.Automate.Sdk;
|
|||||||
using Speckle.Core.Logging;
|
using Speckle.Core.Logging;
|
||||||
using Speckle.Core.Models.Extensions;
|
using Speckle.Core.Models.Extensions;
|
||||||
|
|
||||||
static class AutomateFunction
|
public static class AutomateFunction
|
||||||
{
|
{
|
||||||
public static async Task Run(
|
public static async Task Run(
|
||||||
AutomationContext automationContext,
|
AutomationContext automationContext,
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ using System.ComponentModel.DataAnnotations;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// This class is used to generate a JSON Schema to ensure that the user provided values
|
/// This class is used to generate a JSON Schema to ensure that the user provided values
|
||||||
/// are valid and match the required schema.
|
/// are valid and match the required schema.
|
||||||
struct FunctionInputs
|
public struct FunctionInputs
|
||||||
{
|
{
|
||||||
[Required]
|
[Required]
|
||||||
public string SpeckleTypeToCount;
|
public string SpeckleTypeToCount;
|
||||||
|
|||||||
@@ -0,0 +1,111 @@
|
|||||||
|
# nullable enable
|
||||||
|
namespace TestAutomateFunction;
|
||||||
|
|
||||||
|
using Speckle.Automate.Sdk.Schema;
|
||||||
|
using Speckle.Automate.Sdk;
|
||||||
|
using Speckle.Core.Api;
|
||||||
|
using Speckle.Core.Credentials;
|
||||||
|
using Speckle.Core.Models;
|
||||||
|
using Speckle.Core.Transports;
|
||||||
|
using Utils = TestAutomateUtils;
|
||||||
|
|
||||||
|
[TestFixture]
|
||||||
|
public sealed class AutomationContextTest : IDisposable
|
||||||
|
{
|
||||||
|
private async Task<AutomationRunData> AutomationRunData(Base testObject)
|
||||||
|
{
|
||||||
|
string projectId = await client.StreamCreate(new() { name = "Automate function e2e test" });
|
||||||
|
const string branchName = "main";
|
||||||
|
|
||||||
|
Branch model = await client.BranchGet(projectId, branchName, 1);
|
||||||
|
string modelId = model.id;
|
||||||
|
|
||||||
|
string rootObjId = await Operations.Send(
|
||||||
|
testObject,
|
||||||
|
new List<ITransport> { new ServerTransport(client.Account, projectId) }
|
||||||
|
);
|
||||||
|
|
||||||
|
string versionId = await client.CommitCreate(
|
||||||
|
new()
|
||||||
|
{
|
||||||
|
streamId = projectId,
|
||||||
|
objectId = rootObjId,
|
||||||
|
branchName = model.name
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
var automationName = TestAutomateUtils.RandomString(10);
|
||||||
|
var automationId = TestAutomateUtils.RandomString(10);
|
||||||
|
var automationRevisionId = TestAutomateUtils.RandomString(10);
|
||||||
|
|
||||||
|
await TestAutomateUtils.RegisterNewAutomation(projectId, modelId, client, automationId, automationName, automationRevisionId);
|
||||||
|
|
||||||
|
var automationRunId = TestAutomateUtils.RandomString(10);
|
||||||
|
var functionId = TestAutomateUtils.RandomString(10);
|
||||||
|
var functionName = "Automation name " + TestAutomateUtils.RandomString(10);
|
||||||
|
var functionRelease = TestAutomateUtils.RandomString(10);
|
||||||
|
|
||||||
|
return new AutomationRunData
|
||||||
|
{
|
||||||
|
ProjectId = projectId,
|
||||||
|
ModelId = modelId,
|
||||||
|
BranchName = branchName,
|
||||||
|
VersionId = versionId,
|
||||||
|
SpeckleServerUrl = client.ServerUrl,
|
||||||
|
AutomationId = automationId,
|
||||||
|
AutomationRevisionId = automationRevisionId,
|
||||||
|
AutomationRunId = automationRunId,
|
||||||
|
FunctionId = functionId,
|
||||||
|
FunctionName = functionName,
|
||||||
|
FunctionRelease = functionRelease,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private Client client;
|
||||||
|
private Account account;
|
||||||
|
|
||||||
|
private string GetSpeckleToken()
|
||||||
|
{
|
||||||
|
var envVarName = "SPECKLE_TOKEN";
|
||||||
|
var token = Environment.GetEnvironmentVariable(envVarName);
|
||||||
|
if (token is null)
|
||||||
|
{
|
||||||
|
throw new Exception($"Cannot run tests without a {envVarName} environment variable");
|
||||||
|
}
|
||||||
|
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
|
||||||
|
private string GetSpeckleServerUrl() =>
|
||||||
|
Environment.GetEnvironmentVariable("SPECKLE_SERVER_ULR") ?? "http://127.0.0.1:3000";
|
||||||
|
|
||||||
|
[OneTimeSetUp]
|
||||||
|
public void Setup()
|
||||||
|
{
|
||||||
|
account = new Account
|
||||||
|
{
|
||||||
|
token = GetSpeckleToken(),
|
||||||
|
serverInfo = new ServerInfo { url = GetSpeckleServerUrl()}
|
||||||
|
};
|
||||||
|
client = new Client(account);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task TestFunctionRun()
|
||||||
|
{
|
||||||
|
var automationRunData = await AutomationRunData(TestAutomateUtils.TestObject());
|
||||||
|
var automationContext = await AutomationRunner.RunFunction(
|
||||||
|
AutomateFunction.Run,
|
||||||
|
automationRunData,
|
||||||
|
account.token,
|
||||||
|
new FunctionInputs { SpeckleTypeToCount = "Base" }
|
||||||
|
);
|
||||||
|
|
||||||
|
Assert.That(automationContext.RunStatus, Is.EqualTo("SUCCEEDED"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
client.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net7.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
|
||||||
|
<IsPackable>false</IsPackable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2"/>
|
||||||
|
<PackageReference Include="NUnit" Version="3.13.3"/>
|
||||||
|
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1"/>
|
||||||
|
<PackageReference Include="NUnit.Analyzers" Version="3.3.0"/>
|
||||||
|
<PackageReference Include="coverlet.collector" Version="3.1.2"/>
|
||||||
|
<PackageReference Include="Speckle.Automate.Sdk" Version="2.18.0-fileInput" />
|
||||||
|
<PackageReference Include="Speckle.Objects" Version="2.17.0-automate3" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\SpeckleAutomateDotnetExample\SpeckleAutomateDotnetExample.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using GraphQL;
|
||||||
|
using Speckle.Core.Api;
|
||||||
|
using Speckle.Core.Models;
|
||||||
|
|
||||||
|
namespace TestAutomateFunction;
|
||||||
|
|
||||||
|
public static class TestAutomateUtils
|
||||||
|
{
|
||||||
|
[SuppressMessage("Security", "CA5394:Do not use insecure randomness")]
|
||||||
|
public static string RandomString(int length)
|
||||||
|
{
|
||||||
|
Random rand = new();
|
||||||
|
const string pool = "abcdefghijklmnopqrstuvwxyz0123456789";
|
||||||
|
var chars = Enumerable.Range(0, length).Select(_ => pool[rand.Next(0, pool.Length)]);
|
||||||
|
return new string(chars.ToArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Base TestObject()
|
||||||
|
{
|
||||||
|
Base rootObject = new() { ["foo"] = "bar" };
|
||||||
|
return rootObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task RegisterNewAutomation(
|
||||||
|
string projectId,
|
||||||
|
string modelId,
|
||||||
|
Client speckleClient,
|
||||||
|
string automationId,
|
||||||
|
string automationName,
|
||||||
|
string automationRevisionId
|
||||||
|
)
|
||||||
|
{
|
||||||
|
GraphQLRequest query =
|
||||||
|
new(
|
||||||
|
query: """
|
||||||
|
mutation CreateAutomation(
|
||||||
|
$projectId: String!
|
||||||
|
$modelId: String!
|
||||||
|
$automationName: String!
|
||||||
|
$automationId: String!
|
||||||
|
$automationRevisionId: String!
|
||||||
|
) {
|
||||||
|
automationMutations {
|
||||||
|
create(
|
||||||
|
input: {
|
||||||
|
projectId: $projectId
|
||||||
|
modelId: $modelId
|
||||||
|
automationName: $automationName
|
||||||
|
automationId: $automationId
|
||||||
|
automationRevisionId: $automationRevisionId
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""",
|
||||||
|
variables: new
|
||||||
|
{
|
||||||
|
projectId,
|
||||||
|
modelId,
|
||||||
|
automationName,
|
||||||
|
automationId,
|
||||||
|
automationRevisionId,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
await speckleClient.ExecuteGraphQLRequest<object>(query);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
global using NUnit.Framework;
|
||||||
Reference in New Issue
Block a user