-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathcreate.js
190 lines (141 loc) · 4.33 KB
/
create.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
'use strict'
const run = require('tapdance')
const testInstance = require('../test_instance')
const stderr = require('../../stderr')
const find = require('../../../lib/common/array/find')
const deepEqual = require('../../../lib/common/deep_equal')
const constants = require('../../../lib/common/constants')
const changeEvent = constants.change
const createMethod = constants.create
const updateMethod = constants.update
const primaryKey = constants.primary
const errors = require('../../../lib/common/errors')
const ConflictError = errors.ConflictError
const buffer = Buffer.from ||
((input, encoding) => new Buffer(input, encoding))
const deadcode = buffer('deadc0de', 'hex')
const records = [
{
[primaryKey]: 4,
name: 'Slimer McGee',
birthday: new Date(2011, 5, 30),
friends: [ 1, 3 ],
picture: deadcode
}
]
run((assert, comment) => {
comment('create record')
let store
return testInstance()
.then(instance => {
store = instance
store.on(changeEvent, data => {
assert(deepEqual(data[createMethod].user
.map(x => x.id).sort((a, b) => a - b),
[ 4 ]), 'change event shows created IDs')
assert(deepEqual(data[updateMethod].user
.map(x => x.id).sort((a, b) => a - b),
[ 1, 3 ]), 'change event shows updated IDs')
})
return store.create('user', records)
})
.then(response => {
const results = response.payload.records
assert(deadcode.equals(results[0].picture) &&
deadcode.equals(records[0].picture),
'input object not mutated')
assert(results[0].createdAt !== null, 'input hook applied')
assert(results.length === 1, 'record created')
assert(results[0][primaryKey] === 4, 'record has correct ID')
assert(results[0].birthday instanceof Date,
'field has correct type')
assert(results[0].name === 'Slimer McGee',
'record has correct field value')
return store.find('user', [ 1, 3 ])
})
.then(response => {
assert(deepEqual(response.payload.records.map(record =>
find(record.friends, id => id === 4)),
[ 4, 4 ]), 'related records updated')
return store.disconnect()
})
.catch(error => {
stderr.error(error)
store.disconnect()
throw error
})
})
run((assert, comment) => {
comment('create records with same to-one relationship should fail')
let store
return testInstance()
.then(instance => {
store = instance
return store.create('user', [ { spouse: 2 }, { spouse: 2 } ])
})
.then(() => {
assert(false, 'should have failed')
})
.catch(error => {
assert(true, 'should reject request')
assert(error instanceof ConflictError, 'error type is correct')
})
})
run((assert, comment) => {
comment('create records with non-unique array relationship should fail')
let store
return testInstance()
.then(instance => {
store = instance
return store.create('user', { friends: [ 2, 2 ] })
})
.then(() => {
assert(false, 'should have failed')
})
.catch(error => {
assert(true, 'should reject request')
assert(error instanceof ConflictError, 'error type is correct')
})
})
run((assert, comment) => {
comment('create record with one-to-one relationship and 2nd degree unset')
let store
let didChange
return testInstance()
.then(instance => {
store = instance
store.on(changeEvent, data => {
didChange = true
assert(data.update.animal.find(record =>
record.id === 1 && record.replace.likedBy),
'should update related record')
assert(data.update.user.find(record =>
record.id === 2 && record.replace.likedAnimal === null),
'should update 2nd degree related record')
})
return store.create('user', [ { likedAnimal: 1 } ])
})
.then(() => {
assert(didChange, 'change event emitted')
})
})
run((assert, comment) => {
comment('create record with many-to-one relationship and 2nd degree unset')
let store
let didChange
return testInstance()
.then(instance => {
store = instance
store.on(changeEvent, data => {
didChange = true
assert(deepEqual(data[updateMethod].user
.map(x => x.id).sort((a, b) => a - b),
[ 1, 2 ]),
'should update 2nd degree related records')
})
return store.create('user', [ { ownedPets: [ 1, 2, 3 ] } ])
})
.then(() => {
assert(didChange, 'change event emitted')
})
})