Skip to content

Commit 1a75101

Browse files
drew-grosspeterdotjs
authored andcommitted
Fix flaky test (#2078)
* Debug flaky test * create new object instead of modifying and assigning existing object * use getOneSchema instead of this.data when updating fields * Remove debug stuff * Don't try to validate existing fields * run just one test * Verbose test all * Use schema instead of this.data * Switch to all tests
1 parent 627b416 commit 1a75101

File tree

1 file changed

+37
-31
lines changed

1 file changed

+37
-31
lines changed

src/Controllers/SchemaController.js

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -341,12 +341,9 @@ class SchemaController {
341341
}
342342

343343
updateClass(className, submittedFields, classLevelPermissions, database) {
344-
return this.hasClass(className)
345-
.then(hasClass => {
346-
if (!hasClass) {
347-
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, `Class ${className} does not exist.`);
348-
}
349-
let existingFields = Object.assign(this.data[className], {_id: className});
344+
return this.getOneSchema(className)
345+
.then(schema => {
346+
let existingFields = schema.fields;
350347
Object.keys(submittedFields).forEach(name => {
351348
let field = submittedFields[name];
352349
if (existingFields[name] && field.__op !== 'Delete') {
@@ -360,7 +357,7 @@ class SchemaController {
360357
delete existingFields._rperm;
361358
delete existingFields._wperm;
362359
let newSchema = buildMergedSchemaObject(existingFields, submittedFields);
363-
let validationError = this.validateSchemaData(className, newSchema, classLevelPermissions);
360+
let validationError = this.validateSchemaData(className, newSchema, classLevelPermissions, Object.keys(existingFields));
364361
if (validationError) {
365362
throw new Parse.Error(validationError.code, validationError.error);
366363
}
@@ -395,6 +392,13 @@ class SchemaController {
395392
classLevelPermissions: this.perms[className]
396393
}));
397394
})
395+
.catch(error => {
396+
if (error === undefined) {
397+
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, `Class ${className} does not exist.`);
398+
} else {
399+
throw error;
400+
}
401+
})
398402
}
399403

400404
// Returns a promise that resolves successfully to the new schema
@@ -436,25 +440,27 @@ class SchemaController {
436440
error: invalidClassNameMessage(className),
437441
};
438442
}
439-
return this.validateSchemaData(className, fields, classLevelPermissions);
443+
return this.validateSchemaData(className, fields, classLevelPermissions, []);
440444
}
441445

442-
validateSchemaData(className, fields, classLevelPermissions) {
446+
validateSchemaData(className, fields, classLevelPermissions, existingFieldNames) {
443447
for (let fieldName in fields) {
444-
if (!fieldNameIsValid(fieldName)) {
445-
return {
446-
code: Parse.Error.INVALID_KEY_NAME,
447-
error: 'invalid field name: ' + fieldName,
448-
};
449-
}
450-
if (!fieldNameIsValidForClass(fieldName, className)) {
451-
return {
452-
code: 136,
453-
error: 'field ' + fieldName + ' cannot be added',
454-
};
448+
if (!existingFieldNames.includes(fieldName)) {
449+
if (!fieldNameIsValid(fieldName)) {
450+
return {
451+
code: Parse.Error.INVALID_KEY_NAME,
452+
error: 'invalid field name: ' + fieldName,
453+
};
454+
}
455+
if (!fieldNameIsValidForClass(fieldName, className)) {
456+
return {
457+
code: 136,
458+
error: 'field ' + fieldName + ' cannot be added',
459+
};
460+
}
461+
const error = fieldTypeIsInvalid(fields[fieldName]);
462+
if (error) return { code: error.code, error: error.message };
455463
}
456-
const error = fieldTypeIsInvalid(fields[fieldName]);
457-
if (error) return { code: error.code, error: error.message };
458464
}
459465

460466
for (let fieldName in defaultColumns[className]) {
@@ -552,19 +558,19 @@ class SchemaController {
552558
throw new Parse.Error(136, `field ${fieldName} cannot be changed`);
553559
}
554560

555-
return this.reloadData()
556-
.then(() => this.hasClass(className))
557-
.then(hasClass => {
558-
if (!hasClass) {
561+
return this.getOneSchema(className)
562+
.catch(error => {
563+
if (error === undefined) {
559564
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, `Class ${className} does not exist.`);
560-
}
561-
if (!this.data[className][fieldName]) {
562-
throw new Parse.Error(255, `Field ${fieldName} does not exist, cannot delete.`);
565+
} else {
566+
throw error;
563567
}
564568
})
565-
.then(() => this.getOneSchema(className))
566569
.then(schema => {
567-
if (this.data[className][fieldName].type == 'Relation') {
570+
if (!schema.fields[fieldName]) {
571+
throw new Parse.Error(255, `Field ${fieldName} does not exist, cannot delete.`);
572+
}
573+
if (schema.fields[fieldName].type == 'Relation') {
568574
//For relations, drop the _Join table
569575
return database.adapter.deleteFields(className, schema, [fieldName])
570576
.then(() => database.adapter.deleteClass(`_Join:${fieldName}:${className}`));

0 commit comments

Comments
 (0)