-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassign-entity-ids.unit.test.ts
More file actions
164 lines (147 loc) · 4.39 KB
/
assign-entity-ids.unit.test.ts
File metadata and controls
164 lines (147 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { describe, expect, it } from 'vitest';
import {
createTestFeature,
createTestModel,
createTestScalarField,
} from '#src/testing/definition-helpers.test-helper.js';
import { createTestProjectDefinitionContainer } from '#src/testing/project-definition-container.test-helper.js';
import { assignEntityIds } from './assign-entity-ids.js';
describe('assignEntityIds', () => {
const feature = createTestFeature({ name: 'core' });
const model = createTestModel({
name: 'User',
featureRef: feature.name,
model: {
fields: [
createTestScalarField({
name: 'id',
type: 'uuid',
options: { genUuid: true },
}),
createTestScalarField({ name: 'email', type: 'string' }),
],
primaryKeyFieldRefs: ['id'],
},
});
const container = createTestProjectDefinitionContainer({
features: [feature],
models: [model],
});
const modelMetadata = container
.toEntityServiceContext()
.entityTypeMap.get('model');
if (!modelMetadata) {
throw new Error('Expected model entity type metadata');
}
it('replaces user-provided IDs with generated IDs', () => {
const entityData = {
id: 'user-provided-id',
name: 'Post',
featureRef: 'core',
model: {
fields: [
{
id: 'user-field-id',
name: 'title',
type: 'string',
isOptional: false,
options: { default: '' },
},
],
primaryKeyFieldRefs: ['title'],
},
service: {
create: { enabled: false },
update: { enabled: false },
delete: { enabled: false },
transformers: [],
},
};
const result = assignEntityIds(modelMetadata.elementSchema, entityData);
// Top-level entity ID should be replaced
expect(result.id).not.toBe('user-provided-id');
expect(result.id).toMatch(/^model:/);
// Nested field ID should also be replaced
expect(result.model.fields[0].id).not.toBe('user-field-id');
expect(result.model.fields[0].id).toMatch(/^model-scalar-field:/);
// Non-ID data should be preserved
expect(result.name).toBe('Post');
expect(result.model.fields[0].name).toBe('title');
});
it('preserves IDs when isExistingId returns true', () => {
const existingModelId = model.id;
const existingFieldId = model.model.fields[0].id;
const entityData = {
id: existingModelId,
name: 'User',
featureRef: 'core',
model: {
fields: [
{
id: existingFieldId,
name: 'id',
type: 'uuid',
isOptional: false,
options: { genUuid: true },
},
{
id: 'brand-new-field',
name: 'name',
type: 'string',
isOptional: false,
options: { default: '' },
},
],
primaryKeyFieldRefs: ['id'],
},
service: {
create: { enabled: false },
update: { enabled: false },
delete: { enabled: false },
transformers: [],
},
};
const existingIds = new Set([existingModelId, existingFieldId]);
const result = assignEntityIds(modelMetadata.elementSchema, entityData, {
isExistingId: (id) => existingIds.has(id),
});
// Existing IDs should be preserved
expect(result.id).toBe(existingModelId);
expect(result.model.fields[0].id).toBe(existingFieldId);
// New field ID should be replaced
expect(result.model.fields[1].id).not.toBe('brand-new-field');
expect(result.model.fields[1].id).toMatch(/^model-scalar-field:/);
});
it('assigns IDs when entity has no ID', () => {
const entityData = {
name: 'Tag',
featureRef: 'core',
model: {
fields: [
{
name: 'id',
type: 'uuid',
isOptional: false,
options: { genUuid: true },
},
],
primaryKeyFieldRefs: ['id'],
},
service: {
create: { enabled: false },
update: { enabled: false },
delete: { enabled: false },
transformers: [],
},
};
const result = assignEntityIds(
modelMetadata.elementSchema,
entityData,
) as typeof entityData & {
id: string;
model: { fields: { id: string }[] };
};
expect(result.id).toMatch(/^model:/);
expect(result.model.fields[0].id).toMatch(/^model-scalar-field:/);
});
});