From 80e018a0a053388efad222acc84be2267760fdf5 Mon Sep 17 00:00:00 2001 From: Will Schurman Date: Wed, 29 May 2024 16:40:24 -0700 Subject: [PATCH] chore: add more tests for stub database adapter and test utils --- .../PrivacyPolicyRuleTestUtils-test.ts | 40 ++++++++++++++++++ .../__tests__/StubDatabaseAdapter-test.ts | 42 +++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 packages/entity/src/utils/testing/__tests__/PrivacyPolicyRuleTestUtils-test.ts diff --git a/packages/entity/src/utils/testing/__tests__/PrivacyPolicyRuleTestUtils-test.ts b/packages/entity/src/utils/testing/__tests__/PrivacyPolicyRuleTestUtils-test.ts new file mode 100644 index 000000000..86e558d61 --- /dev/null +++ b/packages/entity/src/utils/testing/__tests__/PrivacyPolicyRuleTestUtils-test.ts @@ -0,0 +1,40 @@ +import { anything, instance, mock } from 'ts-mockito'; + +import { EntityPrivacyPolicyEvaluationContext } from '../../../EntityPrivacyPolicy'; +import { EntityQueryContext } from '../../../EntityQueryContext'; +import ViewerContext from '../../../ViewerContext'; +import AlwaysAllowPrivacyPolicyRule from '../../../rules/AlwaysAllowPrivacyPolicyRule'; +import AlwaysDenyPrivacyPolicyRule from '../../../rules/AlwaysDenyPrivacyPolicyRule'; +import { describePrivacyPolicyRuleWithAsyncTestCase } from '../PrivacyPolicyRuleTestUtils'; + +describe(describePrivacyPolicyRuleWithAsyncTestCase, () => { + describe('default args do not execute', () => { + describePrivacyPolicyRuleWithAsyncTestCase(new AlwaysAllowPrivacyPolicyRule(), { + allowCases: new Map([ + [ + 'case', + async () => ({ + viewerContext: instance(mock(ViewerContext)), + queryContext: instance(mock(EntityQueryContext)), + evaluationContext: instance(mock()), + entity: anything(), + }), + ], + ]), + }); + + describePrivacyPolicyRuleWithAsyncTestCase(new AlwaysDenyPrivacyPolicyRule(), { + denyCases: new Map([ + [ + 'case', + async () => ({ + viewerContext: instance(mock(ViewerContext)), + queryContext: instance(mock(EntityQueryContext)), + evaluationContext: instance(mock()), + entity: anything(), + }), + ], + ]), + }); + }); +}); diff --git a/packages/entity/src/utils/testing/__tests__/StubDatabaseAdapter-test.ts b/packages/entity/src/utils/testing/__tests__/StubDatabaseAdapter-test.ts index 26a0af62b..7f64256b4 100644 --- a/packages/entity/src/utils/testing/__tests__/StubDatabaseAdapter-test.ts +++ b/packages/entity/src/utils/testing/__tests__/StubDatabaseAdapter-test.ts @@ -334,6 +334,34 @@ describe(StubDatabaseAdapter, () => { testIndexedField: 'h1', }); }); + + it('throws error when empty update to match common DBMS behavior', async () => { + const queryContext = instance(mock(EntityQueryContext)); + const databaseAdapter = new StubDatabaseAdapter( + testEntityConfiguration, + StubDatabaseAdapter.convertFieldObjectsToDataStore( + testEntityConfiguration, + new Map([ + [ + testEntityConfiguration.tableName, + [ + { + customIdField: 'hello', + testIndexedField: 'h1', + intField: 3, + stringField: 'a', + dateField: new Date(), + nullableField: null, + }, + ], + ], + ]) + ) + ); + await expect( + databaseAdapter.updateAsync(queryContext, 'customIdField', 'hello', {}) + ).rejects.toThrowError(`Empty update (custom_id = hello)`); + }); }); describe('deleteAsync', () => { @@ -436,6 +464,20 @@ describe(StubDatabaseAdapter, () => { ) ).toEqual(expectedResult); }); + + it('works for empty', () => { + expect( + StubDatabaseAdapter['compareByOrderBys']( + [], + { + hello: 'test', + }, + { + hello: 'blah', + } + ) + ).toEqual(0); + }); }); describe('recursing', () => {