|
| 1 | +// |
| 2 | +// PostgresTransformsTests.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by Guilherme Souza on 07/05/24. |
| 6 | +// |
| 7 | + |
| 8 | +import InlineSnapshotTesting |
| 9 | +import PostgREST |
| 10 | +import XCTest |
| 11 | + |
| 12 | +final class PostgrestTransformsTests: XCTestCase { |
| 13 | + let client = PostgrestClient( |
| 14 | + configuration: PostgrestClient.Configuration( |
| 15 | + url: URL(string: "\(DotEnv.SUPABASE_URL)/rest/v1")!, |
| 16 | + headers: [ |
| 17 | + "apikey": DotEnv.SUPABASE_ANON_KEY, |
| 18 | + ], |
| 19 | + logger: nil |
| 20 | + ) |
| 21 | + ) |
| 22 | + |
| 23 | + func testOrder() async throws { |
| 24 | + let res = try await client.from("users") |
| 25 | + .select() |
| 26 | + .order("username", ascending: false) |
| 27 | + .execute().value as AnyJSON |
| 28 | + |
| 29 | + assertInlineSnapshot(of: res, as: .json) { |
| 30 | + """ |
| 31 | + [ |
| 32 | + { |
| 33 | + "age_range" : "[1,2)", |
| 34 | + "catchphrase" : "'cat' 'fat'", |
| 35 | + "data" : null, |
| 36 | + "status" : "ONLINE", |
| 37 | + "username" : "supabot" |
| 38 | + }, |
| 39 | + { |
| 40 | + "age_range" : "[25,35)", |
| 41 | + "catchphrase" : "'bat' 'cat'", |
| 42 | + "data" : null, |
| 43 | + "status" : "OFFLINE", |
| 44 | + "username" : "kiwicopple" |
| 45 | + }, |
| 46 | + { |
| 47 | + "age_range" : "[20,30)", |
| 48 | + "catchphrase" : "'fat' 'rat'", |
| 49 | + "data" : null, |
| 50 | + "status" : "ONLINE", |
| 51 | + "username" : "dragarcia" |
| 52 | + }, |
| 53 | + { |
| 54 | + "age_range" : "[25,35)", |
| 55 | + "catchphrase" : "'bat' 'rat'", |
| 56 | + "data" : null, |
| 57 | + "status" : "ONLINE", |
| 58 | + "username" : "awailas" |
| 59 | + } |
| 60 | + ] |
| 61 | + """ |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + func testOrderOnMultipleColumns() async throws { |
| 66 | + let res = try await client.from("messages") |
| 67 | + .select() |
| 68 | + .order("channel_id", ascending: false) |
| 69 | + .order("username", ascending: false) |
| 70 | + .execute().value as AnyJSON |
| 71 | + |
| 72 | + assertInlineSnapshot(of: res, as: .json) { |
| 73 | + """ |
| 74 | + [ |
| 75 | + { |
| 76 | + "channel_id" : 2, |
| 77 | + "data" : null, |
| 78 | + "id" : 2, |
| 79 | + "message" : "Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.", |
| 80 | + "username" : "supabot" |
| 81 | + }, |
| 82 | + { |
| 83 | + "channel_id" : 1, |
| 84 | + "data" : null, |
| 85 | + "id" : 1, |
| 86 | + "message" : "Hello World 👋", |
| 87 | + "username" : "supabot" |
| 88 | + } |
| 89 | + ] |
| 90 | + """ |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + func testLimit() async throws { |
| 95 | + let res = try await client.from("users") |
| 96 | + .select() |
| 97 | + .limit(1) |
| 98 | + .execute().value as AnyJSON |
| 99 | + |
| 100 | + assertInlineSnapshot(of: res, as: .json) { |
| 101 | + """ |
| 102 | + [ |
| 103 | + { |
| 104 | + "age_range" : "[1,2)", |
| 105 | + "catchphrase" : "'cat' 'fat'", |
| 106 | + "data" : null, |
| 107 | + "status" : "ONLINE", |
| 108 | + "username" : "supabot" |
| 109 | + } |
| 110 | + ] |
| 111 | + """ |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + func testRange() async throws { |
| 116 | + let res = try await client.from("users") |
| 117 | + .select() |
| 118 | + .range(from: 1, to: 3) |
| 119 | + .execute().value as AnyJSON |
| 120 | + |
| 121 | + assertInlineSnapshot(of: res, as: .json) { |
| 122 | + """ |
| 123 | + [ |
| 124 | + { |
| 125 | + "age_range" : "[25,35)", |
| 126 | + "catchphrase" : "'bat' 'cat'", |
| 127 | + "data" : null, |
| 128 | + "status" : "OFFLINE", |
| 129 | + "username" : "kiwicopple" |
| 130 | + }, |
| 131 | + { |
| 132 | + "age_range" : "[25,35)", |
| 133 | + "catchphrase" : "'bat' 'rat'", |
| 134 | + "data" : null, |
| 135 | + "status" : "ONLINE", |
| 136 | + "username" : "awailas" |
| 137 | + }, |
| 138 | + { |
| 139 | + "age_range" : "[20,30)", |
| 140 | + "catchphrase" : "'fat' 'rat'", |
| 141 | + "data" : null, |
| 142 | + "status" : "ONLINE", |
| 143 | + "username" : "dragarcia" |
| 144 | + } |
| 145 | + ] |
| 146 | + """ |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + func testSingle() async throws { |
| 151 | + let res = try await client.from("users") |
| 152 | + .select() |
| 153 | + .limit(1) |
| 154 | + .single() |
| 155 | + .execute().value as AnyJSON |
| 156 | + |
| 157 | + assertInlineSnapshot(of: res, as: .json) { |
| 158 | + """ |
| 159 | + { |
| 160 | + "age_range" : "[1,2)", |
| 161 | + "catchphrase" : "'cat' 'fat'", |
| 162 | + "data" : null, |
| 163 | + "status" : "ONLINE", |
| 164 | + "username" : "supabot" |
| 165 | + } |
| 166 | + """ |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + func testSingleOnInsert() async throws { |
| 171 | + let res = try await client.from("users") |
| 172 | + .insert(["username": "foo"]) |
| 173 | + .select() |
| 174 | + .single() |
| 175 | + .execute().value as AnyJSON |
| 176 | + |
| 177 | + assertInlineSnapshot(of: res, as: .json) { |
| 178 | + """ |
| 179 | + { |
| 180 | + "age_range" : null, |
| 181 | + "catchphrase" : null, |
| 182 | + "data" : null, |
| 183 | + "status" : "ONLINE", |
| 184 | + "username" : "foo" |
| 185 | + } |
| 186 | + """ |
| 187 | + } |
| 188 | + |
| 189 | + try await client.from("users") |
| 190 | + .delete() |
| 191 | + .eq("username", value: "foo") |
| 192 | + .execute() |
| 193 | + } |
| 194 | + |
| 195 | + func testSelectOnInsert() async throws { |
| 196 | + let res = try await client.from("users") |
| 197 | + .insert(["username": "foo"]) |
| 198 | + .select("status") |
| 199 | + .execute().value as AnyJSON |
| 200 | + |
| 201 | + assertInlineSnapshot(of: res, as: .json) { |
| 202 | + """ |
| 203 | + [ |
| 204 | + { |
| 205 | + "status" : "ONLINE" |
| 206 | + } |
| 207 | + ] |
| 208 | + """ |
| 209 | + } |
| 210 | + |
| 211 | + try await client.from("users") |
| 212 | + .delete() |
| 213 | + .eq("username", value: "foo") |
| 214 | + .execute() |
| 215 | + } |
| 216 | + |
| 217 | + func testSelectOnRpc() async throws { |
| 218 | + let res = try await client.rpc("get_username_and_status", params: ["name_param": "supabot"]) |
| 219 | + .select("status") |
| 220 | + .execute().value as AnyJSON |
| 221 | + |
| 222 | + assertInlineSnapshot(of: res, as: .json) { |
| 223 | + """ |
| 224 | + [ |
| 225 | + { |
| 226 | + "status" : "ONLINE" |
| 227 | + } |
| 228 | + ] |
| 229 | + """ |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + func testCsv() async throws { |
| 234 | + let res = try await client.from("users").select().csv().execute().string() |
| 235 | + assertInlineSnapshot(of: res, as: .json) { |
| 236 | + #""" |
| 237 | + "username,data,age_range,status,catchphrase\nsupabot,,\"[1,2)\",ONLINE,\"'cat' 'fat'\"\nkiwicopple,,\"[25,35)\",OFFLINE,\"'bat' 'cat'\"\nawailas,,\"[25,35)\",ONLINE,\"'bat' 'rat'\"\ndragarcia,,\"[20,30)\",ONLINE,\"'fat' 'rat'\"" |
| 238 | + """# |
| 239 | + } |
| 240 | + } |
| 241 | +} |
0 commit comments