mirror of
https://github.com/hyperoslo/Cache.git
synced 2026-04-07 19:17:36 +00:00
Add MemoryStorageTests
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
BDEDD37D1DBCEB8A007416A6 /* Cache.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BDEDD3561DBCE5B1007416A6 /* Cache.framework */; };
|
||||
D21AD6141F681128003A8172 /* CapsuleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D59A19AF1EE8246F009F6AEE /* CapsuleTests.swift */; };
|
||||
D21AD6161F68112E003A8172 /* ExpiryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D59A19B11EE8246F009F6AEE /* ExpiryTests.swift */; };
|
||||
D21AD6181F6811E9003A8172 /* MemoryStorageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D21AD6171F6811E9003A8172 /* MemoryStorageTests.swift */; };
|
||||
D28C9B6C1F67D4D500C180C1 /* Expiry.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5291C191C28220B00B702C9 /* Expiry.swift */; };
|
||||
D28C9B6D1F67D4D800C180C1 /* Capsule.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5291C181C28220B00B702C9 /* Capsule.swift */; };
|
||||
D28C9B6E1F67D4DE00C180C1 /* Date+Cache.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5291C1F1C28220B00B702C9 /* Date+Cache.swift */; };
|
||||
@@ -73,6 +74,7 @@
|
||||
BDEDD3561DBCE5B1007416A6 /* Cache.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Cache.framework; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
BDEDD3781DBCEB8A007416A6 /* Cache-tvOS-Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "Cache-tvOS-Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
BDF64BC61F54764700134105 /* ExpirationMode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExpirationMode.swift; sourceTree = "<group>"; };
|
||||
D21AD6171F6811E9003A8172 /* MemoryStorageTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemoryStorageTests.swift; sourceTree = "<group>"; };
|
||||
D28C9B7F1F67E38C00C180C1 /* CacheError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CacheError.swift; sourceTree = "<group>"; };
|
||||
D28C9B801F67E38C00C180C1 /* DataSerializer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DataSerializer.swift; sourceTree = "<group>"; };
|
||||
D28C9B811F67E38C00C180C1 /* DiskConfig.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DiskConfig.swift; sourceTree = "<group>"; };
|
||||
@@ -233,6 +235,7 @@
|
||||
children = (
|
||||
D28C9BA61F67E3D400C180C1 /* ImageWrapperTests.swift */,
|
||||
D28C9BA81F67E3E000C180C1 /* PrimitiveWrapperTests.swift */,
|
||||
D21AD6171F6811E9003A8172 /* MemoryStorageTests.swift */,
|
||||
);
|
||||
path = Storage;
|
||||
sourceTree = "<group>";
|
||||
@@ -813,6 +816,7 @@
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
D21AD6161F68112E003A8172 /* ExpiryTests.swift in Sources */,
|
||||
D21AD6181F6811E9003A8172 /* MemoryStorageTests.swift in Sources */,
|
||||
D28C9BAD1F67ED0000C180C1 /* User.swift in Sources */,
|
||||
D28C9BAE1F67ED5500C180C1 /* TestHelper.swift in Sources */,
|
||||
D28C9BA71F67E3D400C180C1 /* ImageWrapperTests.swift in Sources */,
|
||||
|
||||
@@ -55,7 +55,7 @@ extension MemoryStorage: StorageAware {
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate extension MemoryStorage {
|
||||
extension MemoryStorage {
|
||||
/**
|
||||
Removes the object from the cache if it's expired.
|
||||
- Parameter key: Unique key to identify the object in the cache
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
import XCTest
|
||||
@testable import Cache
|
||||
|
||||
final class MemoryStorageTests: XCTestCase {
|
||||
private let key = "youknownothing"
|
||||
private let object = TestHelper.user
|
||||
private var storage: MemoryStorage!
|
||||
|
||||
override func setUp() {
|
||||
super.setUp()
|
||||
let config = MemoryConfig(expiry: .never, countLimit: 10, totalCostLimit: 10)
|
||||
storage = MemoryStorage(config: config)
|
||||
}
|
||||
|
||||
override func tearDown() {
|
||||
storage.removeAll()
|
||||
super.tearDown()
|
||||
}
|
||||
|
||||
/// Test that it saves an object
|
||||
func testAddObject() {
|
||||
storage.setObject(object, forKey: key)
|
||||
let cachedObject: User = try! storage.object(forKey: key)
|
||||
XCTAssertNotNil(cachedObject)
|
||||
}
|
||||
|
||||
func testCacheEntry() {
|
||||
// Returns nil if entry doesn't exist
|
||||
var entry: Entry<User> = try! storage.entry(forKey: key)
|
||||
XCTAssertNil(entry)
|
||||
|
||||
// Returns entry if object exists
|
||||
let expiry = Expiry.date(Date())
|
||||
storage.setObject(object, forKey: key)
|
||||
entry = try! storage.entry(forKey: key)
|
||||
|
||||
XCTAssertEqual(entry.object.firstName, object.firstName)
|
||||
XCTAssertEqual(entry.object.lastName, object.lastName)
|
||||
XCTAssertEqual(entry.expiry.date, expiry.date)
|
||||
}
|
||||
|
||||
/// Test that it resolves cached object
|
||||
func testObject() {
|
||||
storage.setObject(object, forKey: key)
|
||||
let cachedObject: User = try! storage.object(forKey: key)
|
||||
XCTAssertEqual(cachedObject.firstName, object.firstName)
|
||||
XCTAssertEqual(cachedObject.lastName, object.lastName)
|
||||
}
|
||||
|
||||
/// Test that it removes cached object
|
||||
func testRemoveObject() {
|
||||
storage.setObject(object, forKey: key)
|
||||
storage.removeObject(forKey: key)
|
||||
let cachedObject: User = try! storage.object(forKey: key)
|
||||
XCTAssertNil(cachedObject)
|
||||
}
|
||||
|
||||
/// Test that it removes expired object
|
||||
func testRemoveObjectIfExpiredWhenExpired() {
|
||||
let expiry: Expiry = .date(Date().addingTimeInterval(-100000))
|
||||
storage.setObject(object, forKey: key)
|
||||
storage.removeObjectIfExpired(forKey: key)
|
||||
let cachedObject: User = try! storage.object(forKey: key)
|
||||
|
||||
XCTAssertNil(cachedObject)
|
||||
}
|
||||
|
||||
/// Test that it doesn't remove not expired object
|
||||
func testRemoveObjectIfExpiredWhenNotExpired() {
|
||||
storage.setObject(object, forKey: key)
|
||||
storage.removeObjectIfExpired(forKey: key)
|
||||
let cachedObject: User = try! storage.object(forKey: key)
|
||||
|
||||
XCTAssertNotNil(cachedObject)
|
||||
}
|
||||
|
||||
/// Test that it clears cache directory
|
||||
func testRemoveAll() {
|
||||
storage.setObject(object, forKey: key)
|
||||
storage.removeAll()
|
||||
let cachedObject: User = try! storage.object(forKey: key)
|
||||
XCTAssertNil(cachedObject)
|
||||
}
|
||||
|
||||
/// Test that it removes expired objects
|
||||
func testClearExpired() {
|
||||
let expiry1: Expiry = .date(Date().addingTimeInterval(-100000))
|
||||
let expiry2: Expiry = .date(Date().addingTimeInterval(100000))
|
||||
let key1 = "item1"
|
||||
let key2 = "item2"
|
||||
storage.setObject(object, forKey: key1)
|
||||
storage.setObject(object, forKey: key2)
|
||||
storage.removeExpiredObjects()
|
||||
let object1: User = try! storage.object(forKey: key1)
|
||||
let object2: User = try! storage.object(forKey: key2)
|
||||
|
||||
XCTAssertNil(object1)
|
||||
XCTAssertNotNil(object2)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user