Skip to content

Commit

Permalink
feat: require explicit query context specification (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
wschurman authored Apr 11, 2024
1 parent cc6f3dc commit 8b0b31f
Show file tree
Hide file tree
Showing 46 changed files with 1,598 additions and 755 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,19 @@ describe(GenericLocalMemoryCacher, () => {
const cacheKeyMaker = genericCacher['makeCacheKey'].bind(genericCacher);

const date = new Date();
const entity1Created = await LocalMemoryTestEntity.creator(viewerContext)
const entity1Created = await LocalMemoryTestEntity.creator(
viewerContext,
viewerContext.getNonTransactionalQueryContextForDatabaseAdaptorFlavor('postgres')
)
.setField('name', 'blah')
.setField('dateField', date)
.enforceCreateAsync();

// loading an entity should put it in cache
const entity1 = await LocalMemoryTestEntity.loader(viewerContext)
const entity1 = await LocalMemoryTestEntity.loader(
viewerContext,
viewerContext.getNonTransactionalQueryContextForDatabaseAdaptorFlavor('postgres')
)
.enforcing()
.loadByIDAsync(entity1Created.getID());

Expand Down Expand Up @@ -63,9 +69,10 @@ describe(GenericLocalMemoryCacher, () => {
// simulate non existent db fetch, should write negative result ('') to cache
const nonExistentId = uuidv4();

const entityNonExistentResult = await LocalMemoryTestEntity.loader(viewerContext).loadByIDAsync(
nonExistentId
);
const entityNonExistentResult = await LocalMemoryTestEntity.loader(
viewerContext,
viewerContext.getNonTransactionalQueryContextForDatabaseAdaptorFlavor('postgres')
).loadByIDAsync(nonExistentId);
expect(entityNonExistentResult.ok).toBe(false);

const nonExistentCachedResult = await entitySpecificGenericCacher.loadManyAsync([
Expand All @@ -77,12 +84,16 @@ describe(GenericLocalMemoryCacher, () => {

// load again through entities framework to ensure it reads negative result
const entityNonExistentResult2 = await LocalMemoryTestEntity.loader(
viewerContext
viewerContext,
viewerContext.getNonTransactionalQueryContextForDatabaseAdaptorFlavor('postgres')
).loadByIDAsync(nonExistentId);
expect(entityNonExistentResult2.ok).toBe(false);

// invalidate from cache to ensure it invalidates correctly
await LocalMemoryTestEntity.loader(viewerContext).invalidateFieldsAsync(entity1.getAllFields());
await LocalMemoryTestEntity.loader(
viewerContext,
viewerContext.getNonTransactionalQueryContextForDatabaseAdaptorFlavor('postgres')
).invalidateFieldsAsync(entity1.getAllFields());
const cachedResultMiss = await entitySpecificGenericCacher.loadManyAsync([
cacheKeyMaker('id', entity1.getID()),
]);
Expand All @@ -100,13 +111,19 @@ describe(GenericLocalMemoryCacher, () => {
const cacheKeyMaker = genericCacher['makeCacheKey'].bind(genericCacher);

const date = new Date();
const entity1Created = await LocalMemoryTestEntity.creator(viewerContext)
const entity1Created = await LocalMemoryTestEntity.creator(
viewerContext,
viewerContext.getNonTransactionalQueryContextForDatabaseAdaptorFlavor('postgres')
)
.setField('name', 'blah')
.setField('dateField', date)
.enforceCreateAsync();

// loading an entity will try to put it in cache but it's a noop cache, so it should be a miss
const entity1 = await LocalMemoryTestEntity.loader(viewerContext)
const entity1 = await LocalMemoryTestEntity.loader(
viewerContext,
viewerContext.getNonTransactionalQueryContextForDatabaseAdaptorFlavor('postgres')
)
.enforcing()
.loadByIDAsync(entity1Created.getID());

Expand All @@ -133,9 +150,10 @@ describe(GenericLocalMemoryCacher, () => {
// a non existent db fetch should try to write negative result ('') but it's a noop cache, so it should be a miss
const nonExistentId = uuidv4();

const entityNonExistentResult = await LocalMemoryTestEntity.loader(viewerContext).loadByIDAsync(
nonExistentId
);
const entityNonExistentResult = await LocalMemoryTestEntity.loader(
viewerContext,
viewerContext.getNonTransactionalQueryContextForDatabaseAdaptorFlavor('postgres')
).loadByIDAsync(nonExistentId);
expect(entityNonExistentResult.ok).toBe(false);

const nonExistentCachedResult = await entitySpecificGenericCacher.loadManyAsync([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ describe(GenericRedisCacher, () => {
]['cacheAdapter']['genericCacher'];
const cacheKeyMaker = genericCacher['makeCacheKey'].bind(genericCacher);

const entity1Created = await RedisTestEntity.creator(viewerContext)
const entity1Created = await RedisTestEntity.creator(
viewerContext,
viewerContext.getNonTransactionalQueryContextForDatabaseAdaptorFlavor('postgres')
)
.setField('name', 'blah')
.enforceCreateAsync();

Expand All @@ -117,9 +120,22 @@ describe(GenericRedisCacher, () => {
createRedisIntegrationTestEntityCompanionProvider(genericRedisCacheContext)
);
const [entity1, entity2, entity3] = await Promise.all([
RedisTestEntity.loader(viewerContext1).enforcing().loadByIDAsync(entity1Created.getID()),
RedisTestEntity.loader(viewerContext2).enforcing().loadByIDAsync(entity1Created.getID()),
RedisTestEntity.loader(viewerContext3)
RedisTestEntity.loader(
viewerContext1,
viewerContext1.getNonTransactionalQueryContextForDatabaseAdaptorFlavor('postgres')
)
.enforcing()
.loadByIDAsync(entity1Created.getID()),
RedisTestEntity.loader(
viewerContext2,
viewerContext2.getNonTransactionalQueryContextForDatabaseAdaptorFlavor('postgres')
)
.enforcing()
.loadByIDAsync(entity1Created.getID()),
RedisTestEntity.loader(
viewerContext3,
viewerContext3.getNonTransactionalQueryContextForDatabaseAdaptorFlavor('postgres')
)
.enforcing()
.loadByFieldEqualingAsync('name', entity1Created.getField('name')),
]);
Expand All @@ -138,28 +154,36 @@ describe(GenericRedisCacher, () => {
});

const cacheKeyEntity1NameField = cacheKeyMaker('name', entity1Created.getField('name'));
await RedisTestEntity.loader(viewerContext)
await RedisTestEntity.loader(
viewerContext,
viewerContext.getNonTransactionalQueryContextForDatabaseAdaptorFlavor('postgres')
)
.enforcing()
.loadByFieldEqualingAsync('name', entity1Created.getField('name'));
await expect(redis.get(cacheKeyEntity1NameField)).resolves.toEqual(cachedJSON);

// simulate non existent db fetch, should write negative result ('') to cache
const nonExistentId = uuidv4();
const entityNonExistentResult = await RedisTestEntity.loader(viewerContext).loadByIDAsync(
nonExistentId
);
const entityNonExistentResult = await RedisTestEntity.loader(
viewerContext,
viewerContext.getNonTransactionalQueryContextForDatabaseAdaptorFlavor('postgres')
).loadByIDAsync(nonExistentId);
expect(entityNonExistentResult.ok).toBe(false);
const cacheKeyNonExistent = cacheKeyMaker('id', nonExistentId);
const nonExistentCachedValue = await redis.get(cacheKeyNonExistent);
expect(nonExistentCachedValue).toEqual('');
// load again through entities framework to ensure it reads negative result
const entityNonExistentResult2 = await RedisTestEntity.loader(viewerContext).loadByIDAsync(
nonExistentId
);
const entityNonExistentResult2 = await RedisTestEntity.loader(
viewerContext,
viewerContext.getNonTransactionalQueryContextForDatabaseAdaptorFlavor('postgres')
).loadByIDAsync(nonExistentId);
expect(entityNonExistentResult2.ok).toBe(false);

// invalidate from cache to ensure it invalidates correctly in both caches
await RedisTestEntity.loader(viewerContext).invalidateFieldsAsync(entity1.getAllFields());
await RedisTestEntity.loader(
viewerContext,
viewerContext.getNonTransactionalQueryContextForDatabaseAdaptorFlavor('postgres')
).invalidateFieldsAsync(entity1.getAllFields());
await expect(redis.get(cacheKeyEntity1)).resolves.toBeNull();
await expect(redis.get(cacheKeyEntity1NameField)).resolves.toBeNull();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,18 @@ describe(GenericRedisCacher, () => {
]['cacheAdapter']['genericCacher'];
const cacheKeyMaker = genericCacher['makeCacheKey'].bind(genericCacher);

const entity1Created = await RedisTestEntity.creator(viewerContext)
const entity1Created = await RedisTestEntity.creator(
viewerContext,
viewerContext.getNonTransactionalQueryContextForDatabaseAdaptorFlavor('postgres')
)
.setField('name', 'blah')
.enforceCreateAsync();

// loading an entity should put it in cache
const entity1 = await RedisTestEntity.loader(viewerContext)
const entity1 = await RedisTestEntity.loader(
viewerContext,
viewerContext.getNonTransactionalQueryContextForDatabaseAdaptorFlavor('postgres')
)
.enforcing()
.loadByIDAsync(entity1Created.getID());

Expand All @@ -67,9 +73,10 @@ describe(GenericRedisCacher, () => {
// simulate non existent db fetch, should write negative result ('') to cache
const nonExistentId = uuidv4();

const entityNonExistentResult = await RedisTestEntity.loader(viewerContext).loadByIDAsync(
nonExistentId
);
const entityNonExistentResult = await RedisTestEntity.loader(
viewerContext,
viewerContext.getNonTransactionalQueryContextForDatabaseAdaptorFlavor('postgres')
).loadByIDAsync(nonExistentId);
expect(entityNonExistentResult.ok).toBe(false);

const nonExistentCachedValue = await (genericRedisCacheContext.redisClient as Redis).get(
Expand All @@ -78,13 +85,17 @@ describe(GenericRedisCacher, () => {
expect(nonExistentCachedValue).toEqual('');

// load again through entities framework to ensure it reads negative result
const entityNonExistentResult2 = await RedisTestEntity.loader(viewerContext).loadByIDAsync(
nonExistentId
);
const entityNonExistentResult2 = await RedisTestEntity.loader(
viewerContext,
viewerContext.getNonTransactionalQueryContextForDatabaseAdaptorFlavor('postgres')
).loadByIDAsync(nonExistentId);
expect(entityNonExistentResult2.ok).toBe(false);

// invalidate from cache to ensure it invalidates correctly
await RedisTestEntity.loader(viewerContext).invalidateFieldsAsync(entity1.getAllFields());
await RedisTestEntity.loader(
viewerContext,
viewerContext.getNonTransactionalQueryContextForDatabaseAdaptorFlavor('postgres')
).invalidateFieldsAsync(entity1.getAllFields());
const cachedValueNull = await (genericRedisCacheContext.redisClient as Redis).get(
cacheKeyMaker('id', entity1.getID())
);
Expand All @@ -97,11 +108,19 @@ describe(GenericRedisCacher, () => {
);
const date = new Date();
const entity1 = await enforceAsyncResult(
RedisTestEntity.creator(viewerContext).setField('dateField', date).createAsync()
RedisTestEntity.creator(
viewerContext,
viewerContext.getNonTransactionalQueryContextForDatabaseAdaptorFlavor('postgres')
)
.setField('dateField', date)
.createAsync()
);
expect(entity1.getField('dateField')).toEqual(date);

const entity2 = await RedisTestEntity.loader(viewerContext)
const entity2 = await RedisTestEntity.loader(
viewerContext,
viewerContext.getNonTransactionalQueryContextForDatabaseAdaptorFlavor('postgres')
)
.enforcing()
.loadByIDAsync(entity1.getID());
expect(entity2.getField('dateField')).toEqual(date);
Expand All @@ -110,7 +129,12 @@ describe(GenericRedisCacher, () => {
const vc2 = new TestViewerContext(
createRedisIntegrationTestEntityCompanionProvider(genericRedisCacheContext)
);
const entity3 = await RedisTestEntity.loader(vc2).enforcing().loadByIDAsync(entity1.getID());
const entity3 = await RedisTestEntity.loader(
vc2,
vc2.getNonTransactionalQueryContextForDatabaseAdaptorFlavor('postgres')
)
.enforcing()
.loadByIDAsync(entity1.getID());
expect(entity3.getField('dateField')).toEqual(date);
});

Expand All @@ -119,9 +143,17 @@ describe(GenericRedisCacher, () => {
createRedisIntegrationTestEntityCompanionProvider(genericRedisCacheContext)
);
const entity1 = await enforceAsyncResult(
RedisTestEntity.creator(viewerContext).setField('name', '').createAsync()
RedisTestEntity.creator(
viewerContext,
viewerContext.getNonTransactionalQueryContextForDatabaseAdaptorFlavor('postgres')
)
.setField('name', '')
.createAsync()
);
const entity2 = await RedisTestEntity.loader(viewerContext)
const entity2 = await RedisTestEntity.loader(
viewerContext,
viewerContext.getNonTransactionalQueryContextForDatabaseAdaptorFlavor('postgres')
)
.enforcing()
.loadByFieldEqualingAsync('name', '');
expect(entity2?.getID()).toEqual(entity1.getID());
Expand All @@ -130,7 +162,10 @@ describe(GenericRedisCacher, () => {
const vc2 = new TestViewerContext(
createRedisIntegrationTestEntityCompanionProvider(genericRedisCacheContext)
);
const entity3 = await RedisTestEntity.loader(vc2)
const entity3 = await RedisTestEntity.loader(
vc2,
vc2.getNonTransactionalQueryContextForDatabaseAdaptorFlavor('postgres')
)
.enforcing()
.loadByFieldEqualingAsync('name', '');
expect(entity3?.getID()).toEqual(entity1.getID());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ describe(GenericRedisCacher, () => {
redisTestEntityConfiguration
);
const date = new Date();
const entity1Created = await RedisTestEntity.creator(viewerContext)
const entity1Created = await RedisTestEntity.creator(
viewerContext,
viewerContext.getNonTransactionalQueryContextForDatabaseAdaptorFlavor('postgres')
)
.setField('name', 'blah')
.setField('dateField', date)
.enforceCreateAsync();
Expand Down Expand Up @@ -92,7 +95,10 @@ describe(GenericRedisCacher, () => {
redisTestEntityConfiguration
);
const date = new Date();
const entity1Created = await RedisTestEntity.creator(viewerContext)
const entity1Created = await RedisTestEntity.creator(
viewerContext,
viewerContext.getNonTransactionalQueryContextForDatabaseAdaptorFlavor('postgres')
)
.setField('name', 'blah')
.setField('dateField', date)
.enforceCreateAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ describe(GenericRedisCacher, () => {
);

await expect(
RedisTestEntity.creator(vc1).setField('name', 'blah').enforceCreateAsync()
RedisTestEntity.creator(
vc1,
vc1.getNonTransactionalQueryContextForDatabaseAdaptorFlavor('postgres')
)
.setField('name', 'blah')
.enforceCreateAsync()
).rejects.toThrow(EntityCacheAdapterTransientError);
});
});
Loading

0 comments on commit 8b0b31f

Please sign in to comment.