mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	fix: flaky test when updated_at matches the time of query (#10752)
This commit is contained in:
		
							parent
							
								
									3e10ca6611
								
							
						
					
					
						commit
						8f2cf5386d
					
				| @ -1,28 +1,20 @@ | |||||||
| import dbInit, { type ITestDb } from '../helpers/database-init.js'; | import dbInit from '../helpers/database-init.js'; | ||||||
| import type { IUnleashStores } from '../../../lib/types/index.js'; | import { test, expect } from 'vitest'; | ||||||
| import { beforeAll, test, expect } from 'vitest'; |  | ||||||
| let stores: IUnleashStores; |  | ||||||
| let db: ITestDb; |  | ||||||
| 
 |  | ||||||
| beforeAll(async () => { |  | ||||||
|     db = await dbInit(); |  | ||||||
|     stores = db.stores; |  | ||||||
| }); |  | ||||||
| 
 |  | ||||||
| beforeEach(async () => { |  | ||||||
|     await stores.userStore.deleteAll(); |  | ||||||
| }); |  | ||||||
| 
 | 
 | ||||||
| test('should have no users', async () => { | test('should have no users', async () => { | ||||||
|  |     const db = await dbInit(); | ||||||
|  |     const stores = db.stores; | ||||||
|     const readModel = stores.userUpdatesReadModel; |     const readModel = stores.userUpdatesReadModel; | ||||||
|     const lastUpdate = await readModel.getLastUpdatedAt(); |     const lastUpdate = await readModel.getLastUpdatedAt(); | ||||||
|     expect(lastUpdate).toBeNull(); |     expect(lastUpdate).toBeNull(); | ||||||
| 
 | 
 | ||||||
|     const users = await readModel.getUsersUpdatedAfterOrEqual(new Date(0)); |     const users = await readModel.getUsersUpdatedAfterOrEqual(new Date(0), 10); | ||||||
|     expect(users).toEqual([]); |     expect(users).toEqual([]); | ||||||
| }); | }); | ||||||
| 
 | 
 | ||||||
| test('Adding a user should return that user', async () => { | test('Adding a user should return that user', async () => { | ||||||
|  |     const db = await dbInit(); | ||||||
|  |     const stores = db.stores; | ||||||
|     const readModel = stores.userUpdatesReadModel; |     const readModel = stores.userUpdatesReadModel; | ||||||
|     const userStore = stores.userStore; |     const userStore = stores.userStore; | ||||||
|     const beforeInsert = new Date(Date.now() - 1000); |     const beforeInsert = new Date(Date.now() - 1000); | ||||||
| @ -36,7 +28,7 @@ test('Adding a user should return that user', async () => { | |||||||
|         beforeInsert.getTime(), |         beforeInsert.getTime(), | ||||||
|     ); |     ); | ||||||
| 
 | 
 | ||||||
|     const users = await readModel.getUsersUpdatedAfterOrEqual(beforeInsert); |     const users = await readModel.getUsersUpdatedAfterOrEqual(beforeInsert, 10); | ||||||
|     expect(users).toHaveLength(1); |     expect(users).toHaveLength(1); | ||||||
|     expect(users[0].email).toBe('test@example.com'); |     expect(users[0].email).toBe('test@example.com'); | ||||||
|     expect(users[0].createdAt).toBeInstanceOf(Date); |     expect(users[0].createdAt).toBeInstanceOf(Date); | ||||||
| @ -45,6 +37,8 @@ test('Adding a user should return that user', async () => { | |||||||
| }); | }); | ||||||
| 
 | 
 | ||||||
| test('Modifying a user should return that user', async () => { | test('Modifying a user should return that user', async () => { | ||||||
|  |     const db = await dbInit(); | ||||||
|  |     const stores = db.stores; | ||||||
|     const readModel = stores.userUpdatesReadModel; |     const readModel = stores.userUpdatesReadModel; | ||||||
|     const userStore = stores.userStore; |     const userStore = stores.userStore; | ||||||
|     const inserted = await userStore.upsert({ |     const inserted = await userStore.upsert({ | ||||||
| @ -57,7 +51,11 @@ test('Modifying a user should return that user', async () => { | |||||||
|     const lastUpdatedAt = lastUpdate!.lastUpdatedAt; |     const lastUpdatedAt = lastUpdate!.lastUpdatedAt; | ||||||
|     expect(lastUpdatedAt).toBeInstanceOf(Date); |     expect(lastUpdatedAt).toBeInstanceOf(Date); | ||||||
| 
 | 
 | ||||||
|     const users = await readModel.getUsersUpdatedAfterOrEqual(afterInsert); |     const users = await readModel.getUsersUpdatedAfterOrEqual( | ||||||
|  |         afterInsert, | ||||||
|  |         10, | ||||||
|  |         inserted.id, | ||||||
|  |     ); | ||||||
|     expect(users).toHaveLength(0); |     expect(users).toHaveLength(0); | ||||||
| 
 | 
 | ||||||
|     await userStore.update(inserted.id, { name: 'New Name' }); |     await userStore.update(inserted.id, { name: 'New Name' }); | ||||||
| @ -70,7 +68,11 @@ test('Modifying a user should return that user', async () => { | |||||||
|         lastUpdatedAt!.getTime(), |         lastUpdatedAt!.getTime(), | ||||||
|     ); |     ); | ||||||
| 
 | 
 | ||||||
|     const users2 = await readModel.getUsersUpdatedAfterOrEqual(afterInsert); |     const users2 = await readModel.getUsersUpdatedAfterOrEqual( | ||||||
|  |         afterInsert, | ||||||
|  |         10, | ||||||
|  |         inserted.id, | ||||||
|  |     ); | ||||||
|     expect(users2).toHaveLength(1); |     expect(users2).toHaveLength(1); | ||||||
|     expect(users2[0].email).toBe('test@example.com'); |     expect(users2[0].email).toBe('test@example.com'); | ||||||
|     expect(users2[0].name).toBe('New Name'); |     expect(users2[0].name).toBe('New Name'); | ||||||
| @ -80,6 +82,8 @@ test('Modifying a user should return that user', async () => { | |||||||
| }); | }); | ||||||
| 
 | 
 | ||||||
| test('Deleting a user should return that user', async () => { | test('Deleting a user should return that user', async () => { | ||||||
|  |     const db = await dbInit(); | ||||||
|  |     const stores = db.stores; | ||||||
|     const readModel = stores.userUpdatesReadModel; |     const readModel = stores.userUpdatesReadModel; | ||||||
|     const userStore = stores.userStore; |     const userStore = stores.userStore; | ||||||
|     const inserted = await userStore.upsert({ |     const inserted = await userStore.upsert({ | ||||||
| @ -92,7 +96,11 @@ test('Deleting a user should return that user', async () => { | |||||||
|     const lastUpdatedAt = lastUpdate!.lastUpdatedAt; |     const lastUpdatedAt = lastUpdate!.lastUpdatedAt; | ||||||
|     expect(lastUpdatedAt).toBeInstanceOf(Date); |     expect(lastUpdatedAt).toBeInstanceOf(Date); | ||||||
| 
 | 
 | ||||||
|     const users = await readModel.getUsersUpdatedAfterOrEqual(afterInsert); |     const users = await readModel.getUsersUpdatedAfterOrEqual( | ||||||
|  |         afterInsert, | ||||||
|  |         10, | ||||||
|  |         inserted.id, | ||||||
|  |     ); | ||||||
|     expect(users).toHaveLength(0); |     expect(users).toHaveLength(0); | ||||||
| 
 | 
 | ||||||
|     await userStore.delete(inserted.id); |     await userStore.delete(inserted.id); | ||||||
| @ -105,7 +113,11 @@ test('Deleting a user should return that user', async () => { | |||||||
|         lastUpdatedAt!.getTime(), |         lastUpdatedAt!.getTime(), | ||||||
|     ); |     ); | ||||||
| 
 | 
 | ||||||
|     const users2 = await readModel.getUsersUpdatedAfterOrEqual(afterInsert); |     const users2 = await readModel.getUsersUpdatedAfterOrEqual( | ||||||
|  |         afterInsert, | ||||||
|  |         10, | ||||||
|  |         inserted.id, | ||||||
|  |     ); | ||||||
|     expect(users2).toHaveLength(1); |     expect(users2).toHaveLength(1); | ||||||
|     expect(users2[0].email).toBeNull(); // currently we nullify the email but this might change in the future
 |     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].createdAt).toBeInstanceOf(Date); | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user