Skip to content

Commit a882ffe

Browse files
committed
test: Add comprehensive tests for mongoFieldToParseSchemaField type validation and clear error message
- Add tests for valid field type conversions - Add tests for invalid type handling (null, undefined, non-string values) - Ensure Parse.Error code is thrown for invalid inputs - Test coverage for the type validation improvements - Add a clear error explanation
1 parent 1f9b6fe commit a882ffe

File tree

2 files changed

+104
-1
lines changed

2 files changed

+104
-1
lines changed

spec/MongoSchemaCollectionAdapter.spec.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,107 @@ describe('MongoSchemaCollection', () => {
9696
});
9797
done();
9898
});
99+
100+
describe('mongoFieldToParseSchemaField function', () => {
101+
// Test successful type conversions
102+
it('should convert valid mongo field types to parse schema fields', () => {
103+
const testCases = [
104+
{ input: 'string', expected: { type: 'String' } },
105+
{ input: 'number', expected: { type: 'Number' } },
106+
{ input: 'boolean', expected: { type: 'Boolean' } },
107+
{ input: 'date', expected: { type: 'Date' } },
108+
{ input: 'map', expected: { type: 'Object' } },
109+
{ input: 'object', expected: { type: 'Object' } },
110+
{ input: 'array', expected: { type: 'Array' } },
111+
{ input: 'geopoint', expected: { type: 'GeoPoint' } },
112+
{ input: 'file', expected: { type: 'File' } },
113+
{ input: 'bytes', expected: { type: 'Bytes' } },
114+
{ input: 'polygon', expected: { type: 'Polygon' } },
115+
{ input: '*_User', expected: { type: 'Pointer', targetClass: '_User' } },
116+
{ input: '*Post', expected: { type: 'Pointer', targetClass: 'Post' } },
117+
{ input: 'relation<_User>', expected: { type: 'Relation', targetClass: '_User' } },
118+
{ input: 'relation<Post>', expected: { type: 'Relation', targetClass: 'Post' } },
119+
];
120+
121+
testCases.forEach(({ input, expected }) => {
122+
const result = MongoSchemaCollection._TESTmongoSchemaToParseSchema({
123+
_id: 'TestClass',
124+
testField: input,
125+
});
126+
127+
expect(result.fields.testField).toEqual(expected);
128+
});
129+
});
130+
131+
// Test error handling for invalid types (non-string values)
132+
it('should throw error for invalid field types', () => {
133+
const invalidInputs = [
134+
null,
135+
undefined,
136+
123,
137+
true,
138+
false,
139+
{},
140+
[],
141+
'',
142+
];
143+
144+
invalidInputs.forEach(invalidInput => {
145+
expect(() => {
146+
MongoSchemaCollection._TESTmongoSchemaToParseSchema({
147+
_id: 'TestClass',
148+
testField: invalidInput,
149+
});
150+
}).toThrow();
151+
});
152+
});
153+
154+
it('should throw error with correct message for null input', () => {
155+
try {
156+
MongoSchemaCollection._TESTmongoSchemaToParseSchema({
157+
_id: 'TestClass',
158+
testField: null,
159+
});
160+
} catch (error) {
161+
expect(error.code).toBe(255);
162+
expect(error.message).toContain('Invalid field type');
163+
}
164+
});
165+
166+
it('should throw error with correct message for undefined input', () => {
167+
try {
168+
MongoSchemaCollection._TESTmongoSchemaToParseSchema({
169+
_id: 'TestClass',
170+
testField: undefined,
171+
});
172+
} catch (error) {
173+
expect(error.code).toBe(255);
174+
expect(error.message).toContain('Invalid field type');
175+
}
176+
});
177+
178+
it('should throw error with correct message for non-string input', () => {
179+
try {
180+
MongoSchemaCollection._TESTmongoSchemaToParseSchema({
181+
_id: 'TestClass',
182+
testField: 123,
183+
});
184+
} catch (error) {
185+
expect(error.code).toBe(255);
186+
expect(error.message).toContain('Invalid field type');
187+
}
188+
});
189+
190+
it('should throw error with correct message for empty string input', () => {
191+
try {
192+
MongoSchemaCollection._TESTmongoSchemaToParseSchema({
193+
_id: 'TestClass',
194+
testField: '',
195+
});
196+
} catch (error) {
197+
expect(error.code).toBe(255);
198+
expect(error.message).toContain('Invalid field type');
199+
}
200+
});
201+
});
99202
});

src/Adapters/Storage/Mongo/MongoSchemaCollection.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function mongoFieldToParseSchemaField(type) {
66
if (!type || typeof type !== 'string') {
77
throw new Parse.Error(
88
Parse.Error.INVALID_SCHEMA_OPERATION,
9-
`Invalid field type: ${type}. Expected a string. Field type must be one of: string, number, boolean, date, map, object, array, geopoint, file, bytes, polygon, or a valid relation/pointer format.`
9+
`Invalid field type: ${type}. Expected a string. Fix the type mismatch in your schema configuration.`
1010
);
1111
}
1212

0 commit comments

Comments
 (0)