11import XCTest
2- #if canImport(FoundationNetworking)
3- import FoundationNetworking
4- #endif
5- import Appwrite
6- import AsyncHTTPClient
7- import NIO
82
93class Tests : XCTestCase {
104
11- override func setUp( ) {
12- super. setUp ( )
13- self . writeToFile ( string: " Test Started " )
14- }
15-
16- override func tearDown( ) {
17- super. tearDown ( )
18- }
19-
20- func test( ) throws {
21- let group = DispatchGroup ( )
22-
23- let client = Client ( )
24- . setEndpointRealtime ( " wss://demo.appwrite.io/v1 " )
25- . setProject ( " console " )
26- . addHeader ( key: " Origin " , value: " http://localhost " )
27- . setSelfSigned ( )
28-
29- let foo = Foo ( client)
30- let bar = Bar ( client)
31- let general = General ( client)
32- let realtime = Realtime ( client)
33- var realtimeResponse = " Realtime failed! "
34-
35- realtime. subscribe ( channels: [ " tests " ] ) { message in
36- realtimeResponse = message. payload![ " response " ] as! String
37- }
38-
39- // Foo Tests
40- group. enter ( )
41- foo. get ( x: " string " , y: 123 , z: [ " string in array " ] ) { result in
42- switch result {
43- case . failure( let error) : self . writeToFile ( string: error. message)
44- case . success( let mock) : self . writeToFile ( string: mock. result)
45- }
46- group. leave ( )
47- }
48- group. wait ( )
49- group. enter ( )
50- foo. post ( x: " string " , y: 123 , z: [ " string in array " ] ) { result in
51- switch result {
52- case . failure( let error) : self . writeToFile ( string: error. message)
53- case . success( let mock) : self . writeToFile ( string: mock. result)
54- }
55- group. leave ( )
56- }
57- group. wait ( )
58- group. enter ( )
59- foo. put ( x: " string " , y: 123 , z: [ " string in array " ] ) { result in
60- switch result {
61- case . failure( let error) : self . writeToFile ( string: error. message)
62- case . success( let mock) : self . writeToFile ( string: mock. result)
63- }
64- group. leave ( )
65- }
66- group. wait ( )
67- group. enter ( )
68- foo. patch ( x: " string " , y: 123 , z: [ " string in array " ] ) { result in
69- switch result {
70- case . failure( let error) : self . writeToFile ( string: error. message)
71- case . success( let mock) : self . writeToFile ( string: mock. result)
72- }
73- group. leave ( )
74- }
75- group. wait ( )
76- group. enter ( )
77- foo. delete ( x: " string " , y: 123 , z: [ " string in array " ] ) { result in
78- switch result {
79- case . failure( let error) : self . writeToFile ( string: error. message)
80- case . success( let mock) : self . writeToFile ( string: mock. result)
81- }
82- group. leave ( )
83- }
84- group. wait ( )
85-
86- // Bar Tests
87- group. enter ( )
88- bar. get ( xrequired: " string " , xdefault: 123 , z: [ " string in array " ] ) { result in
89- switch result {
90- case . failure( let error) : self . writeToFile ( string: error. message)
91- case . success( let mock) : self . writeToFile ( string: mock. result)
92- }
93- group. leave ( )
94- }
95- group. wait ( )
96- group. enter ( )
97- bar. post ( xrequired: " string " , xdefault: 123 , z: [ " string in array " ] ) { result in
98- switch result {
99- case . failure( let error) : self . writeToFile ( string: error. message)
100- case . success( let mock) : self . writeToFile ( string: mock. result)
101- }
102- group. leave ( )
103- }
104- group. wait ( )
105- group. enter ( )
106- bar. put ( xrequired: " string " , xdefault: 123 , z: [ " string in array " ] ) { result in
107- switch result {
108- case . failure( let error) : self . writeToFile ( string: error. message)
109- case . success( let mock) : self . writeToFile ( string: mock. result)
110- }
111- group. leave ( )
112- }
113- group. wait ( )
114- group. enter ( )
115- bar. patch ( xrequired: " string " , xdefault: 123 , z: [ " string in array " ] ) { result in
116- switch result {
117- case . failure( let error) : self . writeToFile ( string: error. message)
118- case . success( let mock) : self . writeToFile ( string: mock. result)
119- }
120- group. leave ( )
121- }
122- group. wait ( )
123- group. enter ( )
124- bar. delete ( xrequired: " string " , xdefault: 123 , z: [ " string in array " ] ) { result in
125- switch result {
126- case . failure( let error) : self . writeToFile ( string: error. message)
127- case . success( let mock) : self . writeToFile ( string: mock. result)
128- }
129- group. leave ( )
130- }
131- group. wait ( )
132-
133- // General Tests
134- group. enter ( )
135- general. redirect ( ) { result in
136- switch result {
137- case . failure( let error) : self . writeToFile ( string: error. message)
138- case . success( let mock) : self . writeToFile ( string: ( mock as! [ String : Any ] ) [ " result " ] as! String )
139- }
140- group. leave ( )
141- }
142- group. wait ( )
143- group. enter ( )
144-
145- let url = URL ( fileURLWithPath: " \( FileManager . default. currentDirectoryPath) /../../resources/file.png " )
146- let buffer = ByteBuffer ( data: try ! Data ( contentsOf: url) )
147- let file = File ( name: " file.png " , buffer: buffer)
148- general. upload ( x: " string " , y: 123 , z: [ " string in array " ] , file: file) { result in
149- switch result {
150- case . failure( let error) : self . writeToFile ( string: error. message)
151- case . success( let mock) : self . writeToFile ( string: mock. result)
152- }
153- group. leave ( )
154- }
155- group. wait ( )
156-
157- group. enter ( )
158- general. error400 ( ) { result in
159- switch result {
160- case . failure( let error) : self . writeToFile ( string: error. message)
161- case . success( let error) : self . writeToFile ( string: error. message)
162- }
163- group. leave ( )
164- }
165- group. wait ( )
166- group. enter ( )
167- general. error500 ( ) { result in
168- switch result {
169- case . failure( let error) : self . writeToFile ( string: error. message)
170- case . success( let error) : self . writeToFile ( string: error. message)
171- }
172- group. leave ( )
173- }
174- group. wait ( )
175- group. enter ( )
176- general. error502 ( ) { result in
177- switch result {
178- case . failure( let error) : self . writeToFile ( string: error. message)
179- case . success( let error) : self . writeToFile ( string: ( error as! Error ) . message)
180- }
181- group. leave ( )
182- }
183- group. wait ( )
184-
185- self . writeToFile ( string: realtimeResponse)
186- }
187-
188- private func writeToFile( string: String ) {
189- let url = URL ( fileURLWithPath: " \( FileManager . default. currentDirectoryPath) /../../../result.txt " )
190- try ! string. appendLine ( to: url)
191- }
192- }
193-
194- struct Response : Decodable {
195- let result : String ?
196- let message : String ?
197- }
198-
199- extension String {
200- func appendLine( to url: URL ) throws {
201- try self . appending ( " \n " ) . append ( to: url)
202- }
203-
204- func append( to url: URL ) throws {
205- let data = self . data ( using: . utf8)
206- try data? . append ( to: url)
207- }
208- }
209-
210- extension Data {
211- func append( to url: URL ) throws {
212- if let fileHandle = try ? FileHandle ( forWritingTo: url) {
213- defer {
214- fileHandle. closeFile ( )
215- }
216- fileHandle. seekToEndOfFile ( )
217- fileHandle. write ( self )
218- } else {
219- try write ( to: url)
220- }
221- }
2225}
0 commit comments