-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Bracket creation and simulation tests (#8)
* feat: Color styling for pending bracket view games * test: Add bracket simulation tests * feat: Filter by league for live game scores * chore: Asset symbol extension setting * feat: Add refresh button for live games
- Loading branch information
Showing
9 changed files
with
278 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// | ||
// CoreDataTestStack.swift | ||
// foam-madness-tests | ||
// | ||
// Created by Michael Virgo on 5/20/24. | ||
// Copyright © 2024 mvirgo. All rights reserved. | ||
// | ||
|
||
import CoreData | ||
|
||
class CoreDataTestStack { | ||
static let shared = CoreDataTestStack() | ||
|
||
private static var persistentContainer: NSPersistentContainer = { | ||
let container = NSPersistentContainer(name: "foam-madness") | ||
let description = NSPersistentStoreDescription() | ||
description.type = NSInMemoryStoreType | ||
container.persistentStoreDescriptions = [description] | ||
container.loadPersistentStores { (storeDescription, error) in | ||
if let error = error as NSError? { | ||
fatalError("Unresolved error \(error), \(error.userInfo)") | ||
} | ||
} | ||
return container | ||
}() | ||
|
||
var context: NSManagedObjectContext { | ||
return Self.persistentContainer.viewContext | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
foam-madness-tests/foam_madness_sim_custom_bracket_test.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// | ||
// foam_madness_sim_custom_bracket_test.swift | ||
// foam-madness-tests | ||
// | ||
// Created by Michael Virgo on 5/20/24. | ||
// Copyright © 2024 mvirgo. All rights reserved. | ||
// | ||
|
||
import CoreData | ||
import XCTest | ||
@testable import foam_madness | ||
|
||
final class foam_madness_sim_custom_bracket_test: XCTestCase { | ||
var bracketCreationController: BracketCreationController! | ||
var viewContext: NSManagedObjectContext! | ||
|
||
override func setUpWithError() throws { | ||
viewContext = CoreDataTestStack.shared.context | ||
bracketCreationController = BracketCreationController(context: viewContext) | ||
} | ||
|
||
override func tearDownWithError() throws { | ||
viewContext = nil | ||
} | ||
|
||
func createCustomTournament(numTeams: Int, fillType: CustomType) -> Tournament { | ||
return bracketCreationController.createCustomBracket( | ||
numTeams: numTeams, | ||
isWomens: false, | ||
tournamentName: "\(fillType)\(numTeams)", | ||
isSimulated: true, | ||
useLeft: false, | ||
shotsPerRound: AppConstants.defaultShotsPerRound) | ||
} | ||
|
||
// Check all allowed custom bracket sizes with random fills | ||
func testCreateAndSimRandomFillCustomBracket() throws { | ||
for numTeams in numTeamsArray { | ||
let tournament = createCustomTournament(numTeams: numTeams, fillType: CustomType.random) | ||
bracketCreationController.fillTournamentWithRandomTeams(tournament) | ||
let winner = bracketCreationController.simulateTournament(tournament: tournament) | ||
|
||
XCTAssert(winner != "") | ||
} | ||
} | ||
|
||
// Check all allowed custom bracket sizes with an existing bracket fill | ||
func testCreateAndSimExistingFillCustomBracket() throws { | ||
let brackets = BracketHelper.loadBrackets() | ||
let fillBracket = brackets.randomElement()! | ||
|
||
for numTeams in numTeamsArray { | ||
if (numTeams == 4) { | ||
// Skip existing for 4 team bracket - not allowed | ||
continue | ||
} | ||
let tournament = createCustomTournament(numTeams: numTeams, fillType: CustomType.existing) | ||
bracketCreationController.fillTournamentFromExistingCustom(tournament, fillBracket.file) | ||
let winner = bracketCreationController.simulateTournament(tournament: tournament) | ||
|
||
XCTAssert(winner != "") | ||
} | ||
} | ||
|
||
// Check creation of custom brackets has no teams initially filled | ||
func testCreateCustomBracketHasNoTeams() throws { | ||
for numTeams in numTeamsArray { | ||
let tournament = createCustomTournament(numTeams: numTeams, fillType: CustomType.selectAll) | ||
let games = Array(tournament.games!) as! [Game] | ||
let minRound = games.min(by: { $0.round < $1.round })?.round ?? 0 | ||
let gamesInMinRound = games.filter({ $0.round == minRound }).count | ||
|
||
XCTAssert(gamesInMinRound == numTeams / 2) | ||
|
||
for game in games { | ||
if game.teams?.count ?? 0 > 0 { | ||
XCTFail("Improperly found set teams in selectAll custom tournament of size \(numTeams)") | ||
} | ||
} | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
foam-madness-tests/foam_madness_sim_existing_bracket_test.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// | ||
// foam_madness_sim_existing_bracket_test.swift | ||
// foam-madness-tests | ||
// | ||
// Created by Michael Virgo on 5/20/24. | ||
// Copyright © 2024 mvirgo. All rights reserved. | ||
// | ||
|
||
import CoreData | ||
import XCTest | ||
@testable import foam_madness | ||
|
||
final class foam_madness_sim_existing_bracket_test: XCTestCase { | ||
var bracketCreationController: BracketCreationController! | ||
var viewContext: NSManagedObjectContext! | ||
|
||
override func setUpWithError() throws { | ||
viewContext = CoreDataTestStack.shared.context | ||
bracketCreationController = BracketCreationController(context: viewContext) | ||
} | ||
|
||
override func tearDownWithError() throws { | ||
viewContext = nil | ||
} | ||
|
||
func testCreateAndSimExistingBrackets() throws { | ||
let brackets = BracketHelper.loadBrackets() | ||
|
||
for bracket in brackets { | ||
let tournament = bracketCreationController.createBracketFromFile( | ||
bracketLocation: bracket.file, | ||
tournamentName: bracket.name, | ||
isSimulated: true, | ||
useLeft: false, | ||
shotsPerRound: AppConstants.defaultShotsPerRound | ||
) | ||
let winner = bracketCreationController.simulateTournament(tournament: tournament) | ||
|
||
XCTAssert(winner != "") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// | ||
// LiveGamesModel.swift | ||
// foam-madness | ||
// | ||
// Created by Michael Virgo on 5/20/24. | ||
// Copyright © 2024 mvirgo. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
|
||
class LiveGamesModel: ObservableObject { | ||
@Published var liveGames: [Event] = [] | ||
@Published var loading = false | ||
private var lastRefreshTime: Date? | ||
|
||
init() { | ||
refreshData() | ||
} | ||
|
||
func refreshData() { | ||
let now = Date() | ||
if let lastRefreshTime = lastRefreshTime, now.timeIntervalSince(lastRefreshTime) < AppConstants.refreshDebounceSeconds { | ||
// Skip if within the debounce delay | ||
return | ||
} | ||
lastRefreshTime = now | ||
loadGames() | ||
} | ||
|
||
private func loadGames() { | ||
loading = true | ||
liveGames = [] | ||
APIClient.getScores(url: APIClient.Endpoints.getNCAAMScores.url, completion: handleLiveGameScores(response:error:)) | ||
APIClient.getScores(url: APIClient.Endpoints.getNCAAWScores.url, completion: handleLiveGameScores(response:error:)) | ||
APIClient.getScores(url: APIClient.Endpoints.getNBAScores.url, completion: handleLiveGameScores(response:error:)) | ||
APIClient.getScores(url: APIClient.Endpoints.getWNBAScores.url, completion: handleLiveGameScores(response:error:)) | ||
} | ||
|
||
private func handleLiveGameScores(response: LiveGamesResponse?, error: Error?) { | ||
if let error = error { | ||
print(error.localizedDescription) | ||
} else if let response = response { | ||
// Add events to liveGames array | ||
for (_, var game) in response.events.enumerated() { | ||
// Note that there will only be one league in NCAA or (W)NBA APIs | ||
// Add game | ||
game.league = response.leagues[0].abbreviation | ||
liveGames.append(game) | ||
} | ||
} | ||
loading = false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.