|
1 | 1 | 'use strict';
|
2 | 2 |
|
| 3 | +const Parse = require('parse/node'); |
3 | 4 | const MongoSchemaCollection = require('../lib/Adapters/Storage/Mongo/MongoSchemaCollection')
|
4 | 5 | .default;
|
5 | 6 |
|
@@ -96,4 +97,95 @@ describe('MongoSchemaCollection', () => {
|
96 | 97 | });
|
97 | 98 | done();
|
98 | 99 | });
|
| 100 | + |
| 101 | + describe('mongoFieldToParseSchemaField function', () => { |
| 102 | + // Test successful type conversions |
| 103 | + it('should convert valid mongo field types to parse schema fields', () => { |
| 104 | + const testCases = [ |
| 105 | + { input: 'string', expected: { type: 'String' } }, |
| 106 | + { input: 'number', expected: { type: 'Number' } }, |
| 107 | + { input: 'boolean', expected: { type: 'Boolean' } }, |
| 108 | + { input: 'date', expected: { type: 'Date' } }, |
| 109 | + { input: 'map', expected: { type: 'Object' } }, |
| 110 | + { input: 'object', expected: { type: 'Object' } }, |
| 111 | + { input: 'array', expected: { type: 'Array' } }, |
| 112 | + { input: 'geopoint', expected: { type: 'GeoPoint' } }, |
| 113 | + { input: 'file', expected: { type: 'File' } }, |
| 114 | + { input: 'bytes', expected: { type: 'Bytes' } }, |
| 115 | + { input: 'polygon', expected: { type: 'Polygon' } }, |
| 116 | + { input: '*_User', expected: { type: 'Pointer', targetClass: '_User' } }, |
| 117 | + { input: '*Post', expected: { type: 'Pointer', targetClass: 'Post' } }, |
| 118 | + { input: 'relation<_User>', expected: { type: 'Relation', targetClass: '_User' } }, |
| 119 | + { input: 'relation<Post>', expected: { type: 'Relation', targetClass: 'Post' } }, |
| 120 | + ]; |
| 121 | + |
| 122 | + testCases.forEach(({ input, expected }) => { |
| 123 | + const result = MongoSchemaCollection._TESTmongoSchemaToParseSchema({ |
| 124 | + _id: 'TestClass', |
| 125 | + testField: input, |
| 126 | + }); |
| 127 | + |
| 128 | + expect(result.fields.testField).toEqual(expected); |
| 129 | + }); |
| 130 | + }); |
| 131 | + |
| 132 | + // Test error handling for invalid types (non-string values) |
| 133 | + it('should throw Parse.Error for invalid field types', () => { |
| 134 | + const invalidInputs = [ |
| 135 | + null, |
| 136 | + undefined, |
| 137 | + 123, |
| 138 | + true, |
| 139 | + false, |
| 140 | + {}, |
| 141 | + [], |
| 142 | + '', |
| 143 | + ]; |
| 144 | + |
| 145 | + invalidInputs.forEach(invalidInput => { |
| 146 | + expect(() => { |
| 147 | + MongoSchemaCollection._TESTmongoSchemaToParseSchema({ |
| 148 | + _id: 'TestClass', |
| 149 | + testField: invalidInput, |
| 150 | + }); |
| 151 | + }).toThrow(Parse.Error); |
| 152 | + }); |
| 153 | + }); |
| 154 | + |
| 155 | + it('should throw Parse.Error with correct error code and message for null input', () => { |
| 156 | + expect(() => { |
| 157 | + MongoSchemaCollection._TESTmongoSchemaToParseSchema({ |
| 158 | + _id: 'TestClass', |
| 159 | + testField: null, |
| 160 | + }); |
| 161 | + }).toThrow(Parse.Error); |
| 162 | + }); |
| 163 | + |
| 164 | + it('should throw Parse.Error with correct error code and message for undefined input', () => { |
| 165 | + expect(() => { |
| 166 | + MongoSchemaCollection._TESTmongoSchemaToParseSchema({ |
| 167 | + _id: 'TestClass', |
| 168 | + testField: undefined, |
| 169 | + }); |
| 170 | + }).toThrow(Parse.Error); |
| 171 | + }); |
| 172 | + |
| 173 | + it('should throw Parse.Error with correct error code and message for non-string input', () => { |
| 174 | + expect(() => { |
| 175 | + MongoSchemaCollection._TESTmongoSchemaToParseSchema({ |
| 176 | + _id: 'TestClass', |
| 177 | + testField: 123, |
| 178 | + }); |
| 179 | + }).toThrow(Parse.Error); |
| 180 | + }); |
| 181 | + |
| 182 | + it('should throw Parse.Error with correct error code and message for empty string input', () => { |
| 183 | + expect(() => { |
| 184 | + MongoSchemaCollection._TESTmongoSchemaToParseSchema({ |
| 185 | + _id: 'TestClass', |
| 186 | + testField: '', |
| 187 | + }); |
| 188 | + }).toThrow(Parse.Error); |
| 189 | + }); |
| 190 | + }); |
99 | 191 | });
|
0 commit comments