### Testing Fresh Database Installation with GRDBSnapshotTesting (Swift) Source: https://github.com/groue/grdbsnapshottesting/blob/main/Sources/GRDBSnapshotTesting/Documentation.docc/TestingDatabaseContent.md This snippet demonstrates how to test a fresh installation of an application's database. It initializes an in-memory `DatabaseQueue`, migrates it to the latest version using a `DatabaseMigrator`, and then asserts its schema and content against a snapshot using `assertSnapshot(of:as:.dumpContent())`. This ensures the initial database setup is correct. ```Swift import XCTest import GRDB import GRDBSnapshotTesting import SnapshotTesting final class MyDatabaseTests: XCTestCase { var migrator: DatabaseMigrator { // Return the DatabaseMigrator for your database } func test_migrate_empty_database_to_latest_version() throws { // Given an empty and temporary in-memory database, let dbQueue = try DatabaseQueue() // When it is migrated to the latest version, try migrator.migrate(dbQueue) // Then it contains the expected schema and content. assertSnapshot(of: dbQueue, as: .dumpContent()) } } ``` -------------------------------- ### Testing Multiple SQL Statements with GRDBSnapshotTesting (Swift) Source: https://github.com/groue/grdbsnapshottesting/blob/main/Sources/GRDBSnapshotTesting/Documentation.docc/TestingDatabaseRequests.md This snippet demonstrates how to test multiple SQL statements, including DDL and DML, by joining them with semicolons within a multi-line string. It uses `assertSnapshot` to capture the state after executing these statements, showcasing the ability to test complex database setup and query sequences. ```Swift func test_sql() throws { let dbQueue = try DatabaseQueue() try dbQueue.write { db in assertSnapshot( of: """ CREATE TABLE player(id, name, score); INSERT INTO player VALUES (1, 'Arthur', 1000); INSERT INTO player VALUES (2, 'Barbara', 500); SELECT * FROM player ORDER BY name; SELECT MAX(score) AS maxScore FROM player; """, as: .dump(db)) } } ``` -------------------------------- ### Testing Full Database Content with GRDBSnapshotTesting (Swift) Source: https://github.com/groue/grdbsnapshottesting/blob/main/README.md This test function demonstrates how to capture and assert the full content of a GRDB database using `assertInlineSnapshot` and the `.dumpContent()` strategy. It includes both the schema (`sqlite_master`) and the data for the 'player' table, providing a comprehensive snapshot of the database state. This is useful for verifying the initial setup or the complete state after a series of operations. ```Swift import GRDB import GRDBSnapshotTesting import InlineSnapshotTesting import XCTest class MyDatabaseTests: XCTestCase { func test_full_database_content() throws { let dbQueue = try makeMyDatabase() assertInlineSnapshot(of: dbQueue, as: .dumpContent()) { """ sqlite_master CREATE TABLE player ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, score INTEGER NOT NULL); player - id: 1 name: 'Arthur' score: 500 - id: 2 name: 'Barbara' score: 1000 """ } } ``` -------------------------------- ### Demonstrating Ordered vs. Unordered SQL Requests for Snapshot Testing (Swift) Source: https://github.com/groue/grdbsnapshottesting/blob/main/Sources/GRDBSnapshotTesting/Documentation.docc/TestingDatabaseRequests.md This example highlights the importance of providing an explicit order (e.g., `ORDER BY id`) for SQL requests when performing snapshot testing to ensure reliable and consistent results. It contrasts an unordered request, which is not recommended, with a totally ordered request, which is recommended for stable snapshots. ```Swift // NOT RECOMMENDED: unordered request let request: SQLRequest = "SELECT * FROM player" assertSnapshot(of: request, as: .dump(db)) // RECOMMENDED: totally ordered request let request: SQLRequest = "SELECT * FROM player ORDER BY id" assertSnapshot(of: request, as: .dump(db)) ``` -------------------------------- ### Testing Single Raw SQL Literal with GRDBSnapshotTesting (Swift) Source: https://github.com/groue/grdbsnapshottesting/blob/main/Sources/GRDBSnapshotTesting/Documentation.docc/TestingDatabaseRequests.md This example illustrates how to use `assertSnapshot` to test a single raw SQL literal string. It performs a write operation on a `DatabaseQueue` and asserts the snapshot of the SQL query's result, demonstrating support for direct SQL string testing. ```Swift func test_sql() throws { let dbQueue = try DatabaseQueue() try dbQueue.write { db in assertSnapshot( of: "SELECT * FROM player ORDER BY id", as: .dump(db)) } } ``` -------------------------------- ### Configuring Swift Package Manager for Database Snapshot Testing Source: https://github.com/groue/grdbsnapshottesting/blob/main/Sources/GRDBSnapshotTesting/Documentation.docc/TestingDatabaseContent.md This snippet shows the Swift Package Manager configuration (`Package.swift`) required to include GRDBSnapshotTesting and its dependencies in a test target. It specifies the necessary product dependencies, excludes the `__Snapshots__` directory, and includes `Fixtures` as resources, enabling the use of database fixtures in tests. ```Swift // Package.swift: .testTarget( name: "MyDatabaseTests", dependencies: [ "MyDatabase", .product(name: "GRDB", package: "GRDB.swift"), .product(name: "GRDBSnapshotTesting", package: "GRDBSnapshotTesting"), .product(name: "SnapshotTesting", package: "swift-snapshot-testing") ], exclude: ["__Snapshots__"], resources: [.copy("Fixtures")] ) ``` -------------------------------- ### Testing Specific Database Migration from v1 to v2 (Swift) Source: https://github.com/groue/grdbsnapshottesting/blob/main/Sources/GRDBSnapshotTesting/Documentation.docc/TestingDatabaseContent.md This snippet illustrates testing a specific database migration from version 1 to version 2. It loads an in-memory copy of a `database_v1_empty.sqlite` fixture, applies the migration up to 'v2' using `migrator.migrate(dbQueue, upTo: "v2")`, and then verifies the resulting database content with a snapshot. This ensures the migration correctly transforms an empty v1 database. ```Swift import XCTest import GRDB import GRDBSnapshotTesting import SnapshotTesting final class MyDatabaseMigrationsTests: XCTestCase { var migrator: DatabaseMigrator { // Return the DatabaseMigrator for your database } func test_migrate_empty_v1_to_v2() throws { // Given a copy of the database_v1_empty.sqlite fixture let path = try XCTUnwrap(Bundle.module.path( forResource: "database_v1_empty", ofType: "sqlite", inDirectory: "Fixtures")) let dbQueue = try DatabaseQueue.inMemoryCopy(fromPath: path) // When it is migrated to v2, try migrator.migrate(dbQueue, upTo: "v2") // Then it contains the expected content. assertSnapshot(of: dbQueue, as: .dumpContent()) } func test_migrate_populated_v1_to_v2() throws { // Given a copy of the database_v1_populated.sqlite fixture let path = try XCTUnwrap(Bundle.module.path( forResource: "database_v1_populated", ofType: "sqlite", inDirectory: "Fixtures")) let dbQueue = try DatabaseQueue.inMemoryCopy(fromPath: path) // When it is migrated to v2, try migrator.migrate(dbQueue, upTo: "v2") // Then it contains the expected content. assertSnapshot(of: dbQueue, as: .dumpContent()) } } ``` -------------------------------- ### Testing Raw SQL Queries with GRDBSnapshotTesting (Swift) Source: https://github.com/groue/grdbsnapshottesting/blob/main/README.md This test function demonstrates how to snapshot the results of a raw SQL query executed against a GRDB database. It uses `assertSnapshot` with the `.dump(db)` strategy to capture the output of the SQL string 'SELECT * FROM player ORDER BY id'. This is useful for verifying complex queries or ensuring backward compatibility with existing SQL logic. ```Swift import GRDB import GRDBSnapshotTesting import InlineSnapshotTesting import XCTest class MyDatabaseTests: XCTestCase { func test_sql() throws { let dbQueue = try makeMyDatabase() try dbQueue.read { db in assertSnapshot(of: "SELECT * FROM player ORDER BY id", as: .dump(db)) } } } ``` -------------------------------- ### Testing QueryInterfaceRequest and SQLRequest with GRDBSnapshotTesting (Swift) Source: https://github.com/groue/grdbsnapshottesting/blob/main/Sources/GRDBSnapshotTesting/Documentation.docc/TestingDatabaseRequests.md This snippet shows how to test the content of specific database requests, including `QueryInterfaceRequest` (e.g., `Player.all()`) and `SQLRequest` (e.g., `SELECT * FROM player ORDER BY id`), using `assertSnapshot(of:as:.dump(db))`. It requires a `GRDB.DatabaseQueue` instance and performs read operations to assert the snapshot of the request's results. ```Swift func test_specific_request_content() throws { let dbQueue = try makeMyDatabase() try dbQueue.read { db in // QueryInterfaceRequest let request = Player.all() assertSnapshot(of: request, as: .dump(db)) // SQLRequest let request: SQLRequest = "SELECT * FROM player ORDER BY id" assertSnapshot(of: request, as: .dump(db)) } } ``` -------------------------------- ### Testing GRDB Requests with GRDBSnapshotTesting (Swift) Source: https://github.com/groue/grdbsnapshottesting/blob/main/README.md This test function illustrates how to snapshot the results of a GRDB request (e.g., `Player.all()`) within a database read transaction. It uses `assertSnapshot` with the `.dump(db)` strategy to capture the output of the request, ensuring that the data returned by the GRDB query matches the expected snapshot. This is ideal for verifying the correctness of data models and their associated queries. ```Swift import GRDB import GRDBSnapshotTesting import InlineSnapshotTesting import XCTest class MyDatabaseTests: XCTestCase { func test_request() throws { let dbQueue = try makeMyDatabase() try dbQueue.read { db in assertSnapshot(of: Player.all(), as: .dump(db)) } } ``` -------------------------------- ### Handling In-Development Database Migrations with XCTExpectFailure (Swift) Source: https://github.com/groue/grdbsnapshottesting/blob/main/Sources/GRDBSnapshotTesting/Documentation.docc/TestingDatabaseContent.md This snippet illustrates how to manage snapshot test failures for the latest, actively developed database migration using XCTExpectFailure. It allows developers to temporarily ignore failures for a schema version (e.g., v3) that is still under development, preventing test failures until the migration is stabilized and ready for release. ```Swift import XCTest import GRDB import GRDBSnapshotTesting import SnapshotTesting final class MyDatabaseTests: XCTestCase { var migrator: DatabaseMigrator { // Return the DatabaseMigrator for your database } func test_migrate_empty_database_to_latest_version() throws { // TODO: remove this line when the latest migration is stabilized. XCTExpectFailure("Schema v3 is in development.") // Given an empty and temporary in-memory database, let dbQueue = try DatabaseQueue() // When it is migrated to the latest version, try migrator.migrate(dbQueue) // Then it contains the expected schema and content. assertSnapshot(of: dbQueue, as: .dumpContent()) } } final class MyDatabaseMigrationsTests: XCTestCase { var migrator: DatabaseMigrator { // Return the DatabaseMigrator for your database } func test_that_migration_v1_is_never_modified() throws { ... } func test_that_migration_v2_is_never_modified() throws { ... } func test_that_migration_v3_is_never_modified() throws { // TODO: remove this line when the latest migration is stabilized. XCTExpectFailure("Schema v3 is in development.") let dbQueue = try DatabaseQueue() try migrator.migrate(dbQueue, upTo: "v3") assertSnapshot(of: dbQueue, as: .dumpContent()) } ``` -------------------------------- ### Testing Specific Tables with GRDBSnapshotTesting (Swift) Source: https://github.com/groue/grdbsnapshottesting/blob/main/README.md This test function shows how to snapshot the content of specific tables within a GRDB database using `assertSnapshot` and the `.dumpTables()` strategy. It's configured to dump the 'player' and 'team' tables, allowing focused testing on particular parts of the database without capturing the entire content. This is efficient for tests that only modify or depend on a subset of tables. ```Swift import GRDB import GRDBSnapshotTesting import InlineSnapshotTesting import XCTest class MyDatabaseTests: XCTestCase { func test_tables() throws { let dbQueue = try makeMyDatabase() assertSnapshot(of: dbQueue, as: .dumpTables(["player", "team"])) } ``` -------------------------------- ### Testing Immutable Database Migrations with GRDB and SnapshotTesting (Swift) Source: https://github.com/groue/grdbsnapshottesting/blob/main/Sources/GRDBSnapshotTesting/Documentation.docc/TestingDatabaseContent.md This snippet demonstrates how to use GRDBSnapshotTesting to verify that past database migrations (e.g., v1, v2) are never modified after release. It uses assertSnapshot(of:as:.dumpContent()) to capture the database state after migrating up to a specific version, ensuring consistency and preventing unexpected schema changes. ```Swift import XCTest import GRDB import GRDBSnapshotTesting import SnapshotTesting final class MyDatabaseMigrationsTests: XCTestCase { var migrator: DatabaseMigrator { // Return the DatabaseMigrator for your database } func test_that_migration_v1_is_never_modified() throws { let dbQueue = try DatabaseQueue() try migrator.migrate(dbQueue, upTo: "v1") assertSnapshot(of: dbQueue, as: .dumpContent()) } func test_that_migration_v2_is_never_modified() throws { let dbQueue = try DatabaseQueue() try migrator.migrate(dbQueue, upTo: "v2") assertSnapshot(of: dbQueue, as: .dumpContent()) } // Add below tests for future migrations. } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.