|
| 1 | +// CoreDataTestSuiteTests.swift |
| 2 | +// CoreDataRepository |
| 3 | +// |
| 4 | +// This source code is licensed under the MIT License (MIT) found in the |
| 5 | +// LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +// CoreDataTestSuiteTests.swift |
| 8 | +// CoreDataRepository |
| 9 | +// |
| 10 | +// This source code is licensed under the MIT License (MIT) found in the |
| 11 | +// LICENSE file in the root directory of this source tree. |
| 12 | + |
| 13 | +import CoreData |
| 14 | +import CoreDataRepository |
| 15 | +import Internal |
| 16 | +import Testing |
| 17 | + |
| 18 | +extension CoreDataRepositoryTests { |
| 19 | + @Suite |
| 20 | + struct CoreDataTestSuiteTests: CoreDataTestSuite { |
| 21 | + let container: NSPersistentContainer |
| 22 | + let repositoryContext: NSManagedObjectContext |
| 23 | + let repository: CoreDataRepository |
| 24 | + |
| 25 | + @Test |
| 26 | + func verify_Fetchable_Success() async throws { |
| 27 | + let modelType = FetchableModel_UuidId.self |
| 28 | + let _value = modelType.seeded(1) |
| 29 | + |
| 30 | + let existingValue = try await repositoryContext.perform(schedule: .immediate) { |
| 31 | + let managed = try _value.asManagedModel(in: repositoryContext) |
| 32 | + try repositoryContext.save() |
| 33 | + try repositoryContext.parent?.save() |
| 34 | + return try modelType.init(managed: managed) |
| 35 | + } |
| 36 | + |
| 37 | + try await verify(existingValue) |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + func verify_Fetchable_Failure() async throws { |
| 42 | + let modelType = FetchableModel_UuidId.self |
| 43 | + let _value = modelType.seeded(1) |
| 44 | + |
| 45 | + await withKnownIssue { |
| 46 | + try await verify(_value) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + func verify_Readable_Success() async throws { |
| 52 | + let modelType = IdentifiableModel_UuidId.self |
| 53 | + let _value = modelType.seeded(1) |
| 54 | + |
| 55 | + let existingValue = try await repositoryContext.perform(schedule: .immediate) { |
| 56 | + let managed = try _value.asManagedModel(in: repositoryContext) |
| 57 | + try repositoryContext.save() |
| 58 | + try repositoryContext.parent?.save() |
| 59 | + return try modelType.init(managed: managed) |
| 60 | + } |
| 61 | + |
| 62 | + try await verify(existingValue) |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + func verify_Readable_Failure() async throws { |
| 67 | + let modelType = IdentifiableModel_UuidId.self |
| 68 | + let _value = modelType.seeded(1) |
| 69 | + |
| 70 | + await withKnownIssue { |
| 71 | + try await verify(_value) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + func verifyDoesNotExist_Fetchable_Success() async throws { |
| 77 | + let modelType = FetchableModel_UuidId.self |
| 78 | + let _value = modelType.seeded(1) |
| 79 | + |
| 80 | + try await verifyDoesNotExist(_value) |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + func verifyDoesNotExist_Fetchable_Failure() async throws { |
| 85 | + let modelType = FetchableModel_UuidId.self |
| 86 | + let _value = modelType.seeded(1) |
| 87 | + |
| 88 | + let existingValue = try await repositoryContext.perform(schedule: .immediate) { |
| 89 | + let managed = try _value.asManagedModel(in: repositoryContext) |
| 90 | + try repositoryContext.save() |
| 91 | + try repositoryContext.parent?.save() |
| 92 | + return try modelType.init(managed: managed) |
| 93 | + } |
| 94 | + |
| 95 | + await withKnownIssue { |
| 96 | + try await verifyDoesNotExist(existingValue) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + func verifyDoesNotExist_Readable_Success() async throws { |
| 102 | + let modelType = IdentifiableModel_UuidId.self |
| 103 | + let _value = modelType.seeded(1) |
| 104 | + |
| 105 | + try await verifyDoesNotExist(_value) |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + func verifyDoesNotExist_Readable_Failure() async throws { |
| 110 | + let modelType = IdentifiableModel_UuidId.self |
| 111 | + let _value = modelType.seeded(1) |
| 112 | + |
| 113 | + let existingValue = try await repositoryContext.perform(schedule: .immediate) { |
| 114 | + let managed = try _value.asManagedModel(in: repositoryContext) |
| 115 | + try repositoryContext.save() |
| 116 | + try repositoryContext.parent?.save() |
| 117 | + return try modelType.init(managed: managed) |
| 118 | + } |
| 119 | + |
| 120 | + await withKnownIssue { |
| 121 | + try await verifyDoesNotExist(existingValue) |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + func verifyHistory_Success() async throws { |
| 127 | + let modelType = FetchableModel_UuidId.self |
| 128 | + let _value = modelType.seeded(1) |
| 129 | + |
| 130 | + let author: String = #function |
| 131 | + let date = Date.now |
| 132 | + |
| 133 | + try await repositoryContext.perform(schedule: .immediate) { |
| 134 | + repositoryContext.transactionAuthor = author |
| 135 | + _ = try _value.asManagedModel(in: repositoryContext) |
| 136 | + try repositoryContext.save() |
| 137 | + try repositoryContext.parent?.save() |
| 138 | + } |
| 139 | + |
| 140 | + try verify(transactionAuthor: author, timeStamp: date) |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + func verifyHistory_Failure_NoHistory() async throws { |
| 145 | + let author: String = #function |
| 146 | + let date = Date.now |
| 147 | + |
| 148 | + withKnownIssue { |
| 149 | + try verify(transactionAuthor: author, timeStamp: date) |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + func verifyHistory_Failure_WrongAuthor() async throws { |
| 155 | + let modelType = FetchableModel_UuidId.self |
| 156 | + let _value = modelType.seeded(1) |
| 157 | + |
| 158 | + let author: String = #function |
| 159 | + let date = Date.now |
| 160 | + |
| 161 | + try await repositoryContext.perform(schedule: .immediate) { |
| 162 | + repositoryContext.transactionAuthor = author |
| 163 | + _ = try _value.asManagedModel(in: repositoryContext) |
| 164 | + try repositoryContext.save() |
| 165 | + try repositoryContext.parent?.save() |
| 166 | + } |
| 167 | + |
| 168 | + withKnownIssue { |
| 169 | + try verify(transactionAuthor: "WRONG", timeStamp: date) |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + func verifyHistory_Failure_WrongDate() async throws { |
| 175 | + let modelType = FetchableModel_UuidId.self |
| 176 | + let _value = modelType.seeded(1) |
| 177 | + |
| 178 | + let author: String = #function |
| 179 | + |
| 180 | + try await repositoryContext.perform(schedule: .immediate) { |
| 181 | + repositoryContext.transactionAuthor = author |
| 182 | + _ = try _value.asManagedModel(in: repositoryContext) |
| 183 | + try repositoryContext.save() |
| 184 | + try repositoryContext.parent?.save() |
| 185 | + } |
| 186 | + |
| 187 | + let date = Date.now |
| 188 | + |
| 189 | + withKnownIssue { |
| 190 | + try verify(transactionAuthor: author, timeStamp: date) |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + @Test |
| 195 | + func verifyHistoryDoesNotExist_Success_Date() async throws { |
| 196 | + let modelType = FetchableModel_UuidId.self |
| 197 | + let _value = modelType.seeded(1) |
| 198 | + |
| 199 | + try await repositoryContext.perform(schedule: .immediate) { |
| 200 | + _ = try _value.asManagedModel(in: repositoryContext) |
| 201 | + try repositoryContext.save() |
| 202 | + try repositoryContext.parent?.save() |
| 203 | + } |
| 204 | + |
| 205 | + let date = Date.now |
| 206 | + |
| 207 | + try verifyDoesNotExist(transactionAuthor: nil, timeStamp: date) |
| 208 | + } |
| 209 | + |
| 210 | + @Test |
| 211 | + func verifyHistoryDoesNotExist_Success_Author() async throws { |
| 212 | + let modelType = FetchableModel_UuidId.self |
| 213 | + let _value = modelType.seeded(1) |
| 214 | + |
| 215 | + let author: String = #function |
| 216 | + let date = Date.now |
| 217 | + |
| 218 | + try await repositoryContext.perform(schedule: .immediate) { |
| 219 | + repositoryContext.transactionAuthor = author |
| 220 | + _ = try _value.asManagedModel(in: repositoryContext) |
| 221 | + try repositoryContext.save() |
| 222 | + try repositoryContext.parent?.save() |
| 223 | + } |
| 224 | + |
| 225 | + try verifyDoesNotExist(transactionAuthor: "WRONG_AUTHOR", timeStamp: date) |
| 226 | + } |
| 227 | + |
| 228 | + @Test |
| 229 | + func verifyHistoryDoesNotExist_Failure_Date() async throws { |
| 230 | + let modelType = FetchableModel_UuidId.self |
| 231 | + let _value = modelType.seeded(1) |
| 232 | + |
| 233 | + let date = Date.now |
| 234 | + |
| 235 | + try await repositoryContext.perform(schedule: .immediate) { |
| 236 | + _ = try _value.asManagedModel(in: repositoryContext) |
| 237 | + try repositoryContext.save() |
| 238 | + try repositoryContext.parent?.save() |
| 239 | + } |
| 240 | + |
| 241 | + withKnownIssue { |
| 242 | + try verifyDoesNotExist(transactionAuthor: nil, timeStamp: date) |
| 243 | + } |
| 244 | + } |
| 245 | + |
| 246 | + @Test |
| 247 | + func verifyHistoryDoesNotExist_Failure_Author() async throws { |
| 248 | + let modelType = FetchableModel_UuidId.self |
| 249 | + let _value = modelType.seeded(1) |
| 250 | + |
| 251 | + let author: String = #function |
| 252 | + let date = Date.now |
| 253 | + |
| 254 | + try await repositoryContext.perform(schedule: .immediate) { |
| 255 | + repositoryContext.transactionAuthor = author |
| 256 | + _ = try _value.asManagedModel(in: repositoryContext) |
| 257 | + try repositoryContext.save() |
| 258 | + try repositoryContext.parent?.save() |
| 259 | + } |
| 260 | + |
| 261 | + withKnownIssue { |
| 262 | + try verifyDoesNotExist(transactionAuthor: author, timeStamp: date) |
| 263 | + } |
| 264 | + } |
| 265 | + |
| 266 | + init( |
| 267 | + container: NSPersistentContainer, |
| 268 | + repositoryContext: NSManagedObjectContext, |
| 269 | + repository: CoreDataRepository |
| 270 | + ) { |
| 271 | + self.container = container |
| 272 | + self.repositoryContext = repositoryContext |
| 273 | + self.repository = repository |
| 274 | + } |
| 275 | + } |
| 276 | +} |
0 commit comments