From a77bb1bc571cd75de89fe681ba57d64a543effdb Mon Sep 17 00:00:00 2001 From: Guilherme Souza Date: Fri, 29 Nov 2024 17:50:00 -0300 Subject: [PATCH] fix tests --- Sources/Supabase/SupabaseClient.swift | 2 +- .../AuthClientMultipleInstancesTests.swift | 1 + Tests/AuthTests/AuthClientTests.swift | 1 + Tests/AuthTests/RequestsTests.swift | 1 + .../FunctionsTests/FunctionsClientTests.swift | 1 + Tests/FunctionsTests/RequestTests.swift | 1 + .../AuthClientIntegrationTests.swift | 77 ++++---- .../PostgrestIntegrationTests.swift | 5 +- .../Potsgrest/PostgresTransformsTests.swift | 1 + .../Potsgrest/PostgrestBasicTests.swift | 28 ++- .../Potsgrest/PostgrestFilterTests.swift | 99 ++++++---- .../PostgrestResourceEmbeddingTests.swift | 29 ++- .../RealtimeIntegrationTests.swift | 179 +++++++++--------- .../StorageClientIntegrationTests.swift | 6 +- .../StorageFileIntegrationTests.swift | 3 +- .../PostgRESTTests/BuildURLRequestTests.swift | 7 +- .../PostgrestBuilderTests.swift | 5 +- .../RealtimeTests/RealtimeChannelTests.swift | 19 +- Tests/RealtimeTests/_PushTests.swift | 1 + Tests/StorageTests/SupabaseStorageTests.swift | 1 + Tests/SupabaseTests/SupabaseClientTests.swift | 5 +- 21 files changed, 274 insertions(+), 198 deletions(-) diff --git a/Sources/Supabase/SupabaseClient.swift b/Sources/Supabase/SupabaseClient.swift index fe2d9493..4b34a4ab 100644 --- a/Sources/Supabase/SupabaseClient.swift +++ b/Sources/Supabase/SupabaseClient.swift @@ -101,7 +101,7 @@ public final class SupabaseClient { _headers.dictionary } - private var listenForAuthEventsTask: Task? + var listenForAuthEventsTask: Task? private var _storage: SupabaseStorageClient? private var _rest: PostgrestClient? private var _functions: FunctionsClient? diff --git a/Tests/AuthTests/AuthClientMultipleInstancesTests.swift b/Tests/AuthTests/AuthClientMultipleInstancesTests.swift index 26998388..a3a3cd12 100644 --- a/Tests/AuthTests/AuthClientMultipleInstancesTests.swift +++ b/Tests/AuthTests/AuthClientMultipleInstancesTests.swift @@ -9,6 +9,7 @@ import TestHelpers import XCTest +@MainActor final class AuthClientMultipleInstancesTests: XCTestCase { func testMultipleAuthClientInstances() { let url = URL(string: "http://localhost:54321/auth")! diff --git a/Tests/AuthTests/AuthClientTests.swift b/Tests/AuthTests/AuthClientTests.swift index 17905696..5a55b83f 100644 --- a/Tests/AuthTests/AuthClientTests.swift +++ b/Tests/AuthTests/AuthClientTests.swift @@ -18,6 +18,7 @@ import XCTest import FoundationNetworking #endif +@MainActor final class AuthClientTests: XCTestCase { var sessionManager: SessionManager! diff --git a/Tests/AuthTests/RequestsTests.swift b/Tests/AuthTests/RequestsTests.swift index a2b36c79..213cc4a2 100644 --- a/Tests/AuthTests/RequestsTests.swift +++ b/Tests/AuthTests/RequestsTests.swift @@ -18,6 +18,7 @@ import XCTest struct UnimplementedError: Error {} +@MainActor final class RequestsTests: XCTestCase { func testSignUpWithEmailAndPassword() async { let sut = makeSUT() diff --git a/Tests/FunctionsTests/FunctionsClientTests.swift b/Tests/FunctionsTests/FunctionsClientTests.swift index e4972dce..3c72dfbe 100644 --- a/Tests/FunctionsTests/FunctionsClientTests.swift +++ b/Tests/FunctionsTests/FunctionsClientTests.swift @@ -9,6 +9,7 @@ import XCTest import FoundationNetworking #endif +@MainActor final class FunctionsClientTests: XCTestCase { let url = URL(string: "http://localhost:5432/functions/v1")! let apiKey = "supabase.anon.key" diff --git a/Tests/FunctionsTests/RequestTests.swift b/Tests/FunctionsTests/RequestTests.swift index 30d8bab1..1e5db4b7 100644 --- a/Tests/FunctionsTests/RequestTests.swift +++ b/Tests/FunctionsTests/RequestTests.swift @@ -9,6 +9,7 @@ import SnapshotTesting import XCTest +@MainActor final class RequestTests: XCTestCase { let url = URL(string: "http://localhost:5432/functions/v1")! let apiKey = "supabase.anon.key" diff --git a/Tests/IntegrationTests/AuthClientIntegrationTests.swift b/Tests/IntegrationTests/AuthClientIntegrationTests.swift index d72637c8..1f46f95f 100644 --- a/Tests/IntegrationTests/AuthClientIntegrationTests.swift +++ b/Tests/IntegrationTests/AuthClientIntegrationTests.swift @@ -5,16 +5,18 @@ // Created by Guilherme Souza on 27/03/24. // -@testable import Auth import ConcurrencyExtras import CustomDump import TestHelpers import XCTest +@testable import Auth + #if canImport(FoundationNetworking) import FoundationNetworking #endif +@MainActor final class AuthClientIntegrationTests: XCTestCase { let authClient = makeClient() @@ -55,7 +57,7 @@ final class AuthClientIntegrationTests: XCTestCase { let password = mockPassword() let metadata: [String: AnyJSON] = [ - "test": .integer(42), + "test": .integer(42) ] let response = try await authClient.signUp( @@ -74,23 +76,23 @@ final class AuthClientIntegrationTests: XCTestCase { } } -// func testSignUpAndSignInWithPhone() async throws { -// try await XCTAssertAuthChangeEvents([.initialSession, .signedIn, .signedOut, .signedIn]) { -// let phone = mockPhoneNumber() -// let password = mockPassword() -// let metadata: [String: AnyJSON] = [ -// "test": .integer(42), -// ] -// let response = try await authClient.signUp(phone: phone, password: password, data: metadata) -// XCTAssertNotNil(response.session) -// XCTAssertEqual(response.user.phone, phone) -// XCTAssertEqual(response.user.userMetadata["test"], 42) -// -// try await authClient.signOut() -// -// try await authClient.signIn(phone: phone, password: password) -// } -// } + // func testSignUpAndSignInWithPhone() async throws { + // try await XCTAssertAuthChangeEvents([.initialSession, .signedIn, .signedOut, .signedIn]) { + // let phone = mockPhoneNumber() + // let password = mockPassword() + // let metadata: [String: AnyJSON] = [ + // "test": .integer(42), + // ] + // let response = try await authClient.signUp(phone: phone, password: password, data: metadata) + // XCTAssertNotNil(response.session) + // XCTAssertEqual(response.user.phone, phone) + // XCTAssertEqual(response.user.userMetadata["test"], 42) + // + // try await authClient.signOut() + // + // try await authClient.signIn(phone: phone, password: password) + // } + // } func testSignInWithEmail_invalidEmail() async throws { let email = mockEmail() @@ -108,12 +110,12 @@ final class AuthClientIntegrationTests: XCTestCase { } } -// func testSignInWithOTP_usingEmail() async throws { -// let email = mockEmail() -// -// try await authClient.signInWithOTP(email: email) -// try await authClient.verifyOTP(email: email, token: "123456", type: .magiclink) -// } + // func testSignInWithOTP_usingEmail() async throws { + // let email = mockEmail() + // + // try await authClient.signInWithOTP(email: email) + // try await authClient.verifyOTP(email: email, token: "123456", type: .magiclink) + // } func testSignOut_otherScope_shouldSignOutLocally() async throws { try await XCTAssertAuthChangeEvents([.initialSession, .signedIn]) { @@ -291,10 +293,10 @@ final class AuthClientIntegrationTests: XCTestCase { } } - private func mockEmail(length: Int = Int.random(in: 5 ... 10)) -> String { + private func mockEmail(length: Int = Int.random(in: 5...10)) -> String { var username = "" - for _ in 0 ..< length { - let randomAscii = Int.random(in: 97 ... 122) // ASCII values for lowercase letters + for _ in 0.. String { // Generate random country code (1 to 3 digits) - let countryCode = String(format: "%d", Int.random(in: 1 ... 999)) + let countryCode = String(format: "%d", Int.random(in: 1...999)) // Generate random area code (3 digits) - let areaCode = String(format: "%03d", Int.random(in: 100 ... 999)) + let areaCode = String(format: "%03d", Int.random(in: 100...999)) // Generate random subscriber number (7 digits) - let subscriberNumber = String(format: "%07d", Int.random(in: 1000000 ... 9999999)) + let subscriberNumber = String(format: "%07d", Int.random(in: 1_000_000...9_999_999)) // Format the phone number in E.164 format let phoneNumber = "\(countryCode)\(areaCode)\(subscriberNumber)" @@ -322,12 +324,13 @@ final class AuthClientIntegrationTests: XCTestCase { "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_=+" var password = "" - for _ in 0 ..< length { - let randomIndex = Int.random(in: 0 ..< allowedCharacters.count) - let character = allowedCharacters[allowedCharacters.index( - allowedCharacters.startIndex, - offsetBy: randomIndex - )] + for _ in 0.. PostgrestBuilder + let build: (PostgrestClient) async throws -> PostgrestBuilder init( name: String, record: Bool = false, file: StaticString = #file, line: UInt = #line, - build: @escaping @Sendable (PostgrestClient) async throws -> PostgrestBuilder + build: @escaping (PostgrestClient) async throws -> PostgrestBuilder ) { self.name = name self.record = record diff --git a/Tests/PostgRESTTests/PostgrestBuilderTests.swift b/Tests/PostgRESTTests/PostgrestBuilderTests.swift index 1fa049a4..d8318561 100644 --- a/Tests/PostgRESTTests/PostgrestBuilderTests.swift +++ b/Tests/PostgRESTTests/PostgrestBuilderTests.swift @@ -8,6 +8,7 @@ @testable import PostgREST import XCTest +@MainActor final class PostgrestBuilderTests: XCTestCase { let url = URL(string: "http://localhost:54321/rest/v1")! @@ -16,8 +17,8 @@ final class PostgrestBuilderTests: XCTestCase { let postgrest2 = try postgrest1.rpc("void_func").setHeader(name: .init("apikey")!, value: "bar") // Original client object isn't affected - XCTAssertEqual(postgrest1.from("users").select().mutableState.request.headers[.init("apikey")!], "foo") + XCTAssertEqual(postgrest1.from("users").select().request.headers[.init("apikey")!], "foo") // Derived client object uses new header value - XCTAssertEqual(postgrest2.mutableState.request.headers[.init("apikey")!], "bar") + XCTAssertEqual(postgrest2.request.headers[.init("apikey")!], "bar") } } diff --git a/Tests/RealtimeTests/RealtimeChannelTests.swift b/Tests/RealtimeTests/RealtimeChannelTests.swift index 286e29b3..b3a1c156 100644 --- a/Tests/RealtimeTests/RealtimeChannelTests.swift +++ b/Tests/RealtimeTests/RealtimeChannelTests.swift @@ -13,6 +13,7 @@ import XCTestDynamicOverlay @testable import Realtime +@MainActor final class RealtimeChannelTests: XCTestCase { var socket: RealtimeClientV2! var sut: RealtimeChannelV2! @@ -47,34 +48,34 @@ final class RealtimeChannelTests: XCTestCase { func testAttachCallbacks() async { var subscriptions = Set() - await sut.onPostgresChange( + sut.onPostgresChange( AnyAction.self, schema: "public", table: "users", filter: "id=eq.1" ) { _ in }.store(in: &subscriptions) - await sut.onPostgresChange( + sut.onPostgresChange( InsertAction.self, schema: "private" ) { _ in }.store(in: &subscriptions) - await sut.onPostgresChange( + sut.onPostgresChange( UpdateAction.self, table: "messages" ) { _ in }.store(in: &subscriptions) - await sut.onPostgresChange( + sut.onPostgresChange( DeleteAction.self ) { _ in }.store(in: &subscriptions) - await sut.onBroadcast(event: "test") { _ in }.store(in: &subscriptions) - await sut.onBroadcast(event: "cursor-pos") { _ in }.store(in: &subscriptions) + sut.onBroadcast(event: "test") { _ in }.store(in: &subscriptions) + sut.onBroadcast(event: "cursor-pos") { _ in }.store(in: &subscriptions) - await sut.onPresenceChange { _ in }.store(in: &subscriptions) + sut.onPresenceChange { _ in }.store(in: &subscriptions) - await sut.onSystem { + sut.onSystem { } .store(in: &subscriptions) - let callbacks = await sut.callbackManager.callbacks + let callbacks = sut.callbackManager.callbacks assertInlineSnapshot(of: callbacks, as: .dump) { """ â–¿ 8 elements diff --git a/Tests/RealtimeTests/_PushTests.swift b/Tests/RealtimeTests/_PushTests.swift index ce3afefd..8ca762df 100644 --- a/Tests/RealtimeTests/_PushTests.swift +++ b/Tests/RealtimeTests/_PushTests.swift @@ -12,6 +12,7 @@ import XCTest @testable import Realtime +@MainActor final class _PushTests: XCTestCase { var ws: FakeWebSocket! var socket: RealtimeClientV2! diff --git a/Tests/StorageTests/SupabaseStorageTests.swift b/Tests/StorageTests/SupabaseStorageTests.swift index 5742538d..0b5d15da 100644 --- a/Tests/StorageTests/SupabaseStorageTests.swift +++ b/Tests/StorageTests/SupabaseStorageTests.swift @@ -10,6 +10,7 @@ import XCTestDynamicOverlay import FoundationNetworking #endif +@MainActor final class SupabaseStorageTests: XCTestCase { let supabaseURL = URL(string: "http://localhost:54321/storage/v1")! let bucketId = "tests" diff --git a/Tests/SupabaseTests/SupabaseClientTests.swift b/Tests/SupabaseTests/SupabaseClientTests.swift index c487177d..6926bddb 100644 --- a/Tests/SupabaseTests/SupabaseClientTests.swift +++ b/Tests/SupabaseTests/SupabaseClientTests.swift @@ -16,6 +16,7 @@ final class AuthLocalStorageMock: AuthLocalStorage { func remove(key _: String) throws {} } +@MainActor final class SupabaseClientTests: XCTestCase { func testClientInitialization() async { final class Logger: SupabaseLogger { @@ -88,7 +89,7 @@ final class SupabaseClientTests: XCTestCase { XCTAssertEqual(client.auth.configuration.storageKey, "sb-project-ref-auth-token") XCTAssertNotNil( - client.mutableState.listenForAuthEventsTask, + client.listenForAuthEventsTask, "should listen for internal auth events" ) } @@ -117,7 +118,7 @@ final class SupabaseClientTests: XCTestCase { ) XCTAssertNil( - client.mutableState.listenForAuthEventsTask, + client.listenForAuthEventsTask, "should not listen for internal auth events when using 3p authentication" )