krz/daybyday

iOS learning games for young children: days, months, seasons, numbers, letters, animals and more. education ios kids swift https://krz.sh/apps/daybyday/

DayByDayTests/StickerStoreTests.swift

main
daybyday/DayByDayTests/StickerStoreTests.swift history · blame · raw

86 lines · 4290 bytes

7 symbols in this file
 1import Foundation
 2import SwiftData
 3import Testing
 4import UIKit
 5@testable import DayByDay
 6
 7struct StickerStoreTests {
 8    let container = ProfileStore.makeContainer(inMemory: true)
 9    var store: ProfileStore { ProfileStore(context: container.mainContext) }
10
11    func day(_ y: Int, _ m: Int, _ d: Int) -> DateComponents { DateComponents(year: y, month: m, day: d) }
12
13    @Test func stickerUpsertsPerDay() throws {
14        let s = store
15        let maya = try s.create(name: "Maya", avatar: .cat, color: .red, birthday: nil)
16        s.addSticker(for: maya, on: day(2026, 10, 16), weather: .sunny)
17        s.addSticker(for: maya, on: day(2026, 10, 16), weather: .rainy)
18        let stickers = s.stickers(for: maya, year: 2026, month: 10)
19        #expect(stickers.count == 1)
20        #expect(stickers.first?.weatherRaw == LearnWeather.rainy.rawValue)
21        #expect(s.sticker(for: maya, on: day(2026, 10, 16)) != nil)
22        #expect(s.sticker(for: maya, on: day(2026, 10, 17)) == nil)
23    }
24
25    @Test func stickersByMonth() throws {
26        let s = store
27        let maya = try s.create(name: "Maya", avatar: .cat, color: .red, birthday: nil)
28        s.addSticker(for: maya, on: day(2026, 9, 30), weather: .sunny)
29        s.addSticker(for: maya, on: day(2026, 10, 1), weather: .cloudy)
30        s.addSticker(for: maya, on: day(2026, 10, 2), weather: .windy)
31        #expect(s.stickers(for: maya, year: 2026, month: 10).map(\.day).sorted() == [1, 2])
32        #expect(s.stickers(for: maya, year: 2025, month: 10).isEmpty)
33    }
34
35    @Test func picturesSetReplaceRemove() throws {
36        let s = store
37        let maya = try s.create(name: "Maya", avatar: .cat, color: .red, birthday: nil)
38        s.setPicture(.swim, on: .monday, for: maya)
39        #expect(s.picture(on: .monday, for: maya) == .swim)
40        s.setPicture(.school, on: .monday, for: maya)
41        #expect(s.picture(on: .monday, for: maya) == .school)
42        s.setPicture(nil, on: .monday, for: maya)
43        #expect(s.picture(on: .monday, for: maya) == nil)
44        #expect(try container.mainContext.fetch(FetchDescriptor<DayPicture>()).isEmpty)
45    }
46
47    @Test func deleteRemovesOnlyThatChildsData() throws {
48        let s = store
49        let maya = try s.create(name: "Maya", avatar: .cat, color: .red, birthday: nil)
50        let sam = try s.create(name: "Sam", avatar: .dog, color: .blue, birthday: nil)
51        for p in [maya, sam] {
52            s.addSticker(for: p, on: day(2026, 10, 16), weather: .sunny)
53            s.setPicture(.park, on: .friday, for: p)
54        }
55        s.delete(maya)
56        #expect(try container.mainContext.fetch(FetchDescriptor<Sticker>()).count == 1)
57        #expect(try container.mainContext.fetch(FetchDescriptor<DayPicture>()).count == 1)
58        #expect(s.picture(on: .friday, for: sam) == .park)
59    }
60
61    @Test(arguments: DayPictureKind.allCases) func pictureSymbolsExist(_ kind: DayPictureKind) {
62        #expect(UIImage(systemName: kind.symbol) != nil, "\(kind.symbol)")
63        #expect(kind.name.hasSuffix(" day"))
64    }
65
66    /// A store written before stickers existed opens with the new schema and keeps its data.
67    @Test func oldStoreOpens() throws {
68        let url = FileManager.default.temporaryDirectory
69            .appendingPathComponent("old-\(UUID().uuidString).store")
70        do {
71            let old = try ModelContainer(for: Profile.self, AnswerRecord.self,
72                                         configurations: ModelConfiguration(url: url))
73            let s = ProfileStore(context: old.mainContext)
74            let maya = try s.create(name: "Maya", avatar: .cat, color: .red, birthday: (month: 5, day: 1))
75            s.record(DayByDay.Category.days.quizItems[0], correct: true, for: maya)
76        }
77        let new = try ModelContainer(for: Schema(ProfileStore.modelTypes),
78                                     configurations: ModelConfiguration(url: url))
79        let profiles = try new.mainContext.fetch(FetchDescriptor<Profile>())
80        #expect(profiles.map(\.name) == ["Maya"])
81        #expect(profiles.first?.answers.count == 1)
82        let s = ProfileStore(context: new.mainContext)
83        s.addSticker(for: profiles[0], on: day(2026, 10, 16), weather: .snowy)
84        #expect(s.stickers(for: profiles[0], year: 2026, month: 10).count == 1)
85    }
86}