-
Notifications
You must be signed in to change notification settings - Fork 177
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
WEB-1031 Provide test utils from sdk #3467
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
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 TestEnvironment | ||
{ | ||
public static TestAppSettings? AppSettings { get; private set; } | ||
|
||
private static string GetEnvironmentVariable(string environmentVariableName) | ||
{ | ||
var value = TryGetEnvironmentVariable(environmentVariableName); | ||
|
||
if (value is null) | ||
{ | ||
throw new Exception($"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); | ||
|
||
TestAppSettings appSettings = JsonSerializer.Deserialize<TestAppSettings>(json ?? "{}"); | ||
|
||
AppSettings = appSettings; | ||
|
||
return AppSettings; | ||
} | ||
|
||
public static string GetSpeckleToken() | ||
{ | ||
return GetAppSettings()?.SpeckleToken ?? GetEnvironmentVariable("SPECKLE_TOKEN"); | ||
} | ||
|
||
public static string GetSpeckleServerUrl() | ||
{ | ||
return GetAppSettings()?.SpeckleServerUrl | ||
?? TryGetEnvironmentVariable("SPECKLE_SERVER_URL") | ||
?? "http://127.0.0.1:3000"; | ||
} | ||
|
||
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 = TestEnvironment.GetSpeckleAutomationId(), | ||
projectId = TestEnvironment.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 = TestEnvironment.GetSpeckleProjectId(), | ||
SpeckleServerUrl = TestEnvironment.GetSpeckleServerUrl(), | ||
AutomationId = TestEnvironment.GetSpeckleAutomationId(), | ||
AutomationRunId = runData["automationRunId"], | ||
FunctionRunId = runData["functionRunId"], | ||
Triggers = new List<AutomationRunTriggerBase>() | ||
{ | ||
new VersionCreationTrigger(modelId: modelId, versionId: versionId) | ||
} | ||
}; | ||
|
||
return data; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all the other methods use the
GetEnvVariable
method, why is this using theTryGet...
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just how I went about doing the fallback to
http://127.0.0.1:3000
! we don't appear to tolerate/provide fallbacks for the othersalthough it is a bit silly when you say it like that, open to other ways