|
| 1 | +using NpgsqlRestTests.Setup; |
| 2 | + |
| 3 | +namespace NpgsqlRestTests.SqlFileSourceTests; |
| 4 | + |
| 5 | +public static partial class SqlFiles |
| 6 | +{ |
| 7 | + public static void MultiCommandSingleTests() |
| 8 | + { |
| 9 | + // Per-command @single on first command only |
| 10 | + File.WriteAllText(Path.Combine(Dir, "multi_single_first.sql"), """ |
| 11 | + -- HTTP GET |
| 12 | + -- @param $1 id |
| 13 | + -- @single |
| 14 | + select id, name from sql_describe_test where id = $1; |
| 15 | + select id, name from sql_describe_test where id = $1; |
| 16 | + """); |
| 17 | + |
| 18 | + // Per-command @single on second command only |
| 19 | + File.WriteAllText(Path.Combine(Dir, "multi_single_second.sql"), """ |
| 20 | + -- HTTP GET |
| 21 | + -- @param $1 id |
| 22 | + select id, name from sql_describe_test where id = $1; |
| 23 | + -- @single |
| 24 | + select id, name from sql_describe_test where id = $1; |
| 25 | + """); |
| 26 | + |
| 27 | + // Per-command @single on both commands |
| 28 | + File.WriteAllText(Path.Combine(Dir, "multi_single_both.sql"), """ |
| 29 | + -- HTTP GET |
| 30 | + -- @param $1 id |
| 31 | + -- @single |
| 32 | + select id, name from sql_describe_test where id = $1; |
| 33 | + -- @single |
| 34 | + select id, name from sql_describe_test where id = $1; |
| 35 | + """); |
| 36 | + |
| 37 | + // Per-command @single with a void command in between |
| 38 | + File.WriteAllText(Path.Combine(Dir, "multi_single_with_void.sql"), """ |
| 39 | + -- HTTP POST |
| 40 | + -- @param $1 id |
| 41 | + -- @single |
| 42 | + select id, name from sql_describe_test where id = $1; |
| 43 | + update sql_describe_test set active = true where id = $1; |
| 44 | + select count(*) as total from sql_describe_test; |
| 45 | + """); |
| 46 | + |
| 47 | + // Multi-command without @single for comparison |
| 48 | + File.WriteAllText(Path.Combine(Dir, "multi_no_single.sql"), """ |
| 49 | + -- HTTP GET |
| 50 | + -- @param $1 id |
| 51 | + select id, name from sql_describe_test where id = $1; |
| 52 | + select id, name from sql_describe_test where id = $1; |
| 53 | + """); |
| 54 | + |
| 55 | + // Per-command @single with multi-row query — only first row returned |
| 56 | + File.WriteAllText(Path.Combine(Dir, "multi_single_multi_row.sql"), """ |
| 57 | + -- HTTP GET |
| 58 | + select id, name from sql_describe_test order by id; |
| 59 | + -- @single |
| 60 | + select id, name from sql_describe_test order by id; |
| 61 | + """); |
| 62 | + |
| 63 | + // Positional @result combined with @single |
| 64 | + File.WriteAllText(Path.Combine(Dir, "multi_single_named.sql"), """ |
| 65 | + -- HTTP GET |
| 66 | + -- @param $1 id |
| 67 | + -- @single |
| 68 | + -- @result user |
| 69 | + select id, name from sql_describe_test where id = $1; |
| 70 | + -- @result items |
| 71 | + select id, name from sql_describe_test order by id; |
| 72 | + """); |
| 73 | + |
| 74 | + // Positional @result without @single |
| 75 | + File.WriteAllText(Path.Combine(Dir, "multi_positional_result.sql"), """ |
| 76 | + -- HTTP GET |
| 77 | + -- @param $1 id |
| 78 | + -- @result lookup |
| 79 | + select id, name from sql_describe_test where id = $1; |
| 80 | + -- @result all_items |
| 81 | + select id, name from sql_describe_test order by id; |
| 82 | + """); |
| 83 | + |
| 84 | + // Positional @result with "is" syntax |
| 85 | + File.WriteAllText(Path.Combine(Dir, "multi_positional_result_is.sql"), """ |
| 86 | + -- HTTP GET |
| 87 | + -- @param $1 id |
| 88 | + -- @result is details |
| 89 | + select id, name from sql_describe_test where id = $1; |
| 90 | + select count(*) as total from sql_describe_test; |
| 91 | + """); |
| 92 | + |
| 93 | + // Mixed: @resultN (numbered) and positional @result coexist |
| 94 | + File.WriteAllText(Path.Combine(Dir, "multi_mixed_result.sql"), """ |
| 95 | + -- HTTP GET |
| 96 | + -- @param $1 id |
| 97 | + -- @result2 count |
| 98 | + select id, name from sql_describe_test where id = $1; |
| 99 | + -- @result first |
| 100 | + select id, name from sql_describe_test order by id limit 1; |
| 101 | + """); |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +[Collection("SqlFileSourceFixture")] |
| 106 | +public class MultiCommandSingleTests(SqlFileSourceTestFixture test) |
| 107 | +{ |
| 108 | + [Fact] |
| 109 | + public async Task MultiSingleFirst_FirstIsObjectSecondIsArray() |
| 110 | + { |
| 111 | + using var response = await test.Client.GetAsync("/api/multi-single-first?id=1"); |
| 112 | + var content = await response.Content.ReadAsStringAsync(); |
| 113 | + response.StatusCode.Should().Be(HttpStatusCode.OK, $"Response: {content}"); |
| 114 | + content.Should().Be("{\"result1\":{\"id\":1,\"name\":\"test1\"},\"result2\":[{\"id\":1,\"name\":\"test1\"}]}"); |
| 115 | + } |
| 116 | + |
| 117 | + [Fact] |
| 118 | + public async Task MultiSingleSecond_FirstIsArraySecondIsObject() |
| 119 | + { |
| 120 | + using var response = await test.Client.GetAsync("/api/multi-single-second?id=1"); |
| 121 | + var content = await response.Content.ReadAsStringAsync(); |
| 122 | + response.StatusCode.Should().Be(HttpStatusCode.OK, $"Response: {content}"); |
| 123 | + content.Should().Be("{\"result1\":[{\"id\":1,\"name\":\"test1\"}],\"result2\":{\"id\":1,\"name\":\"test1\"}}"); |
| 124 | + } |
| 125 | + |
| 126 | + [Fact] |
| 127 | + public async Task MultiSingleBoth_BothAreObjects() |
| 128 | + { |
| 129 | + using var response = await test.Client.GetAsync("/api/multi-single-both?id=1"); |
| 130 | + var content = await response.Content.ReadAsStringAsync(); |
| 131 | + response.StatusCode.Should().Be(HttpStatusCode.OK, $"Response: {content}"); |
| 132 | + content.Should().Be("{\"result1\":{\"id\":1,\"name\":\"test1\"},\"result2\":{\"id\":1,\"name\":\"test1\"}}"); |
| 133 | + } |
| 134 | + |
| 135 | + [Fact] |
| 136 | + public async Task MultiSingleWithVoid_SingleObjectThenVoidThenArray() |
| 137 | + { |
| 138 | + using var response = await test.Client.PostAsync("/api/multi-single-with-void", |
| 139 | + new StringContent("{\"id\":1}", System.Text.Encoding.UTF8, "application/json")); |
| 140 | + var content = await response.Content.ReadAsStringAsync(); |
| 141 | + response.StatusCode.Should().Be(HttpStatusCode.OK, $"Response: {content}"); |
| 142 | + |
| 143 | + using var doc = JsonDocument.Parse(content); |
| 144 | + // result1: single object |
| 145 | + doc.RootElement.GetProperty("result1").ValueKind.Should().Be(JsonValueKind.Object); |
| 146 | + doc.RootElement.GetProperty("result1").GetProperty("id").GetInt32().Should().Be(1); |
| 147 | + // result2: rows affected (number) |
| 148 | + doc.RootElement.GetProperty("result2").ValueKind.Should().Be(JsonValueKind.Number); |
| 149 | + // result3: array |
| 150 | + doc.RootElement.GetProperty("result3").ValueKind.Should().Be(JsonValueKind.Array); |
| 151 | + } |
| 152 | + |
| 153 | + [Fact] |
| 154 | + public async Task MultiNoSingle_BothAreArrays() |
| 155 | + { |
| 156 | + using var response = await test.Client.GetAsync("/api/multi-no-single?id=1"); |
| 157 | + var content = await response.Content.ReadAsStringAsync(); |
| 158 | + response.StatusCode.Should().Be(HttpStatusCode.OK, $"Response: {content}"); |
| 159 | + content.Should().Be("{\"result1\":[{\"id\":1,\"name\":\"test1\"}],\"result2\":[{\"id\":1,\"name\":\"test1\"}]}"); |
| 160 | + } |
| 161 | + |
| 162 | + [Fact] |
| 163 | + public async Task MultiSingleMultiRow_SingleReturnsOnlyFirstRow() |
| 164 | + { |
| 165 | + using var response = await test.Client.GetAsync("/api/multi-single-multi-row"); |
| 166 | + var content = await response.Content.ReadAsStringAsync(); |
| 167 | + response.StatusCode.Should().Be(HttpStatusCode.OK, $"Response: {content}"); |
| 168 | + |
| 169 | + using var doc = JsonDocument.Parse(content); |
| 170 | + // result1: array (no @single) |
| 171 | + doc.RootElement.GetProperty("result1").ValueKind.Should().Be(JsonValueKind.Array); |
| 172 | + doc.RootElement.GetProperty("result1").GetArrayLength().Should().BeGreaterThan(1); |
| 173 | + // result2: single object (first row only) |
| 174 | + doc.RootElement.GetProperty("result2").ValueKind.Should().Be(JsonValueKind.Object); |
| 175 | + doc.RootElement.GetProperty("result2").GetProperty("id").GetInt32().Should().Be(1); |
| 176 | + } |
| 177 | + |
| 178 | + [Fact] |
| 179 | + public async Task MultiSingleNamed_PositionalResultWithSingle() |
| 180 | + { |
| 181 | + using var response = await test.Client.GetAsync("/api/multi-single-named?id=1"); |
| 182 | + var content = await response.Content.ReadAsStringAsync(); |
| 183 | + response.StatusCode.Should().Be(HttpStatusCode.OK, $"Response: {content}"); |
| 184 | + |
| 185 | + using var doc = JsonDocument.Parse(content); |
| 186 | + // "user" key with single object |
| 187 | + doc.RootElement.GetProperty("user").ValueKind.Should().Be(JsonValueKind.Object); |
| 188 | + doc.RootElement.GetProperty("user").GetProperty("id").GetInt32().Should().Be(1); |
| 189 | + // "items" key with array |
| 190 | + doc.RootElement.GetProperty("items").ValueKind.Should().Be(JsonValueKind.Array); |
| 191 | + } |
| 192 | + |
| 193 | + [Fact] |
| 194 | + public async Task MultiPositionalResult_CustomNames() |
| 195 | + { |
| 196 | + using var response = await test.Client.GetAsync("/api/multi-positional-result?id=1"); |
| 197 | + var content = await response.Content.ReadAsStringAsync(); |
| 198 | + response.StatusCode.Should().Be(HttpStatusCode.OK, $"Response: {content}"); |
| 199 | + |
| 200 | + using var doc = JsonDocument.Parse(content); |
| 201 | + doc.RootElement.TryGetProperty("lookup", out _).Should().BeTrue(); |
| 202 | + doc.RootElement.TryGetProperty("all_items", out _).Should().BeTrue(); |
| 203 | + // Should NOT have result1/result2 |
| 204 | + doc.RootElement.TryGetProperty("result1", out _).Should().BeFalse(); |
| 205 | + doc.RootElement.TryGetProperty("result2", out _).Should().BeFalse(); |
| 206 | + } |
| 207 | + |
| 208 | + [Fact] |
| 209 | + public async Task MultiPositionalResultIs_IsSyntax() |
| 210 | + { |
| 211 | + using var response = await test.Client.GetAsync("/api/multi-positional-result-is?id=1"); |
| 212 | + var content = await response.Content.ReadAsStringAsync(); |
| 213 | + response.StatusCode.Should().Be(HttpStatusCode.OK, $"Response: {content}"); |
| 214 | + |
| 215 | + using var doc = JsonDocument.Parse(content); |
| 216 | + doc.RootElement.TryGetProperty("details", out _).Should().BeTrue(); |
| 217 | + // Second result uses default name (no positional annotation) |
| 218 | + doc.RootElement.TryGetProperty("result2", out _).Should().BeTrue(); |
| 219 | + } |
| 220 | + |
| 221 | + [Fact] |
| 222 | + public async Task MultiMixedResult_NumberedAndPositionalCoexist() |
| 223 | + { |
| 224 | + using var response = await test.Client.GetAsync("/api/multi-mixed-result?id=1"); |
| 225 | + var content = await response.Content.ReadAsStringAsync(); |
| 226 | + response.StatusCode.Should().Be(HttpStatusCode.OK, $"Response: {content}"); |
| 227 | + |
| 228 | + using var doc = JsonDocument.Parse(content); |
| 229 | + // First result: no annotation → default "result1" |
| 230 | + doc.RootElement.TryGetProperty("result1", out _).Should().BeTrue(); |
| 231 | + // Second result: positional @result wins over @result2 (numbered) |
| 232 | + doc.RootElement.TryGetProperty("first", out _).Should().BeTrue(); |
| 233 | + } |
| 234 | +} |
0 commit comments