tests: add ndb support to tests

stops it from crashing
This commit is contained in:
William Casarin
2023-09-10 15:50:27 -07:00
parent 22d635d850
commit 69c7acea76
92 changed files with 193 additions and 131 deletions

View File

@@ -11,14 +11,15 @@ class Ndb {
let ndb: ndb_t
static var db_path: String {
(FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.absoluteString.replacingOccurrences(of: "file://", with: ""))!
let path = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.absoluteString
return remove_file_prefix(path!)
}
static var empty: Ndb {
Ndb(ndb: ndb_t(ndb: nil))
}
init?() {
init?(path: String? = nil) {
//try? FileManager.default.removeItem(atPath: Ndb.db_path + "/lock.mdb")
//try? FileManager.default.removeItem(atPath: Ndb.db_path + "/data.mdb")
@@ -27,7 +28,9 @@ class Ndb {
let ingest_threads: Int32 = 4
var mapsize: Int = 1024 * 1024 * 1024 * 32
let ok = Ndb.db_path.withCString { testdir in
let path = path.map(remove_file_prefix) ?? Ndb.db_path
let ok = path.withCString { testdir in
var ok = false
while !ok && mapsize > 1024 * 1024 * 700 {
ok = ndb_init(&ndb_p, testdir, mapsize, ingest_threads) != 0
@@ -199,3 +202,8 @@ func getDebugCheckedRoot<T: FlatBufferObject>(byteBuffer: inout ByteBuffer) thro
return getRoot(byteBuffer: &byteBuffer)
}
#endif
func remove_file_prefix(_ str: String) -> String {
return str.replacingOccurrences(of: "file://", with: "")
}

View File

@@ -8,12 +8,26 @@
import XCTest
@testable import damus
func test_ndb_dir() -> String? {
do {
let fileManager = FileManager.default
let tempDir = fileManager.temporaryDirectory.appendingPathComponent(UUID().uuidString)
try fileManager.createDirectory(at: tempDir, withIntermediateDirectories: true, attributes: nil)
return remove_file_prefix(tempDir.absoluteString)
} catch {
return nil
}
}
final class NdbTests: XCTestCase {
var db_dir: String = ""
override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
try? FileManager.default.removeItem(atPath: Ndb.db_path + "/lock.mdb")
try? FileManager.default.removeItem(atPath: Ndb.db_path + "/data.mdb")
guard let db = test_ndb_dir() else {
XCTFail("Could not create temp directory")
return
}
db_dir = db
}
override func tearDownWithError() throws {
@@ -41,26 +55,27 @@ final class NdbTests: XCTestCase {
func test_ndb_init() {
do {
let ndb = Ndb()!
let ndb = Ndb(path: db_dir)!
let ok = ndb.process_events(test_wire_events)
XCTAssertTrue(ok)
}
do {
let ndb = Ndb()!
let ndb = Ndb(path: db_dir)!
let id = NoteId(hex: "d12c17bde3094ad32f4ab862a6cc6f5c289cfe7d5802270bdf34904df585f349")!
let note = ndb.lookup_note(id)
let txn = NdbTxn(ndb: ndb)
let note = ndb.lookup_note_with_txn(id: id, txn: txn)
XCTAssertNotNil(note)
guard let note else { return }
let pk = Pubkey(hex: "32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245")!
XCTAssertEqual(note.pubkey, pk)
let profile = ndb.lookup_profile(pk)
let profile = ndb.lookup_profile_with_txn(pk, txn: txn)
XCTAssertNotNil(profile)
guard let profile else { return }
XCTAssertEqual(profile.profile?.name, "jb55")
XCTAssertEqual(profile.lnurl, "fixme")
XCTAssertEqual(profile.lnurl, nil)
}