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

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

Open
brightmileDaniel opened this issue Jun 27, 2023 · 0 comments

Comments

@brightmileDaniel
Copy link

brightmileDaniel commented Jun 27, 2023

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant