Skip to content

Commit cc5a7ff

Browse files
committed
add more tests
1 parent 850b54f commit cc5a7ff

File tree

4 files changed

+36
-31
lines changed

4 files changed

+36
-31
lines changed

packages/compass-data-modeling/src/components/drawer/diagram-editor-side-panel.spec.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,10 @@ describe('DiagramEditorSidePanel', function () {
204204
});
205205

206206
expect(
207-
modifiedCollection.jsonSchema.properties.airline.properties
207+
modifiedCollection?.jsonSchema.properties?.airline.properties
208208
).to.not.have.property('id'); // deleted field
209209
expect(
210-
modifiedCollection.jsonSchema.properties.airline.properties
210+
modifiedCollection?.jsonSchema.properties?.airline.properties
211211
).to.have.property('name'); // sibling field remains
212212
});
213213

packages/compass-data-modeling/src/services/data-model-storage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ const EditSchemaVariants = z.discriminatedUnion('type', [
9898
z.object({
9999
type: z.literal('RenameField'),
100100
ns: z.string(),
101-
from: FieldPathSchema,
102-
to: z.string(),
101+
field: FieldPathSchema,
102+
newName: z.string(),
103103
}),
104104
z.object({
105105
type: z.literal('RemoveField'),

packages/compass-data-modeling/src/store/diagram.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import toNS from 'mongodb-ns';
3232
import { traverseSchema, updateSchema } from '../utils/schema-traversal';
3333
import {
3434
isRelationshipInvolvingField,
35-
isSameFieldOrChild,
35+
isSameFieldOrAncestor,
3636
} from '../utils/utils';
3737

3838
function isNonEmptyArray<T>(arr: T[]): arr is [T, ...T[]] {
@@ -391,7 +391,10 @@ const updateSelectedItemsFromAppliedEdit = (
391391
return {
392392
type: 'field',
393393
namespace: edit.ns,
394-
fieldPath: [...edit.from.slice(0, edit.from.length - 1), edit.to],
394+
fieldPath: [
395+
...edit.field.slice(0, edit.field.length - 1),
396+
edit.newName,
397+
],
395398
};
396399
}
397400
}
@@ -681,7 +684,7 @@ export function renameField(
681684
field: FieldPath,
682685
newName: string
683686
): DataModelingThunkAction<boolean, ApplyEditAction | ApplyEditFailedAction> {
684-
return applyEdit({ type: 'RenameField', ns, from: field, to: newName });
687+
return applyEdit({ type: 'RenameField', ns, field, newName });
685688
}
686689

687690
function getPositionForNewCollection(
@@ -765,16 +768,16 @@ function renameFieldInRelationshipSide(
765768
if (
766769
side.ns !== edit.ns ||
767770
!side.fields ||
768-
!isSameFieldOrChild(side.fields, edit.from)
771+
!isSameFieldOrAncestor(edit.field, side.fields)
769772
) {
770773
return side;
771774
}
772775
return {
773776
...side,
774777
fields: [
775-
...side.fields.slice(0, edit.from.length - 1),
776-
edit.to,
777-
...side.fields.slice(edit.from.length),
778+
...side.fields.slice(0, edit.field.length - 1),
779+
edit.newName,
780+
...side.fields.slice(edit.field.length),
778781
],
779782
};
780783
}
@@ -927,7 +930,7 @@ function _applyEdit(edit: Edit, model?: StaticModel): StaticModel {
927930
// Update any relationships involving the field being renamed.
928931
relationships: model.relationships.map((r) => {
929932
if (
930-
!isRelationshipInvolvingField(r.relationship, edit.ns, edit.from)
933+
!isRelationshipInvolvingField(r.relationship, edit.ns, edit.field)
931934
) {
932935
return r;
933936
}
@@ -945,9 +948,9 @@ function _applyEdit(edit: Edit, model?: StaticModel): StaticModel {
945948
...collection,
946949
jsonSchema: updateSchema({
947950
jsonSchema: collection.jsonSchema,
948-
fieldPath: edit.from,
951+
fieldPath: edit.field,
949952
update: 'renameField',
950-
newFieldName: edit.to,
953+
newFieldName: edit.newName,
951954
}),
952955
};
953956
}),

packages/compass-data-modeling/src/utils/utils.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@ export function areFieldPathsEqual(
77
return JSON.stringify(fieldA) === JSON.stringify(fieldB);
88
}
99

10-
export function isSameFieldOrChild(
11-
fieldA: FieldPath,
12-
fieldB: FieldPath
10+
export function isSameFieldOrAncestor(
11+
ancestor: FieldPath,
12+
child: FieldPath
1313
): boolean {
14-
if (fieldA.length === fieldB.length)
15-
return areFieldPathsEqual(fieldA, fieldB);
16-
if (fieldA.length < fieldB.length) return false;
17-
// fieldA is shorter than fieldB, check if fieldA is a parent of fieldB
18-
const pathA = JSON.stringify(fieldA);
19-
const pathB = JSON.stringify(fieldB);
20-
// ignore the closing bracket - last character in pathB
21-
return pathA.slice(0, pathB.length - 1) === pathB.slice(0, pathB.length - 1);
14+
if (ancestor.length === child.length)
15+
return areFieldPathsEqual(ancestor, child);
16+
if (ancestor.length > child.length) return false;
17+
const pathAncestor = JSON.stringify(ancestor);
18+
const pathChild = JSON.stringify(child);
19+
// ignore the last character - closing bracket
20+
return (
21+
pathAncestor.slice(0, pathAncestor.length - 1) ===
22+
pathChild.slice(0, pathAncestor.length - 1)
23+
);
2224
}
2325

2426
export function isRelationshipOfAField(
@@ -29,10 +31,10 @@ export function isRelationshipOfAField(
2931
const [local, foreign] = relationship;
3032
return (
3133
(local.ns === namespace &&
32-
local.fields !== null &&
34+
!!local.fields &&
3335
areFieldPathsEqual(local.fields, fieldPath)) ||
3436
(foreign.ns === namespace &&
35-
foreign.fields !== null &&
37+
!!foreign.fields &&
3638
areFieldPathsEqual(foreign.fields, fieldPath))
3739
);
3840
}
@@ -45,10 +47,10 @@ export function isRelationshipInvolvingField(
4547
const [local, foreign] = relationship;
4648
return (
4749
(local.ns === namespace &&
48-
local.fields !== null &&
49-
isSameFieldOrChild(local.fields, fieldPath)) ||
50+
!!local.fields &&
51+
isSameFieldOrAncestor(fieldPath, local.fields)) ||
5052
(foreign.ns === namespace &&
51-
foreign.fields !== null &&
52-
isSameFieldOrChild(foreign.fields, fieldPath))
53+
!!foreign.fields &&
54+
isSameFieldOrAncestor(fieldPath, foreign.fields))
5355
);
5456
}

0 commit comments

Comments
 (0)