Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
grdsdev committed Nov 29, 2024
1 parent aedd2ad commit a77bb1b
Show file tree
Hide file tree
Showing 21 changed files with 274 additions and 198 deletions.
2 changes: 1 addition & 1 deletion Sources/Supabase/SupabaseClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public final class SupabaseClient {
_headers.dictionary
}

private var listenForAuthEventsTask: Task<Void, Never>?
var listenForAuthEventsTask: Task<Void, Never>?
private var _storage: SupabaseStorageClient?
private var _rest: PostgrestClient?
private var _functions: FunctionsClient?
Expand Down
1 change: 1 addition & 0 deletions Tests/AuthTests/AuthClientMultipleInstancesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import TestHelpers
import XCTest

@MainActor
final class AuthClientMultipleInstancesTests: XCTestCase {
func testMultipleAuthClientInstances() {
let url = URL(string: "http://localhost:54321/auth")!
Expand Down
1 change: 1 addition & 0 deletions Tests/AuthTests/AuthClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import XCTest
import FoundationNetworking
#endif

@MainActor
final class AuthClientTests: XCTestCase {
var sessionManager: SessionManager!

Expand Down
1 change: 1 addition & 0 deletions Tests/AuthTests/RequestsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import XCTest

struct UnimplementedError: Error {}

@MainActor
final class RequestsTests: XCTestCase {
func testSignUpWithEmailAndPassword() async {
let sut = makeSUT()
Expand Down
1 change: 1 addition & 0 deletions Tests/FunctionsTests/FunctionsClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions Tests/FunctionsTests/RequestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
77 changes: 40 additions & 37 deletions Tests/IntegrationTests/AuthClientIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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(
Expand All @@ -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()
Expand All @@ -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]) {
Expand Down Expand Up @@ -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..<length {
let randomAscii = Int.random(in: 97...122) // ASCII values for lowercase letters
let randomCharacter = Character(UnicodeScalar(randomAscii)!)
username.append(randomCharacter)
}
Expand All @@ -303,13 +305,13 @@ final class AuthClientIntegrationTests: XCTestCase {

private func mockPhoneNumber() -> 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)"
Expand All @@ -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..<length {
let randomIndex = Int.random(in: 0..<allowedCharacters.count)
let character = allowedCharacters[
allowedCharacters.index(
allowedCharacters.startIndex,
offsetBy: randomIndex
)]
password.append(character)
}

Expand Down
5 changes: 3 additions & 2 deletions Tests/IntegrationTests/PostgrestIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ struct User: Codable, Hashable {
let email: String
}

@MainActor
@available(iOS 15.0.0, macOS 12.0.0, tvOS 13.0, *)
final class IntegrationTests: XCTestCase {
let client = PostgrestClient(
url: URL(string: "\(DotEnv.SUPABASE_URL)/rest/v1")!,
headers: [
"Apikey": DotEnv.SUPABASE_ANON_KEY,
"Apikey": DotEnv.SUPABASE_ANON_KEY
],
logger: nil
)
Expand Down Expand Up @@ -128,7 +129,7 @@ final class IntegrationTests: XCTestCase {
.ilike("email", value: "johndoe+test%").execute().value
XCTAssertEqual(
fetchedUsers[...],
users[1 ... 2]
users[1...2]
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import InlineSnapshotTesting
import PostgREST
import XCTest

@MainActor
final class PostgrestTransformsTests: XCTestCase {
let client = PostgrestClient(
configuration: PostgrestClient.Configuration(
Expand Down
28 changes: 19 additions & 9 deletions Tests/IntegrationTests/Potsgrest/PostgrestBasicTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import InlineSnapshotTesting
import PostgREST
import XCTest

@MainActor
final class PostgrestBasicTests: XCTestCase {
let client = PostgrestClient(
configuration: PostgrestClient.Configuration(
url: URL(string: "\(DotEnv.SUPABASE_URL)/rest/v1")!,
headers: [
"apikey": DotEnv.SUPABASE_ANON_KEY,
"apikey": DotEnv.SUPABASE_ANON_KEY
],
logger: nil
)
Expand Down Expand Up @@ -85,7 +86,9 @@ final class PostgrestBasicTests: XCTestCase {
}

func testRPC() async throws {
let response = try await client.rpc("get_status", params: ["name_param": "supabot"]).execute().value as AnyJSON
let response =
try await client.rpc("get_status", params: ["name_param": "supabot"]).execute().value
as AnyJSON
assertInlineSnapshot(of: response, as: .json) {
"""
"ONLINE"
Expand All @@ -99,7 +102,8 @@ final class PostgrestBasicTests: XCTestCase {
}

func testIgnoreDuplicates_upsert() async throws {
let response = try await client.from("users")
let response =
try await client.from("users")
.upsert(["username": "dragarcia"], onConflict: "username", ignoreDuplicates: true)
.select().execute().value as AnyJSON
assertInlineSnapshot(of: response, as: .json) {
Expand All @@ -113,7 +117,8 @@ final class PostgrestBasicTests: XCTestCase {

func testBasicInsertUpdateAndDelete() async throws {
// Basic insert
var response = try await client.from("messages")
var response =
try await client.from("messages")
.insert(AnyJSON.object(["message": "foo", "username": "supabot", "channel_id": 1]))
.select("channel_id,data,message,username")
.execute()
Expand All @@ -131,7 +136,8 @@ final class PostgrestBasicTests: XCTestCase {
"""
}

response = try await client.from("messages").select("channel_id,data,message,username").execute().value
response = try await client.from("messages").select("channel_id,data,message,username")
.execute().value
assertInlineSnapshot(of: response, as: .json) {
"""
[
Expand Down Expand Up @@ -159,7 +165,8 @@ final class PostgrestBasicTests: XCTestCase {

// Upsert

response = try await client.from("messages")
response =
try await client.from("messages")
.upsert(
AnyJSON.object(
[
Expand All @@ -186,7 +193,8 @@ final class PostgrestBasicTests: XCTestCase {
"""
}

response = try await client.from("messages").select("channel_id,data,message,username").execute().value
response = try await client.from("messages").select("channel_id,data,message,username")
.execute().value
assertInlineSnapshot(of: response, as: .json) {
"""
[
Expand Down Expand Up @@ -249,7 +257,8 @@ final class PostgrestBasicTests: XCTestCase {
"""
}

response = try await client.from("messages").select("channel_id,data,message,username").execute().value
response = try await client.from("messages").select("channel_id,data,message,username")
.execute().value
assertInlineSnapshot(of: response, as: .json) {
"""
[
Expand Down Expand Up @@ -331,7 +340,8 @@ final class PostgrestBasicTests: XCTestCase {
"""
}

response = try await client.from("messages").select("channel_id,data,message,username").execute().value
response = try await client.from("messages").select("channel_id,data,message,username")
.execute().value
assertInlineSnapshot(of: response, as: .json) {
"""
[
Expand Down
Loading

0 comments on commit a77bb1b

Please sign in to comment.