Skip to content

Commit

Permalink
Minor backtest API changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jhonabreul committed Jan 29, 2024
1 parent 60e88cc commit 8b88fe9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 108 deletions.
73 changes: 0 additions & 73 deletions Api/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -633,79 +633,6 @@ public RestResponse DeleteBacktest(int projectId, string backtestId)
return result;
}

/// <summary>
/// List all the tags for a backtest
/// </summary>
/// <param name="projectId">Project for the backtest we want to update</param>
/// <param name="backtestId">Backtest id we want to update</param>
/// <returns><see cref="BacktestTags"/></returns>
public BacktestTags GetBacktestTags(int projectId, string backtestId)
{
var request = new RestRequest("backtests/tags/list", Method.POST)
{
RequestFormat = DataFormat.Json
};

request.AddParameter("application/json", JsonConvert.SerializeObject(new
{
projectId,
backtestId,
}), ParameterType.RequestBody);

ApiConnection.TryRequest(request, out BacktestTags result);
return result;
}

/// <summary>
/// Add new tags to a backtest
/// </summary>
/// <param name="projectId">Project for the backtest we want to update</param>
/// <param name="backtestId">Backtest id we want to update</param>
/// <param name="tags">The tags to add to the backtest</param>
/// <returns><see cref="RestResponse"/></returns>
public RestResponse AddBacktestTags(int projectId, string backtestId, IReadOnlyCollection<string> tags)
{
var request = new RestRequest("backtests/tags/create", Method.POST)
{
RequestFormat = DataFormat.Json
};

request.AddParameter("application/json", JsonConvert.SerializeObject(new
{
projectId,
backtestId,
tags
}), ParameterType.RequestBody);

ApiConnection.TryRequest(request, out RestResponse result);
return result;
}

/// <summary>
/// Deletes tags from a backtest
/// </summary>
/// <param name="projectId">Project for the backtest we want to update</param>
/// <param name="backtestId">Backtest id we want to update</param>
/// <param name="tags">The tags to delete from the backtest</param>
/// <returns><see cref="RestResponse"/></returns>
public RestResponse DeleteBacktestTags(int projectId, string backtestId, IReadOnlyCollection<string> tags)
{
var request = new RestRequest("backtests/tags/delete", Method.POST)
{
RequestFormat = DataFormat.Json
};

request.AddParameter("application/json", JsonConvert.SerializeObject(new
{
projectId,
backtestId,
tags
}), ParameterType.RequestBody);

ApiConnection.TryRequest(request, out RestResponse result);
return result;
}

/// <summary>
/// Updates the tags collection for a backtest
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions Common/Api/Backtest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ public class Backtest : RestResponse
/// </summary>
[JsonProperty(PropertyName = "parameterSet")]
public ParameterSet ParameterSet { get; set; }

/// <summary>
/// Collection of tags for the backtest
/// </summary>
[JsonProperty(PropertyName = "tags")]
public List<string> Tags { get; set; }
}

/// <summary>
Expand Down
52 changes: 17 additions & 35 deletions Tests/Api/ProjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,48 +363,30 @@ public void UpdateBacktestName()
}

[Test]
public void AddAndDeleteBacktestTags()
public void UpdatesBacktestTags()
{
// We will be using the existing TestBacktest for this test
var tags = new List<string> { "tag1", "tag2", "tag3" };

// Add the tags to the backtest
var addTagsResult = ApiClient.AddBacktestTags(TestProject.ProjectId, TestBacktest.BacktestId, tags);
var addTagsResult = ApiClient.UpdateBacktestTags(TestProject.ProjectId, TestBacktest.BacktestId, tags);
Assert.IsTrue(addTagsResult.Success, $"Error adding tags to backtest:\n {string.Join("\n ", addTagsResult.Errors)}");

// Read the backtest tags and verify they were added
var readTagsResult = ApiClient.GetBacktestTags(TestProject.ProjectId, TestBacktest.BacktestId);
Assert.IsTrue(readTagsResult.Success, $"Error reading backtest tags:\n {string.Join("\n ", readTagsResult.Errors)}");
CollectionAssert.AreEquivalent(tags, readTagsResult.Tags);

// Delete one tag from the backtest
var deleteTagResult = ApiClient.DeleteBacktestTags(TestProject.ProjectId, TestBacktest.BacktestId, tags.Take(1).ToList());
Assert.IsTrue(deleteTagResult.Success, $"Error deleting tag from backtest:\n {string.Join("\n ", deleteTagResult.Errors)}");

// Read the backtest tags and verify the tag was deleted
readTagsResult = ApiClient.GetBacktestTags(TestProject.ProjectId, TestBacktest.BacktestId);
Assert.IsTrue(readTagsResult.Success, $"Error reading backtest tags:\n {string.Join("\n ", readTagsResult.Errors)}");
var remainingTags = tags.Skip(1).ToList();
CollectionAssert.AreEquivalent(remainingTags, readTagsResult.Tags);

// Delete the remaining tags from the backtest
deleteTagResult = ApiClient.DeleteBacktestTags(TestProject.ProjectId, TestBacktest.BacktestId, remainingTags);
Assert.IsTrue(deleteTagResult.Success, $"Error deleting tag from backtest:\n {string.Join("\n ", deleteTagResult.Errors)}");

// Read the backtest tags and verify the tags were deleted
readTagsResult = ApiClient.GetBacktestTags(TestProject.ProjectId, TestBacktest.BacktestId);
Assert.IsTrue(readTagsResult.Success, $"Error reading backtest tags:\n {string.Join("\n ", readTagsResult.Errors)}");
CollectionAssert.IsEmpty(readTagsResult.Tags);

// Override the whole set of tags
var newTags = new List<string> { "tag4", "tag5", "tag6" };
var updateTagsResult = ApiClient.UpdateBacktestTags(TestProject.ProjectId, TestBacktest.BacktestId, newTags);
Assert.IsTrue(updateTagsResult.Success, $"Error updating backtest tags:\n {string.Join("\n ", updateTagsResult.Errors)}");

// Read the backtest tags and verify the tags were updated
readTagsResult = ApiClient.GetBacktestTags(TestProject.ProjectId, TestBacktest.BacktestId);
Assert.IsTrue(readTagsResult.Success, $"Error reading backtest tags:\n {string.Join("\n ", readTagsResult.Errors)}");
CollectionAssert.AreEquivalent(newTags, readTagsResult.Tags);
// Read the backtest and verify the tags were added
var backtestsResult = ApiClient.ListBacktests(TestProject.ProjectId);
Assert.IsTrue(backtestsResult.Success, $"Error getting backtests:\n {string.Join("\n ", backtestsResult.Errors)}");
Assert.AreEqual(1, backtestsResult.Backtests.Count);
CollectionAssert.AreEquivalent(tags, backtestsResult.Backtests[0].Tags);

// Remove all tags from the backtest
var deleteTagsResult = ApiClient.UpdateBacktestTags(TestProject.ProjectId, TestBacktest.BacktestId, new List<string>());
Assert.IsTrue(deleteTagsResult.Success, $"Error deleting tags from backtest:\n {string.Join("\n ", deleteTagsResult.Errors)}");

// Read the backtest and verify the tags were deleted
backtestsResult = ApiClient.ListBacktests(TestProject.ProjectId);
Assert.IsTrue(backtestsResult.Success, $"Error getting backtests:\n {string.Join("\n ", backtestsResult.Errors)}");
Assert.AreEqual(1, backtestsResult.Backtests.Count);
Assert.AreEqual(0, backtestsResult.Backtests[0].Tags.Count);
}

private static string GetTimestamp()
Expand Down

0 comments on commit 8b88fe9

Please sign in to comment.