Skip to content

Commit 343f0c5

Browse files
committed
add file
1 parent cc5a7ff commit 343f0c5

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import { expect } from 'chai';
2+
import {
3+
isRelationshipInvolvingField,
4+
isRelationshipOfAField,
5+
isSameFieldOrAncestor,
6+
} from './utils';
7+
8+
describe('isSameFieldOrAncestor', function () {
9+
it('should return true for the same field', function () {
10+
expect(isSameFieldOrAncestor(['a', 'b'], ['a', 'b'])).to.be.true;
11+
});
12+
13+
it('should return true for a child field', function () {
14+
expect(isSameFieldOrAncestor(['a', 'b'], ['a', 'b', 'c'])).to.be.true;
15+
});
16+
17+
it('should return false for a parent field', function () {
18+
expect(isSameFieldOrAncestor(['a', 'b', 'c'], ['a', 'b'])).to.be.false;
19+
});
20+
21+
it('should return false for another field', function () {
22+
expect(isSameFieldOrAncestor(['a', 'b'], ['a', 'c'])).to.be.false;
23+
});
24+
});
25+
26+
describe('isRelationshipOfAField', function () {
27+
const relationship = [
28+
{ ns: 'db.coll1', fields: ['a', 'b'] },
29+
{ ns: 'db.coll2', fields: ['c', 'd'] },
30+
];
31+
32+
it('should return true for exact match', function () {
33+
expect(isRelationshipOfAField(relationship, 'db.coll1', ['a', 'b'])).to.be
34+
.true;
35+
expect(isRelationshipOfAField(relationship, 'db.coll2', ['c', 'd'])).to.be
36+
.true;
37+
});
38+
39+
it('should return false for other fields', function () {
40+
expect(isRelationshipOfAField(relationship, 'db.coll1', ['a'])).to.be.false;
41+
expect(isRelationshipOfAField(relationship, 'db.coll1', ['a', 'c'])).to.be
42+
.false;
43+
});
44+
45+
it('should handle incomplete relationships', function () {
46+
expect(
47+
isRelationshipOfAField([{}, relationship[1]], 'db.coll2', ['c', 'd'])
48+
).to.be.true;
49+
expect(
50+
isRelationshipOfAField(
51+
[{ ns: 'db.coll2', fields: null }, relationship[1]],
52+
'db.coll2',
53+
['c', 'd']
54+
)
55+
).to.be.true;
56+
});
57+
});
58+
59+
describe('isRelationshipInvolvingAField', function () {
60+
const relationship = [
61+
{ ns: 'db.coll1', fields: ['a', 'b'] },
62+
{ ns: 'db.coll2', fields: ['c', 'd', 'e'] },
63+
];
64+
65+
it('should return true for exact match', function () {
66+
expect(isRelationshipInvolvingField(relationship, 'db.coll1', ['a', 'b']))
67+
.to.be.true;
68+
expect(
69+
isRelationshipInvolvingField(relationship, 'db.coll2', ['c', 'd', 'e'])
70+
).to.be.true;
71+
});
72+
73+
it('should return true for fields that are on the path (ancestors)', function () {
74+
expect(isRelationshipInvolvingField(relationship, 'db.coll1', ['a'])).to.be
75+
.true;
76+
expect(isRelationshipInvolvingField(relationship, 'db.coll2', ['c', 'd']))
77+
.to.be.true;
78+
expect(isRelationshipInvolvingField(relationship, 'db.coll2', ['c'])).to.be
79+
.true;
80+
});
81+
82+
it('should return false fields that are not on the path', function () {
83+
expect(
84+
isRelationshipInvolvingField(relationship, 'db.coll1', ['a', 'b', 'c'])
85+
).to.be.false;
86+
expect(isRelationshipInvolvingField(relationship, 'db.coll2', ['g'])).to.be
87+
.false;
88+
});
89+
90+
it('should handle incomplete relationships', function () {
91+
expect(
92+
isRelationshipInvolvingField([{}, relationship[1]], 'db.coll2', [
93+
'c',
94+
'd',
95+
])
96+
).to.be.true;
97+
expect(
98+
isRelationshipInvolvingField(
99+
[{ ns: 'db.coll2', fields: null }, relationship[1]],
100+
'db.coll2',
101+
['c', 'd']
102+
)
103+
).to.be.true;
104+
});
105+
});

0 commit comments

Comments
 (0)