Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
373 changes: 373 additions & 0 deletions spec/ParseDecimal128.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,373 @@
'use strict';

const request = require('../lib/request');

describe('Parse Decimal128', () => {
it('should save and retrieve a Decimal128 value via REST API', async () => {
const createResponse = await request({
method: 'POST',
url: 'http://localhost:8378/1/classes/TestDecimal',
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-Master-Key': 'test',
'Content-Type': 'application/json',
},
body: {
amount: {
__type: 'Decimal128',
value: '12345678901234567890.123456789',
},
},
});
expect(createResponse.data.objectId).toBeDefined();

const getResponse = await request({
url: `http://localhost:8378/1/classes/TestDecimal/${createResponse.data.objectId}`,
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-Master-Key': 'test',
},
});
expect(getResponse.data.amount).toEqual({
__type: 'Decimal128',
value: '12345678901234567890.123456789',
});
});

it('should update a Decimal128 value', async () => {
const createResponse = await request({
method: 'POST',
url: 'http://localhost:8378/1/classes/TestDecimal',
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-Master-Key': 'test',
'Content-Type': 'application/json',
},
body: {
amount: {
__type: 'Decimal128',
value: '100.50',
},
},
});

await request({
method: 'PUT',
url: `http://localhost:8378/1/classes/TestDecimal/${createResponse.data.objectId}`,
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-Master-Key': 'test',
'Content-Type': 'application/json',
},
body: {
amount: {
__type: 'Decimal128',
value: '200.75',
},
},
});

const getResponse = await request({
url: `http://localhost:8378/1/classes/TestDecimal/${createResponse.data.objectId}`,
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-Master-Key': 'test',
},
});
expect(getResponse.data.amount).toEqual({
__type: 'Decimal128',
value: '200.75',
});
});

it('should query with $gt comparison on Decimal128', async () => {
const values = ['10.5', '20.5', '30.5'];
for (const val of values) {
await request({
method: 'POST',
url: 'http://localhost:8378/1/classes/DecimalQuery',
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-Master-Key': 'test',
'Content-Type': 'application/json',
},
body: {
amount: { __type: 'Decimal128', value: val },
},
});
}

const queryResponse = await request({
url: 'http://localhost:8378/1/classes/DecimalQuery',
qs: {
where: JSON.stringify({
amount: { $gt: { __type: 'Decimal128', value: '15.0' } },
}),
},
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-Master-Key': 'test',
},
});
expect(queryResponse.data.results.length).toBe(2);
const resultValues = queryResponse.data.results.map(r => r.amount.value).sort();
expect(resultValues).toEqual(['20.5', '30.5']);
});

it('should query with $lt comparison on Decimal128', async () => {
const values = ['100', '200', '300'];
for (const val of values) {
await request({
method: 'POST',
url: 'http://localhost:8378/1/classes/DecimalLt',
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-Master-Key': 'test',
'Content-Type': 'application/json',
},
body: {
amount: { __type: 'Decimal128', value: val },
},
});
}

const queryResponse = await request({
url: 'http://localhost:8378/1/classes/DecimalLt',
qs: {
where: JSON.stringify({
amount: { $lt: { __type: 'Decimal128', value: '250' } },
}),
},
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-Master-Key': 'test',
},
});
expect(queryResponse.data.results.length).toBe(2);
const resultValues = queryResponse.data.results.map(r => r.amount.value).sort();
expect(resultValues).toEqual(['100', '200']);
});

it('should handle high-precision Decimal128 values', async () => {
const highPrecisionValue = '12345678901234567890.12345678901234';
const createResponse = await request({
method: 'POST',
url: 'http://localhost:8378/1/classes/DecimalPrecision',
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-Master-Key': 'test',
'Content-Type': 'application/json',
},
body: {
amount: { __type: 'Decimal128', value: highPrecisionValue },
},
});

const getResponse = await request({
url: `http://localhost:8378/1/classes/DecimalPrecision/${createResponse.data.objectId}`,
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-Master-Key': 'test',
},
});
expect(getResponse.data.amount).toEqual({
__type: 'Decimal128',
value: highPrecisionValue,
});
});

it('should handle negative Decimal128 values', async () => {
const createResponse = await request({
method: 'POST',
url: 'http://localhost:8378/1/classes/DecimalNeg',
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-Master-Key': 'test',
'Content-Type': 'application/json',
},
body: {
amount: { __type: 'Decimal128', value: '-12345.6789' },
},
});

const getResponse = await request({
url: `http://localhost:8378/1/classes/DecimalNeg/${createResponse.data.objectId}`,
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-Master-Key': 'test',
},
});
expect(getResponse.data.amount).toEqual({
__type: 'Decimal128',
value: '-12345.6789',
});
});

it('should handle zero Decimal128 value', async () => {
const createResponse = await request({
method: 'POST',
url: 'http://localhost:8378/1/classes/DecimalZero',
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-Master-Key': 'test',
'Content-Type': 'application/json',
},
body: {
amount: { __type: 'Decimal128', value: '0' },
},
});

const getResponse = await request({
url: `http://localhost:8378/1/classes/DecimalZero/${createResponse.data.objectId}`,
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-Master-Key': 'test',
},
});
expect(getResponse.data.amount).toEqual({
__type: 'Decimal128',
value: '0',
});
});

it('should set Decimal128 field via schema API', async () => {
await request({
method: 'POST',
url: 'http://localhost:8378/1/schemas/DecimalSchema',
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-Master-Key': 'test',
'Content-Type': 'application/json',
},
body: {
className: 'DecimalSchema',
fields: {
amount: { type: 'Decimal128' },
},
},
});

const schemaResponse = await request({
url: 'http://localhost:8378/1/schemas/DecimalSchema',
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-Master-Key': 'test',
},
});
expect(schemaResponse.data.fields.amount.type).toBe('Decimal128');
});

it('should delete Decimal128 field value', async () => {
const createResponse = await request({
method: 'POST',
url: 'http://localhost:8378/1/classes/DecimalDel',
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-Master-Key': 'test',
'Content-Type': 'application/json',
},
body: {
amount: { __type: 'Decimal128', value: '42.0' },
},
});

await request({
method: 'PUT',
url: `http://localhost:8378/1/classes/DecimalDel/${createResponse.data.objectId}`,
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-Master-Key': 'test',
'Content-Type': 'application/json',
},
body: {
amount: { __op: 'Delete' },
},
});

const getResponse = await request({
url: `http://localhost:8378/1/classes/DecimalDel/${createResponse.data.objectId}`,
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-Master-Key': 'test',
},
});
expect(getResponse.data.amount).toBeUndefined();
});

it('should query with equality on Decimal128', async () => {
await request({
method: 'POST',
url: 'http://localhost:8378/1/classes/DecimalEq',
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-Master-Key': 'test',
'Content-Type': 'application/json',
},
body: {
amount: { __type: 'Decimal128', value: '99.99' },
label: 'target',
},
});
await request({
method: 'POST',
url: 'http://localhost:8378/1/classes/DecimalEq',
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-Master-Key': 'test',
'Content-Type': 'application/json',
},
body: {
amount: { __type: 'Decimal128', value: '50.00' },
label: 'other',
},
});

const queryResponse = await request({
url: 'http://localhost:8378/1/classes/DecimalEq',
qs: {
where: JSON.stringify({
amount: { __type: 'Decimal128', value: '99.99' },
}),
},
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-Master-Key': 'test',
},
});
expect(queryResponse.data.results.length).toBe(1);
expect(queryResponse.data.results[0].label).toBe('target');
});

it('should handle Decimal128 nested inside an Object field', async () => {
const createResponse = await request({
method: 'POST',
url: 'http://localhost:8378/1/classes/DecimalNested',
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-Master-Key': 'test',
'Content-Type': 'application/json',
},
body: {
metadata: {
price: { __type: 'Decimal128', value: '19.99' },
currency: 'USD',
},
},
});
expect(createResponse.data.objectId).toBeDefined();

const getResponse = await request({
url: `http://localhost:8378/1/classes/DecimalNested/${createResponse.data.objectId}`,
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-Master-Key': 'test',
},
});
expect(getResponse.data.metadata).toBeDefined();
expect(getResponse.data.metadata.currency).toBe('USD');
expect(getResponse.data.metadata.price).toEqual({
__type: 'Decimal128',
value: '19.99',
});
});
});
Loading
Loading