mirror of
https://github.com/Stone-Red-Code/CuteUtils.git
synced 2026-09-09 07:46:13 +02:00
Finalize DynDto tests
This commit is contained in:
@@ -8,58 +8,142 @@ namespace CuteUtils.Tests;
|
|||||||
public class DynDtoTests
|
public class DynDtoTests
|
||||||
{
|
{
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void Convert_ToDto_ReturnsExpandoObject()
|
public void ToDto_ShouldConvertObjectToDynamicDto()
|
||||||
{
|
{
|
||||||
TestModel testModel = new()
|
// Arrange
|
||||||
|
TestData data = new TestData
|
||||||
{
|
{
|
||||||
Id = 13,
|
Name = "John",
|
||||||
Name = "test",
|
Age = 30
|
||||||
Value = (15, "test"),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
ExpandoObject dto = testModel.ToDto();
|
// Act
|
||||||
|
dynamic dto = data.ToDto();
|
||||||
|
|
||||||
Assert.IsNotNull(dto);
|
// Assert
|
||||||
|
Assert.AreEqual("John", dto.Name);
|
||||||
Assert.IsFalse(dto.Any(p => p.Key == "Id"));
|
Assert.AreEqual(30, dto.Age);
|
||||||
Assert.IsTrue(dto.Any(p => p.Key == "Name"));
|
|
||||||
Assert.IsTrue(dto.Any(p => p.Key == "Value"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void Convert_ToDto_ReturnsGeneric()
|
public void ToDto_ShouldConvertObjectToSpecifiedDtoType()
|
||||||
{
|
{
|
||||||
TestModel testModel = new()
|
// Arrange
|
||||||
|
TestData data = new TestData
|
||||||
{
|
{
|
||||||
Id = 13,
|
Name = "John",
|
||||||
Name = "test",
|
Age = 30
|
||||||
Value = (15, "test"),
|
};
|
||||||
|
TestDataDto dto = new TestDataDto();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
dto = data.ToDto(dto);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.AreEqual("John", dto.Name);
|
||||||
|
Assert.AreEqual(30, dto.Age);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ToDto_ShouldConvertObjectToNewInstanceDto()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
TestData data = new TestData
|
||||||
|
{
|
||||||
|
Name = "John",
|
||||||
|
Age = 30
|
||||||
};
|
};
|
||||||
|
|
||||||
TestDto dto = testModel.ToDto<TestDto>();
|
// Act
|
||||||
|
TestDataDto dto = data.ToDto<TestDataDto>();
|
||||||
|
|
||||||
Assert.IsNotNull(dto);
|
// Assert
|
||||||
|
Assert.AreEqual("John", dto.Name);
|
||||||
Assert.IsTrue(dto.Id == 0);
|
Assert.AreEqual(30, dto.Age);
|
||||||
Assert.IsTrue(!string.IsNullOrWhiteSpace(dto.Name));
|
|
||||||
Assert.IsTrue(dto.Value is not (0, ""));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private class TestModel
|
[TestMethod]
|
||||||
|
public void FromDto_ShouldConvertDynamicDtoToObject()
|
||||||
{
|
{
|
||||||
public required int Id { get; set; }
|
// Arrange
|
||||||
|
ExpandoObject dto = new ExpandoObject();
|
||||||
|
_ = dto.TryAdd("Name", "John");
|
||||||
|
_ = dto.TryAdd("Age", 30);
|
||||||
|
TestData data = new TestData();
|
||||||
|
|
||||||
[DynDtoName(nameof(Name))]
|
// Act
|
||||||
public required string Name { get; set; }
|
data = dto.FromDto(data);
|
||||||
|
|
||||||
[DynDtoName(nameof(Value))]
|
// Assert
|
||||||
public required object Value { get; set; }
|
Assert.AreEqual("John", data.Name);
|
||||||
|
Assert.AreEqual(30, data.Age);
|
||||||
}
|
}
|
||||||
|
|
||||||
private class TestDto
|
[TestMethod]
|
||||||
|
public void FromDto_ShouldConvertDynamicDtoToNewInstanceObject()
|
||||||
{
|
{
|
||||||
public int Id { get; init; } = 0;
|
// Arrange
|
||||||
|
ExpandoObject dto = new ExpandoObject();
|
||||||
|
_ = dto.TryAdd("Name", "John");
|
||||||
|
_ = dto.TryAdd("Age", 30);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
TestData data = dto.FromDto<TestData>();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.AreEqual("John", data.Name);
|
||||||
|
Assert.AreEqual(30, data.Age);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void FromDto_ShouldConvertDtoToObject()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
TestDataDto dto = new TestDataDto
|
||||||
|
{
|
||||||
|
Name = "John",
|
||||||
|
Age = 30
|
||||||
|
};
|
||||||
|
TestData data = new TestData();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
data = dto.FromDto(data);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.AreEqual("John", data.Name);
|
||||||
|
Assert.AreEqual(30, data.Age);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void FromDto_ShouldConvertDtoToNewInstanceObject()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
TestDataDto dto = new TestDataDto
|
||||||
|
{
|
||||||
|
Name = "John",
|
||||||
|
Age = 30
|
||||||
|
};
|
||||||
|
|
||||||
|
// Act
|
||||||
|
TestData data = dto.FromDto<TestData>();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.AreEqual("John", data.Name);
|
||||||
|
Assert.AreEqual(30, data.Age);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class TestData
|
||||||
|
{
|
||||||
|
[DynDtoName("Name")]
|
||||||
public string Name { get; set; } = string.Empty;
|
public string Name { get; set; } = string.Empty;
|
||||||
public object Value { get; init; } = (0, string.Empty);
|
|
||||||
|
[DynDtoName("Age")]
|
||||||
|
public int Age { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class TestDataDto
|
||||||
|
{
|
||||||
|
public string Name { get; set; } = string.Empty;
|
||||||
|
public int Age { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user