BlinkUX / sequelize-mock

A simple mock interface specifically for testing code relying on Sequelize models
https://sequelize-mock.readthedocs.io
MIT License
139 stars 73 forks source link

SequelizeMockEmptyQueryQueueError: No query results are queued. Unexpected query attempted to be run #100

Open brightmileDaniel opened 1 year ago

brightmileDaniel commented 1 year ago

I have a few chai tests that call functions that should get a result back from my mock db tables, I'm trying to mock my SQL db using sequalize-mock so the tests won't fall over. I'm trying to add seed data to the mockDb so the tests can work with them, however, I keep getting this weird error:

SequelizeMockEmptyQueryQueueError: No query results are queued. Unexpected query attempted to be run

Any ideas why this happens?

Here is my mock db init which is running before each test.

This is glboally available in SQL Mock file

const SequelizeMock = require("sequelize-mock");
const dbMock = new SequelizeMock({});
export const initializeMockDatabase = async () => {
    const GlobalParam = await generateSqlSchema(
        dbMock,
        GLOBAL_PARAMS_SCHEMA,
    );

    const globalParamsfilePath = path.resolve(
        __dirname,
        "../mocks/global_params.json",
    );
    const globalParamsData = fs.readFileSync(globalParamsfilePath, "utf8");
    const globalParams = JSON.parse(globalParamsData);

    // Loop through the globalParams object and create records
    for (const key of Object.keys(globalParams)) {
        const globalParam = GlobalParam.build({
            name: key,
            value: globalParams[key as keyof typeof globalParams],
        });
        await globalParam.save();
    }
};
const generateSqlSchema= async <T extends object>(
    db: SqlDatabase,
    schema: SyncDataSchema<T>,
) => {
    const schemaName = schema.id;

    let model = db.models[schemaName];

    if (!model) {
        const attributes: any = {};
        for (const property of schema.properties) {
            const attribute: ModelAttributeColumnOptions = {
                type: SyncDataUtils.convertSyncDataType(property.type),
                primaryKey: property.key === schema.keyProperty,
            };
            if (attribute) {
                attributes[property.key] = attribute;
            }
        }
        model = await syncSqlSchema(db, schemaName, attributes);
    }

    return model;
};

const syncSqlSchema = async <
    T extends object,
    M extends Model,
    TCreationAttributes = M["_attributes"],
>(
    database: SqlDatabase,
    modelName: string,
    attributes: ModelAttributes<M, TCreationAttributes>,
    options?: ModelOptions,
): Promise<SqlSchema<T>> => {
    const model = database.define(modelName, attributes, {
        ...options,
        freezeTableName: true,
    });
    await model.sync({
        alter: {
            drop: false,
        },

    });
    return model;
};

And then in the before:

before(async function () {
    this.timeout(90 * 1000);

    chai.use(spies);
    chai.use(chaiAsPromised);

    const projectId = MiscConstants.PROJECT_ID.TEST;

    process.env.GCLOUD_PROJECT = projectId;
    process.env.LOCAL_PUBSUB = true;

    await Mocks.setup();
    Mocks.enable();

    // Initialize the mock database
    await initializeMockDatabase();

    // Require all pub-subs
    require("firebase-functions").registerFunctions(require("../../index"));

    //await Emulators.setupEmulators();
});

And executing queries like:

export const executeSqlQuery = async <T, R = any>(
    query: string,
): Promise<[R[]]> => {
    return attemptSqlOperation(async () => {
        const convertedQuery = convertToPostgres(query);
        //console.log(convertedQuery.replace(/\n/g, " "));
        return (await dbMock.query(convertedQuery)) as any;
    });
};

Any ideas? What I'm missing?