Skip to content

Commit

Permalink
(dotnet-sdk): Fix assign LH values to Dotnet ones for Int64, Int32, D…
Browse files Browse the repository at this point in the history
…ouble and Float types
  • Loading branch information
KarlaCarvajal committed Nov 25, 2024
1 parent b7f8f3f commit 992f3bf
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 25 deletions.
4 changes: 2 additions & 2 deletions sdk-dotnet/LittleHorse.Sdk.Tests/TestClasses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class Car

public class Person
{
public string FirstName { get; set; }
public string? FirstName { get; set; }
public int Age { get; set; }
public List<Car> Cars { get; set; }
public List<Car>? Cars { get; set; }
}
4 changes: 2 additions & 2 deletions sdk-dotnet/LittleHorse.Sdk.Tests/Utils/JsonHandlerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public void JsonHandler_WithJsonString_ShouldReturnCustomObject()

var result = JsonHandler.DeserializeFromJson(jsonString, typeof(Person));

var actual = (Person) result;
var actual = (Person) result!;

Assert.Equal("Test", actual.FirstName);
Assert.Equal(35, actual.Age);
Assert.Equal(1, actual.Cars[0].Id);
Assert.Equal(1, actual.Cars![0].Id);
Assert.Equal(134.45E-2f, actual.Cars[0].Cost);
}
}
Expand Down
75 changes: 58 additions & 17 deletions sdk-dotnet/LittleHorse.Sdk.Tests/Worker/VariableMappingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void VariableMapping_WithValidLHTypes_ShouldBeBuiltSuccessfully()
int position = 0;
string paramName = "param_test";

var test_allowed_types = new List<Type>()
var testAllowedTypes = new List<Type>()
{
typeof(Int64), typeof(Int32), typeof(Int16)
, typeof(UInt16), typeof(UInt32), typeof(UInt64)
Expand All @@ -36,7 +36,7 @@ public void VariableMapping_WithValidLHTypes_ShouldBeBuiltSuccessfully()
, typeof(List<object>), typeof(List<string>), typeof(List<int>)
};

foreach (var type in test_allowed_types)
foreach (var type in testAllowedTypes)
{
var variableType = LHMappingHelper.MapDotNetTypeToLHVariableType(type);
TaskDef taskDef = getTaskDefForTest(variableType);
Expand Down Expand Up @@ -118,19 +118,41 @@ public void VariableMapping_WithMismatchTypeBytes_ShouldThrowException()
}

[Fact]
public void VariableMapping_WithAssignIntegralValue_ShouldReturnIntObject()
public void VariableMapping_WithAssignIntValue_ShouldReturnInt32Object()
{
int expectedValue = 29;
var test_allowed_types = new List<Type>() { typeof(Int64), typeof(Int32), typeof(Int16)
, typeof(UInt16), typeof(UInt32), typeof(UInt64)
var testAllowedTypes = new List<Type>() { typeof(Int32), typeof(Int16)
, typeof(UInt16), typeof(UInt32)
, typeof(sbyte), typeof(byte), typeof(short), typeof(ushort)
, typeof(int), typeof(uint), typeof(long)
, typeof(ulong), typeof(nint), typeof(nuint)};
, typeof(int), typeof(uint)
, typeof(nint), typeof(nuint)};

int position = 0;
string paramName = "param_test";

foreach (var type in test_allowed_types)
foreach (var type in testAllowedTypes)
{
var variableMapping = getVariableMappingForTest(type, paramName, position);
VariableValue variableValue = new VariableValue {Int = expectedValue};
ScheduledTask taskInstance = getScheduledTaskForTest(variableValue, paramName);
var mockWorkerContext = new Mock<LHWorkerContext>(taskInstance, new DateTime());

var result = variableMapping.Assign(taskInstance, mockWorkerContext.Object);

Assert.Equal((int) taskInstance.Variables[0].Value.Int, result);
}
}

[Fact]
public void VariableMapping_WithAssignLongValue_ShouldReturnInt64Object()
{
int expectedValue = 29;
var testAllowedTypes = new List<Type>() { typeof(Int64), typeof(UInt64), typeof(long), typeof(ulong)};

int position = 0;
string paramName = "param_test";

foreach (var type in testAllowedTypes)
{
var variableMapping = getVariableMappingForTest(type, paramName, position);
VariableValue variableValue = new VariableValue {Int = expectedValue};
Expand All @@ -144,15 +166,15 @@ public void VariableMapping_WithAssignIntegralValue_ShouldReturnIntObject()
}

[Fact]
public void VariableMapping_WithAssignFloatingValue_ShouldReturnFloatObject()
public void VariableMapping_WithAssignDoubleValue_ShouldReturnDoubleObject()
{
float expectedValue = 3_000.5F;
var test_allowed_types = new List<Type>() { typeof(float), typeof(double)};
var testAllowedTypes = new List<Type>() { typeof(double), typeof(Double)};

int position = 0;
string paramName = "param_test";

foreach (var type in test_allowed_types)
foreach (var type in testAllowedTypes)
{
var variableMapping = getVariableMappingForTest(type, paramName, position);
VariableValue variableValue = new VariableValue {Double = expectedValue};
Expand All @@ -165,6 +187,25 @@ public void VariableMapping_WithAssignFloatingValue_ShouldReturnFloatObject()
}
}

[Fact]
public void VariableMapping_WithAssignDoubleValue_ShouldReturnFloatObject()
{
float expectedValue = 3_000.5F;
var type = typeof(float);

int position = 0;
string paramName = "param_test";

var variableMapping = getVariableMappingForTest(type, paramName, position);
VariableValue variableValue = new VariableValue {Double = expectedValue};
ScheduledTask taskInstance = getScheduledTaskForTest(variableValue, paramName);
var mockWorkerContext = new Mock<LHWorkerContext>(taskInstance, new DateTime());

var result = variableMapping.Assign(taskInstance, mockWorkerContext.Object);

Assert.Equal((float) taskInstance.Variables[0].Value.Double, result);
}

[Fact]
public void VariableMapping_WithAssignStringValue_ShouldReturnStrObject()
{
Expand Down Expand Up @@ -230,13 +271,13 @@ public void VariableMapping_WithAssignArrayObjectValue_ShouldReturnArrayObject()

var result = variableMapping.Assign(taskInstance, mockWorkerContext.Object);

var expectedList = (List<Person>)JsonConvert.DeserializeObject(value, type);
var actualList = (List<Person>)result;
var expectedList = (List<Person>)JsonConvert.DeserializeObject(value, type)!;
var actualList = (List<Person>)result!;

Assert.Equal(expectedList.Count, actualList.Count);
Assert.Equal(expectedList[0].FirstName, actualList[0].FirstName);
Assert.Equal(expectedList[0].Age, actualList[0].Age);
Assert.Equal(expectedList[0].Cars.Count, actualList[0].Cars.Count);
Assert.Equal(expectedList[0].Cars!.Count, actualList[0].Cars!.Count);
}

[Fact]
Expand All @@ -253,12 +294,12 @@ public void VariableMapping_WithAssignJsonStringValue_ShouldReturnCustomObject()

var result = variableMapping.Assign(taskInstance, mockWorkerContext.Object);

var expectedObject = (Person)JsonConvert.DeserializeObject(value, type);
var actualObject = (Person)result;
var expectedObject = (Person)JsonConvert.DeserializeObject(value, type)!;
var actualObject = (Person)result!;

Assert.Equal(expectedObject.FirstName, actualObject.FirstName);
Assert.Equal(expectedObject.Age, actualObject.Age);
Assert.Equal(expectedObject.Cars.Count, actualObject.Cars.Count);
Assert.Equal(expectedObject.Cars!.Count, actualObject.Cars!.Count);
}

private TaskDef getTaskDefForTest(VariableType type)
Expand Down
8 changes: 8 additions & 0 deletions sdk-dotnet/LittleHorse.Sdk/Helper/LHMappingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,5 +203,13 @@ private static Double GetFloatingValue(object obj)
_ => throw new LHInputVarSubstitutionException("Object value can not be converted to a Double.")
};
}

public static bool isInt64Type(Type type)
{
return type.IsAssignableFrom(typeof(Int64))
|| type.IsAssignableFrom(typeof(UInt64))
|| type.IsAssignableFrom(typeof(long))
|| type.IsAssignableFrom(typeof(ulong));
}
}
}
8 changes: 4 additions & 4 deletions sdk-dotnet/LittleHorse.Sdk/Worker/VariableMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,19 @@ public VariableMapping(TaskDef taskDef, int position, Type type, string? paramNa
switch (val.ValueCase)
{
case VariableValue.ValueOneofCase.Int:
if (LHMappingHelper.IsInt(_type))
if (LHMappingHelper.isInt64Type(_type))
{
return val.Int;
}

return (int)val.Int;
return (int) val.Int;
case VariableValue.ValueOneofCase.Double:
if (LHMappingHelper.IsFloat(_type))
if (_type == typeof(double) || _type == typeof(Double))
{
return val.Double;
}

return (float)val.Double;
return (float) val.Double;
case VariableValue.ValueOneofCase.Str:
return val.Str;
case VariableValue.ValueOneofCase.Bytes:
Expand Down

0 comments on commit 992f3bf

Please sign in to comment.