|
| 1 | +import InlineSnapshotTesting |
| 2 | +import SnapshotTestingCustomDump |
| 3 | +import XCTest |
| 4 | + |
| 5 | +@testable import Realtime |
| 6 | + |
| 7 | +final class BroadcastEventTests: XCTestCase { |
| 8 | + func testBroadcastChangeDecoding() throws { |
| 9 | + // Test INSERT operation |
| 10 | + let insertJSON = Data( |
| 11 | + """ |
| 12 | + { |
| 13 | + "schema": "public", |
| 14 | + "table": "users", |
| 15 | + "operation": "INSERT", |
| 16 | + "record": { |
| 17 | + "id": 1, |
| 18 | + "name": "John Doe", |
| 19 | + |
| 20 | + }, |
| 21 | + "old_record": null |
| 22 | + } |
| 23 | + """.utf8 |
| 24 | + ) |
| 25 | + |
| 26 | + let insertChange = try JSONDecoder().decode(BroadcastChange.self, from: insertJSON) |
| 27 | + |
| 28 | + assertInlineSnapshot(of: insertChange, as: .customDump) { |
| 29 | + """ |
| 30 | + BroadcastChange( |
| 31 | + schema: "public", |
| 32 | + table: "users", |
| 33 | + operation: .insert( |
| 34 | + new: [ |
| 35 | + "email": .string("[email protected]"), |
| 36 | + "id": .integer(1), |
| 37 | + "name": .string("John Doe") |
| 38 | + ] |
| 39 | + ) |
| 40 | + ) |
| 41 | + """ |
| 42 | + } |
| 43 | + |
| 44 | + // Test UPDATE operation |
| 45 | + let updateJSON = Data( |
| 46 | + """ |
| 47 | + { |
| 48 | + "schema": "public", |
| 49 | + "table": "users", |
| 50 | + "operation": "UPDATE", |
| 51 | + "record": { |
| 52 | + "id": 1, |
| 53 | + "name": "John Updated", |
| 54 | + |
| 55 | + }, |
| 56 | + "old_record": { |
| 57 | + "id": 1, |
| 58 | + "name": "John Doe", |
| 59 | + |
| 60 | + } |
| 61 | + } |
| 62 | + """.utf8 |
| 63 | + ) |
| 64 | + |
| 65 | + let updateChange = try JSONDecoder().decode(BroadcastChange.self, from: updateJSON) |
| 66 | + assertInlineSnapshot(of: updateChange, as: .customDump) { |
| 67 | + """ |
| 68 | + BroadcastChange( |
| 69 | + schema: "public", |
| 70 | + table: "users", |
| 71 | + operation: .update( |
| 72 | + new: [ |
| 73 | + "email": .string("[email protected]"), |
| 74 | + "id": .integer(1), |
| 75 | + "name": .string("John Updated") |
| 76 | + ], |
| 77 | + old: [ |
| 78 | + "email": .string("[email protected]"), |
| 79 | + "id": .integer(1), |
| 80 | + "name": .string("John Doe") |
| 81 | + ] |
| 82 | + ) |
| 83 | + ) |
| 84 | + """ |
| 85 | + } |
| 86 | + |
| 87 | + // Test DELETE operation |
| 88 | + let deleteJSON = Data( |
| 89 | + """ |
| 90 | + { |
| 91 | + "schema": "public", |
| 92 | + "table": "users", |
| 93 | + "operation": "DELETE", |
| 94 | + "record": null, |
| 95 | + "old_record": { |
| 96 | + "id": 1, |
| 97 | + "name": "John Doe", |
| 98 | + |
| 99 | + } |
| 100 | + } |
| 101 | + """.utf8 |
| 102 | + ) |
| 103 | + |
| 104 | + let deleteChange = try JSONDecoder().decode(BroadcastChange.self, from: deleteJSON) |
| 105 | + |
| 106 | + assertInlineSnapshot(of: deleteChange, as: .customDump) { |
| 107 | + """ |
| 108 | + BroadcastChange( |
| 109 | + schema: "public", |
| 110 | + table: "users", |
| 111 | + operation: .delete( |
| 112 | + old: [ |
| 113 | + "email": .string("[email protected]"), |
| 114 | + "id": .integer(1), |
| 115 | + "name": .string("John Doe") |
| 116 | + ] |
| 117 | + ) |
| 118 | + ) |
| 119 | + """ |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + func testBroadcastChangeEncoding() throws { |
| 124 | + // Test INSERT operation encoding |
| 125 | + let insertChange = BroadcastChange( |
| 126 | + schema: "public", |
| 127 | + table: "users", |
| 128 | + operation: .insert(new: [ |
| 129 | + "id": 1, |
| 130 | + "name": "John Doe", |
| 131 | + |
| 132 | + ]) |
| 133 | + ) |
| 134 | + |
| 135 | + let insertJSON = try JSONObject(insertChange) |
| 136 | + assertInlineSnapshot(of: insertJSON, as: .json) { |
| 137 | + """ |
| 138 | + { |
| 139 | + "old_record" : null, |
| 140 | + "operation" : "INSERT", |
| 141 | + "record" : { |
| 142 | + |
| 143 | + "id" : 1, |
| 144 | + "name" : "John Doe" |
| 145 | + }, |
| 146 | + "schema" : "public", |
| 147 | + "table" : "users" |
| 148 | + } |
| 149 | + """ |
| 150 | + } |
| 151 | + |
| 152 | + // Test UPDATE operation encoding |
| 153 | + let updateChange = BroadcastChange( |
| 154 | + schema: "public", |
| 155 | + table: "users", |
| 156 | + operation: .update( |
| 157 | + new: ["id": 1, "name": "John Updated"], |
| 158 | + old: ["id": 1, "name": "John Doe"] |
| 159 | + ) |
| 160 | + ) |
| 161 | + |
| 162 | + let updateJSON = try JSONObject(updateChange) |
| 163 | + assertInlineSnapshot(of: updateJSON, as: .json) { |
| 164 | + """ |
| 165 | + { |
| 166 | + "old_record" : { |
| 167 | + "id" : 1, |
| 168 | + "name" : "John Doe" |
| 169 | + }, |
| 170 | + "operation" : "UPDATE", |
| 171 | + "record" : { |
| 172 | + "id" : 1, |
| 173 | + "name" : "John Updated" |
| 174 | + }, |
| 175 | + "schema" : "public", |
| 176 | + "table" : "users" |
| 177 | + } |
| 178 | + """ |
| 179 | + } |
| 180 | + |
| 181 | + // Test DELETE operation encoding |
| 182 | + let deleteChange = BroadcastChange( |
| 183 | + schema: "public", |
| 184 | + table: "users", |
| 185 | + operation: .delete(old: [ |
| 186 | + "id": 1, |
| 187 | + "name": "John Doe", |
| 188 | + ]) |
| 189 | + ) |
| 190 | + |
| 191 | + let deleteJSON = try JSONObject(deleteChange) |
| 192 | + assertInlineSnapshot(of: deleteJSON, as: .json) { |
| 193 | + """ |
| 194 | + { |
| 195 | + "old_record" : { |
| 196 | + "id" : 1, |
| 197 | + "name" : "John Doe" |
| 198 | + }, |
| 199 | + "operation" : "DELETE", |
| 200 | + "record" : null, |
| 201 | + "schema" : "public", |
| 202 | + "table" : "users" |
| 203 | + } |
| 204 | + """ |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + func testBroadcastEvent() throws { |
| 209 | + let eventJSON = Data( |
| 210 | + """ |
| 211 | + { |
| 212 | + "type": "broadcast", |
| 213 | + "event": "test_event", |
| 214 | + "payload": { |
| 215 | + "schema": "public", |
| 216 | + "table": "users", |
| 217 | + "operation": "INSERT", |
| 218 | + "record": { |
| 219 | + "id": 1, |
| 220 | + "name": "John Doe" |
| 221 | + }, |
| 222 | + "old_record": null |
| 223 | + } |
| 224 | + } |
| 225 | + """.utf8 |
| 226 | + ) |
| 227 | + |
| 228 | + let event = try JSONDecoder().decode(BroadcastEvent.self, from: eventJSON) |
| 229 | + |
| 230 | + XCTAssertEqual(event.type, "broadcast") |
| 231 | + XCTAssertEqual(event.event, "test_event") |
| 232 | + |
| 233 | + let change = try event.broadcastChange() |
| 234 | + XCTAssertEqual(change.schema, "public") |
| 235 | + XCTAssertEqual(change.table, "users") |
| 236 | + |
| 237 | + if case .insert(let new) = change.operation { |
| 238 | + XCTAssertEqual(new["id"]?.intValue, 1) |
| 239 | + XCTAssertEqual(new["name"]?.stringValue, "John Doe") |
| 240 | + } else { |
| 241 | + XCTFail("Expected INSERT operation") |
| 242 | + } |
| 243 | + } |
| 244 | +} |
0 commit comments