commit afe3dcf039b1e0e76f3fe8c97d553c92146b26a4
parent 091a8ae090a356b60fd6cbd0f4a18dc91d37ed07
Author: William Casarin <jb55@jb55.com>
Date: Wed, 10 Jan 2024 15:38:21 -0800
test: switch test to use failable transactions
Diffstat:
3 files changed, 12 insertions(+), 13 deletions(-)
diff --git a/damusTests/Mocking/MockProfiles.swift b/damusTests/Mocking/MockProfiles.swift
@@ -20,7 +20,7 @@ class MockProfiles: Profiles {
super.init(ndb: ndb)
}
- override func lookup(id: Pubkey) -> NdbTxn<Profile?> {
+ override func lookup(id: Pubkey) -> NdbTxn<Profile?>? {
return NdbTxn(ndb: self.ndb) { txn in
return self.mocked_profiles[id]
}
diff --git a/damusTests/ProfileViewTests.swift b/damusTests/ProfileViewTests.swift
@@ -28,19 +28,18 @@ final class ProfileViewTests: XCTestCase {
let pk5 = Pubkey(hex: "f2aa579bb998627e04a8f553842a09446360c9d708c6141dd119c479f6ab9d29")!
let ndb = Ndb(path: Ndb.db_path)!
- let txn = NdbTxn(ndb: ndb)
let damus_name = "17ldvg64:nq5mhr77"
- XCTAssertEqual(followedByString(txn: txn, [pk1], ndb: ndb, locale: enUsLocale), "Followed by \(damus_name)")
- XCTAssertEqual(followedByString(txn: txn, [pk1, pk2], ndb: ndb, locale: enUsLocale), "Followed by \(damus_name) & 1rppft3m:4qxhsgnj")
- XCTAssertEqual(followedByString(txn: txn, [pk1, pk2, pk3], ndb: ndb, locale: enUsLocale), "Followed by \(damus_name), 1rppft3m:4qxhsgnj & 1kshyfd2:cq04aze0")
- XCTAssertEqual(followedByString(txn: txn, [pk1, pk2, pk3, pk4,], ndb: ndb, locale: enUsLocale), "Followed by \(damus_name), 1rppft3m:4qxhsgnj, 1kshyfd2:cq04aze0 & 1 other")
- XCTAssertEqual(followedByString(txn: txn, [pk1, pk2, pk3, pk4, pk5], ndb: ndb, locale: enUsLocale), "Followed by \(damus_name), 1rppft3m:4qxhsgnj, 1kshyfd2:cq04aze0 & 2 others")
+ XCTAssertEqual(followedByString([pk1], ndb: ndb, locale: enUsLocale), "Followed by \(damus_name)")
+ XCTAssertEqual(followedByString([pk1, pk2], ndb: ndb, locale: enUsLocale), "Followed by \(damus_name) & 1rppft3m:4qxhsgnj")
+ XCTAssertEqual(followedByString([pk1, pk2, pk3], ndb: ndb, locale: enUsLocale), "Followed by \(damus_name), 1rppft3m:4qxhsgnj & 1kshyfd2:cq04aze0")
+ XCTAssertEqual(followedByString([pk1, pk2, pk3, pk4,], ndb: ndb, locale: enUsLocale), "Followed by \(damus_name), 1rppft3m:4qxhsgnj, 1kshyfd2:cq04aze0 & 1 other")
+ XCTAssertEqual(followedByString([pk1, pk2, pk3, pk4, pk5], ndb: ndb, locale: enUsLocale), "Followed by \(damus_name), 1rppft3m:4qxhsgnj, 1kshyfd2:cq04aze0 & 2 others")
let pubkeys = [pk1, pk2, pk3, pk4, pk5, pk1, pk2, pk3, pk4, pk5]
Bundle.main.localizations.map { Locale(identifier: $0) }.forEach {
for count in 1...10 {
- XCTAssertNoThrow(followedByString(txn: txn, pubkeys.prefix(count).map { $0 }, ndb: ndb, locale: $0))
+ XCTAssertNoThrow(followedByString(pubkeys.prefix(count).map { $0 }, ndb: ndb, locale: $0))
}
}
}
diff --git a/nostrdb/Test/NdbTests.swift b/nostrdb/Test/NdbTests.swift
@@ -63,7 +63,7 @@ final class NdbTests: XCTestCase {
do {
let ndb = Ndb(path: db_dir)!
let id = NoteId(hex: "d12c17bde3094ad32f4ab862a6cc6f5c289cfe7d5802270bdf34904df585f349")!
- let txn = NdbTxn(ndb: ndb)
+ guard let txn = NdbTxn(ndb: ndb) else { return XCTAssert(false) }
let note = ndb.lookup_note_with_txn(id: id, txn: txn)
XCTAssertNotNil(note)
guard let note else { return }
@@ -93,7 +93,7 @@ final class NdbTests: XCTestCase {
let note_ids = ndb.text_search(query: "barked")
XCTAssertEqual(note_ids.count, 1)
let expected_note_id = NoteId(hex: "b17a540710fe8495b16bfbaf31c6962c4ba8387f3284a7973ad523988095417e")!
- let note_id = ndb.lookup_note_by_key(note_ids[0]).map({ n in n?.id }).value
+ let note_id = ndb.lookup_note_by_key(note_ids[0])?.map({ n in n?.id }).value
XCTAssertEqual(note_id, .some(expected_note_id))
}
}
@@ -159,13 +159,13 @@ final class NdbTests: XCTestCase {
func test_inherited_transactions() throws {
let ndb = Ndb(path: db_dir)!
do {
- let txn1 = NdbTxn(ndb: ndb)
-
+ guard let txn1 = NdbTxn(ndb: ndb) else { return XCTAssert(false) }
+
let ntxn = (Thread.current.threadDictionary.value(forKey: "ndb_txn") as? ndb_txn)!
XCTAssertEqual(txn1.txn.lmdb, ntxn.lmdb)
XCTAssertEqual(txn1.txn.mdb_txn, ntxn.mdb_txn)
- let txn2 = NdbTxn(ndb: ndb)
+ guard let txn2 = NdbTxn(ndb: ndb) else { return XCTAssert(false) }
XCTAssertEqual(txn1.inherited, false)
XCTAssertEqual(txn2.inherited, true)