Skip to content

Fix ChannelTopic init from string and add unit tests #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 11 additions & 27 deletions Sources/Realtime/Defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public enum ChannelState: String {
/// Represents the different events that can be sent through
/// a channel regarding a Channel's lifecycle or
/// that can be registered to be notified of.
public enum ChannelEvent {
public enum ChannelEvent: RawRepresentable {
case heartbeat
case join
case leave
Expand All @@ -85,7 +85,7 @@ public enum ChannelEvent {

case channelReply(String)

public var raw: String {
public var rawValue: String {
switch self {
case .heartbeat: return "heartbeat"
case .join: return "phx_join"
Expand All @@ -103,8 +103,8 @@ public enum ChannelEvent {
}
}

public init?(from type: String) {
switch type.lowercased() {
public init?(rawValue: String) {
switch rawValue.lowercased() {
case "heartbeat": self = .heartbeat
case "phx_join": self = .join
case "phx_leave": self = .leave
Expand All @@ -124,30 +124,20 @@ public enum ChannelEvent {
switch event {
case .join, .leave, .reply, .error, .close: return true
case .heartbeat, .all, .insert, .update, .delete, .channelReply: return false
// Most likely new events will be about notification
// not about lifecycle.
@unknown default: return false
}
}
}

extension ChannelEvent: Equatable {
public static func ==(lhs: ChannelEvent, rhs: ChannelEvent) -> Bool {
return lhs.raw == rhs.raw
}
}

/// Represents the different topic
// a channel can subscribe to.
public enum ChannelTopic {
/// Represents the different topic a channel can subscribe to.
public enum ChannelTopic: RawRepresentable, Equatable {
case all
case schema(_ schema: String)
case table(_ table: String, schema: String)
case column(_ column: String, value: String, table: String, schema: String)

case heartbeat

public var raw: String {
public var rawValue: String {
switch self {
case .all: return "realtime:*"
case .schema(let name): return "realtime:\(name)"
Expand All @@ -157,13 +147,13 @@ public enum ChannelTopic {
}
}

public init?(from type: String) {
if type == "realtime:*" || type == "*" {
public init?(rawValue: String) {
if rawValue == "realtime:*" || rawValue == "*" {
self = .all
} else if type == "phoenix" {
} else if rawValue == "phoenix" {
self = .heartbeat
} else {
let parts = type.split(separator: ":")
let parts = rawValue.replacingOccurrences(of: "realtime:", with: "").split(separator: ":")
switch parts.count {
case 1:
self = .schema(String(parts[0]))
Expand All @@ -183,9 +173,3 @@ public enum ChannelTopic {
}
}
}

extension ChannelTopic {
public static func ==(lhs: ChannelTopic, rhs: ChannelTopic) -> Bool {
return lhs.raw == rhs.raw
}
}
4 changes: 2 additions & 2 deletions Sources/Realtime/Message.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public class Message {
let event = json["event"] as? String,
let payload = json["payload"] as? [String: Any]
{
self.topic = ChannelTopic(from: topic) ?? .all
self.event = ChannelEvent(from: event) ?? .all
self.topic = ChannelTopic(rawValue: topic) ?? .all
self.event = ChannelEvent(rawValue: event) ?? .all
self.payload = payload
} else {
return nil
Expand Down
4 changes: 2 additions & 2 deletions Sources/Realtime/RealtimeClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -502,8 +502,8 @@ public class RealtimeClient: TransportDelegate {
{
let callback: (() throws -> Void) = {
var body: [String: Any] = [
"topic": topic.raw,
"event": event.raw,
"topic": topic.rawValue,
"event": event.rawValue,
"payload": payload,
]

Expand Down
7 changes: 0 additions & 7 deletions Tests/LinuxMain.swift

This file was deleted.

19 changes: 19 additions & 0 deletions Tests/RealtimeTests/ChannelTopicTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import XCTest

@testable import Realtime

final class ChannelTopicTests: XCTestCase {

func testRawValue() {
XCTAssertEqual(ChannelTopic.all, ChannelTopic(rawValue: "realtime:*"))
XCTAssertEqual(ChannelTopic.all, ChannelTopic(rawValue: "*"))
XCTAssertEqual(ChannelTopic.schema("public"), ChannelTopic(rawValue: "realtime:public"))
XCTAssertEqual(
ChannelTopic.table("users", schema: "public"), ChannelTopic(rawValue: "realtime:public:users")
)
XCTAssertEqual(
ChannelTopic.column("email", value: "mail@supabase.io", table: "users", schema: "public"),
ChannelTopic(rawValue: "realtime:public:users:email=eq.mail@supabase.io"))
XCTAssertEqual(ChannelTopic.heartbeat, ChannelTopic(rawValue: "phoenix"))
}
}
16 changes: 0 additions & 16 deletions Tests/RealtimeTests/RealtimeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,6 @@ final class RealtimeTests: XCTestCase {
}
}

func testTopicSerialization() {
XCTAssertEqual(ChannelTopic.all.raw, "realtime:*")
XCTAssertEqual(ChannelTopic.schema("public").raw,
"realtime:public")
XCTAssertEqual(ChannelTopic.table("users", schema: "public").raw,
"realtime:public:users")
XCTAssertEqual(ChannelTopic.column("id", value: "99", table: "users", schema: "public").raw,
"realtime:public:users:id=eq.99")
}

func testChannelCreation() {
let client = RealtimeClient(endPoint: "\(Self.supabaseUrl())/realtime/v1", params: ["apikey": Self.supabaseKey()])
let allChanges = client.channel(.all)
Expand Down Expand Up @@ -118,10 +108,4 @@ final class RealtimeTests: XCTestCase {
}
}
}

static var allTests = [
("testConnection", testConnection),
("testTopicSerialization", testTopicSerialization),
("testChannelCreation", testChannelCreation),
]
}
9 changes: 0 additions & 9 deletions Tests/RealtimeTests/XCTestManifests.swift

This file was deleted.