|
1 | 1 | // These tests check that the Schema operates correctly. |
2 | 2 | var Config = require('../Config'); |
3 | 3 | var Schema = require('../Schema'); |
| 4 | +var dd = require('deep-diff'); |
4 | 5 |
|
5 | 6 | var config = new Config('test'); |
6 | 7 |
|
@@ -142,7 +143,8 @@ describe('Schema', () => { |
142 | 143 | _id: 'NewClass', |
143 | 144 | objectId: 'string', |
144 | 145 | updatedAt: 'string', |
145 | | - createdAt: 'string' |
| 146 | + createdAt: 'string', |
| 147 | + foo: 'string', |
146 | 148 | }) |
147 | 149 | done(); |
148 | 150 | }); |
@@ -183,7 +185,8 @@ describe('Schema', () => { |
183 | 185 | _id: 'NewClass', |
184 | 186 | objectId: 'string', |
185 | 187 | updatedAt: 'string', |
186 | | - createdAt: 'string' |
| 188 | + createdAt: 'string', |
| 189 | + foo: 'string', |
187 | 190 | }); |
188 | 191 | }); |
189 | 192 | Promise.all([p1,p2]) |
@@ -229,4 +232,136 @@ describe('Schema', () => { |
229 | 232 | done(); |
230 | 233 | }); |
231 | 234 | }); |
| 235 | + |
| 236 | + it('refuses to add fields with invalid types', done => { |
| 237 | + config.database.loadSchema() |
| 238 | + .then(schema => schema.addClassIfNotExists('NewClass', { |
| 239 | + foo: {type: 7} |
| 240 | + })) |
| 241 | + .catch(error => { |
| 242 | + expect(error.code).toEqual(Parse.Error.INVALID_JSON); |
| 243 | + expect(error.error).toEqual('invalid JSON'); |
| 244 | + done(); |
| 245 | + }); |
| 246 | + }); |
| 247 | + |
| 248 | + it('refuses to add fields with invalid pointer types', done => { |
| 249 | + config.database.loadSchema() |
| 250 | + .then(schema => schema.addClassIfNotExists('NewClass', { |
| 251 | + foo: {type: 'Pointer'}, |
| 252 | + })) |
| 253 | + .catch(error => { |
| 254 | + expect(error.code).toEqual(135); |
| 255 | + expect(error.error).toEqual('type Pointer needs a class name'); |
| 256 | + done(); |
| 257 | + }); |
| 258 | + }); |
| 259 | + |
| 260 | + it('refuses to add fields with invalid pointer target', done => { |
| 261 | + config.database.loadSchema() |
| 262 | + .then(schema => schema.addClassIfNotExists('NewClass', { |
| 263 | + foo: {type: 'Pointer', targetClass: 7}, |
| 264 | + })) |
| 265 | + .catch(error => { |
| 266 | + expect(error.code).toEqual(Parse.Error.INVALID_JSON); |
| 267 | + expect(error.error).toEqual('invalid JSON'); |
| 268 | + done(); |
| 269 | + }); |
| 270 | + }); |
| 271 | + |
| 272 | + it('refuses to add fields with invalid Relation type', done => { |
| 273 | + config.database.loadSchema() |
| 274 | + .then(schema => schema.addClassIfNotExists('NewClass', { |
| 275 | + foo: {type: 'Relation', uselessKey: 7}, |
| 276 | + })) |
| 277 | + .catch(error => { |
| 278 | + expect(error.code).toEqual(135); |
| 279 | + expect(error.error).toEqual('type Relation needs a class name'); |
| 280 | + done(); |
| 281 | + }); |
| 282 | + }); |
| 283 | + |
| 284 | + it('refuses to add fields with invalid relation target', done => { |
| 285 | + config.database.loadSchema() |
| 286 | + .then(schema => schema.addClassIfNotExists('NewClass', { |
| 287 | + foo: {type: 'Relation', targetClass: 7}, |
| 288 | + })) |
| 289 | + .catch(error => { |
| 290 | + expect(error.code).toEqual(Parse.Error.INVALID_JSON); |
| 291 | + expect(error.error).toEqual('invalid JSON'); |
| 292 | + done(); |
| 293 | + }); |
| 294 | + }); |
| 295 | + |
| 296 | + it('refuses to add fields with uncreatable pointer target class', done => { |
| 297 | + config.database.loadSchema() |
| 298 | + .then(schema => schema.addClassIfNotExists('NewClass', { |
| 299 | + foo: {type: 'Pointer', targetClass: 'not a valid class name'}, |
| 300 | + })) |
| 301 | + .catch(error => { |
| 302 | + expect(error.code).toEqual(Parse.Error.INVALID_CLASS_NAME); |
| 303 | + expect(error.error).toEqual('Invalid classname: not a valid class name, classnames can only have alphanumeric characters and _, and must start with an alpha character '); |
| 304 | + done(); |
| 305 | + }); |
| 306 | + }); |
| 307 | + |
| 308 | + it('refuses to add fields with uncreatable relation target class', done => { |
| 309 | + config.database.loadSchema() |
| 310 | + .then(schema => schema.addClassIfNotExists('NewClass', { |
| 311 | + foo: {type: 'Relation', targetClass: 'not a valid class name'}, |
| 312 | + })) |
| 313 | + .catch(error => { |
| 314 | + expect(error.code).toEqual(Parse.Error.INVALID_CLASS_NAME); |
| 315 | + expect(error.error).toEqual('Invalid classname: not a valid class name, classnames can only have alphanumeric characters and _, and must start with an alpha character '); |
| 316 | + done(); |
| 317 | + }); |
| 318 | + }); |
| 319 | + |
| 320 | + it('will create classes', done => { |
| 321 | + config.database.loadSchema() |
| 322 | + .then(schema => schema.addClassIfNotExists('NewClass', { |
| 323 | + aNumber: {type: 'Number'}, |
| 324 | + aString: {type: 'String'}, |
| 325 | + aBool: {type: 'Boolean'}, |
| 326 | + aDate: {type: 'Date'}, |
| 327 | + aObject: {type: 'Object'}, |
| 328 | + aArray: {type: 'Array'}, |
| 329 | + aGeoPoint: {type: 'GeoPoint'}, |
| 330 | + aFile: {type: 'File'}, |
| 331 | + aPointer: {type: 'Pointer', targetClass: 'ThisClassDoesNotExistYet'}, |
| 332 | + aRelation: {type: 'Relation', targetClass: 'NewClass'}, |
| 333 | + })) |
| 334 | + .then(mongoObj => { |
| 335 | + expect(mongoObj).toEqual({ |
| 336 | + _id: 'NewClass', |
| 337 | + objectId: 'string', |
| 338 | + createdAt: 'string', |
| 339 | + updatedAt: 'string', |
| 340 | + aNumber: 'number', |
| 341 | + aString: 'string', |
| 342 | + aBool: 'boolean', |
| 343 | + aDate: 'date', |
| 344 | + aObject: 'object', |
| 345 | + aArray: 'array', |
| 346 | + aGeoPoint: 'geopoint', |
| 347 | + aFile: 'file', |
| 348 | + aPointer: '*ThisClassDoesNotExistYet', |
| 349 | + aRelation: 'relation<NewClass>', |
| 350 | + }); |
| 351 | + done(); |
| 352 | + }); |
| 353 | + }); |
| 354 | + |
| 355 | + it('refuses to create two geopoints', done => { |
| 356 | + config.database.loadSchema() |
| 357 | + .then(schema => schema.addClassIfNotExists('NewClass', { |
| 358 | + geo1: {type: 'GeoPoint'}, |
| 359 | + geo2: {type: 'GeoPoint'}, |
| 360 | + })) |
| 361 | + .catch(error => { |
| 362 | + expect(error.code).toEqual(Parse.Error.INCORRECT_TYPE); |
| 363 | + expect(error.error).toEqual('currently, only one GeoPoint field may exist in an object. Adding geo2 when geo1 already exists.'); |
| 364 | + done(); |
| 365 | + }); |
| 366 | + }); |
232 | 367 | }); |
0 commit comments