Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored tests and prepare for tests with records #53

Merged
merged 5 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 43 additions & 8 deletions tests/fixtures/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { diffModels } from '@/src/utils/migration';
import { getLocalPackages } from '@/src/utils/misc';
import { type LocalPackages, getLocalPackages } from '@/src/utils/misc';
import { getModels } from '@/src/utils/model';
import { Protocol } from '@/src/utils/protocol';
import type { Model } from '@ronin/compiler';
import type { Model, Statement } from '@ronin/compiler';
import { type Database, Engine } from '@ronin/engine';
import { MemoryResolver } from '@ronin/engine/resolvers/memory';

Expand All @@ -15,14 +15,19 @@ const engine = new Engine({
*
* @param models - The models that should be inserted into the database.
* @param statements - The statements that should be executed.
* @param insertStatements - The statements that should be executed to insert records into
* the database.
*
* @returns A list of rows resulting from the executed statements.
*/
export const queryEphemeralDatabase = async (models: Array<Model>): Promise<Database> => {
export const queryEphemeralDatabase = async (
models: Array<Model>,
insertStatements: Array<Statement> = [],
): Promise<Database> => {
const databaseId = Math.random().toString(36).substring(7);
const database = await engine.createDatabase({ id: databaseId });

await prefillDatabase(database, models);
await prefillDatabase(database, models, insertStatements);

return database;
};
Expand All @@ -32,10 +37,13 @@ export const queryEphemeralDatabase = async (models: Array<Model>): Promise<Data
*
* @param databaseName - The name of the database to prefill.
* @param models - The models that should be inserted into the database.
* @param insertStatements - The statements that should be executed to insert records into
* the database.
*/
export const prefillDatabase = async (
db: Database,
models: Array<Model>,
insertStatements: Array<Statement> = [],
): Promise<void> => {
const { Transaction, ROOT_MODEL } = (await getLocalPackages()).compiler;

Expand All @@ -45,7 +53,11 @@ export const prefillDatabase = async (
models.map((model) => ({ create: { model } })),
);

// Create the root model and all other models.
await db.query([...rootModelTransaction.statements, ...modelTransaction.statements]);

// Insert records into the database.
await db.query(insertStatements);
};

/**
Expand All @@ -54,7 +66,10 @@ export const prefillDatabase = async (
*
* @param definedModels - The new/updated model definitions to migrate to.
* @param existingModels - The current models in the database.
* @param enableRename - Whether to enable model renaming during migration (defaults to `false`).
* @param enableRename - Whether to enable model renaming during migration
* (defaults to `false`).
* @param insertStatements - The statements that should be executed to insert records into
* the database.
*
* @returns Object containing:
* - db: The ephemeral database instance.
Expand All @@ -67,8 +82,15 @@ export async function runMigration(
definedModels: Array<Model>,
existingModels: Array<Model>,
enableRename = false,
) {
const db = await queryEphemeralDatabase(existingModels);
insertStatements = [],
): Promise<{
db: Database;
packages: LocalPackages;
models: Array<Model>;
statements: Array<Statement>;
modelDiff: Array<string>;
}> {
const db = await queryEphemeralDatabase(existingModels, insertStatements);

const packages = await getLocalPackages();
const models = await getModels(packages, db);
Expand All @@ -90,14 +112,27 @@ export async function runMigration(
};
}

/**
* Retrieves the number of records in a table.
*
* @param db - The database instance to query.
* @param modelSlug - The slug of the model to get the row count for.
*
* @returns The number of records in the table.
*/
export async function getRowCount(db: Database, modelSlug: string): Promise<number> {
const res = await db.query([`SELECT COUNT(*) FROM ${modelSlug};`]);
return res[0].rows[0]['COUNT(*)'];
}

/**
* Retrieves a list of all table names from a SQLite database.
*
* @param db - The database instance to query.
*
* @returns A list of table names mapped from the query results.
*/
export async function getSQLTables(db: Database) {
export async function getSQLTables(db: Database): Promise<Array<Record<string, string>>> {
const res = await db.query(['SELECT name FROM sqlite_master WHERE type="table";']);
return res[0].rows;
}
Loading
Loading