-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests-relation-one-to-many.tests.js
158 lines (147 loc) · 4.51 KB
/
tests-relation-one-to-many.tests.js
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
/* eslint-env mocha */
/* eslint-disable func-names, prefer-arrow-callback */
import { chai } from 'meteor/practicalmeteor:chai'
const expect = chai.expect
// Import and rename a variable exported by denormalization.js.
import { Denormalize, AUTOFORM_IS_ACTIVE } from 'meteor/thebarty:denormalization'
Denormalize.Debug = true
const OneToMany = new Mongo.Collection('onetomany')
const ManyToOne = new Mongo.Collection('manytoone')
// TESTS
if (Meteor.isServer) {
describe('One-To-Many Relations work', function () {
// CONFIGURATION
// INSERT
// UPDATE
// REMOVE
it('configuration works and throws errors where wanted', function () {
// VALID config
expect(() => {
OneToMany.attachDenormalizedSchema(
{
manyIds: {
type: [String], // CORRECT
denormalize: {
relation: Denormalize.HAS_MANY,
relatedCollection: ManyToOne,
relatedReferenceProperty: 'oneId',
},
},
},
)
}).to.not.throw()
// INVALID configs
expect(() => {
OneToMany.attachDenormalizedSchema(
{
manyIds: {
type: String, // WRONG TYPE should throw
denormalize: {
relation: Denormalize.HAS_MANY,
relatedCollection: ManyToOne,
relatedReferenceProperty: 'oneId',
},
},
},
)
}).to.throw()
expect(() => {
OneToMany.attachDenormalizedSchema(
{
manyIds: {
type: String,
denormalize: {
// relation: Denormalize.HAS_MANY,
relatedCollection: ManyToOne,
relatedReferenceProperty: 'oneId',
},
},
},
)
}).to.throw()
expect(() => {
OneToMany.attachDenormalizedSchema(
{
manyIds: {
type: String,
denormalize: {
relation: Denormalize.HAS_MANY,
// relatedCollection: ManyToOne,
relatedReferenceProperty: 'oneId',
},
},
},
)
}).to.throw()
expect(() => {
OneToMany.attachDenormalizedSchema(
{
manyIds: {
type: String,
denormalize: {
relation: Denormalize.HAS_MANY,
relatedCollection: ManyToOne,
// relatedReferenceProperty: 'oneId',
},
},
},
)
}).to.throw()
})
it('relation can be defined one-way', function () {
OneToMany.remove({})
ManyToOne.remove({})
// Check that it works if only ONE collection has ``denormalize``-configs
OneToMany.attachDenormalizedSchema(
{
string: {
type: String,
},
manyIds: {
type: [String],
optional: false, // MANDATORY
denormalize: {
relation: Denormalize.HAS_MANY,
relatedCollection: ManyToOne,
relatedReferenceProperty: 'oneId',
},
},
},
)
ManyToOne.attachDenormalizedSchema(
{
string: {
type: String,
},
// NO ``denormalize``-setting
},
)
// insert works
const manyToOneId1 = ManyToOne.insert({
string: 'many to one 1',
})
expect(() => {
const oneToManyId1 = OneToMany.insert({
string: 'one to many 1'
})
}).to.throw()
const oneToManyId1 = OneToMany.insert({
string: 'one to many 1',
manyIds: [
manyToOneId1,
]
})
const oneToMany1 = OneToMany.findOne(oneToManyId1)
// expect(oneToMany1.manyCache.instances.length).to.equal(1)
// expect(oneToMany1.manyCache.instances[0]._id).to.equal(manyToOneId1)
// expect(oneToMany1.manyCache.instances[0].string).to.equal('many to one 1')
// expect update to work (even when relatedCollection ManyToOne does NOT have a denormalize-setting)
// TODOD: MAKE THIS WORK
ManyToOne.update(manyToOneId1, {$set: { string: 'many to one 1 NEW' } })
const oneToMany2 = OneToMany.findOne(oneToManyId1)
expect(oneToMany1.manyCache.instances.length).to.equal(1)
expect(oneToMany1.manyCache.instances[0]._id).to.equal(manyToOneId1)
expect(oneToMany1.manyCache.instances[0].string).to.equal('many to one 1 NEW')
})
})
}