【发布时间】:2021-12-18 03:53:21
【问题描述】:
我有一个查询 postgres 数据库 (node-postgres lib) 并从中检索数据并将其映射到平面数组的方法。
export const getActiveUsersLast7DaysForProject = async (projectId: string): Promise<User[]> => {
let userIds: string[] = [];
const client = new PG.Client(config.encryptedCredentials); // prod only
await client.connect();
await client
.query(
'
SELECT user_id FROM my_db.users_active ua
WHERE ua.project_id='12345'
GROUP BY user_id'
)
.then(
(res: any) => (userIds = res.rows.flatMap((user: any) => user.user_id))
)
.catch((e: Error) => console.error(e.stack));
return userIds;
};
如何测试它?当前,当它尝试在本地连接到数据库时测试失败(正如预期的那样,因为凭据不适用于 localhost)。我可以存根/模拟query() 以返回一些测试查询结果吗?
另外,如何拦截对真实数据库的调用并使用模拟数据库,例如pg-mem?
【问题讨论】:
标签: node.js postgresql jasmine node-postgres