JSONImporterTests.swift 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //
  2. // JSONImporterTests.swift
  3. // Trio
  4. //
  5. // Created by Cengiz Deniz on 21.04.25.
  6. //
  7. import CoreData
  8. import Foundation
  9. import Swinject
  10. import Testing
  11. @testable import Trio
  12. class BundleReference {}
  13. @Suite("JSON Importer Tests") struct JSONImporterTests: Injectable {
  14. var coreDataStack: CoreDataStack!
  15. var context: NSManagedObjectContext!
  16. var importer: JSONImporter!
  17. init() async throws {
  18. // In-memory Core Data for tests
  19. coreDataStack = try await CoreDataStack.createForTests()
  20. context = coreDataStack.newTaskContext()
  21. importer = JSONImporter(context: context, coreDataStack: coreDataStack)
  22. }
  23. @Test("Import glucose history with value checks") func testImportGlucoseHistoryDetails() async throws {
  24. let testBundle = Bundle(for: BundleReference.self)
  25. let path = testBundle.path(forResource: "glucose", ofType: "json")!
  26. let url = URL(filePath: path)
  27. try await importer.importGlucoseHistory(url: url)
  28. // run the import againt to check our deduplication logic
  29. try await importer.importGlucoseHistory(url: url)
  30. let allReadings = try await coreDataStack.fetchEntitiesAsync(
  31. ofType: GlucoseStored.self,
  32. onContext: context,
  33. predicate: NSPredicate(format: "TRUEPREDICATE"),
  34. key: "date",
  35. ascending: false
  36. ) as? [GlucoseStored] ?? []
  37. #expect(allReadings.count == 274)
  38. #expect(allReadings.first?.glucose == 115)
  39. #expect(allReadings.first?.date == Date(timeIntervalSince1970: 1_745_868_771.726578))
  40. #expect(allReadings.last?.glucose == 127)
  41. #expect(allReadings.last?.date == Date(timeIntervalSince1970: 1_745_782_670.3270996))
  42. let manualCount = allReadings.filter({ $0.isManual }).count
  43. #expect(manualCount == 1)
  44. }
  45. }