forked from supabase-community/postgrest-swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntegrationTests.swift
135 lines (114 loc) · 4.01 KB
/
IntegrationTests.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
import PostgREST
import XCTest
struct Todo: Codable, Hashable {
let id: UUID
var description: String
var isComplete: Bool
var tags: [String]
let createdAt: Date
enum CodingKeys: String, CodingKey {
case id
case description
case isComplete = "is_complete"
case tags
case createdAt = "created_at"
}
}
struct NewTodo: Codable, Hashable {
var description: String
var isComplete: Bool = false
var tags: [String]
enum CodingKeys: String, CodingKey {
case description
case isComplete = "is_complete"
case tags
}
}
struct User: Codable, Hashable {
let email: String
}
@available(iOS 15.0.0, macOS 12.0.0, tvOS 13.0, *)
final class IntegrationTests: XCTestCase {
let client = PostgrestClient(
url: URL(string: "http://localhost:54321/rest/v1")!,
headers: [
"apikey":
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0",
],
schema: "public"
)
override func setUp() async throws {
try await super.setUp()
try XCTSkipUnless(
ProcessInfo.processInfo.environment["INTEGRATION_TESTS"] != nil,
"INTEGRATION_TESTS not defined."
)
// Run fresh test by deleting all data. Delete without a where clause isn't supported, so have
// to do this `neq` trick to delete all data.
try await client.from("todos").delete().neq(column: "id", value: UUID().uuidString).execute()
try await client.from("users").delete().neq(column: "id", value: UUID().uuidString).execute()
}
func testIntegration() async throws {
var todos: [Todo] = try await client.from("todos").select().execute().value
XCTAssertEqual(todos, [])
let insertedTodo: Todo = try await client.from("todos")
.insert(
values: NewTodo(
description: "Implement integration tests for postgrest-swift",
tags: ["tag 01", "tag 02"]
),
returning: .representation
)
.single()
.execute()
.value
todos = try await client.from("todos").select().execute().value
XCTAssertEqual(todos, [insertedTodo])
let insertedTodos: [Todo] = try await client.from("todos")
.insert(
values: [
NewTodo(description: "Make supabase swift libraries production ready", tags: ["tag 01"]),
NewTodo(description: "Drink some coffee", tags: ["tag 02"]),
],
returning: .representation
)
.execute()
.value
todos = try await client.from("todos").select().execute().value
XCTAssertEqual(todos, [insertedTodo] + insertedTodos)
let drinkCoffeeTodo = insertedTodos[1]
let updatedTodo: Todo = try await client.from("todos")
.update(values: ["is_complete": true])
.eq(column: "id", value: drinkCoffeeTodo.id.uuidString)
.single()
.execute()
.value
XCTAssertTrue(updatedTodo.isComplete)
let completedTodos: [Todo] = try await client.from("todos")
.select()
.eq(column: "is_complete", value: true)
.execute()
.value
XCTAssertEqual(completedTodos, [updatedTodo])
try await client.from("todos").delete().eq(column: "is_complete", value: true).execute()
todos = try await client.from("todos").select().execute().value
XCTAssertTrue(completedTodos.allSatisfy { todo in !todos.contains(todo) })
let todosWithSpecificTag: [Todo] = try await client.from("todos").select()
.contains(column: "tags", value: ["tag 01"]).execute().value
XCTAssertEqual(todosWithSpecificTag, [insertedTodo, insertedTodos[0]])
}
func testQueryWithPlusSign() async throws {
let users = [
User(email: "[email protected]"),
User(email: "[email protected]"),
User(email: "[email protected]"),
]
try await client.from("users").insert(values: users).execute()
let fetchedUsers: [User] = try await client.from("users").select()
.ilike(column: "email", value: "johndoe+test%").execute().value
XCTAssertEqual(
fetchedUsers[...],
users[1 ... 2]
)
}
}