Skip to content

Commit f6cb964

Browse files
authored
test(postgrest): integration tests (#363)
fix(postgrest): text search * test(postgrest): integration tests test: add more integration tests * doc comment
1 parent 597efe9 commit f6cb964

File tree

7 files changed

+1695
-1
lines changed

7 files changed

+1695
-1
lines changed

Package.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ let package = Package(
7575
dependencies: [
7676
.product(name: "CustomDump", package: "swift-custom-dump"),
7777
.product(name: "XCTestDynamicOverlay", package: "xctest-dynamic-overlay"),
78+
.product(name: "InlineSnapshotTesting", package: "swift-snapshot-testing"),
7879
"_Helpers",
7980
"Auth",
8081
"TestHelpers",

Sources/PostgREST/PostgrestFilterBuilder.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,10 +452,12 @@ public class PostgrestFilterBuilder: PostgrestTransformBuilder {
452452
type: TextSearchType? = nil
453453
) -> PostgrestFilterBuilder {
454454
let queryValue = query.queryValue
455+
let configPart = config.map { "(\($0))" }
456+
455457
mutableState.withValue {
456458
$0.request.query.append(
457459
URLQueryItem(
458-
name: column, value: "\(type?.rawValue ?? "")fts\(config ?? "").\(queryValue)"
460+
name: column, value: "\(type?.rawValue ?? "")fts\(configPart ?? "").\(queryValue)"
459461
)
460462
)
461463
}

Sources/PostgREST/Types.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public struct PostgrestResponse<T: Sendable>: Sendable {
3232
self.count = count
3333
self.value = value
3434
}
35+
36+
/// Returns the response converting the returned Data into Unicode characters using a given encoding.
37+
public func string(encoding: String.Encoding = .utf8) -> String? {
38+
String(data: data, encoding: encoding)
39+
}
3540
}
3641

3742
/// Returns count as part of the response when specified.
Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
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

Comments
 (0)