From 91c17aca41b16b7792488b2f3978a82c7a759bb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 20 Jan 2021 19:14:27 +0100 Subject: [PATCH] GRDBCombineDemo tests --- .../GRDBCombineDemo.xcodeproj/project.pbxproj | 127 ++++++++++- .../xcschemes/GRDBCombineDemo.xcscheme | 88 ++++++++ .../GRDBCombineDemo/Player.swift | 5 +- .../AppDatabaseTests.swift | 206 ++++++++++++++++++ .../GRDBCombineDemoTests/Info.plist | 22 ++ .../GRDBCombineDemoTests/PlayerTests.swift | 102 +++++++++ 6 files changed, 547 insertions(+), 3 deletions(-) create mode 100644 Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo.xcodeproj/xcshareddata/xcschemes/GRDBCombineDemo.xcscheme create mode 100644 Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemoTests/AppDatabaseTests.swift create mode 100644 Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemoTests/Info.plist create mode 100644 Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemoTests/PlayerTests.swift diff --git a/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo.xcodeproj/project.pbxproj b/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo.xcodeproj/project.pbxproj index 93ad8ffa0..853e801c3 100644 --- a/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo.xcodeproj/project.pbxproj +++ b/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo.xcodeproj/project.pbxproj @@ -7,6 +7,8 @@ objects = { /* Begin PBXBuildFile section */ + 56026CAC25B8A7EF00D1DF3F /* PlayerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56026CAA25B8A7EF00D1DF3F /* PlayerTests.swift */; }; + 56026CAD25B8A7EF00D1DF3F /* AppDatabaseTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56026CAB25B8A7EF00D1DF3F /* AppDatabaseTests.swift */; }; 567C3E1A2520B6DE0011F6E9 /* GRDBCombineDemoApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567C3E192520B6DE0011F6E9 /* GRDBCombineDemoApp.swift */; }; 567C3E1E2520B6DF0011F6E9 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 567C3E1D2520B6DF0011F6E9 /* Assets.xcassets */; }; 567C3E212520B6DF0011F6E9 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 567C3E202520B6DF0011F6E9 /* Preview Assets.xcassets */; }; @@ -26,6 +28,13 @@ /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ + 56026C9D25B8A7D000D1DF3F /* PBXContainerItemProxy */ = { + isa = PBXContainerItemProxy; + containerPortal = 567C3E0E2520B6DE0011F6E9 /* Project object */; + proxyType = 1; + remoteGlobalIDString = 567C3E152520B6DE0011F6E9; + remoteInfo = GRDBCombineDemo; + }; 567C3E342520B7000011F6E9 /* PBXContainerItemProxy */ = { isa = PBXContainerItemProxy; containerPortal = 567C3E292520B7000011F6E9 /* GRDB.xcodeproj */; @@ -106,6 +115,10 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + 56026C9825B8A7D000D1DF3F /* GRDBCombineDemoTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = GRDBCombineDemoTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; + 56026C9C25B8A7D000D1DF3F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 56026CAA25B8A7EF00D1DF3F /* PlayerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PlayerTests.swift; sourceTree = ""; }; + 56026CAB25B8A7EF00D1DF3F /* AppDatabaseTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDatabaseTests.swift; sourceTree = ""; }; 567C3E162520B6DE0011F6E9 /* GRDBCombineDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = GRDBCombineDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 567C3E192520B6DE0011F6E9 /* GRDBCombineDemoApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GRDBCombineDemoApp.swift; sourceTree = ""; }; 567C3E1D2520B6DF0011F6E9 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; @@ -126,6 +139,13 @@ /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ + 56026C9525B8A7D000D1DF3F /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; 567C3E132520B6DE0011F6E9 /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; @@ -137,6 +157,16 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + 56026C9925B8A7D000D1DF3F /* GRDBCombineDemoTests */ = { + isa = PBXGroup; + children = ( + 56026C9C25B8A7D000D1DF3F /* Info.plist */, + 56026CAB25B8A7EF00D1DF3F /* AppDatabaseTests.swift */, + 56026CAA25B8A7EF00D1DF3F /* PlayerTests.swift */, + ); + path = GRDBCombineDemoTests; + sourceTree = ""; + }; 56185BC125B8047D00B9C30F /* Resources */ = { isa = PBXGroup; children = ( @@ -160,6 +190,7 @@ children = ( 567C3E292520B7000011F6E9 /* GRDB.xcodeproj */, 567C3E182520B6DE0011F6E9 /* GRDBCombineDemo */, + 56026C9925B8A7D000D1DF3F /* GRDBCombineDemoTests */, 567C3E172520B6DE0011F6E9 /* Products */, 567C3E4D2520B70E0011F6E9 /* Frameworks */, ); @@ -169,6 +200,7 @@ isa = PBXGroup; children = ( 567C3E162520B6DE0011F6E9 /* GRDBCombineDemo.app */, + 56026C9825B8A7D000D1DF3F /* GRDBCombineDemoTests.xctest */, ); name = Products; sourceTree = ""; @@ -242,6 +274,24 @@ /* End PBXGroup section */ /* Begin PBXNativeTarget section */ + 56026C9725B8A7D000D1DF3F /* GRDBCombineDemoTests */ = { + isa = PBXNativeTarget; + buildConfigurationList = 56026CA825B8A7D000D1DF3F /* Build configuration list for PBXNativeTarget "GRDBCombineDemoTests" */; + buildPhases = ( + 56026C9425B8A7D000D1DF3F /* Sources */, + 56026C9525B8A7D000D1DF3F /* Frameworks */, + 56026C9625B8A7D000D1DF3F /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + 56026C9E25B8A7D000D1DF3F /* PBXTargetDependency */, + ); + name = GRDBCombineDemoTests; + productName = GRDBCombineDemoTests; + productReference = 56026C9825B8A7D000D1DF3F /* GRDBCombineDemoTests.xctest */; + productType = "com.apple.product-type.bundle.unit-test"; + }; 567C3E152520B6DE0011F6E9 /* GRDBCombineDemo */ = { isa = PBXNativeTarget; buildConfigurationList = 567C3E252520B6DF0011F6E9 /* Build configuration list for PBXNativeTarget "GRDBCombineDemo" */; @@ -267,9 +317,13 @@ 567C3E0E2520B6DE0011F6E9 /* Project object */ = { isa = PBXProject; attributes = { - LastSwiftUpdateCheck = 1200; + LastSwiftUpdateCheck = 1230; LastUpgradeCheck = 1200; TargetAttributes = { + 56026C9725B8A7D000D1DF3F = { + CreatedOnToolsVersion = 12.3; + TestTargetID = 567C3E152520B6DE0011F6E9; + }; 567C3E152520B6DE0011F6E9 = { CreatedOnToolsVersion = 12.0; }; @@ -295,6 +349,7 @@ projectRoot = ""; targets = ( 567C3E152520B6DE0011F6E9 /* GRDBCombineDemo */, + 56026C9725B8A7D000D1DF3F /* GRDBCombineDemoTests */, ); }; /* End PBXProject section */ @@ -359,6 +414,13 @@ /* End PBXReferenceProxy section */ /* Begin PBXResourcesBuildPhase section */ + 56026C9625B8A7D000D1DF3F /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; 567C3E142520B6DE0011F6E9 /* Resources */ = { isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; @@ -373,6 +435,15 @@ /* End PBXResourcesBuildPhase section */ /* Begin PBXSourcesBuildPhase section */ + 56026C9425B8A7D000D1DF3F /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 56026CAC25B8A7EF00D1DF3F /* PlayerTests.swift in Sources */, + 56026CAD25B8A7EF00D1DF3F /* AppDatabaseTests.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; 567C3E122520B6DE0011F6E9 /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; @@ -393,6 +464,11 @@ /* End PBXSourcesBuildPhase section */ /* Begin PBXTargetDependency section */ + 56026C9E25B8A7D000D1DF3F /* PBXTargetDependency */ = { + isa = PBXTargetDependency; + target = 567C3E152520B6DE0011F6E9 /* GRDBCombineDemo */; + targetProxy = 56026C9D25B8A7D000D1DF3F /* PBXContainerItemProxy */; + }; 567C3E522520B71C0011F6E9 /* PBXTargetDependency */ = { isa = PBXTargetDependency; name = GRDBiOS; @@ -420,6 +496,46 @@ /* End PBXVariantGroup section */ /* Begin XCBuildConfiguration section */ + 56026C9F25B8A7D000D1DF3F /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + BUNDLE_LOADER = "$(TEST_HOST)"; + CODE_SIGN_STYLE = Automatic; + INFOPLIST_FILE = GRDBCombineDemoTests/Info.plist; + IPHONEOS_DEPLOYMENT_TARGET = 14.3; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + "@loader_path/Frameworks", + ); + PRODUCT_BUNDLE_IDENTIFIER = com.github.groue.GRDBCombineDemoTests; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + TEST_HOST = "$(BUILT_PRODUCTS_DIR)/GRDBCombineDemo.app/GRDBCombineDemo"; + }; + name = Debug; + }; + 56026CA025B8A7D000D1DF3F /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + BUNDLE_LOADER = "$(TEST_HOST)"; + CODE_SIGN_STYLE = Automatic; + INFOPLIST_FILE = GRDBCombineDemoTests/Info.plist; + IPHONEOS_DEPLOYMENT_TARGET = 14.3; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + "@loader_path/Frameworks", + ); + PRODUCT_BUNDLE_IDENTIFIER = com.github.groue.GRDBCombineDemoTests; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + TEST_HOST = "$(BUILT_PRODUCTS_DIR)/GRDBCombineDemo.app/GRDBCombineDemo"; + }; + name = Release; + }; 567C3E232520B6DF0011F6E9 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { @@ -583,6 +699,15 @@ /* End XCBuildConfiguration section */ /* Begin XCConfigurationList section */ + 56026CA825B8A7D000D1DF3F /* Build configuration list for PBXNativeTarget "GRDBCombineDemoTests" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 56026C9F25B8A7D000D1DF3F /* Debug */, + 56026CA025B8A7D000D1DF3F /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; 567C3E112520B6DE0011F6E9 /* Build configuration list for PBXProject "GRDBCombineDemo" */ = { isa = XCConfigurationList; buildConfigurations = ( diff --git a/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo.xcodeproj/xcshareddata/xcschemes/GRDBCombineDemo.xcscheme b/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo.xcodeproj/xcshareddata/xcschemes/GRDBCombineDemo.xcscheme new file mode 100644 index 000000000..3b324622e --- /dev/null +++ b/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo.xcodeproj/xcshareddata/xcschemes/GRDBCombineDemo.xcscheme @@ -0,0 +1,88 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Player.swift b/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Player.swift index 3c37da3c9..9bb6843fa 100644 --- a/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Player.swift +++ b/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Player.swift @@ -2,8 +2,9 @@ import GRDB /// The Player struct. /// -/// Identifiable conformance supports SwiftUI list animations -struct Player: Identifiable { +/// Identifiable conformance supports SwiftUI list animations. +/// Equatable conformance supports tests. +struct Player: Identifiable, Equatable { /// The player id. /// /// Int64 is the recommended type for auto-incremented database ids. diff --git a/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemoTests/AppDatabaseTests.swift b/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemoTests/AppDatabaseTests.swift new file mode 100644 index 000000000..c2812385d --- /dev/null +++ b/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemoTests/AppDatabaseTests.swift @@ -0,0 +1,206 @@ +import XCTest +import GRDB +@testable import GRDBCombineDemo + +class AppDatabaseTests: XCTestCase { + func test_database_schema() throws { + // Given an empty database + let dbQueue = DatabaseQueue() + + // When we instantiate an AppDatabase + _ = try AppDatabase(dbQueue) + + // Then the player table exists, with id, name & score columns + try dbQueue.read { db in + try XCTAssert(db.tableExists("player")) + let columns = try db.columns(in: "player") + let columnNames = Set(columns.map { $0.name }) + XCTAssertEqual(columnNames, ["id", "name", "score"]) + } + } + + func test_savePlayer_inserts() throws { + // Given an empty players database + let dbQueue = DatabaseQueue() + let appDatabase = try AppDatabase(dbQueue) + + // When we save a new player + var player = Player(id: nil, name: "Arthur", score: 100) + try appDatabase.savePlayer(&player) + + // Then the player exists in the database + try XCTAssertTrue(dbQueue.read(player.exists)) + } + + func test_savePlayer_updates() throws { + // Given a players database that contains a player + let dbQueue = DatabaseQueue() + let appDatabase = try AppDatabase(dbQueue) + var player = Player(id: nil, name: "Arthur", score: 100) + try dbQueue.write { db in + try player.insert(db) + } + + // When we modify and save the player + player.name = "Barbara" + player.score = 1000 + try appDatabase.savePlayer(&player) + + // Then the player has been updated in the database + let fetchedPlayer = try dbQueue.read { db in + try XCTUnwrap(Player.fetchOne(db, key: player.id)) + } + XCTAssertEqual(fetchedPlayer, player) + } + + func test_deletePlayers() throws { + // Given a players database that contains four players + let dbQueue = DatabaseQueue() + let appDatabase = try AppDatabase(dbQueue) + var player1 = Player(id: nil, name: "Arthur", score: 100) + var player2 = Player(id: nil, name: "Barbara", score: 200) + var player3 = Player(id: nil, name: "Craig", score: 150) + var player4 = Player(id: nil, name: "David", score: 120) + try dbQueue.write { db in + try player1.insert(db) + try player2.insert(db) + try player3.insert(db) + try player4.insert(db) + } + + // When we delete two players + try appDatabase.deletePlayers(ids: [player1.id!, player3.id!]) + + // Then the deleted players no longer exist + try dbQueue.read { db in + try XCTAssertFalse(player1.exists(db)) + try XCTAssertFalse(player3.exists(db)) + } + + // Then the database still contains two players + try XCTAssertEqual(dbQueue.read(Player.fetchCount), 2) + } + + func test_deleteAllPlayers() throws { + // Given a players database that contains players + let dbQueue = DatabaseQueue() + let appDatabase = try AppDatabase(dbQueue) + var player1 = Player(id: nil, name: "Arthur", score: 100) + var player2 = Player(id: nil, name: "Barbara", score: 200) + var player3 = Player(id: nil, name: "Craig", score: 150) + var player4 = Player(id: nil, name: "David", score: 120) + try dbQueue.write { db in + try player1.insert(db) + try player2.insert(db) + try player3.insert(db) + try player4.insert(db) + } + + // When we delete all players + try appDatabase.deleteAllPlayers() + + // Then the database does not contain any player + try XCTAssertEqual(dbQueue.read(Player.fetchCount), 0) + } + + func test_refreshPlayers_populates_an_empty_database() throws { + // Given an empty players database + let dbQueue = DatabaseQueue() + let appDatabase = try AppDatabase(dbQueue) + + // When we refresh players + try appDatabase.refreshPlayers() + + // Then the database is not empty + try XCTAssert(dbQueue.read(Player.fetchCount) > 0) + } + + func test_createRandomPlayersIfEmpty_populates_an_empty_database() throws { + // Given an empty players database + let dbQueue = DatabaseQueue() + let appDatabase = try AppDatabase(dbQueue) + + // When we create random players + try appDatabase.createRandomPlayersIfEmpty() + + // Then the database is not empty + try XCTAssert(dbQueue.read(Player.fetchCount) > 0) + } + + func test_createRandomPlayersIfEmpty_does_not_modify_a_non_empty_database() throws { + // Given a players database that contains one player + let dbQueue = DatabaseQueue() + let appDatabase = try AppDatabase(dbQueue) + var player = Player(id: nil, name: "Arthur", score: 100) + try dbQueue.write { db in + try player.insert(db) + } + + // When we create random players + try appDatabase.createRandomPlayersIfEmpty() + + // Then the database still only contains the original player + let players = try dbQueue.read(Player.fetchAll) + XCTAssertEqual(players, [player]) + } + + func test_playersOrderedByNamePublisher() throws { + // Given a players database that contains two players + let dbQueue = DatabaseQueue() + let appDatabase = try AppDatabase(dbQueue) + var player1 = Player(id: nil, name: "Arthur", score: 100) + var player2 = Player(id: nil, name: "Barbara", score: 1000) + try dbQueue.write { db in + try player1.insert(db) + try player2.insert(db) + } + + // When we observe players and wait for the first value + let exp = expectation(description: "Players") + var players: [Player]? + let cancellable = appDatabase.playersOrderedByNamePublisher().sink { completion in + if case let .failure(error) = completion { + XCTFail("Unexpected error \(error)") + } + } receiveValue: { + players = $0 + exp.fulfill() + } + withExtendedLifetime(cancellable) { + waitForExpectations(timeout: 1, handler: nil) + } + + // Then the players are the two players ordered by name + XCTAssertEqual(players, [player1, player2]) + } + + func test_playersOrderedByScorePublisher() throws { + // Given a players database that contains two players + let dbQueue = DatabaseQueue() + let appDatabase = try AppDatabase(dbQueue) + var player1 = Player(id: nil, name: "Arthur", score: 100) + var player2 = Player(id: nil, name: "Barbara", score: 1000) + try dbQueue.write { db in + try player1.insert(db) + try player2.insert(db) + } + + // When we observe players and wait for the first value + let exp = expectation(description: "Players") + var players: [Player]? + let cancellable = appDatabase.playersOrderedByScorePublisher().sink { completion in + if case let .failure(error) = completion { + XCTFail("Unexpected error \(error)") + } + } receiveValue: { + players = $0 + exp.fulfill() + } + withExtendedLifetime(cancellable) { + waitForExpectations(timeout: 1, handler: nil) + } + + // Then the players are the two players ordered by score descending + XCTAssertEqual(players, [player2, player1]) + } +} diff --git a/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemoTests/Info.plist b/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemoTests/Info.plist new file mode 100644 index 000000000..64d65ca49 --- /dev/null +++ b/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemoTests/Info.plist @@ -0,0 +1,22 @@ + + + + + CFBundleDevelopmentRegion + $(DEVELOPMENT_LANGUAGE) + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + $(PRODUCT_BUNDLE_PACKAGE_TYPE) + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemoTests/PlayerTests.swift b/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemoTests/PlayerTests.swift new file mode 100644 index 000000000..a7433ae9d --- /dev/null +++ b/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemoTests/PlayerTests.swift @@ -0,0 +1,102 @@ +import XCTest +import GRDB +@testable import GRDBCombineDemo + +class PlayerTests: XCTestCase { + func testInsert() throws { + // Given an empty players database + let dbQueue = DatabaseQueue() + _ = try AppDatabase(dbQueue) + + // When we insert a player + var player = Player(id: nil, name: "Arthur", score: 100) + try dbQueue.write { db in + try player.insert(db) + } + + // Then the player gets a non-nil id + XCTAssertNotNil(player.id) + } + + func testRoundtrip() throws { + // Given an empty players database + let dbQueue = DatabaseQueue() + _ = try AppDatabase(dbQueue) + + // When we insert a player and fetch the player with the same id + var insertedPlayer = Player(id: nil, name: "Arthur", score: 100) + let fetchedPlayer: Player? = try dbQueue.write { db in + try insertedPlayer.insert(db) + return try Player.fetchOne(db, key: insertedPlayer.id) + } + + // Then the fetched player is equal to the inserted player + XCTAssertEqual(insertedPlayer, fetchedPlayer) + } + + func testOrderedByScore() throws { + // Given an players database that contains players with distinct scores + let dbQueue = DatabaseQueue() + _ = try AppDatabase(dbQueue) + var player1 = Player(id: 1, name: "Arthur", score: 100) + var player2 = Player(id: 2, name: "Barbara", score: 200) + var player3 = Player(id: 3, name: "Craig", score: 150) + var player4 = Player(id: 4, name: "David", score: 120) + try dbQueue.write { db in + try player1.insert(db) + try player2.insert(db) + try player3.insert(db) + try player4.insert(db) + } + + // When we fetch players ordered by score + let players = try dbQueue.read(Player.all().orderedByScore().fetchAll) + + // Then fetched players are ordered by score descending + XCTAssertEqual(players, [player2, player3, player4, player1]) + } + + func testOrderedByScoreSortsIdenticalScoresByName() throws { + // Given an players database that contains players with common scores + let dbQueue = DatabaseQueue() + _ = try AppDatabase(dbQueue) + var player1 = Player(id: 1, name: "Arthur", score: 100) + var player2 = Player(id: 2, name: "Barbara", score: 200) + var player3 = Player(id: 3, name: "Craig", score: 200) + var player4 = Player(id: 4, name: "David", score: 200) + try dbQueue.write { db in + try player1.insert(db) + try player2.insert(db) + try player3.insert(db) + try player4.insert(db) + } + + // When we fetch players ordered by score + let players = try dbQueue.read(Player.all().orderedByScore().fetchAll) + + // Then fetched players are ordered by score descending and by name + XCTAssertEqual(players, [player2, player3, player4, player1]) + } + + func testOrderedByName() throws { + // Given an players database that contains players with distinct names + let dbQueue = DatabaseQueue() + _ = try AppDatabase(dbQueue) + var player1 = Player(id: 1, name: "Arthur", score: 100) + var player2 = Player(id: 2, name: "Barbara", score: 200) + var player3 = Player(id: 3, name: "Craig", score: 150) + var player4 = Player(id: 4, name: "David", score: 120) + try dbQueue.write { db in + try player1.insert(db) + try player2.insert(db) + try player3.insert(db) + try player4.insert(db) + } + + // When we fetch players ordered by name + let players = try dbQueue.read(Player.all().orderedByName().fetchAll) + + // Then fetched players are ordered by name + XCTAssertEqual(players, [player1, player2, player3, player4]) + } +}