From 85e56570c71a97b98aca9400c166369f9527656e Mon Sep 17 00:00:00 2001 From: Chuck Driesler Date: Thu, 6 Jun 2024 18:26:41 +0100 Subject: [PATCH] WEB-1031 Update test to use new test automations capabilities (#21) * test env setup * make and fail request * successful test automation submission * use new sdk version for tests * nits --- .gitignore | 2 + .../SpeckleAutomateDotnetExample.csproj | 5 +- TestAutomateFunction/AutomationContextTest.cs | 84 +++---------------- .../TestAutomateFunction.csproj | 8 +- TestAutomateFunction/TestAutomateUtils.cs | 69 --------------- TestAutomateFunction/appsettings.example.json | 6 ++ 6 files changed, 29 insertions(+), 145 deletions(-) delete mode 100644 TestAutomateFunction/TestAutomateUtils.cs create mode 100644 TestAutomateFunction/appsettings.example.json diff --git a/.gitignore b/.gitignore index 2b7e4fb..5e7e65a 100644 --- a/.gitignore +++ b/.gitignore @@ -576,3 +576,5 @@ FodyWeavers.xsd # Additional files built by Visual Studio # End of https://www.toptal.com/developers/gitignore/api/dotnetcore,linux,visualstudiocode,rider,visualstudio,windows,macos + +appsettings.json \ No newline at end of file diff --git a/SpeckleAutomateDotnetExample/SpeckleAutomateDotnetExample.csproj b/SpeckleAutomateDotnetExample/SpeckleAutomateDotnetExample.csproj index 0449b76..787f804 100644 --- a/SpeckleAutomateDotnetExample/SpeckleAutomateDotnetExample.csproj +++ b/SpeckleAutomateDotnetExample/SpeckleAutomateDotnetExample.csproj @@ -8,7 +8,8 @@ - - + + + diff --git a/TestAutomateFunction/AutomationContextTest.cs b/TestAutomateFunction/AutomationContextTest.cs index 525b457..beea937 100644 --- a/TestAutomateFunction/AutomationContextTest.cs +++ b/TestAutomateFunction/AutomationContextTest.cs @@ -1,91 +1,24 @@ -# nullable enable namespace TestAutomateFunction; -using Speckle.Automate.Sdk.Schema; using Speckle.Automate.Sdk; +using Speckle.Automate.Sdk.Test; 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(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 { 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()} + token = TestAutomateEnvironment.GetSpeckleToken(), + serverInfo = new ServerInfo { url = TestAutomateEnvironment.GetSpeckleServerUrl().ToString() } }; client = new Client(account); } @@ -93,12 +26,18 @@ public sealed class AutomationContextTest : IDisposable [Test] public async Task TestFunctionRun() { - var automationRunData = await AutomationRunData(TestAutomateUtils.TestObject()); + var inputs = new FunctionInputs + { + SpeckleTypeToCount = "Base", + SpeckleTypeTargetCount = 1 + }; + + var automationRunData = await TestAutomateUtils.CreateTestRun(client); var automationContext = await AutomationRunner.RunFunction( AutomateFunction.Run, automationRunData, account.token, - new FunctionInputs { SpeckleTypeToCount = "Base" } + inputs ); Assert.That(automationContext.RunStatus, Is.EqualTo("SUCCEEDED")); @@ -107,5 +46,6 @@ public sealed class AutomationContextTest : IDisposable public void Dispose() { client.Dispose(); + TestAutomateEnvironment.Clear(); } } diff --git a/TestAutomateFunction/TestAutomateFunction.csproj b/TestAutomateFunction/TestAutomateFunction.csproj index efc5480..10b7954 100644 --- a/TestAutomateFunction/TestAutomateFunction.csproj +++ b/TestAutomateFunction/TestAutomateFunction.csproj @@ -14,12 +14,16 @@ - - + + + PreserveNewest + + + diff --git a/TestAutomateFunction/TestAutomateUtils.cs b/TestAutomateFunction/TestAutomateUtils.cs deleted file mode 100644 index d5bcf25..0000000 --- a/TestAutomateFunction/TestAutomateUtils.cs +++ /dev/null @@ -1,69 +0,0 @@ -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(query); - } -} diff --git a/TestAutomateFunction/appsettings.example.json b/TestAutomateFunction/appsettings.example.json new file mode 100644 index 0000000..30540f2 --- /dev/null +++ b/TestAutomateFunction/appsettings.example.json @@ -0,0 +1,6 @@ +{ + "SpeckleToken": "YOUR-TOKEN-HERE", + "SpeckleServerUrl": "http://127.0.0.1:3000", + "SpeckleProjectId": "YOUR-PROJECT-ID-HERE", + "SpeckleAutomationId": "YOUR-AUTOMATION-ID-HERE" +} \ No newline at end of file