Skip to content

Commit 8710966

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

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

spec/MongoSchemaCollectionAdapter.spec.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
const Parse = require('parse/node');
34
const MongoSchemaCollection = require('../lib/Adapters/Storage/Mongo/MongoSchemaCollection')
45
.default;
56

@@ -96,4 +97,95 @@ describe('MongoSchemaCollection', () => {
9697
});
9798
done();
9899
});
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+
});
99191
});

0 commit comments

Comments
 (0)