-
Notifications
You must be signed in to change notification settings - Fork 179
/
Copy pathTests.swift
223 lines (182 loc) · 7.71 KB
/
Tests.swift
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
import XCTest
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
import Appwrite
import AppwriteEnums
import AsyncHTTPClient
import NIO
class Tests: XCTestCase {
override func setUp() {
super.setUp()
print("Test Started")
}
override func tearDown() {
super.tearDown()
}
func test() async throws {
let client = Client()
.setProject("123456")
.addHeader(key: "Origin", value: "http://localhost")
.setSelfSigned()
// Ping pong test
let ping = try await client.ping()
let pingResult = parse(from: ping)!
print(pingResult)
// reset configs
client.setProject("console")
client.setEndpointRealtime("ws://cloud.appwrite.io/v1")
let foo = Foo(client)
let bar = Bar(client)
let general = General(client)
let realtime = Realtime(client)
var realtimeResponse = "Realtime failed!"
let expectation = XCTestExpectation(description: "realtime server")
try await realtime.subscribe(channels: ["tests"]) { message in
realtimeResponse = message.payload!["response"] as! String
expectation.fulfill()
}
var mock: Mock
// Foo Tests
mock = try await foo.get(x: "string", y: 123, z: ["string in array"])
print(mock.result)
mock = try await foo.post(x: "string", y: 123, z: ["string in array"])
print(mock.result)
mock = try await foo.put(x: "string", y: 123, z: ["string in array"])
print(mock.result)
mock = try await foo.patch(x: "string", y: 123, z: ["string in array"])
print(mock.result)
mock = try await foo.delete(x: "string", y: 123, z: ["string in array"])
print(mock.result)
// Bar Tests
mock = try await bar.get(required: "string", default: 123, z: ["string in array"])
print(mock.result)
mock = try await bar.post(required: "string", default: 123, z: ["string in array"])
print(mock.result)
mock = try await bar.put(required: "string", default: 123, z: ["string in array"])
print(mock.result)
mock = try await bar.patch(required: "string", default: 123, z: ["string in array"])
print(mock.result)
mock = try await bar.delete(required: "string", default: 123, z: ["string in array"])
print(mock.result)
// General Tests
let result = try await general.redirect()
print((result as! [String: Any])["result"] as! String)
do {
var file = InputFile.fromPath("\(FileManager.default.currentDirectoryPath)/../../resources/file.png")
mock = try await general.upload(x: "string", y: 123, z: ["string in array"], file: file, onProgress: nil)
print(mock.result)
} catch {
print(error.localizedDescription)
}
do {
var file = InputFile.fromPath("\(FileManager.default.currentDirectoryPath)/../../resources/large_file.mp4")
mock = try await general.upload(x: "string", y: 123, z: ["string in array"], file: file, onProgress: nil)
print(mock.result)
} catch {
print(error.localizedDescription)
}
do {
var url = URL(fileURLWithPath: "\(FileManager.default.currentDirectoryPath)/../../resources/file.png")
var buffer = ByteBuffer(data: try! Data(contentsOf: url))
var file = InputFile.fromBuffer(buffer, filename: "file.png", mimeType: "image/png")
mock = try await general.upload(x: "string", y: 123, z: ["string in array"], file: file, onProgress: nil)
print(mock.result)
} catch {
print(error.localizedDescription)
}
do {
var url = URL(fileURLWithPath: "\(FileManager.default.currentDirectoryPath)/../../resources/large_file.mp4")
var buffer = ByteBuffer(data: try! Data(contentsOf: url))
var file = InputFile.fromBuffer(buffer, filename: "large_file.mp4", mimeType: "video/mp4")
mock = try await general.upload(x: "string", y: 123, z: ["string in array"], file: file, onProgress: nil)
print(mock.result)
} catch {
print(error.localizedDescription)
}
mock = try await general.xenum(mockType: .first)
print(mock.result)
do {
try await general.error400()
} catch let error as AppwriteError {
print(error.message)
print(error.response)
}
do {
try await general.error500()
} catch let error as AppwriteError {
print(error.message)
print(error.response)
}
do {
try await general.error502()
} catch let error as AppwriteError {
print(error.message)
print(error.response)
}
print("Invalid endpoint URL: htp://cloud.appwrite.io/v1") // Indicates fatalError by client.setEndpoint
wait(for: [expectation], timeout: 10.0)
print(realtimeResponse)
mock = try await general.setCookie()
print(mock.result)
mock = try await general.getCookie()
print(mock.result)
try! await general.empty()
// Query helper tests
print(Query.equal("released", value: [true]))
print(Query.equal("title", value: ["Spiderman", "Dr. Strange"]))
print(Query.notEqual("title", value: "Spiderman"))
print(Query.lessThan("releasedYear", value: 1990))
print(Query.greaterThan("releasedYear", value: 1990))
print(Query.search("name", value: "john"))
print(Query.isNull("name"))
print(Query.isNotNull("name"))
print(Query.between("age", start: 50, end: 100))
print(Query.between("age", start: 50.5, end: 100.5))
print(Query.between("name", start: "Anna", end: "Brad"))
print(Query.startsWith("name", value: "Ann"))
print(Query.endsWith("name", value: "nne"))
print(Query.select(["name", "age"]))
print(Query.orderAsc("title"))
print(Query.orderDesc("title"))
print(Query.cursorAfter("my_movie_id"))
print(Query.cursorBefore("my_movie_id"))
print(Query.limit(50))
print(Query.offset(20))
print(Query.contains("title", value: "Spider"))
print(Query.contains("labels", value: "first"))
print(Query.or([
Query.equal("released", value: true),
Query.lessThan("releasedYear", value: 1990)
]))
print(Query.and([
Query.equal("released", value: false),
Query.greaterThan("releasedYear", value: 2015)
]))
// Permission & Role helper tests
print(Permission.read(Role.any()))
print(Permission.write(Role.user(ID.custom("userid"))))
print(Permission.create(Role.users()))
print(Permission.update(Role.guests()))
print(Permission.delete(Role.team("teamId", "owner")))
print(Permission.delete(Role.team("teamId")))
print(Permission.create(Role.member("memberId")))
print(Permission.update(Role.users("verified")))
print(Permission.update(Role.user(ID.custom("userid"), "unverified")))
print(Permission.create(Role.label("admin")))
// ID helper tests
print(ID.unique())
print(ID.custom("custom_id"))
mock = try await general.headers()
print(mock.result)
}
func parse(from json: String) -> String? {
if let data = json.data(using: .utf8),
let jsonObject = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
let result = jsonObject["result"] as? String {
return result
}
return nil
}
}