| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- //
- // JSONImporterTests.swift
- // Trio
- //
- // Created by Cengiz Deniz on 21.04.25.
- //
- import CoreData
- import Foundation
- import Swinject
- import Testing
- @testable import Trio
- class BundleReference {}
- @Suite("JSON Importer Tests", .serialized) struct JSONImporterTests: Injectable {
- var coreDataStack: CoreDataStack!
- var context: NSManagedObjectContext!
- var importer: JSONImporter!
- init() async throws {
- // In-memory Core Data for tests
- coreDataStack = try await CoreDataStack.createForTests()
- context = coreDataStack.newTaskContext()
- importer = JSONImporter(context: context, coreDataStack: coreDataStack)
- }
- @Test("Import glucose history with value checks") func testImportGlucoseHistoryDetails() async throws {
- let testBundle = Bundle(for: BundleReference.self)
- let path = testBundle.path(forResource: "glucose", ofType: "json")!
- let url = URL(filePath: path)
- let now = Date("2025-04-28T19:32:52.000Z")!
- try await importer.importGlucoseHistory(url: url, now: now)
- // run the import againt to check our deduplication logic
- try await importer.importGlucoseHistory(url: url, now: now)
- let allReadings = try await coreDataStack.fetchEntitiesAsync(
- ofType: GlucoseStored.self,
- onContext: context,
- predicate: NSPredicate(format: "TRUEPREDICATE"),
- key: "date",
- ascending: false
- ) as? [GlucoseStored] ?? []
- #expect(allReadings.count == 274)
- #expect(allReadings.first?.glucose == 115)
- #expect(allReadings.first?.date == Date("2025-04-28T19:32:51.727Z"))
- #expect(allReadings.last?.glucose == 127)
- #expect(allReadings.last?.date == Date("2025-04-27T19:37:50.327Z"))
- let manualCount = allReadings.filter({ $0.isManual }).count
- #expect(manualCount == 1)
- }
- @Test("Skip importing old glucose values") func testSkipImportOldGlucoseValues() async throws {
- let testBundle = Bundle(for: BundleReference.self)
- let path = testBundle.path(forResource: "glucose", ofType: "json")!
- let url = URL(filePath: path)
- // more than 24 hours in the future from the most recent entry
- let now = Date("2025-04-29T19:32:52.000Z")!
- try await importer.importGlucoseHistory(url: url, now: now)
- let allReadings = try await coreDataStack.fetchEntitiesAsync(
- ofType: GlucoseStored.self,
- onContext: context,
- predicate: NSPredicate(format: "TRUEPREDICATE"),
- key: "date",
- ascending: false
- ) as? [GlucoseStored] ?? []
- #expect(allReadings.isEmpty)
- }
- @Test("Import carb history with value checks") func testImportCarbHistoryDetails() async throws {
- let testBundle = Bundle(for: BundleReference.self)
- let path = testBundle.path(forResource: "carbhistory", ofType: "json")!
- let url = URL(filePath: path)
- let now = Date("2025-04-28T19:32:52.000Z")!
- try await importer.importCarbHistory(url: url, now: now)
- // run the import againt to check our deduplication logic
- try await importer.importCarbHistory(url: url, now: now)
- let allCarbEntries = try await coreDataStack.fetchEntitiesAsync(
- ofType: CarbEntryStored.self,
- onContext: context,
- predicate: NSPredicate(format: "TRUEPREDICATE"),
- key: "date",
- ascending: false
- ) as? [CarbEntryStored] ?? []
- #expect(allCarbEntries.count == 8)
- #expect(allCarbEntries.first?.carbs == 10)
- #expect(allCarbEntries.first?.note == "Snack 🍪")
- #expect(allCarbEntries.first?.date == Date("2025-04-28T18:36:06.968Z"))
- #expect(allCarbEntries.last?.carbs == 25)
- #expect(allCarbEntries.last?.date == Date("2025-04-28T05:03:43.332Z"))
- }
- @Test("Skip importing old carb entries") func testSkipImportOldCarbEntries() async throws {
- let testBundle = Bundle(for: BundleReference.self)
- let path = testBundle.path(forResource: "carbhistory", ofType: "json")!
- let url = URL(filePath: path)
- // more than 24 hours in the future from the most recent entry
- let now = Date("2025-04-29T19:32:52.000Z")!
- try await importer.importCarbHistory(url: url, now: now)
- let allCarbEntries = try await coreDataStack.fetchEntitiesAsync(
- ofType: CarbEntryStored.self,
- onContext: context,
- predicate: NSPredicate(format: "TRUEPREDICATE"),
- key: "date",
- ascending: false
- ) as? [CarbEntryStored] ?? []
- #expect(allCarbEntries.isEmpty)
- }
- }
|