1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-09-24 17:51:14 +02:00

Add tests

This commit is contained in:
Gastón Fournier 2025-09-24 17:08:15 +02:00
parent 419952584c
commit 4361151dcd
No known key found for this signature in database
GPG Key ID: AF45428626E17A8E
2 changed files with 92 additions and 9 deletions

View File

@ -62,7 +62,12 @@ export class UserUpdatesReadModel {
}) })
.where('updated_at', '>', date) .where('updated_at', '>', date)
.orderBy('updated_at', 'asc') .orderBy('updated_at', 'asc')
.select([...USER_COLUMNS_PUBLIC, 'updated_at', 'deleted_at']) .select([
...USER_COLUMNS_PUBLIC,
'created_at',
'updated_at',
'deleted_at',
])
.limit(limit); .limit(limit);
return result.map(toResponse); return result.map(toResponse);
} }

View File

@ -1,7 +1,7 @@
import dbInit, { type ITestDb } from '../helpers/database-init.js'; import dbInit, { type ITestDb } from '../helpers/database-init.js';
import getLogger from '../../fixtures/no-logger.js'; import getLogger from '../../fixtures/no-logger.js';
import type { IUnleashStores } from '../../../lib/types/index.js'; import type { IUnleashStores } from '../../../lib/types/index.js';
import { beforeAll, afterAll, test, expect } from 'vitest'; import { beforeAll, test, expect } from 'vitest';
let stores: IUnleashStores; let stores: IUnleashStores;
let db: ITestDb; let db: ITestDb;
@ -12,8 +12,8 @@ beforeAll(async () => {
stores = db.stores; stores = db.stores;
}); });
afterAll(async () => { beforeEach(async () => {
await db.destroy(); await stores.userStore.deleteAll();
}); });
test('should have no users', async () => { test('should have no users', async () => {
@ -25,11 +25,89 @@ test('should have no users', async () => {
expect(users).toEqual([]); expect(users).toEqual([]);
}); });
test('should have no users', async () => { test('Adding a user should return that user', async () => {
const readModel = stores.userUpdatesReadModel; const readModel = stores.userUpdatesReadModel;
const lastUpdatedAt = await readModel.getLastUpdatedAt(); const userStore = stores.userStore;
expect(lastUpdatedAt).toBeNull(); const beforeInsert = new Date(Date.now() - 1000);
await userStore.upsert({ email: 'test@example.com' });
const users = await readModel.getUsersUpdatedAfter(new Date(0)); const lastUpdatedAt = await readModel.getLastUpdatedAt();
expect(users).toEqual([]); expect(lastUpdatedAt).toBeDefined();
expect(lastUpdatedAt).toBeInstanceOf(Date);
// check that it's recent
expect(lastUpdatedAt!.getTime()).toBeGreaterThanOrEqual(
beforeInsert.getTime(),
);
const users = await readModel.getUsersUpdatedAfter(beforeInsert);
expect(users).toHaveLength(1);
expect(users[0].email).toBe('test@example.com');
expect(users[0].createdAt).toBeInstanceOf(Date);
expect(users[0].updatedAt).toBeInstanceOf(Date);
expect(users[0].deletedAt).toBeNull();
});
test('Modifying a user should return that user', async () => {
const readModel = stores.userUpdatesReadModel;
const userStore = stores.userStore;
const inserted = await userStore.upsert({
email: 'test@example.com',
});
const afterInsert = new Date();
const lastUpdatedAt = await readModel.getLastUpdatedAt();
expect(lastUpdatedAt).toBeDefined();
expect(lastUpdatedAt).toBeInstanceOf(Date);
const users = await readModel.getUsersUpdatedAfter(afterInsert);
expect(users).toHaveLength(0);
await userStore.update(inserted.id, { name: 'New Name' });
const lastUpdatedAt2 = await readModel.getLastUpdatedAt();
expect(lastUpdatedAt2).toBeDefined();
expect(lastUpdatedAt2).toBeInstanceOf(Date);
expect(lastUpdatedAt2!.getTime()).toBeGreaterThanOrEqual(
lastUpdatedAt!.getTime(),
);
const users2 = await readModel.getUsersUpdatedAfter(afterInsert);
expect(users2).toHaveLength(1);
expect(users2[0].email).toBe('test@example.com');
expect(users2[0].name).toBe('New Name');
expect(users2[0].createdAt).toBeInstanceOf(Date);
expect(users2[0].updatedAt).toBeInstanceOf(Date);
expect(users2[0].deletedAt).toBeNull();
});
test('Deleting a user should return that user', async () => {
const readModel = stores.userUpdatesReadModel;
const userStore = stores.userStore;
const inserted = await userStore.upsert({
email: 'test@example.com',
});
const afterInsert = new Date();
const lastUpdatedAt = await readModel.getLastUpdatedAt();
expect(lastUpdatedAt).toBeDefined();
expect(lastUpdatedAt).toBeInstanceOf(Date);
const users = await readModel.getUsersUpdatedAfter(afterInsert);
expect(users).toHaveLength(0);
await userStore.delete(inserted.id);
const lastUpdatedAt2 = await readModel.getLastUpdatedAt();
expect(lastUpdatedAt2).toBeDefined();
expect(lastUpdatedAt2).toBeInstanceOf(Date);
expect(lastUpdatedAt2!.getTime()).toBeGreaterThanOrEqual(
lastUpdatedAt!.getTime(),
);
const users2 = await readModel.getUsersUpdatedAfter(afterInsert);
expect(users2).toHaveLength(1);
expect(users2[0].email).toBeNull(); // currently we nullify the email but this might change in the future
expect(users2[0].createdAt).toBeInstanceOf(Date);
expect(users2[0].updatedAt).toBeInstanceOf(Date);
expect(users2[0].deletedAt).toBeInstanceOf(Date);
}); });