|
| 1 | +// |
| 2 | +// DiskCacheSpecs.swift |
| 3 | +// MapCache_Tests |
| 4 | +// |
| 5 | +// Created by merlos on 06/02/2019. |
| 6 | +// Copyright © 2019 CocoaPods. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Quick |
| 10 | +import Nimble |
| 11 | +@testable import MapCache |
| 12 | + |
| 13 | +class DiskCacheSpecs: QuickSpec { |
| 14 | + override func spec() { |
| 15 | + |
| 16 | + /// Removes the cache folder |
| 17 | + func removeCache(cacheName: String) { |
| 18 | + let diskCache = DiskCache(withName: cacheName) |
| 19 | + do { |
| 20 | + try FileManager.default.removeItem(at: diskCache.folderURL) |
| 21 | + } catch { |
| 22 | + print ("ERROR removing DiskCache folder") |
| 23 | + } |
| 24 | + let exists = FileManager.default.fileExists(atPath: diskCache.path) |
| 25 | + expect(exists) == false |
| 26 | + } |
| 27 | + |
| 28 | + describe("DiskCache initialization") { |
| 29 | + it("can create the cache folder") { |
| 30 | + let diskCache = DiskCache(withName: "path") |
| 31 | + var isDirectory : ObjCBool = false |
| 32 | + //print(diskCache.folderURL) |
| 33 | + let exists = FileManager.default.fileExists(atPath: diskCache.path, isDirectory: &isDirectory) |
| 34 | + expect(exists) == true |
| 35 | + expect(isDirectory.boolValue) == true |
| 36 | + } |
| 37 | + |
| 38 | + afterEach() { |
| 39 | + let diskCache = DiskCache(withName: "path") |
| 40 | + do { |
| 41 | + try FileManager.default.removeItem(at: diskCache.folderURL) |
| 42 | + } catch { |
| 43 | + print ("ERROR removing DiskCache folder") |
| 44 | + } |
| 45 | + let exists = FileManager.default.fileExists(atPath: diskCache.path) |
| 46 | + expect(exists) == false |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + describe("a DiskCache") { |
| 51 | + |
| 52 | + let cacheName = "path2" |
| 53 | + var diskCache: DiskCache! |
| 54 | + let filename1 = "http://www." |
| 55 | + let data1 = "1234567890".data(using: .utf8) |
| 56 | + |
| 57 | + // Set a long file name |
| 58 | + var longFileName: String = "1234567890" |
| 59 | + |
| 60 | + for i in 0...NAME_MAX { |
| 61 | + longFileName.append(String(i)) |
| 62 | + } |
| 63 | + let dataLongFile = "1234567890".data(using: .utf8) |
| 64 | + |
| 65 | + beforeEach { |
| 66 | + diskCache = DiskCache(withName: cacheName) |
| 67 | + } |
| 68 | + |
| 69 | + afterEach { |
| 70 | + removeCache(cacheName: cacheName) |
| 71 | + } |
| 72 | + |
| 73 | + it("can add a file") { |
| 74 | + let data = data1 |
| 75 | + let filename = filename1 |
| 76 | + diskCache.setData(data!, forKey: filename) |
| 77 | + let filePath = diskCache.folderURL.appendingPathComponent(filename.toMD5()).path |
| 78 | + expect(FileManager.default.fileExists(atPath: filePath)).toEventually(equal(true)) |
| 79 | + } |
| 80 | + |
| 81 | + it("can add a file with a very long name") { |
| 82 | + let data = dataLongFile |
| 83 | + let filename = longFileName |
| 84 | + |
| 85 | + diskCache.setData(data!, forKey: filename) |
| 86 | + let filePath = diskCache.folderURL.appendingPathComponent(filename.MD5Filename()).path |
| 87 | + expect(FileManager.default.fileExists(atPath: filePath)).toEventually(equal(true)) |
| 88 | + } |
| 89 | + |
| 90 | + it("keeps track of its size") { |
| 91 | + let size = diskCache.calculateSize() |
| 92 | + expect(size) == 0 |
| 93 | + diskCache.setDataSync(data1!, forKey: filename1) |
| 94 | + expect(diskCache.size).toEventually(equal(10)) |
| 95 | + } |
| 96 | + |
| 97 | + it("can find a file that is in the cache") { |
| 98 | + diskCache.setData(data1!, forKey: filename1) |
| 99 | + expect(diskCache.size).toEventually(equal(10)) |
| 100 | + diskCache.fetchData(forKey: filename1, failure: {error in return}, success: { |
| 101 | + expect($0) == data1 |
| 102 | + }) |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + it("cannot find a file that is not in the cache") { |
| 107 | + diskCache.fetchData(forKey: "filename1", failure: { error in |
| 108 | + guard let error = error as NSError? else { |
| 109 | + return |
| 110 | + } |
| 111 | + expect(error.code).to(equal(NSFileReadNoSuchFileError)) |
| 112 | + return |
| 113 | + }, success: { |
| 114 | + expect($0) == data1 |
| 115 | + }) |
| 116 | + } |
| 117 | + |
| 118 | + //it("can handle weird names2") { |
| 119 | + // let diskCache2 = DiskCache(withName: "weird") |
| 120 | + // let weird1 = "ºª|!@#·$%&¬/()= ?'¿¡^`[]+*¨´{}ç;,.-<>€" |
| 121 | + // print("weird: \(diskCache2.path(forKey: weird1))") |
| 122 | + // diskCache2.setData(data1!, forKey: weird1) |
| 123 | + //} |
| 124 | + |
| 125 | + it("can handle weird names") { |
| 126 | + let weird1 = "ºª|!@#·$%&¬/()= ?'¿¡^`[]+*¨´{}ç;,.-<>€" |
| 127 | + //print("weird: \(diskCache.path(forKey: weird1))") |
| 128 | + expect(diskCache.size).toEventually(equal(0)) |
| 129 | + diskCache.setData(data1!, forKey: weird1) |
| 130 | + let filePath = diskCache.path(forKey: weird1) |
| 131 | + expect(FileManager.default.fileExists(atPath: filePath)).toEventually(equal(true)) |
| 132 | + expect(diskCache.size).toEventually(equal(10)) |
| 133 | + diskCache.fetchData(forKey: weird1, failure: { error in |
| 134 | + guard let error = error as NSError? else { |
| 135 | + expect(1) > 2 |
| 136 | + return |
| 137 | + } |
| 138 | + expect(error.code).toNot(equal(NSFileReadNoSuchFileError)) |
| 139 | + return |
| 140 | + }, success: { |
| 141 | + expect($0) == data1 |
| 142 | + }) |
| 143 | + } |
| 144 | + |
| 145 | + it("can remove the file from the cache") { |
| 146 | + // add the file |
| 147 | + diskCache.setData(data1!, forKey: filename1) |
| 148 | + expect(diskCache.size).toEventually(equal(10)) |
| 149 | + // remove the file |
| 150 | + diskCache.removeData(withKey: filename1) |
| 151 | + expect(diskCache.size).toEventually(equal(0)) |
| 152 | + diskCache.fetchData(forKey: filename1, failure: { error in |
| 153 | + guard let error = error as NSError? else { |
| 154 | + expect(1) > 2 |
| 155 | + return |
| 156 | + } |
| 157 | + expect(error.code).to(equal(NSFileReadNoSuchFileError)) |
| 158 | + return |
| 159 | + }, success: { |
| 160 | + // if the test goes right it should never come here |
| 161 | + expect($0.isEmpty) == true //make it fail |
| 162 | + }) |
| 163 | + } |
| 164 | + |
| 165 | + it("can remove all items from the cache") { |
| 166 | + diskCache.setData(data1!, forKey: filename1) |
| 167 | + diskCache.setData(dataLongFile!, forKey: longFileName) |
| 168 | + expect(diskCache.size).toEventually(equal(20)) |
| 169 | + diskCache.removeAllData({}) |
| 170 | + expect(diskCache.size).toEventually(equal(0)) |
| 171 | + } |
| 172 | + |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments