Skip to content

Commit 6d0c70e

Browse files
vargaatclaude
andauthored
Add missing unit tests (#3711)
Also fix errors and warnings in the log during unit testing. [ignore-commit-lint] Co-authored-by: Claude <[email protected]>
1 parent 8a955fe commit 6d0c70e

File tree

6 files changed

+493
-18
lines changed

6 files changed

+493
-18
lines changed

Core/Core/Common/CommonModels/Store/PreviewStore.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ import CoreData
2323

2424
private var singleSharedPreviewDatabase: NSPersistentContainer = resetSingleSharedPreviewDatabase()
2525
private func resetSingleSharedPreviewDatabase() -> NSPersistentContainer {
26-
let bundle = Bundle.core
27-
let modelURL = bundle.url(forResource: "Database", withExtension: "momd")!
28-
let model = NSManagedObjectModel(contentsOf: modelURL)!
29-
let container = NSPersistentContainer(name: "Database", managedObjectModel: model)
26+
let container = NSPersistentContainer(name: "Database", managedObjectModel: .core)
3027
let description = NSPersistentStoreDescription()
3128
description.type = NSInMemoryStoreType
3229
description.shouldAddStoreAsynchronously = false

Core/Core/Common/Extensions/CoreData/NSPersistentContainerExtensions.swift

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919
import Combine
2020
import CoreData
2121

22+
extension NSManagedObjectModel {
23+
static let core: NSManagedObjectModel = {
24+
let modelFileURL = Bundle.core.url(forResource: "Database", withExtension: "momd")!
25+
return NSManagedObjectModel(contentsOf: modelFileURL)!
26+
}()
27+
}
28+
2229
extension NSPersistentContainer {
2330

2431
// MARK: - Public Static Interface
@@ -28,14 +35,18 @@ extension NSPersistentContainer {
2835
public static func create(appGroup: String? = Bundle.main.appGroupID(),
2936
session: LoginSession? = nil)
3037
-> NSPersistentContainer {
31-
let modelFileURL = Bundle.core.url(forResource: "Database", withExtension: "momd")!
32-
let model = NSManagedObjectModel(contentsOf: modelFileURL)!
3338
registerCoreTransformers()
34-
let container = NSPersistentContainer(name: "Database", managedObjectModel: model)
39+
let container = NSPersistentContainer(name: "Database", managedObjectModel: .core)
40+
41+
let dbUrl = {
42+
let url = URL.Directories.databaseURL(appGroup: appGroup, session: session)
43+
let parentDirectory = url.deletingLastPathComponent()
44+
try? FileManager.default.createDirectory(at: parentDirectory, withIntermediateDirectories: true, attributes: nil)
45+
return url
46+
}()
3547

36-
let url = URL.Directories.databaseURL(appGroup: appGroup, session: session)
3748
container.persistentStoreDescriptions = [
38-
NSPersistentStoreDescription(url: url)
49+
NSPersistentStoreDescription(url: dbUrl)
3950
]
4051

4152
container.loadPersistentStores { _, error in
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
//
2+
// This file is part of Canvas.
3+
// Copyright (C) 2025-present Instructure, Inc.
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as
7+
// published by the Free Software Foundation, either version 3 of the
8+
// License, or (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
//
18+
19+
@testable import Core
20+
import Combine
21+
import XCTest
22+
23+
class SubjectExtensionsTests: XCTestCase {
24+
25+
func test_binding_returnsCurrentValue() {
26+
let subject = CurrentValueSubject<Int, Never>(42)
27+
28+
// WHEN
29+
let binding = subject.binding
30+
31+
// THEN
32+
XCTAssertEqual(binding.wrappedValue, 42)
33+
}
34+
35+
func test_binding_updatesSubjectValue() {
36+
let subject = CurrentValueSubject<String, Never>("initial")
37+
let binding = subject.binding
38+
39+
// WHEN
40+
binding.wrappedValue = "updated"
41+
42+
// THEN
43+
XCTAssertEqual(subject.value, "updated")
44+
}
45+
46+
func test_binding_worksWithComplexType() {
47+
struct TestModel {
48+
var name: String
49+
var count: Int
50+
}
51+
52+
let initialModel = TestModel(name: "test", count: 5)
53+
let subject = CurrentValueSubject<TestModel, Never>(initialModel)
54+
let binding = subject.binding
55+
56+
XCTAssertEqual(binding.wrappedValue.name, "test")
57+
XCTAssertEqual(binding.wrappedValue.count, 5)
58+
59+
// WHEN
60+
let updatedModel = TestModel(name: "updated", count: 10)
61+
binding.wrappedValue = updatedModel
62+
63+
// THEN
64+
XCTAssertEqual(subject.value.name, "updated")
65+
XCTAssertEqual(subject.value.count, 10)
66+
}
67+
68+
func test_binding_worksWithBooleanValue() {
69+
let subject = CurrentValueSubject<Bool, Never>(false)
70+
let binding = subject.binding
71+
72+
XCTAssertFalse(binding.wrappedValue)
73+
74+
// WHEN
75+
binding.wrappedValue = true
76+
77+
// THEN
78+
XCTAssertTrue(subject.value)
79+
}
80+
81+
func test_binding_worksWithOptionalValue() {
82+
let subject = CurrentValueSubject<Int?, Never>(nil)
83+
let binding = subject.binding
84+
85+
XCTAssertNil(binding.wrappedValue)
86+
87+
// WHEN
88+
binding.wrappedValue = 100
89+
90+
// THEN
91+
XCTAssertEqual(subject.value, 100)
92+
93+
// WHEN
94+
binding.wrappedValue = nil
95+
96+
// THEN
97+
XCTAssertNil(subject.value)
98+
}
99+
}

0 commit comments

Comments
 (0)