|
| 1 | +namespace HotChocolate.Types; |
| 2 | + |
| 3 | +public class LookupReturnsListTypeAnalyzerTests |
| 4 | +{ |
| 5 | + [Fact] |
| 6 | + public async Task Method_ListReturn_RaisesError() |
| 7 | + { |
| 8 | + await TestHelper.GetGeneratedSourceSnapshot( |
| 9 | + [""" |
| 10 | + #nullable enable |
| 11 | + using HotChocolate; |
| 12 | + using HotChocolate.Types; |
| 13 | + using HotChocolate.Types.Composite; |
| 14 | + using System.Collections.Generic; |
| 15 | +
|
| 16 | + namespace TestNamespace; |
| 17 | +
|
| 18 | + [QueryType] |
| 19 | + internal static partial class Query |
| 20 | + { |
| 21 | + [Lookup] |
| 22 | + public static List<User?> GetUsersById(int id) => default!; |
| 23 | + } |
| 24 | +
|
| 25 | + public class User |
| 26 | + { |
| 27 | + public int Id { get; set; } |
| 28 | + public string? Name { get; set; } |
| 29 | + } |
| 30 | + """], |
| 31 | + enableAnalyzers: true).MatchMarkdownAsync(); |
| 32 | + } |
| 33 | + |
| 34 | + [Fact] |
| 35 | + public async Task Method_ArrayReturn_RaisesError() |
| 36 | + { |
| 37 | + await TestHelper.GetGeneratedSourceSnapshot( |
| 38 | + [""" |
| 39 | + #nullable enable |
| 40 | + using HotChocolate; |
| 41 | + using HotChocolate.Types; |
| 42 | + using HotChocolate.Types.Composite; |
| 43 | +
|
| 44 | + namespace TestNamespace; |
| 45 | +
|
| 46 | + [QueryType] |
| 47 | + internal static partial class Query |
| 48 | + { |
| 49 | + [Lookup] |
| 50 | + public static User?[] GetUsersById(int id) => default!; |
| 51 | + } |
| 52 | +
|
| 53 | + public class User |
| 54 | + { |
| 55 | + public int Id { get; set; } |
| 56 | + public string? Name { get; set; } |
| 57 | + } |
| 58 | + """], |
| 59 | + enableAnalyzers: true).MatchMarkdownAsync(); |
| 60 | + } |
| 61 | + |
| 62 | + [Fact] |
| 63 | + public async Task Method_IEnumerableReturn_RaisesError() |
| 64 | + { |
| 65 | + await TestHelper.GetGeneratedSourceSnapshot( |
| 66 | + [""" |
| 67 | + #nullable enable |
| 68 | + using HotChocolate; |
| 69 | + using HotChocolate.Types; |
| 70 | + using HotChocolate.Types.Composite; |
| 71 | + using System.Collections.Generic; |
| 72 | +
|
| 73 | + namespace TestNamespace; |
| 74 | +
|
| 75 | + [QueryType] |
| 76 | + internal static partial class Query |
| 77 | + { |
| 78 | + [Lookup] |
| 79 | + public static IEnumerable<User?> GetUsersById(int id) => default!; |
| 80 | + } |
| 81 | +
|
| 82 | + public class User |
| 83 | + { |
| 84 | + public int Id { get; set; } |
| 85 | + public string? Name { get; set; } |
| 86 | + } |
| 87 | + """], |
| 88 | + enableAnalyzers: true).MatchMarkdownAsync(); |
| 89 | + } |
| 90 | + |
| 91 | + [Fact] |
| 92 | + public async Task Method_TaskListReturn_RaisesError() |
| 93 | + { |
| 94 | + await TestHelper.GetGeneratedSourceSnapshot( |
| 95 | + [""" |
| 96 | + #nullable enable |
| 97 | + using HotChocolate; |
| 98 | + using HotChocolate.Types; |
| 99 | + using HotChocolate.Types.Composite; |
| 100 | + using System.Collections.Generic; |
| 101 | + using System.Threading.Tasks; |
| 102 | +
|
| 103 | + namespace TestNamespace; |
| 104 | +
|
| 105 | + [QueryType] |
| 106 | + internal static partial class Query |
| 107 | + { |
| 108 | + [Lookup] |
| 109 | + public static Task<List<User?>> GetUsersByIdAsync(int id) => default!; |
| 110 | + } |
| 111 | +
|
| 112 | + public class User |
| 113 | + { |
| 114 | + public int Id { get; set; } |
| 115 | + public string? Name { get; set; } |
| 116 | + } |
| 117 | + """], |
| 118 | + enableAnalyzers: true).MatchMarkdownAsync(); |
| 119 | + } |
| 120 | + |
| 121 | + [Fact] |
| 122 | + public async Task Property_ListReturn_RaisesError() |
| 123 | + { |
| 124 | + await TestHelper.GetGeneratedSourceSnapshot( |
| 125 | + [""" |
| 126 | + #nullable enable |
| 127 | + using HotChocolate; |
| 128 | + using HotChocolate.Types; |
| 129 | + using HotChocolate.Types.Composite; |
| 130 | + using System.Collections.Generic; |
| 131 | +
|
| 132 | + namespace TestNamespace; |
| 133 | +
|
| 134 | + [QueryType] |
| 135 | + internal static partial class Query |
| 136 | + { |
| 137 | + [Lookup] |
| 138 | + public static List<User?> AllUsers => default!; |
| 139 | + } |
| 140 | +
|
| 141 | + public class User |
| 142 | + { |
| 143 | + public int Id { get; set; } |
| 144 | + public string? Name { get; set; } |
| 145 | + } |
| 146 | + """], |
| 147 | + enableAnalyzers: true).MatchMarkdownAsync(); |
| 148 | + } |
| 149 | + |
| 150 | + [Fact] |
| 151 | + public async Task Method_SingleReturn_NoError() |
| 152 | + { |
| 153 | + await TestHelper.GetGeneratedSourceSnapshot( |
| 154 | + [""" |
| 155 | + #nullable enable |
| 156 | + using HotChocolate; |
| 157 | + using HotChocolate.Types; |
| 158 | + using HotChocolate.Types.Composite; |
| 159 | +
|
| 160 | + namespace TestNamespace; |
| 161 | +
|
| 162 | + [QueryType] |
| 163 | + internal static partial class Query |
| 164 | + { |
| 165 | + [Lookup] |
| 166 | + public static User? GetUserById(int id) => default; |
| 167 | + } |
| 168 | +
|
| 169 | + public class User |
| 170 | + { |
| 171 | + public int Id { get; set; } |
| 172 | + public string? Name { get; set; } |
| 173 | + } |
| 174 | + """], |
| 175 | + enableAnalyzers: true).MatchMarkdownAsync(); |
| 176 | + } |
| 177 | + |
| 178 | + [Fact] |
| 179 | + public async Task Method_NoLookupAttribute_NoError() |
| 180 | + { |
| 181 | + await TestHelper.GetGeneratedSourceSnapshot( |
| 182 | + [""" |
| 183 | + #nullable enable |
| 184 | + using HotChocolate; |
| 185 | + using HotChocolate.Types; |
| 186 | + using HotChocolate.Types.Composite; |
| 187 | + using System.Collections.Generic; |
| 188 | +
|
| 189 | + namespace TestNamespace; |
| 190 | +
|
| 191 | + [QueryType] |
| 192 | + internal static partial class Query |
| 193 | + { |
| 194 | + public static List<User?> GetUsersById(int id) => default!; |
| 195 | + } |
| 196 | +
|
| 197 | + public class User |
| 198 | + { |
| 199 | + public int Id { get; set; } |
| 200 | + public string? Name { get; set; } |
| 201 | + } |
| 202 | + """], |
| 203 | + enableAnalyzers: true).MatchMarkdownAsync(); |
| 204 | + } |
| 205 | +} |
0 commit comments