|
| 1 | +const assert = require('assert'); |
| 2 | +const clear = require('./clear'); |
| 3 | +const Parse = require('../../node'); |
| 4 | + |
| 5 | +describe('Schema', () => { |
| 6 | + before(() => { |
| 7 | + Parse.initialize('integration'); |
| 8 | + Parse.CoreManager.set('SERVER_URL', 'http://localhost:1337/parse'); |
| 9 | + Parse.CoreManager.set('MASTER_KEY', 'notsosecret'); |
| 10 | + Parse.Storage._clear(); |
| 11 | + }); |
| 12 | + |
| 13 | + beforeEach((done) => { |
| 14 | + clear().then(() => { |
| 15 | + done(); |
| 16 | + }); |
| 17 | + }); |
| 18 | + |
| 19 | + it('invalid get all no schema', (done) => { |
| 20 | + Parse.Schema.all().then(() => {}).fail((e) => { |
| 21 | + done(); |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + it('invalid get no schema', (done) => { |
| 26 | + const testSchema = new Parse.Schema('SchemaTest'); |
| 27 | + testSchema.get().then(() => {}).fail((e) => { |
| 28 | + done(); |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + it('save', (done) => { |
| 33 | + const testSchema = new Parse.Schema('SchemaTest'); |
| 34 | + testSchema.save().then((result) => { |
| 35 | + assert.equal(result.className, 'SchemaTest'); |
| 36 | + done(); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + it('get', (done) => { |
| 41 | + const testSchema = new Parse.Schema('SchemaTest'); |
| 42 | + testSchema |
| 43 | + .addField('defaultFieldString') |
| 44 | + .addString('stringField') |
| 45 | + .addNumber('numberField') |
| 46 | + .addBoolean('booleanField') |
| 47 | + .addDate('dateField') |
| 48 | + .addFile('fileField') |
| 49 | + .addGeoPoint('geoPointField') |
| 50 | + .addArray('arrayField') |
| 51 | + .addObject('objectField') |
| 52 | + .addPointer('pointerField', '_User') |
| 53 | + .addRelation('relationField', '_User'); |
| 54 | + |
| 55 | + testSchema.save().then(() => { |
| 56 | + return testSchema.get(); |
| 57 | + }).then((result) => { |
| 58 | + assert.equal(result.fields.defaultFieldString.type, 'String'); |
| 59 | + assert.equal(result.fields.stringField.type, 'String'); |
| 60 | + assert.equal(result.fields.numberField.type, 'Number'); |
| 61 | + assert.equal(result.fields.booleanField.type, 'Boolean'); |
| 62 | + assert.equal(result.fields.dateField.type, 'Date'); |
| 63 | + assert.equal(result.fields.fileField.type, 'File'); |
| 64 | + assert.equal(result.fields.geoPointField.type, 'GeoPoint'); |
| 65 | + assert.equal(result.fields.arrayField.type, 'Array'); |
| 66 | + assert.equal(result.fields.objectField.type, 'Object'); |
| 67 | + assert.equal(result.fields.pointerField.type, 'Pointer'); |
| 68 | + assert.equal(result.fields.relationField.type, 'Relation'); |
| 69 | + assert.equal(result.fields.pointerField.targetClass, '_User'); |
| 70 | + assert.equal(result.fields.relationField.targetClass, '_User'); |
| 71 | + done(); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + it('all', (done) => { |
| 76 | + const testSchema = new Parse.Schema('SchemaTest'); |
| 77 | + testSchema.save().then(() => { |
| 78 | + return Parse.Schema.all(); |
| 79 | + }).then((results) => { |
| 80 | + assert.equal(results.length, 1); |
| 81 | + done(); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + it('update', (done) => { |
| 86 | + const testSchema = new Parse.Schema('SchemaTest'); |
| 87 | + testSchema.addString('name'); |
| 88 | + testSchema.save().then(() => { |
| 89 | + testSchema.deleteField('name'); |
| 90 | + testSchema.addNumber('quantity'); |
| 91 | + testSchema.addBoolean('status'); |
| 92 | + return testSchema.update(); |
| 93 | + }).then((result) => { |
| 94 | + assert.equal(result.fields.status.type, 'Boolean'); |
| 95 | + assert.equal(result.fields.quantity.type, 'Number'); |
| 96 | + assert.equal(result.fields.name, undefined); |
| 97 | + done(); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + it('multiple update', (done) => { |
| 102 | + const testSchema = new Parse.Schema('SchemaTest'); |
| 103 | + testSchema.save().then(() => { |
| 104 | + testSchema.addString('name'); |
| 105 | + return testSchema.update(); |
| 106 | + }).then(() => { |
| 107 | + return testSchema.update(); |
| 108 | + }).then(() => { |
| 109 | + return testSchema.get(); |
| 110 | + }).then((result) => { |
| 111 | + assert.equal(Object.keys(result.fields).length, 5); |
| 112 | + done(); |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + it('delete', (done) => { |
| 117 | + const testSchema1 = new Parse.Schema('SchemaTest1'); |
| 118 | + const testSchema2 = new Parse.Schema('SchemaTest2'); |
| 119 | + testSchema1.save().then(() => { |
| 120 | + return testSchema2.save(); |
| 121 | + }).then(() => { |
| 122 | + return Parse.Schema.all(); |
| 123 | + }).then((results) => { |
| 124 | + assert.equal(results.length, 2); |
| 125 | + return testSchema1.delete(); |
| 126 | + }).then(() => { |
| 127 | + return Parse.Schema.all(); |
| 128 | + }).then((results) => { |
| 129 | + assert.equal(results.length, 1); |
| 130 | + assert.equal(results[0].className, 'SchemaTest2'); |
| 131 | + done(); |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + it('save index', (done) => { |
| 136 | + const testSchema = new Parse.Schema('SchemaTest'); |
| 137 | + const index = { |
| 138 | + name: 1 |
| 139 | + }; |
| 140 | + testSchema.addString('name'); |
| 141 | + testSchema.addIndex('test_index', index); |
| 142 | + testSchema.save().then((result) => { |
| 143 | + assert.notEqual(result.indexes.test_index, undefined); |
| 144 | + done(); |
| 145 | + }); |
| 146 | + }); |
| 147 | + |
| 148 | + it('update index', (done) => { |
| 149 | + const testSchema = new Parse.Schema('SchemaTest'); |
| 150 | + testSchema.save().then((result) => { |
| 151 | + const index = { |
| 152 | + name: 1 |
| 153 | + }; |
| 154 | + testSchema.addString('name'); |
| 155 | + testSchema.addIndex('test_index', index); |
| 156 | + return testSchema.update(); |
| 157 | + }).then((result) => { |
| 158 | + assert.notEqual(result.indexes.test_index, undefined); |
| 159 | + done(); |
| 160 | + }); |
| 161 | + }); |
| 162 | + |
| 163 | + it('delete index', (done) => { |
| 164 | + const testSchema = new Parse.Schema('SchemaTest'); |
| 165 | + testSchema.save().then((result) => { |
| 166 | + const index = { |
| 167 | + name: 1 |
| 168 | + }; |
| 169 | + testSchema.addString('name'); |
| 170 | + testSchema.addIndex('test_index', index); |
| 171 | + return testSchema.update(); |
| 172 | + }).then((result) => { |
| 173 | + assert.notEqual(result.indexes.test_index, undefined); |
| 174 | + testSchema.deleteIndex('test_index'); |
| 175 | + return testSchema.update(); |
| 176 | + }).then((result) => { |
| 177 | + assert.equal(result.indexes.test_index, undefined); |
| 178 | + done(); |
| 179 | + }); |
| 180 | + }); |
| 181 | + |
| 182 | + it('invalid field name', (done) => { |
| 183 | + const testSchema = new Parse.Schema('SchemaTest'); |
| 184 | + try { |
| 185 | + testSchema.addField(null); |
| 186 | + } catch (e) { |
| 187 | + done(); |
| 188 | + } |
| 189 | + }); |
| 190 | + |
| 191 | + it('invalid field type', (done) => { |
| 192 | + const testSchema = new Parse.Schema('SchemaTest'); |
| 193 | + try { |
| 194 | + testSchema.addField('name', 'UnknownType'); |
| 195 | + } catch (e) { |
| 196 | + done(); |
| 197 | + } |
| 198 | + }); |
| 199 | + |
| 200 | + it('invalid index name', (done) => { |
| 201 | + const testSchema = new Parse.Schema('SchemaTest'); |
| 202 | + try { |
| 203 | + testSchema.addIndex(null); |
| 204 | + } catch (e) { |
| 205 | + done(); |
| 206 | + } |
| 207 | + }); |
| 208 | + |
| 209 | + it('invalid index', (done) => { |
| 210 | + const testSchema = new Parse.Schema('SchemaTest'); |
| 211 | + try { |
| 212 | + testSchema.addIndex('name', null); |
| 213 | + } catch (e) { |
| 214 | + done(); |
| 215 | + } |
| 216 | + }); |
| 217 | + |
| 218 | + it('invalid pointer name', (done) => { |
| 219 | + const testSchema = new Parse.Schema('SchemaTest'); |
| 220 | + try { |
| 221 | + testSchema.addPointer(null); |
| 222 | + } catch (e) { |
| 223 | + done(); |
| 224 | + } |
| 225 | + }); |
| 226 | + |
| 227 | + it('invalid pointer class', (done) => { |
| 228 | + const testSchema = new Parse.Schema('SchemaTest'); |
| 229 | + try { |
| 230 | + testSchema.addPointer('name', null); |
| 231 | + } catch (e) { |
| 232 | + done(); |
| 233 | + } |
| 234 | + }); |
| 235 | + |
| 236 | + it('invalid relation name', (done) => { |
| 237 | + const testSchema = new Parse.Schema('SchemaTest'); |
| 238 | + try { |
| 239 | + testSchema.addRelation(null); |
| 240 | + } catch (e) { |
| 241 | + done(); |
| 242 | + } |
| 243 | + }); |
| 244 | + |
| 245 | + it('invalid relation class', (done) => { |
| 246 | + const testSchema = new Parse.Schema('SchemaTest'); |
| 247 | + try { |
| 248 | + testSchema.addRelation('name', null); |
| 249 | + } catch (e) { |
| 250 | + done(); |
| 251 | + } |
| 252 | + }); |
| 253 | + |
| 254 | + it('assert class name', (done) => { |
| 255 | + const testSchema = new Parse.Schema(); |
| 256 | + try { |
| 257 | + testSchema.assertClassName(); |
| 258 | + } catch (e) { |
| 259 | + done(); |
| 260 | + } |
| 261 | + }); |
| 262 | +}); |
0 commit comments