-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathCsvUploadTests.cs
More file actions
419 lines (385 loc) · 14.9 KB
/
Copy pathCsvUploadTests.cs
File metadata and controls
419 lines (385 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
using System.Net.Http.Headers;
using Npgsql;
namespace NpgsqlRestTests;
public static partial class Database
{
public static void CsvUploadTests()
{
script.Append(@"
create table csv_simple_upload_table
(
index int,
id int,
name text,
value int,
prev_result int,
meta json
);
create function csv_simple_upload_process_row(
_index int,
_row text[],
_prev_result int,
_meta json
)
returns int
language plpgsql
as
$$
begin
if _index > 1 then
insert into csv_simple_upload_table (
index,
id,
name,
value,
prev_result,
meta
)
values (
_index,
_row[1]::int,
_row[2],
_row[3]::int,
_prev_result,
_meta
);
end if;
return _index;
end;
$$;
create function csv_simple_upload(
_meta json = null
)
returns json
language plpgsql
as
$$
begin
return _meta;
end;
$$;
comment on function csv_simple_upload(json) is '
upload for csv
param _meta is upload metadata
row_command = select csv_simple_upload_process_row($1,$2,$3,$4)
';
create table csv_mixed_delimiter_upload_table
(
index int,
id int,
name text,
value int
);
create procedure csv_mixed_delimiter_upload_row(
_index int,
_row text[]
)
language plpgsql
as
$$
begin
insert into csv_mixed_delimiter_upload_table (
index,
id,
name,
value
)
values (
_index,
_row[1]::int,
_row[2],
_row[3]::int
);
end;
$$;
-- will use tab, comma and semicolon as delimiters
create function csv_mixed_delimiter_upload(
_meta json = null
)
returns json
language plpgsql
as
$$
begin
return _meta;
end;
$$;
comment on function csv_mixed_delimiter_upload(json) is '
upload for csv
param _meta is upload metadata
delimiters = \t,;
row_command = call csv_mixed_delimiter_upload_row($1,$2)
';
-- CSV fallback to large_object test
create table csv_fallback_lo_upload_table
(
index int,
id int,
name text,
value int
);
create function csv_fallback_lo_upload_process_row(
_index int,
_row text[]
)
returns int
language plpgsql
as
$$
begin
if _index > 1 then
insert into csv_fallback_lo_upload_table (
index,
id,
name,
value
)
values (
_index,
_row[1]::int,
_row[2],
_row[3]::int
);
end if;
return _index;
end;
$$;
create function csv_fallback_lo_upload(
_meta json = null
)
returns json
language plpgsql
as
$$
begin
return _meta;
end;
$$;
comment on function csv_fallback_lo_upload(json) is '
upload for csv
param _meta is upload metadata
check_format = true
fallback_handler = large_object
row_command = select csv_fallback_lo_upload_process_row($1,$2)
';
-- CSV no fallback test (check_format = true, no fallback_handler)
create function csv_no_fallback_upload(
_meta json = null
)
returns json
language plpgsql
as
$$
begin
return _meta;
end;
$$;
comment on function csv_no_fallback_upload(json) is '
upload for csv
param _meta is upload metadata
check_format = true
row_command = call csv_mixed_delimiter_upload_row($1,$2)
';
");
}
}
[Collection("TestFixture")]
public class CsvUploadTests(TestFixture test)
{
[Fact]
public async Task Test_csv_mixed_delimiter_upload_test1()
{
var fileName = "test-mixed-delimiter-upload.csv";
var sb = new StringBuilder();
sb.AppendLine("11\tXXX,333");
sb.AppendLine("12;YYY\t666");
sb.AppendLine("13,;999");
var csvContent = sb.ToString();
var contentBytes = Encoding.UTF8.GetBytes(csvContent);
using var formData = new MultipartFormDataContent();
using var byteContent = new ByteArrayContent(contentBytes);
byteContent.Headers.ContentType = new MediaTypeHeaderValue("text/csv");
formData.Add(byteContent, "file", fileName);
using var result = await test.Client.PostAsync("/api/csv-mixed-delimiter-upload/", formData);
var response = await result.Content.ReadAsStringAsync();
result.StatusCode.Should().Be(HttpStatusCode.OK);
var jsonDoc = JsonDocument.Parse(response);
var rootElement = jsonDoc.RootElement[0]; // Get the first object in the array
rootElement.GetProperty("type").GetString().Should().Be("csv");
rootElement.GetProperty("fileName").GetString().Should().Be(fileName);
rootElement.GetProperty("contentType").GetString().Should().Be("text/csv");
rootElement.GetProperty("status").GetString().Should().Be("Ok");
using var connection = Database.CreateConnection();
await connection.OpenAsync();
using var command = new NpgsqlCommand("select * from csv_mixed_delimiter_upload_table", connection);
using var reader = await command.ExecuteReaderAsync();
var data = new List<(int id, string name, int value)>();
int idx = 0;
while (await reader.ReadAsync())
{
idx++;
if (idx == 1)
{
reader.GetInt32(0).Should().Be(1);
reader.GetInt32(1).Should().Be(11);
reader.GetString(2).Should().Be("XXX");
reader.GetInt32(3).Should().Be(333);
}
if (idx == 2)
{
reader.GetInt32(0).Should().Be(2);
reader.GetInt32(1).Should().Be(12);
reader.GetString(2).Should().Be("YYY");
reader.GetInt32(3).Should().Be(666);
}
if (idx == 3)
{
reader.GetInt32(0).Should().Be(3);
reader.GetInt32(1).Should().Be(13);
reader.IsDBNull(2).Should().BeTrue();
reader.GetInt32(3).Should().Be(999);
}
}
idx.Should().Be(3);
}
[Fact]
public async Task Test_csv_simple_upload_test1()
{
var fileName = "test-csv-upload.csv";
var sb = new StringBuilder();
sb.AppendLine("Id,Name,Value");
sb.AppendLine("10,Item 1,666");
sb.AppendLine("11,,999");
sb.AppendLine("12,Item 3,");
var csvContent = sb.ToString();
var contentBytes = Encoding.UTF8.GetBytes(csvContent);
using var formData = new MultipartFormDataContent();
using var byteContent = new ByteArrayContent(contentBytes);
byteContent.Headers.ContentType = new MediaTypeHeaderValue("text/csv");
formData.Add(byteContent, "file", fileName);
using var result = await test.Client.PostAsync("/api/csv-simple-upload/", formData);
var response = await result.Content.ReadAsStringAsync();
result.StatusCode.Should().Be(HttpStatusCode.OK);
var jsonDoc = JsonDocument.Parse(response);
var rootElement = jsonDoc.RootElement[0]; // Get the first object in the array
rootElement.GetProperty("type").GetString().Should().Be("csv");
rootElement.GetProperty("fileName").GetString().Should().Be(fileName);
rootElement.GetProperty("contentType").GetString().Should().Be("text/csv");
rootElement.GetProperty("status").GetString().Should().Be("Ok");
using var connection = Database.CreateConnection();
await connection.OpenAsync();
using var command = new NpgsqlCommand("select * from csv_simple_upload_table", connection);
using var reader = await command.ExecuteReaderAsync();
var data = new List<(int id, string name, int value, string meta)>();
int idx = 0;
while (await reader.ReadAsync())
{
idx++;
if (idx == 1)
{
reader.GetInt32(0).Should().Be(2);
reader.GetInt32(1).Should().Be(10);
reader.GetString(2).Should().Be("Item 1");
reader.GetInt32(3).Should().Be(666);
reader.GetInt32(4).Should().Be(1);
reader.GetString(5).Should().StartWith("{\"type\":\"csv\",\"fileName\":\"test-csv-upload.csv\",\"contentType\":\"text/csv\",\"size\"");
}
if (idx == 2)
{
reader.GetInt32(0).Should().Be(3);
reader.GetInt32(1).Should().Be(11);
reader.IsDBNull(2).Should().BeTrue();
reader.GetInt32(3).Should().Be(999);
reader.GetInt32(4).Should().Be(2);
reader.GetString(5).Should().StartWith("{\"type\":\"csv\",\"fileName\":\"test-csv-upload.csv\",\"contentType\":\"text/csv\",\"size\"");
}
if (idx == 3)
{
reader.GetInt32(0).Should().Be(4);
reader.GetInt32(1).Should().Be(12);
reader.GetString(2).Should().Be("Item 3");
reader.IsDBNull(3).Should().BeTrue();
reader.GetInt32(4).Should().Be(3);
reader.GetString(5).Should().StartWith("{\"type\":\"csv\",\"fileName\":\"test-csv-upload.csv\",\"contentType\":\"text/csv\",\"size\"");
}
}
idx.Should().Be(3);
}
[Fact]
public async Task Test_csv_fallback_to_large_object()
{
// Upload binary content to a CSV endpoint with fallback_handler = large_object
var binaryContent = new byte[] { 0x00, 0x01, 0x02, 0xFF, 0xFE, 0xFD, 0x80, 0x90, 0xA0, 0xB0, 0xC0, 0xD0, 0xE0, 0xF0 };
using var formData = new MultipartFormDataContent();
using var byteContent = new ByteArrayContent(binaryContent);
byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
formData.Add(byteContent, "file", "binary-data.bin");
using var result = await test.Client.PostAsync("/api/csv-fallback-lo-upload/", formData);
var response = await result.Content.ReadAsStringAsync();
result.StatusCode.Should().Be(HttpStatusCode.OK);
// Should have fallen back to large_object handler
var jsonDoc = JsonDocument.Parse(response);
var rootElement = jsonDoc.RootElement[0];
rootElement.GetProperty("type").GetString().Should().Be("large_object");
rootElement.GetProperty("fileName").GetString().Should().Be("binary-data.bin");
rootElement.GetProperty("success").GetBoolean().Should().BeTrue();
rootElement.GetProperty("oid").ValueKind.Should().Be(JsonValueKind.Number);
// Verify the large object exists in PostgreSQL
var oid = rootElement.GetProperty("oid").GetInt64();
using var connection = Database.CreateConnection();
await connection.OpenAsync();
using var command = new NpgsqlCommand($"select lo_get({oid})", connection);
using var transaction = await connection.BeginTransactionAsync();
var loData = await command.ExecuteScalarAsync() as byte[];
loData.Should().NotBeNull();
loData!.Length.Should().Be(binaryContent.Length);
}
[Fact]
public async Task Test_csv_fallback_valid_csv_still_processes_normally()
{
// Upload valid CSV to endpoint with fallback_handler configured - should NOT fall back
var sb = new StringBuilder();
sb.AppendLine("Id,Name,Value");
sb.AppendLine("20,Fallback Test,777");
var csvContent = Encoding.UTF8.GetBytes(sb.ToString());
using var formData = new MultipartFormDataContent();
using var byteContent = new ByteArrayContent(csvContent);
byteContent.Headers.ContentType = new MediaTypeHeaderValue("text/csv");
formData.Add(byteContent, "file", "valid.csv");
using var result = await test.Client.PostAsync("/api/csv-fallback-lo-upload/", formData);
var response = await result.Content.ReadAsStringAsync();
result.StatusCode.Should().Be(HttpStatusCode.OK);
// Should process as CSV, not fall back
var jsonDoc = JsonDocument.Parse(response);
var rootElement = jsonDoc.RootElement[0];
rootElement.GetProperty("type").GetString().Should().Be("csv");
rootElement.GetProperty("success").GetBoolean().Should().BeTrue();
// Verify the CSV row was inserted
using var connection = Database.CreateConnection();
await connection.OpenAsync();
using var command = new NpgsqlCommand("select * from csv_fallback_lo_upload_table where id = 20", connection);
using var reader = await command.ExecuteReaderAsync();
(await reader.ReadAsync()).Should().BeTrue();
reader.GetInt32(1).Should().Be(20);
reader.GetString(2).Should().Be("Fallback Test");
reader.GetInt32(3).Should().Be(777);
}
[Fact]
public async Task Test_csv_no_fallback_still_reports_format_error()
{
// Upload binary content to CSV endpoint WITHOUT fallback_handler - should report error
var binaryContent = new byte[] { 0x00, 0x01, 0x02, 0xFF, 0xFE, 0xFD, 0x80, 0x90, 0xA0, 0xB0, 0xC0, 0xD0, 0xE0, 0xF0 };
using var formData = new MultipartFormDataContent();
using var byteContent = new ByteArrayContent(binaryContent);
byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
formData.Add(byteContent, "file", "binary-data.bin");
using var result = await test.Client.PostAsync("/api/csv-no-fallback-upload/", formData);
var response = await result.Content.ReadAsStringAsync();
result.StatusCode.Should().Be(HttpStatusCode.OK);
// Should report format error, no fallback
var jsonDoc = JsonDocument.Parse(response);
var rootElement = jsonDoc.RootElement[0];
rootElement.GetProperty("success").GetBoolean().Should().BeFalse();
rootElement.GetProperty("status").GetString().Should().Be("ProbablyBinary");
}
}