Skip to content

Commit

Permalink
chore: add more tests for entity loader
Browse files Browse the repository at this point in the history
  • Loading branch information
wschurman committed May 29, 2024
1 parent 20fb26a commit 1ec850f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
2 changes: 0 additions & 2 deletions packages/entity/src/EntityLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,6 @@ export default class EntityLoader<
* @param querySelectionModifiers - limit, offset, orderBy, and orderByRaw for the query
* @returns array of entity results that match the query, where result error can be UnauthorizedError
* @throws Error when rawWhereClause or bindings are invalid
*
* @deprecated prefer caching loaders
*/
async loadManyByRawWhereClauseAsync(
rawWhereClause: string,
Expand Down
68 changes: 67 additions & 1 deletion packages/entity/src/__tests__/EntityLoader-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import EntityLoader from '../EntityLoader';
import { EntityPrivacyPolicyEvaluationContext } from '../EntityPrivacyPolicy';
import ViewerContext from '../ViewerContext';
import { enforceResultsAsync } from '../entityUtils';
import EntityNotFoundError from '../errors/EntityNotFoundError';
import EntityDataManager from '../internal/EntityDataManager';
import ReadThroughEntityCache from '../internal/ReadThroughEntityCache';
import IEntityMetricsAdapter from '../metrics/IEntityMetricsAdapter';
Expand Down Expand Up @@ -109,6 +110,15 @@ describe(EntityLoader, () => {
await expect(entityLoader.loadByIDNullableAsync(uuidv4())).resolves.toBeNull();
await expect(entityLoader.loadByIDNullableAsync(id1)).resolves.not.toBeNull();

const nonExistentId = uuidv4();
const manyIdResults = await entityLoader.loadManyByIDsNullableAsync([nonExistentId, id1]);
expect(manyIdResults.get(nonExistentId)).toBeNull();
expect(manyIdResults.get(id1)).not.toBeNull();

await expect(enforceAsyncResult(entityLoader.loadByIDAsync(nonExistentId))).rejects.toThrow(
EntityNotFoundError
);

await expect(entityLoader.loadByIDAsync('not-a-uuid')).rejects.toThrowError(
'Entity field not valid: TestEntity (customIdField = not-a-uuid)'
);
Expand Down Expand Up @@ -191,7 +201,7 @@ describe(EntityLoader, () => {
},
{
fieldName: 'intField',
fieldValue: 4,
fieldValues: [4],
},
])
);
Expand Down Expand Up @@ -309,6 +319,62 @@ describe(EntityLoader, () => {
).once();
});

it('loads entities with loadManyByRawWhereClauseAsync', async () => {
const privacyPolicy = new TestEntityPrivacyPolicy();
const spiedPrivacyPolicy = spy(privacyPolicy);
const viewerContext = instance(mock(ViewerContext));
const privacyPolicyEvaluationContext = instance(mock<EntityPrivacyPolicyEvaluationContext>());
const metricsAdapter = instance(mock<IEntityMetricsAdapter>());
const queryContext = StubQueryContextProvider.getQueryContext();

const dataManagerMock = mock(EntityDataManager);
when(
dataManagerMock.loadManyByRawWhereClauseAsync(
queryContext,
anything(),
anything(),
anything()
)
).thenResolve([
{
customIdField: 'id',
stringField: 'huh',
intField: 4,
testIndexedField: '4',
dateField: new Date(),
nullableField: null,
},
]);
const dataManager = instance(dataManagerMock);
const entityLoader = new EntityLoader(
viewerContext,
queryContext,
privacyPolicyEvaluationContext,
testEntityConfiguration,
TestEntity,
/* entitySelectedFields */ undefined,
privacyPolicy,
dataManager,
metricsAdapter
);
const result = await entityLoader.loadManyByRawWhereClauseAsync('id = ?', [1], {
orderBy: [{ fieldName: 'testIndexedField', order: OrderByOrdering.DESCENDING }],
});
expect(result).toHaveLength(1);
expect(result[0]).not.toBeNull();
expect(result[0]!.ok).toBe(true);
expect(result[0]!.enforceValue().getField('testIndexedField')).toEqual('4');
verify(
spiedPrivacyPolicy.authorizeReadAsync(
viewerContext,
queryContext,
privacyPolicyEvaluationContext,
anyOfClass(TestEntity),
anything()
)
).once();
});

it('authorizes loaded entities', async () => {
const privacyPolicy = new TestEntityPrivacyPolicy();
const spiedPrivacyPolicy = spy(privacyPolicy);
Expand Down

0 comments on commit 1ec850f

Please sign in to comment.