Skip to content

Commit 15c853d

Browse files
committed
Add StorageTests
1 parent bbad973 commit 15c853d

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

Cache.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@
8181
D27014AF20D12D84003B45C7 /* AsyncStorageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D292DB031F6AA0730060F614 /* AsyncStorageTests.swift */; };
8282
D27014B020D12E37003B45C7 /* StorageSupportTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2CF98881F695F9400CE8F68 /* StorageSupportTests.swift */; };
8383
D27014B120D12E38003B45C7 /* StorageSupportTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2CF98881F695F9400CE8F68 /* StorageSupportTests.swift */; };
84+
D27014B320D13E2C003B45C7 /* StorageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D236F3191F6BEF73004EE01F /* StorageTests.swift */; };
85+
D27014B420D13E2C003B45C7 /* StorageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D236F3191F6BEF73004EE01F /* StorageTests.swift */; };
8486
D28897051F8B79B300C61DEE /* JSONDecoder+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D28897041F8B79B300C61DEE /* JSONDecoder+Extensions.swift */; };
8587
D28897061F8B79B300C61DEE /* JSONDecoder+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D28897041F8B79B300C61DEE /* JSONDecoder+Extensions.swift */; };
8688
D28897071F8B79B300C61DEE /* JSONDecoder+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D28897041F8B79B300C61DEE /* JSONDecoder+Extensions.swift */; };
@@ -828,6 +830,7 @@
828830
files = (
829831
D2CF98201F69427C00CE8F68 /* TestCase+Extensions.swift in Sources */,
830832
D2CF98231F69427C00CE8F68 /* TestHelper.swift in Sources */,
833+
D27014B420D13E2C003B45C7 /* StorageTests.swift in Sources */,
831834
D27014A320D129A3003B45C7 /* DiskStorageTests.swift in Sources */,
832835
D27014A020D12870003B45C7 /* MemoryStorageTests.swift in Sources */,
833836
D2CF98261F69427C00CE8F68 /* User.swift in Sources */,
@@ -852,6 +855,7 @@
852855
D2CF987F1F69513800CE8F68 /* ImageWrapperTests.swift in Sources */,
853856
D2D4CC1A1FA3166900E4A2D5 /* MD5Tests.swift in Sources */,
854857
D2D4CC281FA342CA00E4A2D5 /* JSONWrapperTests.swift in Sources */,
858+
D27014B320D13E2C003B45C7 /* StorageTests.swift in Sources */,
855859
D28C9BAF1F67EF8300C180C1 /* UIImage+ExtensionsTests.swift in Sources */,
856860
D2CF987D1F69513800CE8F68 /* MemoryCapsuleTests.swift in Sources */,
857861
D27014A120D129A2003B45C7 /* DiskStorageTests.swift in Sources */,

Tests/iOS/Tests/Storage/StorageTests.swift

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ import XCTest
22
import Cache
33

44
final class StorageTests: XCTestCase {
5-
private var storage: Storage!
5+
private var storage: Storage<User>!
66
let user = User(firstName: "John", lastName: "Snow")
77

88
override func setUp() {
99
super.setUp()
1010

11-
storage = try! Storage(diskConfig: DiskConfig(name: "Thor"), memoryConfig: MemoryConfig())
11+
storage = try! Storage<User>(
12+
diskConfig: DiskConfig(name: "Thor"),
13+
memoryConfig: MemoryConfig(),
14+
transformer: TransformerFactory.forCodable(ofType: User.self)
15+
)
1216
}
1317

1418
override func tearDown() {
@@ -18,7 +22,7 @@ final class StorageTests: XCTestCase {
1822

1923
func testSync() throws {
2024
try storage.setObject(user, forKey: "user")
21-
let cachedObject = try storage.object(ofType: User.self, forKey: "user")
25+
let cachedObject = try storage.object(forKey: "user")
2226

2327
XCTAssertEqual(cachedObject, user)
2428
}
@@ -27,7 +31,7 @@ final class StorageTests: XCTestCase {
2731
let expectation = self.expectation(description: #function)
2832
storage.async.setObject(user, forKey: "user", expiry: nil, completion: { _ in })
2933

30-
storage.async.object(ofType: User.self, forKey: "user", completion: { result in
34+
storage.async.object(forKey: "user", completion: { result in
3135
switch result {
3236
case .value(let cachedUser):
3337
XCTAssertEqual(cachedUser, self.user)
@@ -50,20 +54,23 @@ final class StorageTests: XCTestCase {
5054
let lastName: String
5155
}
5256

57+
let person1Storage = storage.transformCodable(ofType: Person1.self)
58+
let person2Storage = storage.transformCodable(ofType: Person2.self)
59+
5360
// Firstly, save object of type Person1
5461
let person = Person1(fullName: "John Snow")
5562

56-
try! storage.setObject(person, forKey: "person")
57-
XCTAssertNil(try? storage.object(ofType: Person2.self, forKey: "person"))
63+
try! person1Storage.setObject(person, forKey: "person")
64+
XCTAssertNil(try? person2Storage.object(forKey: "person"))
5865

5966
// Later, convert to Person2, do the migration, then overwrite
60-
let tempPerson = try! storage.object(ofType: Person1.self, forKey: "person")
67+
let tempPerson = try! person1Storage.object(forKey: "person")
6168
let parts = tempPerson.fullName.split(separator: " ")
6269
let migratedPerson = Person2(firstName: String(parts[0]), lastName: String(parts[1]))
63-
try! storage.setObject(migratedPerson, forKey: "person")
70+
try! person2Storage.setObject(migratedPerson, forKey: "person")
6471

6572
XCTAssertEqual(
66-
try! storage.object(ofType: Person2.self, forKey: "person").firstName,
73+
try! person2Storage.object(forKey: "person").firstName,
6774
"John"
6875
)
6976
}
@@ -79,11 +86,14 @@ final class StorageTests: XCTestCase {
7986
let lastName: String
8087
}
8188

89+
let personStorage = storage.transformCodable(ofType: Person.self)
90+
let alienStorage = storage.transformCodable(ofType: Alien.self)
91+
8292
let person = Person(firstName: "John", lastName: "Snow")
83-
try! storage.setObject(person, forKey: "person")
93+
try! personStorage.setObject(person, forKey: "person")
8494

8595
// As long as it has same properties, it works too
86-
let cachedObject = try! storage.object(ofType: Alien.self, forKey: "person")
96+
let cachedObject = try! alienStorage.object(forKey: "person")
8797
XCTAssertEqual(cachedObject.firstName, "John")
8898
}
8999
}

0 commit comments

Comments
 (0)