/src/HotChocolate/Data/test/Data.Sorting.Tests/Expression/QueryableSortVisitorAscObjectTests.cs

https://github.com/ChilliCream/hotchocolate · C# · 216 lines · 179 code · 30 blank · 7 comment · 3 complexity · 47a7cf5887f642551c4490557e6e5bf5 MD5 · raw file

  1. using System;
  2. using System.Linq;
  3. using HotChocolate.Language;
  4. using Xunit;
  5. namespace HotChocolate.Data.Sorting.Expressions
  6. {
  7. public class QueryableSortVisitorAscObjectTests
  8. : SortVisitorTestBase
  9. {
  10. [Theory]
  11. [InlineData(true, true, false, false)]
  12. [InlineData(false, false, true, true)]
  13. public void Sort_BooleanAsc(params bool[] dataObject)
  14. {
  15. Test_Asc(dataObject);
  16. }
  17. [Theory]
  18. [InlineData(1, 2, 3, 4)]
  19. [InlineData(4, 3, 2, 1)]
  20. public void Sort_IntAsc(params int[] dataObject)
  21. {
  22. Test_Asc(dataObject);
  23. }
  24. [Theory]
  25. [InlineData("a", "b", "c", "d")]
  26. [InlineData("d", "c", "b", "a")]
  27. public void Sort_StringAsc(params string[] dataObject)
  28. {
  29. Test_Asc(dataObject);
  30. }
  31. [Theory]
  32. [InlineData(double.MaxValue, double.MinValue)]
  33. [InlineData(double.MinValue, double.MaxValue)]
  34. public void Sort_DoubleAsc(params double[] dataObject)
  35. {
  36. Test_Asc(dataObject);
  37. }
  38. [Theory]
  39. [InlineData(TestEnum.Bar, TestEnum.Baz, TestEnum.Foo)]
  40. [InlineData(TestEnum.Foo, TestEnum.Bar, TestEnum.Baz)]
  41. public void Sort_EnumAsc(params TestEnum[] dataObject)
  42. {
  43. Test_Asc(dataObject);
  44. }
  45. [Theory]
  46. [InlineData("2018-01-01", "2019-01-01", "2020-01-01")]
  47. [InlineData("2020-01-01", "2019-01-01", "2018-01-01")]
  48. public void Sort_DateTimeAsc(params string[] dataObject)
  49. {
  50. Test_Asc(dataObject.Select(DateTime.Parse).ToArray());
  51. }
  52. [Theory]
  53. [InlineData(null, null, true, true, false, false)]
  54. [InlineData(false, false, true, true, null, null)]
  55. public void Sort_NullableBooleanAsc(params bool?[] dataObject)
  56. {
  57. Test_Asc(dataObject);
  58. }
  59. [Theory]
  60. [InlineData(null, 2, 3, 4)]
  61. [InlineData(4, 3, 2, null)]
  62. public void Sort_NullableIntAsc(params int?[] dataObject)
  63. {
  64. Test_Asc(dataObject);
  65. }
  66. [Theory]
  67. [InlineData(null, double.MaxValue, double.MinValue)]
  68. [InlineData(double.MinValue, double.MaxValue, null)]
  69. public void Sort_NullableDoubleAsc(params double?[] dataObject)
  70. {
  71. Test_Asc(dataObject);
  72. }
  73. [Theory]
  74. [InlineData(null, TestEnum.Bar, TestEnum.Baz, TestEnum.Foo)]
  75. [InlineData(TestEnum.Foo, TestEnum.Bar, TestEnum.Baz, null)]
  76. public void Sort_NullableEnumAsc(params TestEnum?[] dataObject)
  77. {
  78. Test_Asc(dataObject);
  79. }
  80. [Theory]
  81. [InlineData(null, "2018-01-01", "2019-01-01", "2020-01-01")]
  82. [InlineData("2020-01-01", "2019-01-01", "2018-01-01", null)]
  83. public void Sort_NullableDateTimeAsc(params string[] dataObject)
  84. {
  85. Test_Asc(
  86. dataObject.Select(x => x is null ? default : (DateTime?)DateTime.Parse(x))
  87. .ToArray());
  88. }
  89. [Theory]
  90. [InlineData("a", "b", "c", "d")]
  91. [InlineData("d", "c", "b", "a")]
  92. public void Sort_NullableStringAsc(params string[] data)
  93. {
  94. IValueNode value = Utf8GraphQLParser.Syntax.ParseValueLiteral(
  95. "{ bar: { baz: ASC}}");
  96. ExecutorBuilder tester = CreateProviderTester(new FooNullableSortType<string>());
  97. string[] expected = data.OrderBy(x => x).ToArray();
  98. // act
  99. Func<FooNullable<string>[], FooNullable<string>[]> func =
  100. tester.Build<FooNullable<string>>(value);
  101. // assert
  102. FooNullable<string>[] inputs =
  103. data.Select(x => new FooNullable<string> {Bar = new BarNullable<string> {Baz = x}})
  104. .ToArray();
  105. FooNullable<string>[] sorted = func(inputs);
  106. for (var i = 0; i < expected.Length; i++)
  107. {
  108. Assert.Equal(expected[i], sorted[i].Bar?.Baz);
  109. }
  110. }
  111. [Theory]
  112. [InlineData("a", "b", "c", "d")]
  113. [InlineData("d", "c", "b", "a")]
  114. public void Sort_NullableStringAscWithNull(params string[] data)
  115. {
  116. IValueNode value = Utf8GraphQLParser.Syntax.ParseValueLiteral(
  117. "{ bar: { baz: ASC}}");
  118. ExecutorBuilder tester = CreateProviderTester(new FooNullableSortType<string>());
  119. string?[] expected = data.Append(null).OrderBy(x => x).ToArray();
  120. // act
  121. Func<FooNullable<string>[], FooNullable<string>[]> func =
  122. tester.Build<FooNullable<string>>(value);
  123. // assert
  124. FooNullable<string>[] inputs =
  125. data
  126. .Select(x => new FooNullable<string> {Bar = new BarNullable<string> {Baz = x}})
  127. .Prepend(new FooNullable<string> {Bar = null})
  128. .ToArray();
  129. FooNullable<string>[] sorted = func(inputs);
  130. for (var i = 0; i < expected.Length; i++)
  131. {
  132. Assert.Equal(expected[i], sorted[i].Bar?.Baz);
  133. }
  134. }
  135. protected void Test_Asc<T>(params T[] data)
  136. {
  137. // arrange
  138. IValueNode value = Utf8GraphQLParser.Syntax.ParseValueLiteral(
  139. "{ bar: { baz: ASC}}");
  140. ExecutorBuilder tester = CreateProviderTester(new FooSortType<T>());
  141. T[] expected = data.OrderBy(x => x).ToArray();
  142. // act
  143. Func<Foo<T>[], Foo<T>[]> func = tester.Build<Foo<T>>(value);
  144. // assert
  145. Foo<T>[] inputs = data.Select(x => new Foo<T> {Bar = new Bar<T> {Baz = x}}).ToArray();
  146. Foo<T>[] sorted = func(inputs);
  147. for (var i = 0; i < expected.Length; i++)
  148. {
  149. Assert.Equal(expected[i], sorted[i].Bar.Baz);
  150. }
  151. }
  152. public class Bar<T>
  153. {
  154. public T Baz { get; set; } = default!;
  155. }
  156. public class Foo<T>
  157. {
  158. public Bar<T> Bar { get; set; } = default!;
  159. }
  160. public class FooNullable<T>
  161. where T : class
  162. {
  163. public BarNullable<T>? Bar { get; set; }
  164. }
  165. public class BarNullable<T>
  166. where T : class
  167. {
  168. public T? Baz { get; set; }
  169. }
  170. public class FooSortType<T>
  171. : SortInputType<Foo<T>>
  172. {
  173. }
  174. public enum TestEnum
  175. {
  176. Foo = 0,
  177. Bar = 1,
  178. Baz = 2
  179. }
  180. public class FooNullableSortType<T>
  181. : SortInputType<FooNullable<T>>
  182. where T : class
  183. {
  184. }
  185. }
  186. }