-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathUploadTests.cs
More file actions
135 lines (126 loc) · 3.7 KB
/
Copy pathUploadTests.cs
File metadata and controls
135 lines (126 loc) · 3.7 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
namespace NpgsqlRestTests
{
public static partial class Database
{
public static void TsClientUploadTests()
{
script.Append("""
create schema if not exists tsclient_test;
create function tsclient_test.upload_file(_meta json = null)
returns json
language plpgsql
as $$
begin
return _meta;
end;
$$;
comment on function tsclient_test.upload_file(json) is '
tsclient_module=upload_file
upload for file_system
param _meta is upload metadata
';
""");
}
}
}
namespace NpgsqlRestTests.TsClientTests
{
[Collection("TestFixture")]
public class UploadTests
{
private const string Expected = """
const baseUrl = "";
const parseQuery = (query: Record<any, any>) => "?" + Object.keys(query ? query : {})
.map(key => {
const value = (query[key] != null ? query[key] : "") as string;
if (Array.isArray(value)) {
return value.map((s: string) => s ? `${key}=${encodeURIComponent(s)}` : `${key}=`).join("&");
}
return `${key}=${encodeURIComponent(value)}`;
})
.join("&");
interface ITsclientTestUploadFileRequest {
meta?: any | null;
}
interface ITsclientTestUploadFileResponse {
type: string;
fileName: string;
contentType: string;
size: number;
success: boolean;
status: string;
[key: string]: string | number | boolean;
}
/**
* function tsclient_test.upload_file(
* _meta json DEFAULT NULL::json
* )
* returns json
*
* @remarks
* comment on function tsclient_test.upload_file is 'tsclient_module=upload_file
* upload for file_system
* param _meta is upload metadata';
*
* @param request - Object containing request parameters.
* @returns {ITsclientTestUploadFileResponse[]}
*
* @see FUNCTION tsclient_test.upload_file
*/
export async function tsclientTestUploadFile(
files: FileList | null,
request: ITsclientTestUploadFileRequest,
progress?: (loaded: number, total: number) => void,
): Promise<ITsclientTestUploadFileResponse[]> {
return new Promise((resolve, reject) => {
if (!files || files.length === 0) {
reject(new Error("No files to upload"));
return;
}
var xhr = new XMLHttpRequest();
if (progress) {
xhr.upload.addEventListener(
"progress",
(event) => {
if (event.lengthComputable && progress) {
progress(event.loaded, event.total);
}
},
false
);
}
xhr.onload = function () {
if (this.status >= 200 && this.status < 300) {
resolve(JSON.parse(this.responseText) as ITsclientTestUploadFileResponse[]);
} else {
throw new Error(this.responseText);
}
};
xhr.onerror = function () {
reject({
xhr: this,
status: this.status,
statusText: this.statusText || 'Network error occurred',
response: this.response
});
};
xhr.open("POST", baseUrl + "/api/tsclient-test/upload-file" + parseQuery(request));
const formData = new FormData();
for(let i = 0; i < files.length; i++) {
const file = files[i];
formData.append("file", file, file.name);
}
xhr.send(formData);
});
}
""";
[Fact]
public void Test_UploadFile_GeneratedFile()
{
var filePath = Path.Combine(Setup.Program.TsClientOutputPath, "upload_file.ts");
File.Exists(filePath).Should().BeTrue($"Expected file at {filePath}");
var content = File.ReadAllText(filePath);
content.Should().Be(Expected);
}
}
}