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

WEB-1031 Provide test utils from sdk #3467

Merged
merged 2 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
77 changes: 77 additions & 0 deletions Automate/Speckle.Automate.Sdk/Test/TestEnvironment.cs
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")
Copy link
Contributor

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 the TryGet... ?

Copy link
Member Author

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 others

although it is a bit silly when you say it like that, open to other ways

?? "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;
}
}
63 changes: 63 additions & 0 deletions Automate/Speckle.Automate.Sdk/Test/TestUtils.cs
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;
}
}