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/ProfileStoreTests.swift

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

112 lines · 5375 bytes

11 symbols in this file
  1import Foundation
  2import SwiftData
  3import Testing
  4@testable import DayByDay
  5
  6struct ProfileStoreTests {
  7    let container = ProfileStore.makeContainer(inMemory: true)
  8    var store: ProfileStore { ProfileStore(context: container.mainContext) }
  9
 10    @Test func createTrimsName() throws {
 11        let p = try store.create(name: "  Maya ", avatar: .cat, color: .blue, birthday: nil)
 12        #expect(p.name == "Maya")
 13        #expect(p.avatar == .cat)
 14        #expect(p.color == .blue)
 15    }
 16
 17    @Test func blankNameThrows() {
 18        #expect(throws: ProfileError.blankName) {
 19            try store.create(name: "   ", avatar: .dog, color: .red, birthday: nil)
 20        }
 21    }
 22
 23    @Test func birthdayValidation() throws {
 24        let p = try store.create(name: "Leap", avatar: .dog, color: .red, birthday: (month: 2, day: 29))
 25        #expect(p.birthdayMonth == 2 && p.birthdayDay == 29)
 26        for bad in [(2, 30), (4, 31), (13, 1), (0, 5), (5, 0)] {
 27            #expect(throws: ProfileError.invalidBirthday) {
 28                try store.create(name: "X", avatar: .dog, color: .red, birthday: (month: bad.0, day: bad.1))
 29            }
 30        }
 31    }
 32
 33    @Test func allSortedByCreation() throws {
 34        let s = store
 35        let a = try s.create(name: "A", avatar: .cat, color: .red, birthday: nil)
 36        let b = try s.create(name: "B", avatar: .dog, color: .blue, birthday: nil)
 37        #expect(s.all().map(\.id) == [a.id, b.id])
 38    }
 39
 40    @Test func updateValidates() throws {
 41        let s = store
 42        let p = try s.create(name: "Maya", avatar: .cat, color: .red, birthday: nil)
 43        try s.update(p, name: " Mae ", avatar: .bird, color: .green, birthday: (month: 5, day: 1))
 44        #expect(p.name == "Mae" && p.avatar == .bird && p.birthdayMonth == 5)
 45        #expect(throws: ProfileError.blankName) {
 46            try s.update(p, name: "", avatar: .bird, color: .green, birthday: nil)
 47        }
 48    }
 49
 50    @Test func recordsAnswersPerProfile() throws {
 51        let s = store
 52        let maya = try s.create(name: "Maya", avatar: .cat, color: .red, birthday: nil)
 53        let sam = try s.create(name: "Sam", avatar: .dog, color: .blue, birthday: nil)
 54        s.record(Category.days.quizItems[0], correct: true, for: maya)
 55        s.record(Category.months.quizItems[0], correct: false, for: maya)
 56        s.record(Category.days.quizItems[1], correct: true, for: sam)
 57        #expect(s.answers(for: maya, category: .days).count == 1)
 58        #expect(s.answers(for: maya, category: nil).count == 2)
 59        #expect(s.answers(for: sam, category: nil).first?.itemID == "days.monday")
 60    }
 61
 62    @Test func deletingProfileDeletesAnswers() throws {
 63        let s = store
 64        let maya = try s.create(name: "Maya", avatar: .cat, color: .red, birthday: nil)
 65        let sam = try s.create(name: "Sam", avatar: .dog, color: .blue, birthday: nil)
 66        s.record(Category.days.quizItems[0], correct: true, for: maya)
 67        s.record(Category.days.quizItems[1], correct: true, for: sam)
 68        s.delete(maya)
 69        let remaining = try container.mainContext.fetch(FetchDescriptor<AnswerRecord>())
 70        #expect(remaining.count == 1)
 71        #expect(s.all().map(\.name) == ["Sam"])
 72    }
 73
 74    @Test func recordsChosenItemAndGame() throws {
 75        let s = store
 76        let maya = try s.create(name: "Maya", avatar: .cat, color: .red, birthday: nil)
 77        let days = Category.days.quizItems
 78        s.record(days[2], chosen: days[1], game: .findIt, correct: false, for: maya, at: Date(timeIntervalSince1970: 1))
 79        s.record(days[3], correct: true, for: maya, at: Date(timeIntervalSince1970: 2))
 80        let records = s.answers(for: maya, category: .days).sorted { $0.timestamp < $1.timestamp }
 81        #expect(records.map(\.chosenID) == ["days.monday", nil])
 82        #expect(records.map(\.gameRaw) == ["findIt", nil])
 83    }
 84
 85    @Test func storeReviewWeights() throws {
 86        let s = store
 87        let a = try s.create(name: "A", avatar: .cat, color: .red, birthday: nil)
 88        let b = try s.create(name: "B", avatar: .dog, color: .blue, birthday: nil)
 89        let monday = Category.days.item(DayOfWeek.monday)
 90        s.record(monday, correct: false, for: a)
 91        #expect(s.reviewWeights(for: a, category: .days) == [monday: 3])
 92        #expect(s.reviewWeights(for: b, category: .days).isEmpty)
 93        #expect(s.progress(for: a, category: .days).status(monday.id) == .learning)
 94    }
 95
 96    @Test func progressCountsOnlyRecognitionGames() throws {
 97        let s = store
 98        let a = try s.create(name: "A", avatar: .cat, color: .red, birthday: nil)
 99        let b = Category.alphabet.item(LearnLetter.b)
100        s.record(b, game: .trace, correct: false, for: a)
101        let monday = Category.days.item(DayOfWeek.monday)
102        s.record(monday, game: .weekTrain, correct: true, for: a)
103        s.record(monday, game: .weekTrain, correct: true, for: a)
104        #expect(s.progress(for: a, category: .alphabet).status(b.id) == .new)
105        #expect(s.progress(for: a, category: .days).status(monday.id) == .new)
106        s.record(b, game: .findIt, correct: false, for: a)
107        #expect(s.progress(for: a, category: .alphabet).status(b.id) == .learning)
108        let june = Category.months.item(MonthOfYear.june)
109        s.record(june, correct: false, for: a)
110        #expect(s.progress(for: a, category: .months).status(june.id) == .learning)
111    }
112}