|
1 | 1 | const { DefinedSchemas } = require('../lib/DefinedSchemas'); |
2 | 2 | const Config = require('../lib/Config'); |
3 | 3 |
|
4 | | -fdescribe('DefinedSchemas', () => { |
| 4 | +const cleanUpIndexes = schema => { |
| 5 | + if (schema.indexes) { |
| 6 | + delete schema.indexes._id_; |
| 7 | + if (!Object.keys(schema.indexes).length) { |
| 8 | + delete schema.indexes; |
| 9 | + } |
| 10 | + } |
| 11 | +}; |
| 12 | + |
| 13 | +describe('DefinedSchemas', () => { |
| 14 | + let config; |
5 | 15 | beforeEach(async () => { |
6 | | - const config = Config.get('test'); |
| 16 | + config = Config.get('test'); |
| 17 | + await config.database.adapter.deleteAllClasses(); |
| 18 | + }); |
| 19 | + afterAll(async () => { |
7 | 20 | await config.database.adapter.deleteAllClasses(); |
8 | 21 | }); |
9 | 22 |
|
@@ -253,14 +266,102 @@ fdescribe('DefinedSchemas', () => { |
253 | 266 | }); |
254 | 267 |
|
255 | 268 | describe('Indexes', () => { |
256 | | - xit('should create new indexes'); |
257 | | - xit('should re create changed indexes'); |
258 | | - xit('should delete removed indexes'); |
259 | | - describe('User', () => { |
260 | | - xit('should protect default indexes'); |
| 269 | + it('should create new indexes', async () => { |
| 270 | + const server = await reconfigureServer(); |
| 271 | + |
| 272 | + const indexes = { complex: { createdAt: 1, updatedAt: 1 } }; |
| 273 | + |
| 274 | + const schemas = [{ className: 'Test', indexes }]; |
| 275 | + await new DefinedSchemas(schemas, server.config).execute(); |
| 276 | + |
| 277 | + let schema = await new Parse.Schema('Test').get(); |
| 278 | + cleanUpIndexes(schema); |
| 279 | + expect(schema.indexes).toEqual(indexes); |
| 280 | + |
| 281 | + await new DefinedSchemas(schemas, server.config).execute(); |
| 282 | + schema = await new Parse.Schema('Test').get(); |
| 283 | + cleanUpIndexes(schema); |
| 284 | + expect(schema.indexes).toEqual(indexes); |
| 285 | + }); |
| 286 | + it('should re create changed indexes', async () => { |
| 287 | + const server = await reconfigureServer(); |
| 288 | + |
| 289 | + let indexes = { complex: { createdAt: 1, updatedAt: 1 } }; |
| 290 | + |
| 291 | + let schemas = [{ className: 'Test', indexes }]; |
| 292 | + await new DefinedSchemas(schemas, server.config).execute(); |
| 293 | + |
| 294 | + indexes = { complex: { createdAt: 1 } }; |
| 295 | + schemas = [{ className: 'Test', indexes }]; |
| 296 | + |
| 297 | + // Change indexes |
| 298 | + await new DefinedSchemas(schemas, server.config).execute(); |
| 299 | + let schema = await new Parse.Schema('Test').get(); |
| 300 | + cleanUpIndexes(schema); |
| 301 | + expect(schema.indexes).toEqual(indexes); |
| 302 | + |
| 303 | + // Update |
| 304 | + await new DefinedSchemas(schemas, server.config).execute(); |
| 305 | + schema = await new Parse.Schema('Test').get(); |
| 306 | + cleanUpIndexes(schema); |
| 307 | + expect(schema.indexes).toEqual(indexes); |
261 | 308 | }); |
262 | | - describe('Role', () => { |
263 | | - xit('should protect default indexes'); |
| 309 | + it('should delete removed indexes', async () => { |
| 310 | + const server = await reconfigureServer(); |
| 311 | + |
| 312 | + let indexes = { complex: { createdAt: 1, updatedAt: 1 } }; |
| 313 | + |
| 314 | + let schemas = [{ className: 'Test', indexes }]; |
| 315 | + await new DefinedSchemas(schemas, server.config).execute(); |
| 316 | + |
| 317 | + indexes = {}; |
| 318 | + schemas = [{ className: 'Test', indexes }]; |
| 319 | + // Change indexes |
| 320 | + await new DefinedSchemas(schemas, server.config).execute(); |
| 321 | + let schema = await new Parse.Schema('Test').get(); |
| 322 | + cleanUpIndexes(schema); |
| 323 | + expect(schema.indexes).toBeUndefined(); |
| 324 | + |
| 325 | + // Update |
| 326 | + await new DefinedSchemas(schemas, server.config).execute(); |
| 327 | + schema = await new Parse.Schema('Test').get(); |
| 328 | + cleanUpIndexes(schema); |
| 329 | + expect(schema.indexes).toBeUndefined(); |
| 330 | + }); |
| 331 | + it('should keep protected indexes', async () => { |
| 332 | + const server = await reconfigureServer(); |
| 333 | + |
| 334 | + const schemas = [ |
| 335 | + { |
| 336 | + className: '_User', |
| 337 | + indexes: { |
| 338 | + case_insensitive_username: { password: true }, |
| 339 | + case_insensitive_email: { password: true }, |
| 340 | + }, |
| 341 | + }, |
| 342 | + { className: 'Test' }, |
| 343 | + ]; |
| 344 | + // Create |
| 345 | + await new DefinedSchemas(schemas, server.config).execute(); |
| 346 | + let userSchema = await new Parse.Schema('_User').get(); |
| 347 | + let testSchema = await new Parse.Schema('Test').get(); |
| 348 | + cleanUpIndexes(userSchema); |
| 349 | + cleanUpIndexes(testSchema); |
| 350 | + // If indexes are undefined it means that their |
| 351 | + // were not touched |
| 352 | + expect(testSchema.indexes).toBeUndefined(); |
| 353 | + expect(userSchema.indexes).toBeUndefined(); |
| 354 | + |
| 355 | + // Update |
| 356 | + await new DefinedSchemas(schemas, server.config).execute(); |
| 357 | + userSchema = await new Parse.Schema('_User').get(); |
| 358 | + testSchema = await new Parse.Schema('Test').get(); |
| 359 | + cleanUpIndexes(userSchema); |
| 360 | + cleanUpIndexes(testSchema); |
| 361 | + // If indexes are undefined it means that their |
| 362 | + // were not touched |
| 363 | + expect(testSchema.indexes).toBeUndefined(); |
| 364 | + expect(userSchema.indexes).toBeUndefined(); |
264 | 365 | }); |
265 | 366 | }); |
266 | 367 |
|
|
0 commit comments