Skip to content
Snippets Groups Projects
Commit 8157dc37 authored by Dariusz Rybicki's avatar Dariusz Rybicki
Browse files

Add NodeRegistrationReport model

parent 1f96d8bf
No related branches found
No related tags found
2 merge requests!102Release 1.0.0,!23Update Bindings
import Foundation
public struct NodeRegistrationReport: Equatable {
public init(
registered: Int,
total: Int
) {
self.registered = registered
self.total = total
}
public var registered: Int
public var total: Int
}
extension NodeRegistrationReport {
public var ratio: Double {
guard total != 0 else { return 0 }
return Double(registered) / Double(total)
}
}
extension NodeRegistrationReport: Codable {
enum CodingKeys: String, CodingKey {
case registered = "NumberOfNodesRegistered"
case total = "NumberOfNodes"
}
public static func decode(_ data: Data) throws -> Self {
try JSONDecoder().decode(Self.self, from: data)
}
public func encode() throws -> Data {
try JSONEncoder().encode(self)
}
}
import CustomDump
import XCTest
@testable import ElixxirDAppsSDK
final class NodeRegistrationReportTests: XCTestCase {
func testCoding() throws {
let registered: Int = 128
let total: Int = 2048
let jsonString = """
{
"NumberOfNodesRegistered": \(registered),
"NumberOfNodes": \(total)
}
"""
let jsonData = jsonString.data(using: .utf8)!
let model = try NodeRegistrationReport.decode(jsonData)
XCTAssertNoDifference(model, NodeRegistrationReport(
registered: registered,
total: total
))
let encodedModel = try model.encode()
let decodedModel = try NodeRegistrationReport.decode(encodedModel)
XCTAssertNoDifference(decodedModel, model)
}
func testRatio() {
let model = NodeRegistrationReport(
registered: 128,
total: 2048
)
XCTAssertEqual(model.ratio, 0.0625)
}
func testRatioWhenNoNodes() {
let model = NodeRegistrationReport(
registered: 128,
total: 0
)
XCTAssertEqual(model.ratio, 0)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment