-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
Uniqueness.spec.js
128 lines (123 loc) · 3.57 KB
/
Uniqueness.spec.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
'use strict';
const Parse = require('parse/node');
const Config = require('../lib/Config');
describe('Uniqueness', function() {
it('fail when create duplicate value in unique field', done => {
const obj = new Parse.Object('UniqueField');
obj.set('unique', 'value');
obj
.save()
.then(() => {
expect(obj.id).not.toBeUndefined();
const config = Config.get('test');
return config.database.adapter.ensureUniqueness(
'UniqueField',
{ fields: { unique: { __type: 'String' } } },
['unique']
);
})
.then(() => {
const obj = new Parse.Object('UniqueField');
obj.set('unique', 'value');
return obj.save();
})
.then(
() => {
fail('Saving duplicate field should have failed');
done();
},
error => {
expect(error.code).toEqual(Parse.Error.DUPLICATE_VALUE);
done();
}
);
});
it('unique indexing works on pointer fields', done => {
const obj = new Parse.Object('UniquePointer');
obj
.save({ string: 'who cares' })
.then(() => obj.save({ ptr: obj }))
.then(() => {
const config = Config.get('test');
return config.database.adapter.ensureUniqueness(
'UniquePointer',
{
fields: {
string: { __type: 'String' },
ptr: { __type: 'Pointer', targetClass: 'UniquePointer' },
},
},
['ptr']
);
})
.then(() => {
const newObj = new Parse.Object('UniquePointer');
newObj.set('ptr', obj);
return newObj.save();
})
.then(() => {
fail('save should have failed due to duplicate value');
done();
})
.catch(error => {
expect(error.code).toEqual(Parse.Error.DUPLICATE_VALUE);
done();
});
});
it('fails when attempting to ensure uniqueness of fields that are not currently unique', done => {
const o1 = new Parse.Object('UniqueFail');
o1.set('key', 'val');
const o2 = new Parse.Object('UniqueFail');
o2.set('key', 'val');
Parse.Object.saveAll([o1, o2])
.then(() => {
const config = Config.get('test');
return config.database.adapter.ensureUniqueness(
'UniqueFail',
{ fields: { key: { __type: 'String' } } },
['key']
);
})
.catch(error => {
expect(error.code).toEqual(Parse.Error.DUPLICATE_VALUE);
done();
});
});
it_exclude_dbs(['postgres'])('can do compound uniqueness', done => {
const config = Config.get('test');
config.database.adapter
.ensureUniqueness(
'CompoundUnique',
{ fields: { k1: { __type: 'String' }, k2: { __type: 'String' } } },
['k1', 'k2']
)
.then(() => {
const o1 = new Parse.Object('CompoundUnique');
o1.set('k1', 'v1');
o1.set('k2', 'v2');
return o1.save();
})
.then(() => {
const o2 = new Parse.Object('CompoundUnique');
o2.set('k1', 'v1');
o2.set('k2', 'not a dupe');
return o2.save();
})
.then(() => {
const o3 = new Parse.Object('CompoundUnique');
o3.set('k1', 'not a dupe');
o3.set('k2', 'v2');
return o3.save();
})
.then(() => {
const o4 = new Parse.Object('CompoundUnique');
o4.set('k1', 'v1');
o4.set('k2', 'v2');
return o4.save();
})
.catch(error => {
expect(error.code).toEqual(Parse.Error.DUPLICATE_VALUE);
done();
});
});
});