From 0b4ffdb49b45bfd85e83059e32c30279ab84a827 Mon Sep 17 00:00:00 2001
From: Dariusz Rybicki <dariusz@elixxir.io>
Date: Tue, 26 Jul 2022 17:46:23 +0100
Subject: [PATCH] Add error property to Progress model

---
 Sources/ElixxirDAppsSDK/Models/Progress.swift | 10 +++----
 .../Models/ProgressTests.swift                | 27 ++++++++++++++++++-
 2 files changed, 31 insertions(+), 6 deletions(-)

diff --git a/Sources/ElixxirDAppsSDK/Models/Progress.swift b/Sources/ElixxirDAppsSDK/Models/Progress.swift
index 1376957b..ba2e199d 100644
--- a/Sources/ElixxirDAppsSDK/Models/Progress.swift
+++ b/Sources/ElixxirDAppsSDK/Models/Progress.swift
@@ -4,18 +4,19 @@ public struct Progress: Equatable {
   public init(
     completed: Bool,
     transmitted: Int,
-    total: Int
+    total: Int,
+    error: String?
   ) {
     self.completed = completed
     self.transmitted = transmitted
     self.total = total
+    self.error = error
   }
 
   public var completed: Bool
   public var transmitted: Int
   public var total: Int
-  // TODO: add error
-  // public var error: ???
+  public var error: String?
 }
 
 extension Progress: Codable {
@@ -23,8 +24,7 @@ extension Progress: Codable {
     case completed = "Completed"
     case transmitted = "Transmitted"
     case total = "Total"
-    // TODO: add error
-    // case error = "Err"
+    case error = "Err"
   }
 
   public static func decode(_ data: Data) throws -> Self {
diff --git a/Tests/ElixxirDAppsSDKTests/Models/ProgressTests.swift b/Tests/ElixxirDAppsSDKTests/Models/ProgressTests.swift
index 86f8d0f5..451ea593 100644
--- a/Tests/ElixxirDAppsSDKTests/Models/ProgressTests.swift
+++ b/Tests/ElixxirDAppsSDKTests/Models/ProgressTests.swift
@@ -21,7 +21,8 @@ final class ProgressTests: XCTestCase {
     XCTAssertNoDifference(progress, Progress(
       completed: completed,
       transmitted: transmitted,
-      total: total
+      total: total,
+      error: nil
     ))
 
     let encodedProgress = try progress.encode()
@@ -29,4 +30,28 @@ final class ProgressTests: XCTestCase {
 
     XCTAssertNoDifference(decodedProgress, progress)
   }
+
+  func testDecodingProgressWithError() throws {
+    let completed = false
+    let transmitted: Int = 128
+    let total: Int = 2048
+    let error = "something went wrong"
+    let jsonString = """
+    {
+      "Completed": \(completed),
+      "Transmitted": \(transmitted),
+      "Total": \(total),
+      "Err": "\(error)"
+    }
+    """
+    let jsonData = jsonString.data(using: .utf8)!
+    let progress = try Progress.decode(jsonData)
+
+    XCTAssertNoDifference(progress, Progress(
+      completed: completed,
+      transmitted: transmitted,
+      total: total,
+      error: error
+    ))
+  }
 }
-- 
GitLab