Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
standeren committed Nov 1, 2024
1 parent e74e62c commit 774f0d3
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public class OptionConverter : JsonConverter<object>
{
public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
// Handle JSON token type to convert to either string, bool, or double
return reader.TokenType switch
{
JsonTokenType.String => reader.GetString(),
Expand All @@ -21,7 +20,6 @@ JsonTokenType.Number when reader.TryGetDouble(out double d) => d,

public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options)
{
// Serialize based on the type of the object
switch (value)
{
case string s:
Expand Down
1 change: 0 additions & 1 deletion backend/src/Designer/Models/Option.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,4 @@ public class Option
[JsonPropertyName("helpText")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string HelpText { get; set; }

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ public async Task Put_Returns_200OK_When_Creating_New_OptionsList()

var newOptionsList = new List<Option>
{
new Option
new ()
{
Label = "label1",
Value = "value1",
},
new Option
new ()
{
Label = "label2",
Value = "value2",
Expand Down Expand Up @@ -66,6 +66,45 @@ public async Task Put_Returns_200OK_When_Creating_New_OptionsList()
Assert.Equal(newOptionsList[i].HelpText, responseList[i].HelpText);
}
}

[Fact]
public async Task Put_Returns_200OK_When_Option_Values_Are_Bool_String_Double()
{
string repo = "app-with-options";
string optionsListId = "test-options";
// Arrange
string targetRepository = TestDataHelper.GenerateTestRepoName();
await CopyRepositoryForTest(Org, repo, Developer, targetRepository);

string apiUrl = $"/designer/api/{Org}/{targetRepository}/options/{optionsListId}";
using HttpRequestMessage httpRequestMessage = new(HttpMethod.Put, apiUrl);

var stringBoolDoubleOptionsList = new List<Option>
{
new ()
{
Label = "StringValue",
Value = "value1",
},
new ()
{
Label = "BoolValue",
Value = true,
},
new ()
{
Label = "DoubleValue",
Value = 3.1415,
}
};
httpRequestMessage.Content = JsonContent.Create(stringBoolDoubleOptionsList);

// Act
using HttpResponseMessage response = await HttpClient.SendAsync(httpRequestMessage);

// Assert
Assert.Equal(StatusCodes.Status200OK, (int)response.StatusCode);
}

[Fact]
public async Task Put_Returns_200OK_And_Overwrites_Existing_OptionsList()
Expand All @@ -79,12 +118,12 @@ public async Task Put_Returns_200OK_And_Overwrites_Existing_OptionsList()

var newOptionsList = new List<Option>
{
new Option
new ()
{
Label = "aNewLabelThatDidNotExistBefore",
Value = "aNewValueThatDidNotExistBefore",
},
new Option
new ()
{
Label = "label2",
Value = "value2",
Expand Down Expand Up @@ -129,12 +168,12 @@ public async Task Put_Returns_400BadRequest_When_OptionsList_Format_Is_Invalid(s
{
var invalidOptionsList = new List<Option>
{
new Option
new ()
{
// Missing Label
Value = "value1",
},
new Option
new ()
{
Label = "label2",
Value = "value2",
Expand All @@ -153,4 +192,33 @@ public async Task Put_Returns_400BadRequest_When_OptionsList_Format_Is_Invalid(s
// Assert
Assert.Equal(StatusCodes.Status400BadRequest, (int)response.StatusCode);
}

[Fact]
public async Task Put_Returns_400BadRequest_When_Option_Value_Is_Invalid()
{
string repo = "app-with-options";
string optionsListId = "test-options";
// Arrange
string targetRepository = TestDataHelper.GenerateTestRepoName();
await CopyRepositoryForTest(Org, repo, Developer, targetRepository);

string apiUrl = $"/designer/api/{Org}/{targetRepository}/options/{optionsListId}";
using HttpRequestMessage httpRequestMessage = new(HttpMethod.Put, apiUrl);

var invalidOptionsList = new List<Option>
{
new ()
{
Label = "ObjectValue",
Value = { },
},
};
httpRequestMessage.Content = JsonContent.Create(invalidOptionsList);

// Act
using HttpResponseMessage response = await HttpClient.SendAsync(httpRequestMessage);

// Assert
Assert.Equal(StatusCodes.Status400BadRequest, (int)response.StatusCode);
}
}

0 comments on commit 774f0d3

Please sign in to comment.