damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

ProfileDatabaseTests.swift (4707B)


      1 //
      2 //  ProfileDatabaseTests.swift
      3 //  damusTests
      4 //
      5 //  Created by Bryan Montz on 5/13/23.
      6 //
      7 
      8 import XCTest
      9 @testable import damus
     10 
     11 class ProfileDatabaseTests: XCTestCase {
     12     
     13     static let cache_url = (FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first?.appendingPathComponent("test-profiles"))!
     14     let database = ProfileDatabase(cache_url: ProfileDatabaseTests.cache_url)
     15     
     16     override func tearDownWithError() throws {
     17         // This method is called after the invocation of each test method in the class.
     18         try database.remove_all_profiles()
     19     }
     20     
     21     var test_profile: Profile {
     22         Profile(name: "test-name",
     23                 display_name: "test-display-name",
     24                 about: "test-about",
     25                 picture: "test-picture",
     26                 banner: "test-banner",
     27                 website: "test-website",
     28                 lud06: "test-lud06",
     29                 lud16: "test-lud16",
     30                 nip05: "test-nip05",
     31                 damus_donation: 100)
     32     }
     33 
     34     func testStoreAndRetrieveProfile() async throws {
     35         let id = test_pubkey
     36 
     37         let profile = test_profile
     38         
     39         // make sure it's not there yet
     40         XCTAssertNil(database.get(id: id))
     41         
     42         // store the profile
     43         try await database.upsert(id: id, profile: profile, last_update: .now)
     44         
     45         // read the profile out of the database
     46         let retrievedProfile = try XCTUnwrap(database.get(id: id))
     47         
     48         XCTAssertEqual(profile.name, retrievedProfile.name)
     49         XCTAssertEqual(profile.display_name, retrievedProfile.display_name)
     50         XCTAssertEqual(profile.about, retrievedProfile.about)
     51         XCTAssertEqual(profile.picture, retrievedProfile.picture)
     52         XCTAssertEqual(profile.banner, retrievedProfile.banner)
     53         XCTAssertEqual(profile.website, retrievedProfile.website)
     54         XCTAssertEqual(profile.lud06, retrievedProfile.lud06)
     55         XCTAssertEqual(profile.lud16, retrievedProfile.lud16)
     56         XCTAssertEqual(profile.nip05, retrievedProfile.nip05)
     57         XCTAssertEqual(profile.damus_donation, retrievedProfile.damus_donation)
     58     }
     59     
     60     func testRejectOutdatedProfile() async throws {
     61         let id = test_pubkey
     62         
     63         // store a profile
     64         let profile = test_profile
     65         let profile_last_updated = Date.now
     66         try await database.upsert(id: id, profile: profile, last_update: profile_last_updated)
     67         
     68         // try to store a profile with the same id but the last_update date is older than the previously stored profile
     69         let outdatedProfile = test_profile
     70         let outdated_last_updated = profile_last_updated.addingTimeInterval(-60)
     71         
     72         do {
     73             try await database.upsert(id: id, profile: outdatedProfile, last_update: outdated_last_updated)
     74             XCTFail("expected to throw error")
     75         } catch let error as ProfileDatabaseError {
     76             XCTAssertEqual(error, ProfileDatabaseError.outdated_input)
     77         } catch {
     78             XCTFail("not the expected error")
     79         }
     80     }
     81     
     82     func testUpdateExistingProfile() async throws {
     83         let id = test_pubkey
     84 
     85         // store a profile
     86         let profile = test_profile
     87         let profile_last_update = Date.now
     88         try await database.upsert(id: id, profile: profile, last_update: profile_last_update)
     89         
     90         // update the same profile
     91         let updated_profile = test_profile
     92         updated_profile.nip05 = "updated-nip05"
     93         let updated_profile_last_update = profile_last_update.addingTimeInterval(60)
     94         try await database.upsert(id: id, profile: updated_profile, last_update: updated_profile_last_update)
     95         
     96         // retrieve the profile and make sure it was updated
     97         let retrieved_profile = database.get(id: id)
     98         XCTAssertEqual(retrieved_profile?.nip05, "updated-nip05")
     99     }
    100     
    101     func testStoreMultipleAndRemoveAllProfiles() async throws {
    102         XCTAssertEqual(database.count, 0)
    103         
    104         // store a profile
    105         let id = test_pubkey
    106         let profile = test_profile
    107         let profile_last_update = Date.now
    108         try await database.upsert(id: id, profile: profile, last_update: profile_last_update)
    109         
    110         XCTAssertEqual(database.count, 1)
    111         
    112         // store another profile
    113         let id2 = test_pubkey_2
    114         let profile2 = test_profile
    115         let profile_last_update2 = Date.now
    116         try await database.upsert(id: id2, profile: profile2, last_update: profile_last_update2)
    117         
    118         XCTAssertEqual(database.count, 2)
    119         
    120         try database.remove_all_profiles()
    121         
    122         XCTAssertEqual(database.count, 0)
    123     }
    124 }