Skip to content

Commit

Permalink
feat!: Default all loaders and mutators to enforcing
Browse files Browse the repository at this point in the history
  • Loading branch information
wschurman committed Feb 24, 2025
1 parent 828daef commit e072e65
Show file tree
Hide file tree
Showing 35 changed files with 896 additions and 820 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ describe(GenericLocalMemoryCacher, () => {

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

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

const localMemoryCacheAdapterProvider = (
entityCompanionProvider['cacheAdapterFlavors'] as ReadonlyMap<
Expand Down Expand Up @@ -64,9 +63,10 @@ describe(GenericLocalMemoryCacher, () => {
// simulate non existent db fetch, should write negative result ('') to cache
const nonExistentId = uuidv4();

const entityNonExistentResult = await LocalMemoryTestEntity.loader(viewerContext)
.withAuthorizationResults()
.loadByIDAsync(nonExistentId);
const entityNonExistentResult =
await LocalMemoryTestEntity.loaderWithAuthorizationResults(viewerContext).loadByIDAsync(
nonExistentId,
);
expect(entityNonExistentResult.ok).toBe(false);

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

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

// invalidate from cache to ensure it invalidates correctly
await LocalMemoryTestEntity.loader(viewerContext)
.utils()
.invalidateFieldsAsync(entity1.getAllFields());
await LocalMemoryTestEntity.loaderUtils(viewerContext).invalidateFieldsAsync(
entity1.getAllFields(),
);
const cachedResultMiss = await entitySpecificGenericCacher.loadManyAsync([
cacheKeyMaker('id', entity1.getID()),
]);
Expand All @@ -104,15 +105,14 @@ describe(GenericLocalMemoryCacher, () => {

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

// 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)
.enforcing()
.loadByIDAsync(entity1Created.getID());
const entity1 = await LocalMemoryTestEntity.loader(viewerContext).loadByIDAsync(
entity1Created.getID(),
);

const localMemoryCacheAdapterProvider = (
entityCompanionProvider['cacheAdapterFlavors'] as ReadonlyMap<
Expand All @@ -137,9 +137,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)
.withAuthorizationResults()
.loadByIDAsync(nonExistentId);
const entityNonExistentResult =
await LocalMemoryTestEntity.loaderWithAuthorizationResults(viewerContext).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 @@ -104,7 +104,6 @@ describe(GenericRedisCacher, () => {
const cacheKeyMaker = genericCacher['makeCacheKey'].bind(genericCacher);

const entity1Created = await RedisTestEntity.creator(viewerContext)
.enforcing()
.setField('name', 'blah')
.createAsync();

Expand All @@ -120,11 +119,12 @@ 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)
.enforcing()
.loadByFieldEqualingAsync('name', entity1Created.getField('name')),
RedisTestEntity.loader(viewerContext1).loadByIDAsync(entity1Created.getID()),
RedisTestEntity.loader(viewerContext2).loadByIDAsync(entity1Created.getID()),
RedisTestEntity.loader(viewerContext3).loadByFieldEqualingAsync(
'name',
entity1Created.getField('name'),
),
]);

expect(mgetSpy).toHaveBeenCalledTimes(1);
Expand All @@ -141,30 +141,31 @@ describe(GenericRedisCacher, () => {
});

const cacheKeyEntity1NameField = cacheKeyMaker('name', entity1Created.getField('name'));
await RedisTestEntity.loader(viewerContext)
.enforcing()
.loadByFieldEqualingAsync('name', entity1Created.getField('name'));
await RedisTestEntity.loader(viewerContext).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)
.withAuthorizationResults()
.loadByIDAsync(nonExistentId);
const entityNonExistentResult =
await RedisTestEntity.loaderWithAuthorizationResults(viewerContext).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)
.withAuthorizationResults()
.loadByIDAsync(nonExistentId);
const entityNonExistentResult2 =
await RedisTestEntity.loaderWithAuthorizationResults(viewerContext).loadByIDAsync(
nonExistentId,
);
expect(entityNonExistentResult2.ok).toBe(false);

// invalidate from cache to ensure it invalidates correctly in both caches
await RedisTestEntity.loader(viewerContext)
.utils()
.invalidateFieldsAsync(entity1.getAllFields());
await RedisTestEntity.loaderUtils(viewerContext).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 @@ -47,14 +47,13 @@ describe(GenericRedisCacher, () => {
const cacheKeyMaker = genericCacher['makeCacheKey'].bind(genericCacher);

const entity1Created = await RedisTestEntity.creator(viewerContext)
.enforcing()
.setField('name', 'blah')
.createAsync();

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

const cachedJSON = await (genericRedisCacheContext.redisClient as Redis).get(
cacheKeyMaker('id', entity1.getID()),
Expand All @@ -68,9 +67,10 @@ describe(GenericRedisCacher, () => {
// simulate non existent db fetch, should write negative result ('') to cache
const nonExistentId = uuidv4();

const entityNonExistentResult = await RedisTestEntity.loader(viewerContext)
.withAuthorizationResults()
.loadByIDAsync(nonExistentId);
const entityNonExistentResult =
await RedisTestEntity.loaderWithAuthorizationResults(viewerContext).loadByIDAsync(
nonExistentId,
);
expect(entityNonExistentResult.ok).toBe(false);

const nonExistentCachedValue = await (genericRedisCacheContext.redisClient as Redis).get(
Expand All @@ -79,15 +79,14 @@ describe(GenericRedisCacher, () => {
expect(nonExistentCachedValue).toEqual('');

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

// invalidate from cache to ensure it invalidates correctly
await RedisTestEntity.loader(viewerContext)
.utils()
.invalidateFieldsAsync(entity1.getAllFields());
await RedisTestEntity.loaderUtils(viewerContext).invalidateFieldsAsync(entity1.getAllFields());
const cachedValueNull = await (genericRedisCacheContext.redisClient as Redis).get(
cacheKeyMaker('id', entity1.getID()),
);
Expand All @@ -100,23 +99,20 @@ describe(GenericRedisCacher, () => {
);
const date = new Date();
const entity1 = await enforceAsyncResult(
RedisTestEntity.creator(viewerContext)
.withAuthorizationResults()
RedisTestEntity.creatorWithAuthorizationResults(viewerContext)
.setField('dateField', date)
.createAsync(),
);
expect(entity1.getField('dateField')).toEqual(date);

const entity2 = await RedisTestEntity.loader(viewerContext)
.enforcing()
.loadByIDAsync(entity1.getID());
const entity2 = await RedisTestEntity.loader(viewerContext).loadByIDAsync(entity1.getID());
expect(entity2.getField('dateField')).toEqual(date);

// simulate new request
const vc2 = new TestViewerContext(
createRedisIntegrationTestEntityCompanionProvider(genericRedisCacheContext),
);
const entity3 = await RedisTestEntity.loader(vc2).enforcing().loadByIDAsync(entity1.getID());
const entity3 = await RedisTestEntity.loader(vc2).loadByIDAsync(entity1.getID());
expect(entity3.getField('dateField')).toEqual(date);
});

Expand All @@ -125,23 +121,21 @@ describe(GenericRedisCacher, () => {
createRedisIntegrationTestEntityCompanionProvider(genericRedisCacheContext),
);
const entity1 = await enforceAsyncResult(
RedisTestEntity.creator(viewerContext)
.withAuthorizationResults()
RedisTestEntity.creatorWithAuthorizationResults(viewerContext)
.setField('name', '')
.createAsync(),
);
const entity2 = await RedisTestEntity.loader(viewerContext)
.enforcing()
.loadByFieldEqualingAsync('name', '');
const entity2 = await RedisTestEntity.loader(viewerContext).loadByFieldEqualingAsync(
'name',
'',
);
expect(entity2?.getID()).toEqual(entity1.getID());

// simulate new request
const vc2 = new TestViewerContext(
createRedisIntegrationTestEntityCompanionProvider(genericRedisCacheContext),
);
const entity3 = await RedisTestEntity.loader(vc2)
.enforcing()
.loadByFieldEqualingAsync('name', '');
const entity3 = await RedisTestEntity.loader(vc2).loadByFieldEqualingAsync('name', '');
expect(entity3?.getID()).toEqual(entity1.getID());
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ describe(GenericRedisCacher, () => {
);
const date = new Date();
const entity1Created = await RedisTestEntity.creator(viewerContext)
.enforcing()
.setField('name', 'blah')
.setField('dateField', date)
.createAsync();
Expand Down Expand Up @@ -94,7 +93,6 @@ describe(GenericRedisCacher, () => {
);
const date = new Date();
const entity1Created = await RedisTestEntity.creator(viewerContext)
.enforcing()
.setField('name', 'blah')
.setField('dateField', date)
.createAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe(GenericRedisCacher, () => {
);

await expect(
RedisTestEntity.creator(vc1).enforcing().setField('name', 'blah').createAsync(),
RedisTestEntity.creator(vc1).setField('name', 'blah').createAsync(),
).rejects.toThrow(EntityCacheAdapterTransientError);
});
});
Loading

0 comments on commit e072e65

Please sign in to comment.