/
cross-model-lang-relationship.test.ts
94 lines (77 loc) · 4.06 KB
/
cross-model-lang-relationship.test.ts
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
/********************************************************************************
* Copyright (c) 2023 CrossBreeze.
********************************************************************************/
import { describe, expect, test } from '@jest/globals';
import { isReference } from 'langium';
import {
relationship1,
relationship2,
relationship_with_attribute,
relationship_with_attribute_wrong_entity,
relationship_with_duplicate_attributes
} from './test-utils/test-documents/relationship/index.js';
import { createCrossModelTestServices, parseAndForgetDocument, parseDocuments } from './test-utils/utils.js';
import { CrossModelRoot } from '../../src/language-server/generated/ast.js';
import { address } from './test-utils/test-documents/entity/address.js';
import { customer } from './test-utils/test-documents/entity/customer.js';
import { order } from './test-utils/test-documents/entity/order.js';
const services = createCrossModelTestServices();
describe('CrossModel language Relationship', () => {
beforeAll(() => {
parseDocuments(services, [order, customer, address]);
});
test('Simple file for relationship', async () => {
const document = relationship1;
const parsedDocument = await parseAndForgetDocument(services, document);
const model = parsedDocument.parseResult.value as CrossModelRoot;
expect(model).toHaveProperty('relationship');
expect(parsedDocument.parseResult.lexerErrors.length).toBe(0);
expect(parsedDocument.parseResult.parserErrors.length).toBe(0);
expect(model.relationship?.id).toBe('Order_Customer');
expect(model.relationship?.name).toBe('Customer Order relationship');
expect(model.relationship?.type).toBe('1:1');
expect(model.relationship?.description).toBe('A relationship between a customer and an order.');
expect(isReference(model.relationship?.parent)).toBe(true);
expect(isReference(model.relationship?.child)).toBe(true);
expect(model.relationship?.parent?.$refText).toBe('Customer');
expect(model.relationship?.child?.$refText).toBe('Order');
});
test('relationship with indentation error', async () => {
const document = relationship2;
const parsedDocument = await parseAndForgetDocument(services, document);
const model = parsedDocument.parseResult.value as CrossModelRoot;
expect(model).toHaveProperty('relationship');
expect(parsedDocument.parseResult.lexerErrors.length).toBe(0);
expect(parsedDocument.parseResult.parserErrors.length).toBe(1);
});
test('relationship with attributes', async () => {
const parsedDocument = await parseAndForgetDocument(services, relationship_with_attribute, {
validation: true
});
const model = parsedDocument.parseResult.value as CrossModelRoot;
expect(model).toHaveProperty('relationship');
expect(parsedDocument.parseResult.lexerErrors.length).toBe(0);
expect(parsedDocument.parseResult.parserErrors.length).toBe(0);
expect(parsedDocument.diagnostics).toHaveLength(0);
});
test('relationship with wrong entity', async () => {
const parsedDocument = await parseAndForgetDocument(services, relationship_with_attribute_wrong_entity, {
validation: true
});
const model = parsedDocument.parseResult.value as CrossModelRoot;
expect(model).toHaveProperty('relationship');
expect(parsedDocument.parseResult.lexerErrors.length).toBe(0);
expect(parsedDocument.parseResult.parserErrors.length).toBe(0);
expect(parsedDocument.diagnostics).toHaveLength(1);
});
test('relationship with duplicates', async () => {
const parsedDocument = await parseAndForgetDocument(services, relationship_with_duplicate_attributes, {
validation: true
});
const model = parsedDocument.parseResult.value as CrossModelRoot;
expect(model).toHaveProperty('relationship');
expect(parsedDocument.parseResult.lexerErrors.length).toBe(0);
expect(parsedDocument.parseResult.parserErrors.length).toBe(0);
expect(parsedDocument.diagnostics).toHaveLength(2);
});
});