Skip to content

Commit 6b99c32

Browse files
authored
Add example of contains query on integration tests (#45)
1 parent fb91f56 commit 6b99c32

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

Tests/PostgRESTIntegrationTests/IntegrationTests.swift

+17-5
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,27 @@ struct Todo: Codable, Hashable {
55
let id: UUID
66
var description: String
77
var isComplete: Bool
8+
var tags: [String]
89
let createdAt: Date
910

1011
enum CodingKeys: String, CodingKey {
1112
case id
1213
case description
1314
case isComplete = "is_complete"
15+
case tags
1416
case createdAt = "created_at"
1517
}
1618
}
1719

1820
struct NewTodo: Codable, Hashable {
1921
var description: String
2022
var isComplete: Bool = false
23+
var tags: [String]
2124

2225
enum CodingKeys: String, CodingKey {
2326
case description
2427
case isComplete = "is_complete"
28+
case tags
2529
}
2630
}
2731

@@ -44,8 +48,9 @@ final class IntegrationTests: XCTestCase {
4448
"INTEGRATION_TESTS not defined."
4549
)
4650

47-
// Run fresh test by deleting all todos.
48-
try await client.from("todo").delete().execute()
51+
// Run fresh test by deleting all todos. Delete without a where clause isn't supported, so have
52+
// to do this `neq` trick to delete all data.
53+
try await client.from("todo").delete().neq(column: "id", value: UUID().uuidString).execute()
4954
}
5055

5156
func testIntegration() async throws {
@@ -54,7 +59,10 @@ final class IntegrationTests: XCTestCase {
5459

5560
let insertedTodo: Todo = try await client.from("todo")
5661
.insert(
57-
values: NewTodo(description: "Implement integration tests for postgrest-swift"),
62+
values: NewTodo(
63+
description: "Implement integration tests for postgrest-swift",
64+
tags: ["tag 01", "tag 02"]
65+
),
5866
returning: .representation
5967
)
6068
.single()
@@ -67,8 +75,8 @@ final class IntegrationTests: XCTestCase {
6775
let insertedTodos: [Todo] = try await client.from("todo")
6876
.insert(
6977
values: [
70-
NewTodo(description: "Make supabase swift libraries production ready"),
71-
NewTodo(description: "Drink some coffee"),
78+
NewTodo(description: "Make supabase swift libraries production ready", tags: ["tag 01"]),
79+
NewTodo(description: "Drink some coffee", tags: ["tag 02"]),
7280
],
7381
returning: .representation
7482
)
@@ -97,5 +105,9 @@ final class IntegrationTests: XCTestCase {
97105
try await client.from("todo").delete().eq(column: "is_complete", value: true).execute()
98106
todos = try await client.from("todo").select().execute().value
99107
XCTAssertTrue(completedTodos.allSatisfy { todo in !todos.contains(todo) })
108+
109+
let todosWithSpecificTag: [Todo] = try await client.from("todo").select()
110+
.contains(column: "tags", value: ["tag 01"]).execute().value
111+
XCTAssertEqual(todosWithSpecificTag, [insertedTodo, insertedTodos[0]])
100112
}
101113
}

supabase/migrations/20220404094927_init.sql

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ create table todo (
22
id uuid default uuid_generate_v4 () not null primary key,
33
description text not null,
44
is_complete boolean not null default false,
5+
tags text[],
56
created_at timestamptz default (now() at time zone 'utc'::text) not null
67
);

0 commit comments

Comments
 (0)