-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
524 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
Automate/Speckle.Automate.Sdk/Test/TestAutomateEnvironment.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using Speckle.Core.Logging; | ||
using System.Text.Json; | ||
|
||
namespace Speckle.Automate.Sdk.Test; | ||
|
||
public class TestAppSettings | ||
{ | ||
public string? SpeckleToken { get; set; } | ||
public string? SpeckleServerUrl { get; set; } | ||
public string? SpeckleProjectId { get; set; } | ||
public string? SpeckleAutomationId { get; set; } | ||
} | ||
|
||
public static class TestAutomateEnvironment | ||
{ | ||
public static TestAppSettings? AppSettings { get; private set; } | ||
|
||
private static string GetEnvironmentVariable(string environmentVariableName) | ||
{ | ||
var value = TryGetEnvironmentVariable(environmentVariableName); | ||
|
||
if (value is null) | ||
{ | ||
throw new SpeckleException($"Cannot run tests without a {environmentVariableName} environment variable"); | ||
} | ||
|
||
return value; | ||
} | ||
|
||
private static string? TryGetEnvironmentVariable(string environmentVariableName) | ||
{ | ||
return Environment.GetEnvironmentVariable(environmentVariableName); | ||
} | ||
|
||
private static TestAppSettings? GetAppSettings() | ||
{ | ||
if (AppSettings != null) | ||
{ | ||
return AppSettings; | ||
} | ||
|
||
var path = "./appsettings.json"; | ||
var json = File.ReadAllText(path); | ||
|
||
var appSettings = JsonSerializer.Deserialize<TestAppSettings>(json); | ||
|
||
AppSettings = appSettings; | ||
|
||
return AppSettings; | ||
} | ||
|
||
public static string GetSpeckleToken() | ||
{ | ||
return GetAppSettings()?.SpeckleToken ?? GetEnvironmentVariable("SPECKLE_TOKEN"); | ||
} | ||
|
||
public static Uri GetSpeckleServerUrl() | ||
{ | ||
var urlString = | ||
GetAppSettings()?.SpeckleServerUrl ?? TryGetEnvironmentVariable("SPECKLE_SERVER_URL") ?? "http://127.0.0.1:3000"; | ||
|
||
return new Uri(urlString); | ||
} | ||
|
||
public static string GetSpeckleProjectId() | ||
{ | ||
return GetAppSettings()?.SpeckleProjectId ?? GetEnvironmentVariable("SPECKLE_PROJECT_ID"); | ||
} | ||
|
||
public static string GetSpeckleAutomationId() | ||
{ | ||
return GetAppSettings()?.SpeckleAutomationId ?? GetEnvironmentVariable("SPECKLE_AUTOMATION_ID"); | ||
} | ||
|
||
public static void Clear() | ||
{ | ||
AppSettings = null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using GraphQL; | ||
using Speckle.Automate.Sdk.Schema; | ||
using Speckle.Automate.Sdk.Schema.Triggers; | ||
using Speckle.Core.Api; | ||
|
||
namespace Speckle.Automate.Sdk.Test; | ||
|
||
public static class TestAutomateUtils | ||
{ | ||
public static async Task<AutomationRunData> CreateTestRun(Client speckleClient) | ||
{ | ||
GraphQLRequest query = | ||
new( | ||
query: """ | ||
mutation Mutation($projectId: ID!, $automationId: ID!) { | ||
projectMutations { | ||
automationMutations(projectId: $projectId) { | ||
createTestAutomationRun(automationId: $automationId) { | ||
automationRunId | ||
functionRunId | ||
triggers { | ||
payload { | ||
modelId | ||
versionId | ||
} | ||
triggerType | ||
} | ||
} | ||
} | ||
} | ||
} | ||
""", | ||
variables: new | ||
{ | ||
automationId = TestAutomateEnvironment.GetSpeckleAutomationId(), | ||
projectId = TestAutomateEnvironment.GetSpeckleProjectId() | ||
} | ||
); | ||
|
||
dynamic res = await speckleClient.ExecuteGraphQLRequest<object>(query).ConfigureAwait(false); | ||
|
||
var runData = res["projectMutations"]["automationMutations"]["createTestAutomationRun"]; | ||
var triggerData = runData["triggers"][0]["payload"]; | ||
|
||
string modelId = triggerData["modelId"]; | ||
string versionId = triggerData["versionId"]; | ||
|
||
var data = new AutomationRunData() | ||
{ | ||
ProjectId = TestAutomateEnvironment.GetSpeckleProjectId(), | ||
SpeckleServerUrl = TestAutomateEnvironment.GetSpeckleServerUrl().ToString(), | ||
AutomationId = TestAutomateEnvironment.GetSpeckleAutomationId(), | ||
AutomationRunId = runData["automationRunId"], | ||
FunctionRunId = runData["functionRunId"], | ||
Triggers = new List<AutomationRunTriggerBase>() | ||
{ | ||
new VersionCreationTrigger(modelId: modelId, versionId: versionId) | ||
} | ||
}; | ||
|
||
return data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.