Skip to content

Commit

Permalink
Fix ChannelTopic init from string and add unit tests (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
Guilherme Souza authored Feb 21, 2022
1 parent a65d04c commit 2b55564
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 63 deletions.
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: "[email protected]", table: "users", schema: "public"),
ChannelTopic(rawValue: "realtime:public:users:[email protected]"))
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.

0 comments on commit 2b55564

Please sign in to comment.