Skip to content

Commit

Permalink
Merge pull request #18 from mrlegowatch/develop
Browse files Browse the repository at this point in the history
Merge develop to main
  • Loading branch information
mrlegowatch authored Sep 7, 2021
2 parents 4f53341 + cc50686 commit 6a98950
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 16 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/swift.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Swift

on:
push:
branches:
- develop
- main
pull_request:
branches:
- develop
- main

jobs:
build:

runs-on: macos-latest

steps:
- uses: actions/checkout@v2
- name: Build
run: swift build -v
- name: Run tests
run: swift test -v
4 changes: 2 additions & 2 deletions Sources/GarageStorage/Garage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ public class Garage: NSObject {
}
}

/// Creates a Garage with the specified peristent store descriptions and object mapper.
/// Creates a Garage with the specified persistent store descriptions and object mapper.
///
/// - note: Once the Garage has been initialized, you need to execute `loadPersistentStores(completionHandler:)` to instruct the Garage to load the persistent stores and complete the creation of the Core Data stack.
///
/// - parameter persistentStoreDescriptions: An array of PersistentStoreDescription to use in the Garage's Core Data Stack. If nil is passed in, a default description will be used.
public init(with persistentStoreDescriptions: [PersistentStoreDescription]? = nil) {
let garageModel = GarageModel()
let garageModel = GarageModel().makeModel()
self.persistentContainer = PersistentContainer(name: Garage.modelName, managedObjectModel: garageModel)
let descriptions = persistentStoreDescriptions ?? [Garage.defaultDescription]
self.persistentContainer.persistentStoreDescriptions = descriptions
Expand Down
13 changes: 5 additions & 8 deletions Sources/GarageStorage/Model/GarageModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import CoreData

class GarageModel: NSManagedObjectModel {
struct GarageModel {

private func makeEntity(_ name: String) -> NSEntityDescription {
let entity = NSEntityDescription()
Expand All @@ -25,9 +25,7 @@ class GarageModel: NSManagedObjectModel {
return attribute
}

override init() {
super.init()

func makeModel() -> NSManagedObjectModel {
// Create the attributes
let properties: [NSAttributeDescription] = [
makeAttribute(CoreDataObject.Attribute.type, type: .stringAttributeType),
Expand All @@ -43,10 +41,9 @@ class GarageModel: NSManagedObjectModel {
let entity = makeEntity(CoreDataObject.entityName)
entity.properties = properties

self.entities = [entity]
let garageModel = NSManagedObjectModel()
garageModel.entities = [entity]
return garageModel
}

required init?(coder: NSCoder) {
fatalError("init(coder:) is not supported")
}
}
1 change: 1 addition & 0 deletions Tests/GarageStorageTests/BootstrapTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class BootstrapTests: XCTestCase {

// Ensure that the Documents directory exists (first time in Simulator)
override class func setUp() {
TestSetup.classSetUp()
let fileManager = FileManager.default
let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).last!
if !fileManager.fileExists(atPath: documentsDirectory.path) {
Expand Down
4 changes: 4 additions & 0 deletions Tests/GarageStorageTests/MappableObjectTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import CoreData
// This set of tests use Swift-declared Objective-C-compatible MappableObjects.
class MappableObjectTests: XCTestCase {

override class func setUp() {
TestSetup.classSetUp()
}

override func setUp() {
// Reset the underlying storage before running each test.
let garage = Garage()
Expand Down
13 changes: 10 additions & 3 deletions Tests/GarageStorageTests/SwiftCodableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import CoreData
// This set of tests checks Codable (hence "Swift-y") types.
class SwiftCodableTests: XCTestCase {

override class func setUp() {
TestSetup.classSetUp()
}

override func setUp() {
// Reset the underlying storage before running each test.
let garage = Garage()
Expand Down Expand Up @@ -291,14 +295,17 @@ class SwiftCodableTests: XCTestCase {

// Set sam's birthdate to 1950/01/01 04:00:00

let timeZone = TestSetup.timeZone
var dateComponents = DateComponents()
dateComponents.day = 1
dateComponents.month = 1
dateComponents.year = 1950
dateComponents.timeZone = timeZone

let calendar = Calendar.current
var calendar = Calendar.current
calendar.timeZone = timeZone
sam.birthdate = calendar.date(from: dateComponents)!
XCTAssertEqual(sam.birthdate.timeIntervalSinceReferenceDate, -1609441200.0, "Making assumption about the test")
XCTAssertEqual(sam.birthdate.timeIntervalSinceReferenceDate, -1609459200.0, "Making assumption about the test")

XCTAssertNoThrow(try garage.park(sam), "parkObject")
}
Expand All @@ -307,7 +314,7 @@ class SwiftCodableTests: XCTestCase {
let sam = try? garage.retrieve(SwiftPerson.self, identifier: "Sam")
XCTAssertNotNil(sam, "Failed to retrieve 'Sam' from garage store")

XCTAssertEqual(sam?.birthdate.timeIntervalSinceReferenceDate ?? 0, -1609441200.0, "Reconstituted date failed")
XCTAssertEqual(sam?.birthdate.timeIntervalSinceReferenceDate ?? 0, -1609459200.0, "Reconstituted date failed")
}
}

Expand Down
4 changes: 4 additions & 0 deletions Tests/GarageStorageTests/SwiftMigrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import GarageStorage

class SwiftMigrationTests: XCTestCase {

override class func setUp() {
TestSetup.classSetUp()
}

override func setUp() {
// Reset the underlying storage before running each test.
let garage = Garage()
Expand Down
19 changes: 19 additions & 0 deletions Tests/GarageStorageTests/TestSetup.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// TestSetup.swift
// GarageStorageTests
//
// Created by Bob Gilmore on 5/10/21.
//

import Foundation

@objc public class TestSetup: NSObject {

static var timeZone = TimeZone(identifier: "UTC")!

static func classSetUp() {
// Set the test time zone to UTC so that tests can compare with hardcoded UTC dates
NSTimeZone.default = TestSetup.timeZone
}

}
13 changes: 10 additions & 3 deletions Tests/GarageStorageTests/TestableEdgeCaseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import CoreData
// This set of tests require access to internal GarageStorage APIs, to varying degrees.
class TestableEdgeCaseTests: XCTestCase {

override class func setUp() {
TestSetup.classSetUp()
}

override func setUp() {
// Reset the underlying storage before running each test.
let garage = Garage()
Expand Down Expand Up @@ -131,18 +135,21 @@ class TestableEdgeCaseTests: XCTestCase {

func testDateFormatter() {

let timeZone = TestSetup.timeZone
var dateComponents = DateComponents()
dateComponents.day = 1
dateComponents.month = 1
dateComponents.year = 1950
dateComponents.timeZone = timeZone

let calendar = Calendar.current
var calendar = Calendar.current
calendar.timeZone = timeZone
let date = calendar.date(from: dateComponents)!
XCTAssertEqual(date.timeIntervalSinceReferenceDate, -1609441200.0, "Making assumption about the test")
XCTAssertEqual(date.timeIntervalSinceReferenceDate, -1609459200.0, "Making assumption about the test")

do {
let dateString = date.isoString
XCTAssertEqual(dateString, "1950-01-01T00:00:00-05:00", "isoString failed")
XCTAssertEqual(dateString, "1950-01-01T00:00:00Z", "isoString failed")
}

do {
Expand Down

0 comments on commit 6a98950

Please sign in to comment.