Skip to content

Commit c398ee6

Browse files
authored
feat: validation rules generation COMPASS-8859 (#6781)
1 parent 4ffc806 commit c398ee6

File tree

13 files changed

+692
-44
lines changed

13 files changed

+692
-44
lines changed

package-lock.json

Lines changed: 71 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/compass-e2e-tests/helpers/selectors.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,8 @@ export const ValidationActionSelector =
11631163
'[data-testid="validation-action-selector"]';
11641164
export const ValidationLevelSelector =
11651165
'[data-testid="validation-level-selector"]';
1166+
export const GenerateValidationRulesButton =
1167+
'[data-testid="generate-rules-button"]';
11661168

11671169
// Find (Documents and Schema tabs)
11681170
export const queryBar = (tabName: string): string => {

packages/compass-e2e-tests/tests/collection-validation-tab.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {
88
import type { Compass } from '../helpers/compass';
99
import * as Selectors from '../helpers/selectors';
1010
import { createNumbersCollection } from '../helpers/insert-data';
11+
import { expect } from 'chai';
12+
import { isTestingDesktop } from '../helpers/test-runner-context';
1113

1214
const NO_PREVIEW_DOCUMENTS = 'No Preview Documents';
1315
const PASSING_VALIDATOR = '{ $jsonSchema: {} }';
@@ -52,6 +54,45 @@ describe('Collection validation tab', function () {
5254
await browser.setValidation(validation);
5355
}
5456

57+
context('when the schema validation is empty', function () {
58+
before(async function () {
59+
if (isTestingDesktop()) {
60+
await browser.setFeature('enableExportSchema', true);
61+
}
62+
});
63+
64+
it('provides users with a button to generate rules', async function () {
65+
await browser.clickVisible(Selectors.GenerateValidationRulesButton);
66+
const editor = browser.$(Selectors.ValidationEditor);
67+
await editor.waitForDisplayed();
68+
69+
// rules are generated
70+
const generatedRules = await browser.getCodemirrorEditorText(
71+
Selectors.ValidationEditor
72+
);
73+
expect(JSON.parse(generatedRules)).to.deep.equal({
74+
$jsonSchema: {
75+
bsonType: 'object',
76+
required: ['_id', 'i', 'j'],
77+
properties: {
78+
_id: {
79+
bsonType: 'objectId',
80+
},
81+
i: {
82+
bsonType: 'int',
83+
},
84+
j: {
85+
bsonType: 'int',
86+
},
87+
},
88+
},
89+
});
90+
91+
// generated rules can be edited and saved
92+
await browser.setValidation(PASSING_VALIDATOR);
93+
});
94+
});
95+
5596
context('when the schema validation is set or modified', function () {
5697
it('provides users with a single button to load sample documents', async function () {
5798
await addValidation(PASSING_VALIDATOR);

packages/compass-schema-validation/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,15 @@
7777
"@mongodb-js/compass-editor": "^0.36.5",
7878
"@mongodb-js/compass-field-store": "^9.25.5",
7979
"@mongodb-js/compass-logging": "^1.6.5",
80+
"@mongodb-js/compass-schema": "^6.51.5",
8081
"@mongodb-js/compass-telemetry": "^1.4.5",
8182
"@mongodb-js/compass-workspaces": "^0.31.5",
8283
"bson": "^6.10.3",
8384
"compass-preferences-model": "^2.33.5",
8485
"hadron-app-registry": "^9.4.5",
8586
"javascript-stringify": "^2.0.1",
8687
"lodash": "^4.17.21",
88+
"mongodb": "^6.14.1",
8789
"mongodb-ns": "^2.4.2",
8890
"mongodb-query-parser": "^4.3.0",
8991
"react": "^17.0.2",

packages/compass-schema-validation/src/components/validation-states.spec.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ const { renderWithConnections } = createPluginTestHelpers(
3131
describe('ValidationStates [Component]', function () {
3232
let props: any;
3333

34-
const render = (props: any) => {
35-
return renderWithConnections(<ValidationStates {...props} />);
34+
const render = (props: any, options: any = {}) => {
35+
return renderWithConnections(<ValidationStates {...props} />, options);
3636
};
3737

3838
beforeEach(function () {
@@ -255,13 +255,18 @@ describe('ValidationStates [Component]', function () {
255255
props.isZeroState = true;
256256
props.isLoaded = true;
257257
props.serverVersion = '3.2.0';
258-
259-
render(props);
260258
});
261259

262260
it('renders the zero state', function () {
261+
render(props);
263262
expect(screen.getByTestId('empty-content')).to.exist;
264263
});
264+
265+
it('when enableExportSchema is set, shows button for rules generation', function () {
266+
render(props, { preferences: { enableExportSchema: true } });
267+
const btn = screen.getByRole('button', { name: 'Generate rules' });
268+
expect(btn).to.be.visible;
269+
});
265270
});
266271

267272
context('when it is not in the zero state and not loaded', function () {

0 commit comments

Comments
 (0)