More tests
This commit is contained in:
16
Schema.js
16
Schema.js
@@ -100,9 +100,6 @@ function fieldNameIsValidForClass(fieldName, className) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function invalidClassNameMessage(className) {
|
function invalidClassNameMessage(className) {
|
||||||
if (!className) {
|
|
||||||
className = '';
|
|
||||||
}
|
|
||||||
return 'Invalid classname: ' + className + ', classnames can only have alphanumeric characters and _, and must start with an alpha character ';
|
return 'Invalid classname: ' + className + ', classnames can only have alphanumeric characters and _, and must start with an alpha character ';
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -137,7 +134,7 @@ function schemaAPITypeToMongoFieldType(type) {
|
|||||||
return { error: "invalid JSON", code: Parse.Error.INVALID_JSON };
|
return { error: "invalid JSON", code: Parse.Error.INVALID_JSON };
|
||||||
}
|
}
|
||||||
switch (type.type) {
|
switch (type.type) {
|
||||||
default: return { error: 'invalid field type: ' + type.type };
|
default: return { error: 'invalid field type: ' + type.type, code: Parse.Error.INCORRECT_TYPE };
|
||||||
case 'Number': return { result: 'number' };
|
case 'Number': return { result: 'number' };
|
||||||
case 'String': return { result: 'string' };
|
case 'String': return { result: 'string' };
|
||||||
case 'Boolean': return { result: 'boolean' };
|
case 'Boolean': return { result: 'boolean' };
|
||||||
@@ -211,10 +208,10 @@ Schema.prototype.reload = function() {
|
|||||||
// enabled) before calling this function.
|
// enabled) before calling this function.
|
||||||
Schema.prototype.addClassIfNotExists = function(className, fields) {
|
Schema.prototype.addClassIfNotExists = function(className, fields) {
|
||||||
if (this.data[className]) {
|
if (this.data[className]) {
|
||||||
return Promise.reject(new Parse.Error(
|
return Promise.reject({
|
||||||
Parse.Error.DUPLICATE_VALUE,
|
code: Parse.Error.INVALID_CLASS_NAME,
|
||||||
'class ' + className + ' already exists'
|
error: 'class ' + className + ' already exists',
|
||||||
));
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!classNameIsValid(className)) {
|
if (!classNameIsValid(className)) {
|
||||||
@@ -222,9 +219,6 @@ Schema.prototype.addClassIfNotExists = function(className, fields) {
|
|||||||
code: Parse.Error.INVALID_CLASS_NAME,
|
code: Parse.Error.INVALID_CLASS_NAME,
|
||||||
error: invalidClassNameMessage(className),
|
error: invalidClassNameMessage(className),
|
||||||
});
|
});
|
||||||
return Promise.reject({
|
|
||||||
code: Parse.Error.INVALID_CLASS_NAME,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
for (fieldName in fields) {
|
for (fieldName in fields) {
|
||||||
if (!fieldNameIsValid(fieldName)) {
|
if (!fieldNameIsValid(fieldName)) {
|
||||||
|
|||||||
@@ -155,9 +155,11 @@ describe('Schema', () => {
|
|||||||
.then(schema => {
|
.then(schema => {
|
||||||
schema.validateObject('NewClass', {foo: 7})
|
schema.validateObject('NewClass', {foo: 7})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
schema.addClassIfNotExists('NewClass', {
|
schema.reload()
|
||||||
|
.then(schema => schema.addClassIfNotExists('NewClass', {
|
||||||
foo: {type: 'String'}
|
foo: {type: 'String'}
|
||||||
}).catch(error => {
|
}))
|
||||||
|
.catch(error => {
|
||||||
expect(error.code).toEqual(Parse.Error.INVALID_CLASS_NAME)
|
expect(error.code).toEqual(Parse.Error.INVALID_CLASS_NAME)
|
||||||
expect(error.error).toEqual('class NewClass already exists');
|
expect(error.error).toEqual('class NewClass already exists');
|
||||||
done();
|
done();
|
||||||
@@ -223,7 +225,17 @@ describe('Schema', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('refuses to explicitly create the default fields', done => {
|
it('refuses to explicitly create the default fields for custom classes', done => {
|
||||||
|
config.database.loadSchema()
|
||||||
|
.then(schema => schema.addClassIfNotExists('NewClass', {objectId: {type: 'String'}}))
|
||||||
|
.catch(error => {
|
||||||
|
expect(error.code).toEqual(136);
|
||||||
|
expect(error.error).toEqual('field objectId cannot be added');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('refuses to explicitly create the default fields for non-custom classes', done => {
|
||||||
config.database.loadSchema()
|
config.database.loadSchema()
|
||||||
.then(schema => schema.addClassIfNotExists('_Installation', {localeIdentifier: {type: 'String'}}))
|
.then(schema => schema.addClassIfNotExists('_Installation', {localeIdentifier: {type: 'String'}}))
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
@@ -317,6 +329,18 @@ describe('Schema', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('refuses to add fields with unknown types', done => {
|
||||||
|
config.database.loadSchema()
|
||||||
|
.then(schema => schema.addClassIfNotExists('NewClass', {
|
||||||
|
foo: {type: 'Unknown'},
|
||||||
|
}))
|
||||||
|
.catch(error => {
|
||||||
|
expect(error.code).toEqual(Parse.Error.INCORRECT_TYPE);
|
||||||
|
expect(error.error).toEqual('invalid field type: Unknown');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('will create classes', done => {
|
it('will create classes', done => {
|
||||||
config.database.loadSchema()
|
config.database.loadSchema()
|
||||||
.then(schema => schema.addClassIfNotExists('NewClass', {
|
.then(schema => schema.addClassIfNotExists('NewClass', {
|
||||||
@@ -352,6 +376,32 @@ describe('Schema', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('creates the default fields for non-custom classes', done => {
|
||||||
|
config.database.loadSchema()
|
||||||
|
.then(schema => schema.addClassIfNotExists('_Installation', {
|
||||||
|
foo: {type: 'Number'},
|
||||||
|
}))
|
||||||
|
.then(mongoObj => {
|
||||||
|
expect(mongoObj).toEqual({
|
||||||
|
_id: '_Installation',
|
||||||
|
createdAt: 'string',
|
||||||
|
updatedAt: 'string',
|
||||||
|
objectId: 'string',
|
||||||
|
foo: 'number',
|
||||||
|
installationId: 'string',
|
||||||
|
deviceToken: 'string',
|
||||||
|
channels: 'array',
|
||||||
|
deviceType: 'string',
|
||||||
|
pushType: 'string',
|
||||||
|
GCMSenderId: 'string',
|
||||||
|
timeZone: 'string',
|
||||||
|
localeIdentifier: 'string',
|
||||||
|
badge: 'number',
|
||||||
|
});
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('refuses to create two geopoints', done => {
|
it('refuses to create two geopoints', done => {
|
||||||
config.database.loadSchema()
|
config.database.loadSchema()
|
||||||
.then(schema => schema.addClassIfNotExists('NewClass', {
|
.then(schema => schema.addClassIfNotExists('NewClass', {
|
||||||
|
|||||||
Reference in New Issue
Block a user