From 526642a54cd0207d9ab2ab917737b9d02eb5c5d1 Mon Sep 17 00:00:00 2001 From: Dariusz Rybicki <dariusz@elixxir.io> Date: Tue, 26 Jul 2022 17:12:57 +0100 Subject: [PATCH] Add Progress model --- Sources/ElixxirDAppsSDK/Models/Progress.swift | 38 +++++++++++++++++++ .../Models/ProgressTests.swift | 32 ++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 Sources/ElixxirDAppsSDK/Models/Progress.swift create mode 100644 Tests/ElixxirDAppsSDKTests/Models/ProgressTests.swift diff --git a/Sources/ElixxirDAppsSDK/Models/Progress.swift b/Sources/ElixxirDAppsSDK/Models/Progress.swift new file mode 100644 index 00000000..a0f747a4 --- /dev/null +++ b/Sources/ElixxirDAppsSDK/Models/Progress.swift @@ -0,0 +1,38 @@ +import Foundation + +public struct Progress: Equatable { + public init( + completed: Bool, + transmitted: Int, + total: Int + ) { + self.completed = completed + self.transmitted = transmitted + self.total = total + } + + public var completed: Bool + public var transmitted: Int + public var total: Int + // TODO: add error + // public var error: ??? +} +import Bindings + +extension Progress: Codable { + enum CodingKeys: String, CodingKey { + case completed = "Completed" + case transmitted = "Transmitted" + case total = "Total" + // TODO: add error + // case error = "Err" + } + + 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/ProgressTests.swift b/Tests/ElixxirDAppsSDKTests/Models/ProgressTests.swift new file mode 100644 index 00000000..86f8d0f5 --- /dev/null +++ b/Tests/ElixxirDAppsSDKTests/Models/ProgressTests.swift @@ -0,0 +1,32 @@ +import CustomDump +import XCTest +@testable import ElixxirDAppsSDK + +final class ProgressTests: XCTestCase { + func testCoding() throws { + let completed = false + let transmitted: Int = 128 + let total: Int = 2048 + let jsonString = """ + { + "Completed": \(completed), + "Transmitted": \(transmitted), + "Total": \(total), + "Err": null + } + """ + let jsonData = jsonString.data(using: .utf8)! + let progress = try Progress.decode(jsonData) + + XCTAssertNoDifference(progress, Progress( + completed: completed, + transmitted: transmitted, + total: total + )) + + let encodedProgress = try progress.encode() + let decodedProgress = try Progress.decode(encodedProgress) + + XCTAssertNoDifference(decodedProgress, progress) + } +} -- GitLab