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

Merge branch 'fix/get-notifications-report' into 'development'

Fix GetNotificationsReport

See merge request elixxir/elixxir-dapps-sdk-swift!142
parents c11308a8 20f41917
No related branches found
No related tags found
2 merge requests!142Fix GetNotificationsReport,!102Release 1.0.0
...@@ -2,12 +2,12 @@ import Bindings ...@@ -2,12 +2,12 @@ import Bindings
import XCTestDynamicOverlay import XCTestDynamicOverlay
public struct GetNotificationsReport { public struct GetNotificationsReport {
public var run: (String, MessageServiceList) throws -> NotificationReport public var run: (String, MessageServiceList) throws -> [NotificationReport]
public func callAsFunction( public func callAsFunction(
notificationCSV: String, notificationCSV: String,
services: MessageServiceList services: MessageServiceList
) throws -> NotificationReport { ) throws -> [NotificationReport] {
try run(notificationCSV, services) try run(notificationCSV, services)
} }
} }
...@@ -26,7 +26,7 @@ extension GetNotificationsReport { ...@@ -26,7 +26,7 @@ extension GetNotificationsReport {
guard let result = result else { guard let result = result else {
fatalError("BindingsGetNotificationsReport returned nil without providing error") fatalError("BindingsGetNotificationsReport returned nil without providing error")
} }
return try NotificationReport.decode(result) return try [NotificationReport].decode(result)
} }
} }
......
...@@ -45,3 +45,13 @@ extension NotificationReport: Codable { ...@@ -45,3 +45,13 @@ extension NotificationReport: Codable {
try JSONEncoder().encode(self) try JSONEncoder().encode(self)
} }
} }
extension Array where Element == NotificationReport {
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 XXClient import XXClient
import XCTestDynamicOverlay import XCTestDynamicOverlay
public struct MessengerGetNotificationReport { public struct MessengerGetNotificationReports {
public enum Error: Swift.Error, Equatable { public enum Error: Swift.Error, Equatable {
case serviceListMissing case serviceListMissing
} }
public var run: (String) throws -> NotificationReport public var run: (String) throws -> [NotificationReport]
public func callAsFunction(notificationCSV: String) throws -> NotificationReport { public func callAsFunction(notificationCSV: String) throws -> [NotificationReport] {
try run(notificationCSV) try run(notificationCSV)
} }
} }
extension MessengerGetNotificationReport { extension MessengerGetNotificationReports {
public static func live(_ env: MessengerEnvironment) -> MessengerGetNotificationReport { public static func live(_ env: MessengerEnvironment) -> MessengerGetNotificationReports {
MessengerGetNotificationReport { notificationCSV in MessengerGetNotificationReports { notificationCSV in
guard let serviceList = env.serviceList() else { guard let serviceList = env.serviceList() else {
throw Error.serviceListMissing throw Error.serviceListMissing
} }
...@@ -27,8 +27,8 @@ extension MessengerGetNotificationReport { ...@@ -27,8 +27,8 @@ extension MessengerGetNotificationReport {
} }
} }
extension MessengerGetNotificationReport { extension MessengerGetNotificationReports {
public static let unimplemented = MessengerGetNotificationReport( public static let unimplemented = MessengerGetNotificationReports(
run: XCTUnimplemented("\(Self.self)") run: XCTUnimplemented("\(Self.self)")
) )
} }
...@@ -47,7 +47,7 @@ public struct Messenger { ...@@ -47,7 +47,7 @@ public struct Messenger {
public var sendFile: MessengerSendFile public var sendFile: MessengerSendFile
public var receiveFile: MessengerReceiveFile public var receiveFile: MessengerReceiveFile
public var trackServices: MessengerTrackServices public var trackServices: MessengerTrackServices
public var getNotificationReport: MessengerGetNotificationReport public var getNotificationReports: MessengerGetNotificationReports
} }
extension Messenger { extension Messenger {
...@@ -99,7 +99,7 @@ extension Messenger { ...@@ -99,7 +99,7 @@ extension Messenger {
sendFile: .live(env), sendFile: .live(env),
receiveFile: .live(env), receiveFile: .live(env),
trackServices: .live(env), trackServices: .live(env),
getNotificationReport: .live(env) getNotificationReports: .live(env)
) )
} }
} }
...@@ -152,6 +152,6 @@ extension Messenger { ...@@ -152,6 +152,6 @@ extension Messenger {
sendFile: .unimplemented, sendFile: .unimplemented,
receiveFile: .unimplemented, receiveFile: .unimplemented,
trackServices: .unimplemented, trackServices: .unimplemented,
getNotificationReport: .unimplemented getNotificationReports: .unimplemented
) )
} }
...@@ -28,4 +28,43 @@ final class NotificationReportTests: XCTestCase { ...@@ -28,4 +28,43 @@ final class NotificationReportTests: XCTestCase {
XCTAssertNoDifference(decodedModel, model) XCTAssertNoDifference(decodedModel, model)
} }
func testCodingArray() throws {
let source1B64 = "dGVzdGVyMTIzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
let source2B64 = "ciI1cpyUUY/UPaVeMy1zBFWbZRgiZSXhY+cVoM+fCxwD"
let jsonString = """
[
{
"ForMe": true,
"Type": "\(NotificationReport.ReportType.default.rawValue)",
"Source": "\(source1B64)"
},
{
"ForMe": false,
"Type": "\(NotificationReport.ReportType.request.rawValue)",
"Source": "\(source2B64)"
},
]
"""
let jsonData = jsonString.data(using: .utf8)!
let models = try [NotificationReport].decode(jsonData)
XCTAssertNoDifference(models, [
NotificationReport(
forMe: true,
type: .default,
source: Data(base64Encoded: source1B64)!
),
NotificationReport(
forMe: false,
type: .request,
source: Data(base64Encoded: source2B64)!
)
])
let encodedModels = try models.encode()
let decodedModels = try [NotificationReport].decode(encodedModels)
XCTAssertNoDifference(decodedModels, models)
}
} }
...@@ -4,11 +4,11 @@ import XCTestDynamicOverlay ...@@ -4,11 +4,11 @@ import XCTestDynamicOverlay
import XXClient import XXClient
@testable import XXMessengerClient @testable import XXMessengerClient
final class MessengerGetNotificationReportTests: XCTestCase { final class MessengerGetNotificationReportsTests: XCTestCase {
func testGetReport() throws { func testGetReport() throws {
let serviceList = MessageServiceList.stub() let serviceList = MessageServiceList.stub()
let notificationCSV = "notification-csv" let notificationCSV = "notification-csv"
let notificationReport = NotificationReport.stub() let notificationReports = [NotificationReport].stub()
struct GetNotificationsReportParams: Equatable { struct GetNotificationsReportParams: Equatable {
var notificationCSV: String var notificationCSV: String
...@@ -25,11 +25,11 @@ final class MessengerGetNotificationReportTests: XCTestCase { ...@@ -25,11 +25,11 @@ final class MessengerGetNotificationReportTests: XCTestCase {
notificationCSV: notificationCSV, notificationCSV: notificationCSV,
serviceList: serviceList serviceList: serviceList
)) ))
return notificationReport return notificationReports
} }
let getReport: MessengerGetNotificationReport = .live(env) let getReports: MessengerGetNotificationReports = .live(env)
let report = try getReport(notificationCSV: notificationCSV) let reports = try getReports(notificationCSV: notificationCSV)
XCTAssertNoDifference(didGetNotificationsReport, [ XCTAssertNoDifference(didGetNotificationsReport, [
.init( .init(
...@@ -37,25 +37,31 @@ final class MessengerGetNotificationReportTests: XCTestCase { ...@@ -37,25 +37,31 @@ final class MessengerGetNotificationReportTests: XCTestCase {
serviceList: serviceList serviceList: serviceList
) )
]) ])
XCTAssertNoDifference(report, notificationReport) XCTAssertNoDifference(reports, notificationReports)
} }
func testGetReportWhenServiceListMissing() { func testGetReportWhenServiceListMissing() {
var env: MessengerEnvironment = .unimplemented var env: MessengerEnvironment = .unimplemented
env.e2e.get = { .unimplemented } env.e2e.get = { .unimplemented }
env.serviceList.get = { nil } env.serviceList.get = { nil }
let getReport: MessengerGetNotificationReport = .live(env) let getReports: MessengerGetNotificationReports = .live(env)
XCTAssertThrowsError(try getReport(notificationCSV: "")) { error in XCTAssertThrowsError(try getReports(notificationCSV: "")) { error in
XCTAssertNoDifference( XCTAssertNoDifference(
error as? MessengerGetNotificationReport.Error, error as? MessengerGetNotificationReports.Error,
.serviceListMissing .serviceListMissing
) )
} }
} }
} }
extension NotificationReport { private extension Array where Element == NotificationReport {
static func stub() -> [NotificationReport] {
[.stub(), .stub(), .stub()]
}
}
private extension NotificationReport {
static func stub() -> NotificationReport { static func stub() -> NotificationReport {
NotificationReport( NotificationReport(
forMe: .random(), forMe: .random(),
......
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