Skip to content

Commit b741539

Browse files
committed
More tests
1 parent e6feefd commit b741539

File tree

2 files changed

+58
-14
lines changed

2 files changed

+58
-14
lines changed

Schema.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,6 @@ function fieldNameIsValidForClass(fieldName, className) {
100100
}
101101

102102
function invalidClassNameMessage(className) {
103-
if (!className) {
104-
className = '';
105-
}
106103
return 'Invalid classname: ' + className + ', classnames can only have alphanumeric characters and _, and must start with an alpha character ';
107104
}
108105

@@ -137,7 +134,7 @@ function schemaAPITypeToMongoFieldType(type) {
137134
return { error: "invalid JSON", code: Parse.Error.INVALID_JSON };
138135
}
139136
switch (type.type) {
140-
default: return { error: 'invalid field type: ' + type.type };
137+
default: return { error: 'invalid field type: ' + type.type, code: Parse.Error.INCORRECT_TYPE };
141138
case 'Number': return { result: 'number' };
142139
case 'String': return { result: 'string' };
143140
case 'Boolean': return { result: 'boolean' };
@@ -211,20 +208,17 @@ Schema.prototype.reload = function() {
211208
// enabled) before calling this function.
212209
Schema.prototype.addClassIfNotExists = function(className, fields) {
213210
if (this.data[className]) {
214-
return Promise.reject(new Parse.Error(
215-
Parse.Error.DUPLICATE_VALUE,
216-
'class ' + className + ' already exists'
217-
));
211+
return Promise.reject({
212+
code: Parse.Error.INVALID_CLASS_NAME,
213+
error: 'class ' + className + ' already exists',
214+
});
218215
}
219216

220217
if (!classNameIsValid(className)) {
221218
return Promise.reject({
222219
code: Parse.Error.INVALID_CLASS_NAME,
223220
error: invalidClassNameMessage(className),
224221
});
225-
return Promise.reject({
226-
code: Parse.Error.INVALID_CLASS_NAME,
227-
});
228222
}
229223
for (fieldName in fields) {
230224
if (!fieldNameIsValid(fieldName)) {

spec/Schema.spec.js

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,11 @@ describe('Schema', () => {
155155
.then(schema => {
156156
schema.validateObject('NewClass', {foo: 7})
157157
.then(() => {
158-
schema.addClassIfNotExists('NewClass', {
158+
schema.reload()
159+
.then(schema => schema.addClassIfNotExists('NewClass', {
159160
foo: {type: 'String'}
160-
}).catch(error => {
161+
}))
162+
.catch(error => {
161163
expect(error.code).toEqual(Parse.Error.INVALID_CLASS_NAME)
162164
expect(error.error).toEqual('class NewClass already exists');
163165
done();
@@ -223,7 +225,17 @@ describe('Schema', () => {
223225
});
224226
});
225227

226-
it('refuses to explicitly create the default fields', done => {
228+
it('refuses to explicitly create the default fields for custom classes', done => {
229+
config.database.loadSchema()
230+
.then(schema => schema.addClassIfNotExists('NewClass', {objectId: {type: 'String'}}))
231+
.catch(error => {
232+
expect(error.code).toEqual(136);
233+
expect(error.error).toEqual('field objectId cannot be added');
234+
done();
235+
});
236+
});
237+
238+
it('refuses to explicitly create the default fields for non-custom classes', done => {
227239
config.database.loadSchema()
228240
.then(schema => schema.addClassIfNotExists('_Installation', {localeIdentifier: {type: 'String'}}))
229241
.catch(error => {
@@ -317,6 +329,18 @@ describe('Schema', () => {
317329
});
318330
});
319331

332+
it('refuses to add fields with unknown types', done => {
333+
config.database.loadSchema()
334+
.then(schema => schema.addClassIfNotExists('NewClass', {
335+
foo: {type: 'Unknown'},
336+
}))
337+
.catch(error => {
338+
expect(error.code).toEqual(Parse.Error.INCORRECT_TYPE);
339+
expect(error.error).toEqual('invalid field type: Unknown');
340+
done();
341+
});
342+
});
343+
320344
it('will create classes', done => {
321345
config.database.loadSchema()
322346
.then(schema => schema.addClassIfNotExists('NewClass', {
@@ -352,6 +376,32 @@ describe('Schema', () => {
352376
});
353377
});
354378

379+
it('creates the default fields for non-custom classes', done => {
380+
config.database.loadSchema()
381+
.then(schema => schema.addClassIfNotExists('_Installation', {
382+
foo: {type: 'Number'},
383+
}))
384+
.then(mongoObj => {
385+
expect(mongoObj).toEqual({
386+
_id: '_Installation',
387+
createdAt: 'string',
388+
updatedAt: 'string',
389+
objectId: 'string',
390+
foo: 'number',
391+
installationId: 'string',
392+
deviceToken: 'string',
393+
channels: 'array',
394+
deviceType: 'string',
395+
pushType: 'string',
396+
GCMSenderId: 'string',
397+
timeZone: 'string',
398+
localeIdentifier: 'string',
399+
badge: 'number',
400+
});
401+
done();
402+
});
403+
});
404+
355405
it('refuses to create two geopoints', done => {
356406
config.database.loadSchema()
357407
.then(schema => schema.addClassIfNotExists('NewClass', {

0 commit comments

Comments
 (0)