Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: strict cache + storages #13

Merged
merged 9 commits into from
Nov 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 57 additions & 45 deletions Pod/CacheTests.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

67 changes: 0 additions & 67 deletions Pod/Tests/Specs/Cache/CacheFactorySpec.swift

This file was deleted.

19 changes: 9 additions & 10 deletions Pod/Tests/Specs/CacheSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@ class CacheSpec: QuickSpec {
let defaultConfig = Config.defaultConfig

expect(cache.config.frontKind.name).to(equal(defaultConfig.frontKind.name))
expect(cache.config.backKind!.name).to(equal(defaultConfig.backKind!.name))
expect(cache.config.backKind.name).to(equal(defaultConfig.backKind.name))
expect(cache.config.expiry.date).to(equal(defaultConfig.expiry.date))
expect(cache.config.maxSize).to(equal(defaultConfig.maxSize))
}

it("sets the front cache as a memory cache") {
expect(cache.frontCache.self is MemoryCache).to(beTrue())
expect(cache.backCache).toNot(beNil())
expect(cache.backCache!.self is DiskCache).to(beTrue())
expect(cache.frontStorage.self is MemoryStorage).to(beTrue())
expect(cache.backStorage.self is DiskStorage).to(beTrue())
}
}

Expand All @@ -52,12 +51,12 @@ class CacheSpec: QuickSpec {
expectation1.fulfill()
}

cache.frontCache.object(key) { (receivedObject: User?) in
cache.frontStorage.object(key) { (receivedObject: User?) in
expect(receivedObject).toNot(beNil())
expectation2.fulfill()
}

cache.backCache?.object(key) { (receivedObject: User?) in
cache.backStorage.object(key) { (receivedObject: User?) in
expect(receivedObject).toNot(beNil())
expectation3.fulfill()
}
Expand Down Expand Up @@ -97,12 +96,12 @@ class CacheSpec: QuickSpec {
expectation1.fulfill()
}

cache.frontCache.object(key) { (receivedObject: User?) in
cache.frontStorage.object(key) { (receivedObject: User?) in
expect(receivedObject).to(beNil())
expectation2.fulfill()
}

cache.backCache?.object(key) { (receivedObject: User?) in
cache.backStorage.object(key) { (receivedObject: User?) in
expect(receivedObject).to(beNil())
expectation3.fulfill()
}
Expand All @@ -126,12 +125,12 @@ class CacheSpec: QuickSpec {
expectation1.fulfill()
}

cache.frontCache.object(key) { (receivedObject: User?) in
cache.frontStorage.object(key) { (receivedObject: User?) in
expect(receivedObject).to(beNil())
expectation2.fulfill()
}

cache.backCache?.object(key) { (receivedObject: User?) in
cache.backStorage.object(key) { (receivedObject: User?) in
expect(receivedObject).to(beNil())
expectation3.fulfill()
}
Expand Down
4 changes: 2 additions & 2 deletions Pod/Tests/Specs/ConfigSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class ConfigSpec: QuickSpec {
it("returns the correct default config") {
let config = Config.defaultConfig

expect(config.frontKind.name).to(equal(CacheKind.Memory.name))
expect(config.backKind!.name).to(equal(CacheKind.Disk.name))
expect(config.frontKind.name).to(equal(StorageKind.Memory.name))
expect(config.backKind.name).to(equal(StorageKind.Disk.name))
expect(config.expiry.date).to(equal(Expiry.Never.date))
expect(config.maxSize).to(equal(0))
}
Expand Down
21 changes: 0 additions & 21 deletions Pod/Tests/Specs/DataStructures/CacheKindSpec.swift

This file was deleted.

21 changes: 21 additions & 0 deletions Pod/Tests/Specs/DataStructures/StorageKindSpec.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Quick
import Nimble

class StorageKindSpec: QuickSpec {

override func spec() {
describe("StorageKind") {

describe("#name") {
it("returns the correct name for default values") {
expect(StorageKind.Memory.name).to(equal("Memory"))
expect(StorageKind.Disk.name).to(equal("Disk"))
}

it("returns the correct name for custom values") {
expect(StorageKind.Custom("Weirdo").name).to(equal("Weirdo"))
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
import Quick
import Nimble

class DiskCacheSpec: QuickSpec {
class DiskStorageSpec: QuickSpec {

override func spec() {
describe("DiskCache") {
let name = "DudeDiskCache"
describe("DiskStorage") {
let name = "Floppy"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HA! 😁

let key = "youknownothing"
let object = User(firstName: "John", lastName: "Snow")
var cache: DiskCache!
var storage: DiskStorage!
let fileManager = NSFileManager()

beforeEach {
cache = DiskCache(name: name)
storage = DiskStorage(name: name)
}

afterEach {
do {
try fileManager.removeItemAtPath(cache.path)
try fileManager.removeItemAtPath(storage.path)
} catch {}
}

describe("#path") {
it("returns the correct path") {
let paths = NSSearchPathForDirectoriesInDomains(.CachesDirectory,
NSSearchPathDomainMask.UserDomainMask, true)
let path = "\(paths.first!)/\(DiskCache.prefix).\(name.capitalizedString)"
let path = "\(paths.first!)/\(DiskStorage.prefix).\(name.capitalizedString)"

expect(cache.path).to(equal(path))
expect(storage.path).to(equal(path))
}
}

describe("#maxSize") {
it("returns the default maximum size of a cache") {
expect(cache.maxSize).to(equal(0))
expect(storage.maxSize).to(equal(0))
}
}

Expand All @@ -42,8 +42,8 @@ class DiskCacheSpec: QuickSpec {
let expectation = self.expectationWithDescription(
"Create Cache Directory Expectation")

cache.add(key, object: object) {
let fileExist = fileManager.fileExistsAtPath(cache.path)
storage.add(key, object: object) {
let fileExist = fileManager.fileExistsAtPath(storage.path)
expect(fileExist).to(beTrue())
expectation.fulfill()
}
Expand All @@ -54,8 +54,8 @@ class DiskCacheSpec: QuickSpec {
it("saves an object") {
let expectation = self.expectationWithDescription("Save Expectation")

cache.add(key, object: object) {
let fileExist = fileManager.fileExistsAtPath(cache.filePath(key))
storage.add(key, object: object) {
let fileExist = fileManager.fileExistsAtPath(storage.filePath(key))
expect(fileExist).to(beTrue())
expectation.fulfill()
}
Expand All @@ -68,8 +68,8 @@ class DiskCacheSpec: QuickSpec {
it("resolves cached object") {
let expectation = self.expectationWithDescription("Object Expectation")

cache.add(key, object: object) {
cache.object(key) { (receivedObject: User?) in
storage.add(key, object: object) {
storage.object(key) { (receivedObject: User?) in
expect(receivedObject?.firstName).to(equal(object.firstName))
expect(receivedObject?.lastName).to(equal(object.lastName))
expectation.fulfill()
Expand All @@ -84,9 +84,9 @@ class DiskCacheSpec: QuickSpec {
it("removes cached object") {
let expectation = self.expectationWithDescription("Remove Expectation")

cache.add(key, object: object)
cache.remove(key) {
let fileExist = fileManager.fileExistsAtPath(cache.filePath(key))
storage.add(key, object: object)
storage.remove(key) {
let fileExist = fileManager.fileExistsAtPath(storage.filePath(key))
expect(fileExist).to(beFalse())
expectation.fulfill()
}
Expand All @@ -101,9 +101,9 @@ class DiskCacheSpec: QuickSpec {
"Remove If Expired Expectation")
let expiry: Expiry = .Date(NSDate().dateByAddingTimeInterval(-100000))

cache.add(key, object: object, expiry: expiry)
cache.removeIfExpired(key) {
cache.object(key) { (receivedObject: User?) in
storage.add(key, object: object, expiry: expiry)
storage.removeIfExpired(key) {
storage.object(key) { (receivedObject: User?) in
expect(receivedObject).to(beNil())
expectation.fulfill()
}
Expand All @@ -116,9 +116,9 @@ class DiskCacheSpec: QuickSpec {
let expectation = self.expectationWithDescription(
"Don't Remove If Not Expired Expectation")

cache.add(key, object: object)
cache.removeIfExpired(key) {
cache.object(key) { (receivedObject: User?) in
storage.add(key, object: object)
storage.removeIfExpired(key) {
storage.object(key) { (receivedObject: User?) in
expect(receivedObject).notTo(beNil())
expectation.fulfill()
}
Expand All @@ -132,9 +132,9 @@ class DiskCacheSpec: QuickSpec {
it("clears cache directory") {
let expectation = self.expectationWithDescription("Clear Expectation")

cache.add(key, object: object)
cache.clear() {
let fileExist = fileManager.fileExistsAtPath(cache.path)
storage.add(key, object: object)
storage.clear() {
let fileExist = fileManager.fileExistsAtPath(storage.path)
expect(fileExist).to(beFalse())
expectation.fulfill()
}
Expand All @@ -145,14 +145,14 @@ class DiskCacheSpec: QuickSpec {

describe("#fileName") {
it("returns a correct file name") {
expect(cache.fileName(key)).to(equal(key.base64()))
expect(storage.fileName(key)).to(equal(key.base64()))
}
}

describe("#filePath") {
it("returns a correct file path") {
let filePath = "\(cache.path)/\(cache.fileName(key))"
expect(cache.filePath(key)).to(equal(filePath))
let filePath = "\(storage.path)/\(storage.fileName(key))"
expect(storage.filePath(key)).to(equal(filePath))
}
}
}
Expand Down
Loading