-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pluto): add registered dids dao
Fixes ATL-2357
- Loading branch information
1 parent
82dae4e
commit 130c0e5
Showing
6 changed files
with
331 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
Pluto/Sources/PersistentStorage/DAO/CDRegisteredDIDDAO+DIDProvider.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import Combine | ||
import CoreData | ||
import Domain | ||
|
||
extension CDRegisteredDIDDAO: DIDProvider { | ||
func getAll() -> AnyPublisher<[(did: DID, keyPairIndex: Int, alias: String?)], Error> { | ||
fetchController(context: readContext) | ||
.map { $0.map { (DID(from: $0), Int($0.keyIndex), $0.alias) } } | ||
.eraseToAnyPublisher() | ||
} | ||
|
||
func getDIDInfo( | ||
alias: String | ||
) -> AnyPublisher<[(did: DID, keyPairIndex: Int, alias: String?)], Error> { | ||
fetchByKeyValuePublisher(key: "alias", value: alias, context: readContext) | ||
.map { $0.map { | ||
(DID(from: $0), Int($0.keyIndex), $0.alias) | ||
}} | ||
.eraseToAnyPublisher() | ||
} | ||
|
||
func getDIDInfo( | ||
did: DID | ||
) -> AnyPublisher<(did: DID, keyPairIndex: Int, alias: String?)?, Error> { | ||
fetchByKeyValuePublisher(key: "did", value: did.string, context: readContext) | ||
.map { $0.first.map { | ||
(DID(from: $0), Int($0.keyIndex), $0.alias) | ||
}} | ||
.eraseToAnyPublisher() | ||
} | ||
|
||
func getDIDInfo( | ||
keyPairIndex: Int | ||
) -> AnyPublisher<(did: DID, keyPairIndex: Int, alias: String?)?, Error> { | ||
fetchController( | ||
predicate: NSPredicate( | ||
format: "%K == %@", "keyIndex", NSNumber(value: keyPairIndex) | ||
), | ||
context: readContext | ||
) | ||
.map { $0.first.map { | ||
(DID(from: $0), Int($0.keyIndex), $0.alias) | ||
}} | ||
.eraseToAnyPublisher() | ||
} | ||
} | ||
|
||
private extension DID { | ||
init(from: CDDID) { | ||
self.init( | ||
schema: from.schema, | ||
method: from.method, | ||
methodId: from.methodId | ||
) | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
Pluto/Sources/PersistentStorage/DAO/CDRegisteredDIDDAO+DIDStore.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import Combine | ||
import CoreData | ||
import Domain | ||
|
||
extension CDRegisteredDIDDAO: DIDStore { | ||
func addDID(did: DID, keyPairIndex: Int, alias: String?) -> AnyPublisher<Void, Error> { | ||
updateOrCreate(did.string, context: writeContext) { cdobj, _ in | ||
cdobj.parseFrom(did: did, keyPairIndex: keyPairIndex, alias: alias) | ||
} | ||
.map { _ in () } | ||
.eraseToAnyPublisher() | ||
} | ||
|
||
func removeDID(did: DID) -> AnyPublisher<Void, Error> { | ||
deleteByIDsPublisher([did.string], context: writeContext) | ||
} | ||
|
||
func removeAll() -> AnyPublisher<Void, Error> { | ||
deleteAllPublisher(context: writeContext) | ||
} | ||
} | ||
|
||
private extension CDRegisteredDID { | ||
func parseFrom(did: DID, keyPairIndex: Int, alias: String?) { | ||
self.did = did.string | ||
schema = did.schema | ||
method = did.method | ||
methodId = did.methodId | ||
keyIndex = Int64(keyPairIndex) | ||
self.alias = alias | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import Combine | ||
import CoreData | ||
|
||
struct CDRegisteredDIDDAO: CoreDataDAO { | ||
typealias CoreDataObject = CDRegisteredDID | ||
let readContext: NSManagedObjectContext | ||
let writeContext: NSManagedObjectContext | ||
let identifierKey: String? = "did" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,233 @@ | ||
import Domain | ||
@testable import Pluto | ||
import XCTest | ||
|
||
final class CDRegisteredDIDDaoTests: XCTestCase { | ||
private var coreDataManager: CoreDataManager! | ||
|
||
override func setUpWithError() throws { | ||
try super.setUpWithError() | ||
coreDataManager = CoreDataManager(setup: .init( | ||
modelPath: .storeName("PrismPluto", ModelKit.bundle), | ||
storeType: .memory | ||
)) | ||
} | ||
|
||
func testStoreSingleDID() throws { | ||
let dao = CDRegisteredDIDDAO( | ||
readContext: coreDataManager.mainContext, | ||
writeContext: coreDataManager.editContext | ||
) | ||
|
||
let testDID = DID(method: "test", methodId: "test") | ||
let testKeyPairIndex = 0 | ||
let testAlias = "Test" | ||
let expectation = expectation(description: "Awaiting publisher") | ||
let cancellable = dao.addDID( | ||
did: testDID, | ||
keyPairIndex: testKeyPairIndex, | ||
alias: testAlias | ||
).flatMap { | ||
dao.getDIDInfo(did: testDID) | ||
}.sink { _ in } receiveValue: { | ||
XCTAssertEqual(testDID, $0?.did) | ||
XCTAssertEqual(testKeyPairIndex, $0?.keyPairIndex) | ||
XCTAssertEqual(testAlias, $0?.alias) | ||
expectation.fulfill() | ||
} | ||
|
||
waitForExpectations(timeout: 5) | ||
} | ||
|
||
func testStoreNoDuplicatedDID() throws { | ||
let dao = CDRegisteredDIDDAO( | ||
readContext: coreDataManager.mainContext, | ||
writeContext: coreDataManager.editContext | ||
) | ||
|
||
let testDID = DID(method: "test", methodId: "test") | ||
let testKeyPairIndex = 0 | ||
let testAlias = "Test" | ||
let expectation = expectation(description: "Awaiting publisher") | ||
let cancellable = dao.addDID( | ||
did: testDID, | ||
keyPairIndex: testKeyPairIndex, | ||
alias: testAlias | ||
).flatMap { | ||
dao.addDID( | ||
did: testDID, | ||
keyPairIndex: testKeyPairIndex, | ||
alias: testAlias | ||
) | ||
} | ||
.flatMap { | ||
dao.getAll() | ||
}.sink { _ in } receiveValue: { | ||
XCTAssertEqual($0.count, 1) | ||
expectation.fulfill() | ||
} | ||
|
||
waitForExpectations(timeout: 5) | ||
} | ||
|
||
func testGetAllDIDs() throws { | ||
let dao = CDRegisteredDIDDAO( | ||
readContext: coreDataManager.mainContext, | ||
writeContext: coreDataManager.editContext | ||
) | ||
|
||
let testDID1 = DID(method: "test1", methodId: "test1") | ||
let testKeyPairIndex1 = 0 | ||
let testAlias1 = "Test1" | ||
|
||
let testDID2 = DID(method: "test2", methodId: "test2") | ||
let testKeyPairIndex2 = 1 | ||
let testAlias2 = "Test2" | ||
|
||
let expectation = expectation(description: "Awaiting publisher") | ||
let cancellable = dao.addDID( | ||
did: testDID1, | ||
keyPairIndex: testKeyPairIndex1, | ||
alias: testAlias1 | ||
).flatMap { | ||
dao.addDID( | ||
did: testDID2, | ||
keyPairIndex: testKeyPairIndex2, | ||
alias: testAlias2 | ||
) | ||
} | ||
.flatMap { | ||
dao.getAll() | ||
}.sink { _ in } receiveValue: { | ||
XCTAssertEqual($0.count, 2) | ||
expectation.fulfill() | ||
} | ||
|
||
waitForExpectations(timeout: 5) | ||
} | ||
|
||
func testGetDIDInfoByDID() throws { | ||
let dao = CDRegisteredDIDDAO( | ||
readContext: coreDataManager.mainContext, | ||
writeContext: coreDataManager.editContext | ||
) | ||
|
||
let testDID1 = DID(method: "test1", methodId: "test1") | ||
let testKeyPairIndex1 = 0 | ||
let testAlias1 = "Test1" | ||
|
||
let testDID2 = DID(method: "test2", methodId: "test2") | ||
let testKeyPairIndex2 = 1 | ||
let testAlias2 = "Test2" | ||
|
||
let expectation = expectation(description: "Awaiting publisher") | ||
let cancellable = dao.addDID( | ||
did: testDID1, | ||
keyPairIndex: testKeyPairIndex1, | ||
alias: testAlias1 | ||
).flatMap { | ||
dao.addDID( | ||
did: testDID2, | ||
keyPairIndex: testKeyPairIndex2, | ||
alias: testAlias2 | ||
) | ||
} | ||
.flatMap { | ||
dao.getDIDInfo(did: testDID2) | ||
}.sink { _ in } receiveValue: { | ||
XCTAssertEqual(testDID2, $0?.did) | ||
XCTAssertEqual(testKeyPairIndex2, $0?.keyPairIndex) | ||
XCTAssertEqual(testAlias2, $0?.alias) | ||
expectation.fulfill() | ||
} | ||
|
||
waitForExpectations(timeout: 5) | ||
} | ||
|
||
func testGetDIDInfoByAlias() throws { | ||
let dao = CDRegisteredDIDDAO( | ||
readContext: coreDataManager.mainContext, | ||
writeContext: coreDataManager.editContext | ||
) | ||
|
||
let testDID1 = DID(method: "test1", methodId: "test1") | ||
let testKeyPairIndex1 = 0 | ||
let testAlias1 = "Test1" | ||
|
||
let testDID2 = DID(method: "test2", methodId: "test2") | ||
let testKeyPairIndex2 = 1 | ||
let testAlias2 = "Test2" | ||
|
||
let testDID3 = DID(method: "test3", methodId: "test3") | ||
let testKeyPairIndex3 = 2 | ||
let testAlias3 = "Test2" | ||
|
||
let testAlias = "Test2" | ||
|
||
let expectation = expectation(description: "Awaiting publisher") | ||
let cancellable = dao.addDID( | ||
did: testDID1, | ||
keyPairIndex: testKeyPairIndex1, | ||
alias: testAlias1 | ||
).flatMap { | ||
dao.addDID( | ||
did: testDID2, | ||
keyPairIndex: testKeyPairIndex2, | ||
alias: testAlias2 | ||
) | ||
} | ||
.flatMap { | ||
dao.addDID( | ||
did: testDID3, | ||
keyPairIndex: testKeyPairIndex3, | ||
alias: testAlias3 | ||
) | ||
} | ||
.flatMap { | ||
dao.getDIDInfo(alias: testAlias) | ||
}.sink { _ in } receiveValue: { | ||
XCTAssertEqual($0.count, 2) | ||
expectation.fulfill() | ||
} | ||
|
||
waitForExpectations(timeout: 5) | ||
} | ||
|
||
func testGetDIDInfoByKeyIndex() throws { | ||
let dao = CDRegisteredDIDDAO( | ||
readContext: coreDataManager.mainContext, | ||
writeContext: coreDataManager.editContext | ||
) | ||
|
||
let testDID1 = DID(method: "test1", methodId: "test1") | ||
let testKeyPairIndex1 = 0 | ||
let testAlias1 = "Test1" | ||
|
||
let testDID2 = DID(method: "test2", methodId: "test2") | ||
let testKeyPairIndex2 = 1 | ||
let testAlias2 = "Test2" | ||
|
||
let expectation = expectation(description: "Awaiting publisher") | ||
let cancellable = dao.addDID( | ||
did: testDID1, | ||
keyPairIndex: testKeyPairIndex1, | ||
alias: testAlias1 | ||
).flatMap { | ||
dao.addDID( | ||
did: testDID2, | ||
keyPairIndex: testKeyPairIndex2, | ||
alias: testAlias2 | ||
) | ||
} | ||
.flatMap { | ||
dao.getDIDInfo(keyPairIndex: testKeyPairIndex2) | ||
}.sink { _ in } receiveValue: { | ||
XCTAssertEqual(testDID2, $0?.did) | ||
XCTAssertEqual(testKeyPairIndex2, $0?.keyPairIndex) | ||
XCTAssertEqual(testAlias2, $0?.alias) | ||
expectation.fulfill() | ||
} | ||
|
||
waitForExpectations(timeout: 5) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.