parent
7336258392
commit
411021a184
@ -0,0 +1,71 @@ |
||||
// |
||||
// CachedERC1155ContractDictionary.swift |
||||
// AlphaWallet |
||||
// |
||||
// Created by Jerome Chan on 12/4/22. |
||||
// |
||||
|
||||
import Foundation |
||||
|
||||
class CachedERC1155ContractDictionary { |
||||
private let fileUrl: URL |
||||
private var baseDictionary: [AlphaWallet.Address: Bool] = [AlphaWallet.Address: Bool]() |
||||
private var encoder: JSONEncoder |
||||
|
||||
init?(fileName: String) { |
||||
do { |
||||
var url: URL = try FileManager.default.url(for: .cachesDirectory, in: .userDomainMask, appropriateFor: nil, create: true) |
||||
url.appendPathComponent(fileName) |
||||
self.fileUrl = url |
||||
self.encoder = JSONEncoder() |
||||
if FileManager.default.fileExists(atPath: url.path) { |
||||
readFromFileUrl() |
||||
} |
||||
} catch { |
||||
return nil |
||||
} |
||||
} |
||||
|
||||
func isERC1155Contract(for address: AlphaWallet.Address) -> Bool? { |
||||
return baseDictionary[address] |
||||
} |
||||
|
||||
func setContract(for address: AlphaWallet.Address, _ result: Bool) { |
||||
baseDictionary[address] = result |
||||
writeToFileUrl() |
||||
} |
||||
|
||||
func remove() { |
||||
do { |
||||
try FileManager.default.removeItem(at: fileUrl) |
||||
} catch { |
||||
// Do nothing |
||||
verboseLog("CachedERC1155ContractDictionary::remove Exception: \(error)") |
||||
} |
||||
} |
||||
|
||||
private func writeToFileUrl() { |
||||
do { |
||||
let data = try encoder.encode(baseDictionary) |
||||
if let jsonString = String(data: data, encoding: .utf8) { |
||||
try jsonString.write(to: fileUrl, atomically: true, encoding: .utf8) |
||||
} |
||||
} catch { |
||||
// Do nothing |
||||
verboseLog("[CachedERC1155ContractDictionary] writeToFileUrl error: \(error)") |
||||
} |
||||
} |
||||
|
||||
private func readFromFileUrl() { |
||||
do { |
||||
let decoder = JSONDecoder() |
||||
let data = try Data(contentsOf: fileUrl) |
||||
let jsonData = try decoder.decode([AlphaWallet.Address: Bool].self, from: data) |
||||
baseDictionary = jsonData |
||||
} catch { |
||||
verboseLog("[CachedERC1155ContractDictionary] readFromFileUrl error: \(error)") |
||||
baseDictionary = [AlphaWallet.Address: Bool]() |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,73 @@ |
||||
// |
||||
// CachedERC1155ContractDictionaryTestCase.swift |
||||
// AlphaWalletTests |
||||
// |
||||
// Created by Jerome Chan on 13/4/22. |
||||
// |
||||
|
||||
@testable import AlphaWallet |
||||
import AlphaWalletAddress |
||||
import PromiseKit |
||||
import XCTest |
||||
|
||||
class CachedERC1155ContractDictionaryTestCase: XCTestCase { |
||||
|
||||
private enum FileNames: String, CaseIterable { |
||||
case hit = "testCacheHit.json" |
||||
case miss = "testCacheMiss.json" |
||||
case persists = "testCachePersists.json" |
||||
} |
||||
|
||||
private let address1: AlphaWallet.Address = AlphaWallet.Address(string: "0xbbce83173d5c1d122ae64856b4af0d5ae07fa362")! |
||||
private let address2: AlphaWallet.Address = AlphaWallet.Address(string: "0x829BD824B016326A401d083B33D092293333A830")! |
||||
private let address3: AlphaWallet.Address = AlphaWallet.Address(string: "0xbDd147D953c400318bac7316519885688C3C9e07")! |
||||
|
||||
override func setUpWithError() throws { |
||||
// Put setup code here. This method is called before the invocation of each test method in the class. |
||||
try FileNames.allCases.forEach { |
||||
let url = try cacheUrlFor(fileName: $0.rawValue) |
||||
if FileManager.default.fileExists(atPath: url.path) && FileManager.default.isDeletableFile(atPath: url.path) { |
||||
try FileManager.default.removeItem(at: url) |
||||
} |
||||
} |
||||
} |
||||
|
||||
override func tearDownWithError() throws { |
||||
// Put teardown code here. This method is called after the invocation of each test method in the class. |
||||
} |
||||
|
||||
func testCacheHit() throws { |
||||
let cache = CachedERC1155ContractDictionary(fileName: FileNames.hit.rawValue)! |
||||
cache.setContract(for: address1, true) |
||||
XCTAssertTrue(cache.isERC1155Contract(for: address1)!) |
||||
cache.setContract(for: address1, false) |
||||
XCTAssertFalse(cache.isERC1155Contract(for: address1)!) |
||||
cache.setContract(for: address2, true) |
||||
XCTAssertTrue(cache.isERC1155Contract(for: address2)!) |
||||
cache.setContract(for: address2, false) |
||||
XCTAssertFalse(cache.isERC1155Contract(for: address2)!) |
||||
cache.remove() |
||||
} |
||||
|
||||
func testCacheMiss() throws { |
||||
let cache = CachedERC1155ContractDictionary(fileName: FileNames.miss.rawValue)! |
||||
XCTAssertNil(cache.isERC1155Contract(for: address1)) |
||||
cache.remove() |
||||
} |
||||
|
||||
func testCacheFilePersists() throws { |
||||
let cache1 = CachedERC1155ContractDictionary(fileName: FileNames.persists.rawValue)! |
||||
cache1.setContract(for: address1, true) |
||||
cache1.setContract(for: address2, false) |
||||
let url = try cacheUrlFor(fileName: FileNames.persists.rawValue) |
||||
let result = FileManager.default.fileExists(atPath: url.path) |
||||
XCTAssertTrue(result) |
||||
let cache2 = CachedERC1155ContractDictionary(fileName: FileNames.persists.rawValue)! |
||||
XCTAssertTrue(cache2.isERC1155Contract(for: address1)!) |
||||
XCTAssertFalse(cache2.isERC1155Contract(for: address2)!) |
||||
XCTAssertNil(cache2.isERC1155Contract(for: address3)) |
||||
cache1.remove() |
||||
cache2.remove() |
||||
} |
||||
|
||||
} |
@ -0,0 +1,49 @@ |
||||
// |
||||
// GetIsERC1155ContractCoordinatorTestCase.swift |
||||
// AlphaWalletTests |
||||
// |
||||
// Created by Jerome Chan on 14/4/22. |
||||
// |
||||
|
||||
@testable import AlphaWallet |
||||
import AlphaWalletAddress |
||||
import PromiseKit |
||||
import XCTest |
||||
|
||||
class GetIsERC1155ContractCoordinatorTestCase: XCTestCase { |
||||
|
||||
private let fileName = "test-cache.json" |
||||
private let address1: AlphaWallet.Address = AlphaWallet.Address(string: "0xbbce83173d5c1d122ae64856b4af0d5ae07fa362")! |
||||
private var result: Bool? |
||||
|
||||
override func setUpWithError() throws { |
||||
let url = try cacheUrlFor(fileName: fileName) |
||||
if FileManager.default.fileExists(atPath: url.path) && FileManager.default.isDeletableFile(atPath: url.path) { |
||||
try FileManager.default.removeItem(at: url) |
||||
} |
||||
} |
||||
|
||||
override func tearDownWithError() throws { |
||||
// Put teardown code here. This method is called after the invocation of each test method in the class. |
||||
} |
||||
|
||||
func testgetIsERC1155Contract() throws { |
||||
let coordinator = GetIsERC1155ContractCoordinator(forServer: .main, cacheName: fileName) |
||||
let expectation = expectation(description: "Waiting for server response") |
||||
coordinator.getIsERC1155Contract(for: address1) |
||||
.done { returnValue in |
||||
self.result = returnValue |
||||
expectation.fulfill() |
||||
} |
||||
.cauterize() |
||||
wait(for: [expectation], timeout: 10 * 60) |
||||
let cache = CachedERC1155ContractDictionary(fileName: fileName) |
||||
XCTAssertNotNil(cache) |
||||
XCTAssertNotNil(result) |
||||
let cachedResult = cache!.isERC1155Contract(for: address1) |
||||
XCTAssertNotNil(cachedResult) |
||||
XCTAssertEqual(cachedResult!, result!) |
||||
cache!.remove() |
||||
} |
||||
|
||||
} |
@ -0,0 +1,14 @@ |
||||
// |
||||
// urlUtilities.swift |
||||
// AlphaWalletTests |
||||
// |
||||
// Created by Jerome Chan on 14/4/22. |
||||
// |
||||
|
||||
import Foundation |
||||
|
||||
func cacheUrlFor(fileName: String) throws -> URL { |
||||
var url: URL = try FileManager.default.url(for: .cachesDirectory, in: .userDomainMask, appropriateFor: nil, create: true) |
||||
url.appendPathComponent(fileName) |
||||
return url |
||||
} |
Loading…
Reference in new issue