1
0
Fork 0
mirror of synced 2024-07-21 06:05:52 +12:00

Add tests

This commit is contained in:
Adria Navarro 2023-09-18 13:46:59 +02:00
parent 8c16c1908b
commit 333b415635
2 changed files with 106 additions and 4 deletions

View file

@ -0,0 +1,95 @@
import { User } from "@budibase/types"
import { tenancy } from "../.."
import { generator, structures } from "../../../tests"
import { DBTestConfiguration } from "../../../tests/extra"
import { getUsers } from "../user"
import { getGlobalDB, getGlobalDBName } from "../../context"
import _ from "lodash"
import { getDB } from "../../db"
import type * as TenancyType from "../../tenancy"
const config = new DBTestConfiguration()
// This mock is required to ensure that getTenantDB returns always as a singleton.
// This will allow us to spy on the db
const staticDb = getDB(getGlobalDBName(config.tenantId))
jest.mock("../../tenancy", (): typeof TenancyType => ({
...jest.requireActual("../../tenancy"),
getTenantDB: jest.fn().mockImplementation(() => staticDb),
}))
describe("user cache", () => {
describe("getUsers", () => {
const users: User[] = []
beforeAll(async () => {
const userCount = 10
const userIds = generator.arrayOf(() => generator.guid(), {
min: userCount,
max: userCount,
})
await config.doInTenant(async () => {
const db = getGlobalDB()
for (const userId of userIds) {
const user = structures.users.user({ _id: userId })
await db.put(user)
users.push(user)
}
})
})
beforeEach(() => {
jest.clearAllMocks()
})
it("when no user is in cache, all of them are retrieved from db", async () => {
const usersToRequest = _.sampleSize(users, 5)
const userIdsToRequest = usersToRequest.map(x => x._id!)
jest.spyOn(staticDb, "allDocs")
const results = await getUsers(userIdsToRequest, config.tenantId)
expect(results).toHaveLength(5)
expect(results).toEqual(
usersToRequest.map(u => ({
...u,
budibaseAccess: true,
_rev: expect.any(String),
}))
)
expect(tenancy.getTenantDB).toBeCalledTimes(1)
expect(tenancy.getTenantDB).toBeCalledWith(config.tenantId)
expect(staticDb.allDocs).toBeCalledTimes(1)
expect(staticDb.allDocs).toBeCalledWith({
keys: userIdsToRequest,
include_docs: true,
limit: 5,
})
})
it("on a second all, all of them are retrieved from cache", async () => {
const usersToRequest = _.sampleSize(users, 5)
const userIdsToRequest = usersToRequest.map(x => x._id!)
jest.spyOn(staticDb, "allDocs")
await getUsers(userIdsToRequest, config.tenantId)
const resultsFromCache = await getUsers(userIdsToRequest, config.tenantId)
expect(resultsFromCache).toHaveLength(5)
expect(resultsFromCache).toEqual(
usersToRequest.map(u => ({
...u,
budibaseAccess: true,
_rev: expect.any(String),
}))
)
expect(staticDb.allDocs).toBeCalledTimes(1)
})
})
})

View file

@ -115,11 +115,18 @@ export async function getUsers(userIds: string[], tenantId: string) {
// try cache
let usersFromCache = await client.bulkGet(userIds)
const missingUsersFromCache = userIds.filter(uid => !usersFromCache[uid])
const usersFromDb = await populateUsersFromDB(missingUsersFromCache, tenantId)
for (const userToCache of usersFromDb) {
await client.store(userToCache._id, userToCache, EXPIRY_SECONDS)
const users = Object.values(usersFromCache)
if (missingUsersFromCache.length) {
const usersFromDb = await populateUsersFromDB(
missingUsersFromCache,
tenantId
)
for (const userToCache of usersFromDb) {
await client.store(userToCache._id, userToCache, EXPIRY_SECONDS)
}
users.push(...usersFromDb)
}
const users = [...Object.values(usersFromCache), ...usersFromDb]
return users
}