Skip to content

Commit

Permalink
#41: tests refactoring - split helpers functions into appropriate cla…
Browse files Browse the repository at this point in the history
…sses.

Removed from Formater classes all report parameters. They are now reusable classes for any reports.
Moved the StatsReportFromDate (version with Formater) in its own class.
  • Loading branch information
huguesf committed Nov 15, 2019
1 parent 91e2001 commit 307c46f
Show file tree
Hide file tree
Showing 8 changed files with 2,055 additions and 1,952 deletions.
29 changes: 1 addition & 28 deletions Core Classes/HtmlStatsReportFromDate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ protocol ReportFormaterDelegate

protocol StatsReportFromDateFormater
{
var startDate : Date { get }
var endDate : Date { get }
var siteSpecific : Bool { get }

init(_ startDate: Date, toDate endDate: Date, _ siteSpecific: Bool)

func addTitle(_ title : String)
func addNewSectionTitle(_ title : String)
func addBlankLine()
Expand Down Expand Up @@ -81,20 +75,10 @@ class HtmlStatsReportFromDateFormater: StatsReportFromDateFormater
let BG_HEADER = "#CCCCCC"
let BG_FOOTER = "#CCCCCC"

let startDate : Date
let endDate : Date
let siteSpecific : Bool
var report : String = ""
var isGray = false
var isAlternatingRowColor = false

required init(_ startDate: Date, toDate endDate: Date, _ siteSpecific: Bool = false)
{
self.startDate = startDate
self.endDate = endDate
self.siteSpecific = siteSpecific
}

func addTitle(_ title : String)
{
report += "<big>\(title)</big><br>"
Expand Down Expand Up @@ -263,25 +247,14 @@ class ExcelStatsReportFromDateFormater: StatsReportFromDateFormater
let BG_HEADER = "#CCCCCC"
let BG_FOOTER = "#CCCCCC"

let startDate : Date
let endDate : Date
let siteSpecific : Bool
var report : String = ""
var isGray = false
var isAlternatingRowColor = false

var rowsOnCurrentSheet = [ExcelRow]()
var titleCurrentSheet : String? = nil
var sheets = [ExcelSheet]()

required init(_ startDate: Date, toDate endDate: Date, _ siteSpecific: Bool = false)
{
self.startDate = startDate
self.endDate = endDate
self.siteSpecific = siteSpecific
}



func addTitle(_ title: String)
{
let cells = [ExcelCell(title, [TextAttribute.font([TextAttribute.FontStyle.bold])])]
Expand Down
1,679 changes: 1 addition & 1,678 deletions Core Classes/ReportGenerator.swift

Large diffs are not rendered by default.

1,731 changes: 1,731 additions & 0 deletions Core Classes/StatsReportFromDate.swift

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions Timesheets.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@
DAFE05501A65F96800E5EF2F /* DoubleExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAFE054F1A65F96800E5EF2F /* DoubleExtensions.swift */; };
F3011743234D750800830EE9 /* HtmlStatsReportFromDate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3011742234D750800830EE9 /* HtmlStatsReportFromDate.swift */; };
F307AD062346B1680093120F /* XCTestExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = F307AD052346B1680093120F /* XCTestExtensions.swift */; };
F33ED8B5237E38DC0020CEBE /* StatsReportFromDate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F33ED8B4237E38DC0020CEBE /* StatsReportFromDate.swift */; };
F33ED8B7237E4EA20020CEBE /* AttendanceRecordForPersonTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F33ED8B6237E4EA20020CEBE /* AttendanceRecordForPersonTests.swift */; };
F33ED8B9237E53890020CEBE /* CoreDataTestSetupHelpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = F33ED8B8237E53890020CEBE /* CoreDataTestSetupHelpers.swift */; };
F37978462336F9F1006F69DA /* ExcelExport in Frameworks */ = {isa = PBXBuildFile; productRef = F37978452336F9F1006F69DA /* ExcelExport */; };
F379784823370877006F69DA /* ReportTransmitter.swift in Sources */ = {isa = PBXBuildFile; fileRef = F379784723370877006F69DA /* ReportTransmitter.swift */; };
F3FDAF852339ACFC002E11EE /* ReportGeneratorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3FDAF842339ACFC002E11EE /* ReportGeneratorTests.swift */; };
Expand Down Expand Up @@ -507,6 +510,9 @@
DAFE054F1A65F96800E5EF2F /* DoubleExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DoubleExtensions.swift; sourceTree = "<group>"; };
F3011742234D750800830EE9 /* HtmlStatsReportFromDate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HtmlStatsReportFromDate.swift; sourceTree = "<group>"; };
F307AD052346B1680093120F /* XCTestExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XCTestExtensions.swift; sourceTree = "<group>"; };
F33ED8B4237E38DC0020CEBE /* StatsReportFromDate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatsReportFromDate.swift; sourceTree = "<group>"; };
F33ED8B6237E4EA20020CEBE /* AttendanceRecordForPersonTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AttendanceRecordForPersonTests.swift; sourceTree = "<group>"; };
F33ED8B8237E53890020CEBE /* CoreDataTestSetupHelpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoreDataTestSetupHelpers.swift; sourceTree = "<group>"; };
F379784723370877006F69DA /* ReportTransmitter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReportTransmitter.swift; sourceTree = "<group>"; };
F3FDAF822339ACFC002E11EE /* TimesheetsTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = TimesheetsTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
F3FDAF842339ACFC002E11EE /* ReportGeneratorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReportGeneratorTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -867,6 +873,7 @@
DA1D2F891B0A9B8E00160AC9 /* StatsManager.swift */,
F379784723370877006F69DA /* ReportTransmitter.swift */,
F3011742234D750800830EE9 /* HtmlStatsReportFromDate.swift */,
F33ED8B4237E38DC0020CEBE /* StatsReportFromDate.swift */,
);
path = "Core Classes";
sourceTree = "<group>";
Expand Down Expand Up @@ -993,6 +1000,8 @@
F3FDAF842339ACFC002E11EE /* ReportGeneratorTests.swift */,
F3FDAF862339ACFC002E11EE /* Info.plist */,
F307AD052346B1680093120F /* XCTestExtensions.swift */,
F33ED8B6237E4EA20020CEBE /* AttendanceRecordForPersonTests.swift */,
F33ED8B8237E53890020CEBE /* CoreDataTestSetupHelpers.swift */,
);
path = TimesheetsTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -1498,6 +1507,7 @@
DAC9C3BB1A493AA4003B0D80 /* AttendanceRecord.swift in Sources */,
DA58B6771A5B7E3E0091A288 /* TableViewCellStyleRecord.swift in Sources */,
DA644F9E1A45F6D2005BAE6A /* EditMaintenanceIssue.swift in Sources */,
F33ED8B5237E38DC0020CEBE /* StatsReportFromDate.swift in Sources */,
DA8D60671A478AED00B7A37C /* GlidingCentre.swift in Sources */,
DA6AD47A1EFB4C9600CA20BD /* UserDefaults.swift in Sources */,
DAF6321E1A4A3CD7003AA1C3 /* AircraftTimesheet.swift in Sources */,
Expand Down Expand Up @@ -1649,8 +1659,10 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
F33ED8B9237E53890020CEBE /* CoreDataTestSetupHelpers.swift in Sources */,
F307AD062346B1680093120F /* XCTestExtensions.swift in Sources */,
F3FDAF852339ACFC002E11EE /* ReportGeneratorTests.swift in Sources */,
F33ED8B7237E4EA20020CEBE /* AttendanceRecordForPersonTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
35 changes: 35 additions & 0 deletions TimesheetsTests/AttendanceRecordForPersonTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// AttendanceRecordForPersonTests.swift
// TimesheetsTests
//
// Created by Hugues Ferland on 2019-11-14.
//

import XCTest
import CoreData
@testable import Timesheets

class AttendanceRecordForPersonTests: XCTestCase {
var helpers = CoreDataTestSetupHelpers()
var pilotJohnDo : Pilot!

override func setUp() {
let centre = helpers.createGlidingCentre("Middle Island")
helpers.setDefaultCentre(centre)

pilotJohnDo = helpers.createPilot(name: "John Do", typeOfParticipant: "COATS")
}

override func tearDown() {
helpers.rollback()
}

func testCreateAttendanceRecordForPersonSigninPilot()
{
print("Pilot is signed in? \(pilotJohnDo.signedIn)")
dataModel.createAttendanceRecordForPerson(pilotJohnDo)

XCTAssertTrue(pilotJohnDo.signedIn, "The pilot must now be signed in (as per the post condition of createAttendanceRecordForPerson.")
}

}
186 changes: 186 additions & 0 deletions TimesheetsTests/CoreDataTestSetupHelpers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
//
// CoreDataTestSetupHelpers.swift
// TimesheetsTests
//
// Created by Hugues Ferland on 2019-11-14.
//

import Foundation
import CoreData
@testable import Timesheets

class CoreDataTestSetupHelpers
{
var context: NSManagedObjectContext
{
return dataModel.managedObjectContext
}

var defaultCentre : GlidingCentre!
var defaultPilot : Pilot!

func setDefaultCentre(_ centre: GlidingCentre)
{
dataModel.glidingCentre = centre
dataModel.previousRecordsGlidingCentre = centre
defaultCentre = centre
}

func setDefaultPilot(_ pilot: Pilot)
{
defaultPilot = pilot
}

func rollback()
{
context.rollback()
}

func createGlidingCentre(_ name : String) -> GlidingCentre
{
let centre = GlidingCentre(context: context)
centre.name = "Middle Island"
return centre
}

func createPilot(name: String,
typeOfParticipant: String,
withBirthDay birthday : Date = Calendar.current.date(byAdding: DateComponents(year: -20), to: Date())!) -> Pilot
{
let pilot = Pilot(context: context)
pilot.name = name
pilot.firstName = name.components(separatedBy: " ")[0]
pilot.fullName = name
pilot.typeOfParticipant = typeOfParticipant
pilot.glidingCentre = defaultCentre
pilot.email = "\(pilot.name.replacingOccurrences(of: " ", with: ""))@hellkitchen.us"
pilot.address = "13 Anywhere"
pilot.aniversaryOfTowAPC = Date().advanced(by: -10)
pilot.aniversaryOfGliderAPC = Date().advanced(by: -10)
pilot.birthday = birthday
pilot.inactive = false
pilot.highestGliderQual = 3
return pilot
}

func createStaffCadet(name: String,
withBirthDay birthday : Date = Calendar.current.date(byAdding: DateComponents(year: -17), to: Date())!,
squadron : Int16 = 123) -> Pilot
{
let pilot = createPilot(name: name, typeOfParticipant: "Staff Cadet", withBirthDay: birthday)
pilot.squadron = squadron
return pilot
}

func createCadet(name: String,
withBirthDay birthday : Date = Calendar.current.date(byAdding: DateComponents(year: -15), to: Date())!,
squadron : Int16 = 123) -> Pilot
{
let pilot = createPilot(name: name, typeOfParticipant: "cadet", withBirthDay: birthday)
pilot.squadron = squadron
return pilot
}

func createFlight(_ aircraft: AircraftEntity, _ timesheet: AircraftTimesheet, startingOn startDate: Date, forMinutes duration: Int16, sequence: TowplaneSequence = .TowCourse, withPilot pilot : Pilot? = nil, withPassenger passenger : Pilot? = nil) -> FlightRecord
{
let flight = FlightRecord(context: context)
flight.aircraft = aircraft
flight.timesheet = timesheet
flight.flightSequence = sequence.rawValue
flight.pilot = pilot ?? defaultPilot
flight.passenger = passenger
flight.timeUp = startDate
flight.timeDown = Calendar.current.date(byAdding: Calendar.Component.minute, value: Int(duration), to: flight.timeUp)!
flight.flightLengthInMinutes = duration
return flight
}

func createGliderFlight(_ aircraft: AircraftEntity, _ timesheet: AircraftTimesheet, startingOn startDate: Date, forMinutes duration: Int16, sequence: GliderSequence = .StudentTrg, withPilot pilot : Pilot? = nil, withPassenger passenger : Pilot? = nil, towByFlight towFlight : FlightRecord? = nil)
{
let flight = FlightRecord(context: context)
flight.aircraft = aircraft
flight.timesheet = timesheet
flight.flightSequence = sequence.rawValue
flight.pilot = pilot ?? defaultPilot
flight.passenger = passenger
flight.timeUp = startDate
flight.timeDown = Calendar.current.date(byAdding: Calendar.Component.minute, value: Int(duration), to: flight.timeUp)!
flight.flightLengthInMinutes = duration
flight.connectedAircraftRecord = towFlight
}

func createTimesheet(_ aircraft : AircraftEntity, _ forDate : Date) -> AircraftTimesheet {
let timesheet = aircraft.insertNewTimeSheetForAircraft(withContext: context)
timesheet.date = forDate
timesheet.glidingCentre = dataModel.glidingCentre
timesheet.initialTTSN = 0
timesheet.glidingCentre = dataModel.glidingCentre
timesheet.setTTSN()
return timesheet
}

func createLaunch(glider : AircraftEntity, launcher : AircraftEntity, takeOffDate : Date, withPilot pilot : Pilot)
{
let gliderTimesheet = createTimesheet(glider, takeOffDate)
let launcherTimesheet = createTimesheet(launcher, takeOffDate)
let launcherFlight = createFlight(launcher, launcherTimesheet, startingOn: takeOffDate, forMinutes: 20, sequence: .TowCourse)
createGliderFlight(glider, gliderTimesheet, startingOn: takeOffDate, forMinutes: 20, sequence: .Famil, withPilot: pilot, towByFlight: launcherFlight)

launcher.updateTTSN()
glider.updateTTSN()
}

func createTowPlane(registration: String, tailNumber: String) -> AircraftEntity
{
let aircraft = AircraftEntity(context: context)
aircraft.registration = registration
aircraft.tailNumber = tailNumber
aircraft.type = .towplane
aircraft.gliderOrTowplane = Int16(aircraft.type.rawValue)
aircraft.glidingCentre = defaultCentre
return aircraft
}

func createGlider(registration: String, tailNumber: String) -> AircraftEntity
{
let aircraft = AircraftEntity(context: context)
aircraft.registration = registration
aircraft.tailNumber = tailNumber
aircraft.type = .glider
aircraft.gliderOrTowplane = Int16(aircraft.type.rawValue)
aircraft.glidingCentre = defaultCentre
return aircraft
}

func createAutoTow() -> AircraftEntity
{
let aircraft = AircraftEntity(context: context)
aircraft.registration = "AUTO"
aircraft.tailNumber = "GO-UP"
aircraft.type = .auto
aircraft.gliderOrTowplane = Int16(aircraft.type.rawValue)
aircraft.glidingCentre = defaultCentre
return aircraft
}

func createWinchTow(registration : String, tailNumber : String) -> AircraftEntity
{
let aircraft = AircraftEntity(context: context)
aircraft.registration = registration
aircraft.tailNumber = tailNumber
aircraft.type = .winch
aircraft.gliderOrTowplane = Int16(aircraft.type.rawValue)
aircraft.glidingCentre = defaultCentre
return aircraft
}

func createMaintenance(for aircraft: AircraftEntity, on date: Date, withComment comment: String) -> MaintenanceEvent
{
let event = MaintenanceEvent(context: context)
event.aircraft = aircraft
event.date = date
event.comment = comment
return event
}

}
Loading

0 comments on commit 307c46f

Please sign in to comment.