From d18e946595afa6d76cc8e857e2f3667b1e391b50 Mon Sep 17 00:00:00 2001 From: Dariusz Rybicki <dariusz@elixxir.io> Date: Wed, 27 Jul 2022 14:24:21 +0100 Subject: [PATCH] Add BroadcastReport model --- .../Models/BroadcastReport.swift | 29 +++++++++++++++++++ .../Models/BroadcastReportTests.swift | 28 ++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 Sources/ElixxirDAppsSDK/Models/BroadcastReport.swift create mode 100644 Tests/ElixxirDAppsSDKTests/Models/BroadcastReportTests.swift diff --git a/Sources/ElixxirDAppsSDK/Models/BroadcastReport.swift b/Sources/ElixxirDAppsSDK/Models/BroadcastReport.swift new file mode 100644 index 00000000..3114a12b --- /dev/null +++ b/Sources/ElixxirDAppsSDK/Models/BroadcastReport.swift @@ -0,0 +1,29 @@ +import Foundation + +public struct BroadcastReport: Equatable { + public init( + roundId: Int, + ephId: [Int] + ) { + self.roundId = roundId + self.ephId = ephId + } + + public var roundId: Int + public var ephId: [Int] +} + +extension BroadcastReport: Codable { + enum CodingKeys: String, CodingKey { + case roundId = "RoundID" + case ephId = "EphID" + } + + public static func decode(_ data: Data) throws -> Self { + try JSONDecoder().decode(Self.self, from: data) + } + + public func encode() throws -> Data { + try JSONEncoder().encode(self) + } +} diff --git a/Tests/ElixxirDAppsSDKTests/Models/BroadcastReportTests.swift b/Tests/ElixxirDAppsSDKTests/Models/BroadcastReportTests.swift new file mode 100644 index 00000000..b94ca4a3 --- /dev/null +++ b/Tests/ElixxirDAppsSDKTests/Models/BroadcastReportTests.swift @@ -0,0 +1,28 @@ +import CustomDump +import XCTest +@testable import ElixxirDAppsSDK + +final class BroadcastReportTests: XCTestCase { + func testCoding() throws { + let roundId: Int = 42 + let ephId: [Int] = [0, 0, 0, 0, 0, 0, 24, 61] + let jsonString = """ + { + "RoundID": \(roundId), + "EphID": [\(ephId.map { "\($0)" }.joined(separator: ", "))] + } + """ + let jsonData = jsonString.data(using: .utf8)! + let report = try BroadcastReport.decode(jsonData) + + XCTAssertNoDifference(report, BroadcastReport( + roundId: roundId, + ephId: ephId + )) + + let encodedReport = try report.encode() + let decodedReport = try BroadcastReport.decode(encodedReport) + + XCTAssertNoDifference(decodedReport, report) + } +} -- GitLab