Skip to content

Commit

Permalink
validate json objects
Browse files Browse the repository at this point in the history
  • Loading branch information
kbarbounakis committed Jan 30, 2025
1 parent 7fbb029 commit 219a109
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 5 deletions.
3 changes: 2 additions & 1 deletion jest.setup.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require('dotenv').config();
const { JsonLogger } = require('@themost/json-logger');
const { TraceUtils } = require('@themost/common');
process.env.NODE_ENV='development';
process.env.NODE_ENV = 'development';
TraceUtils.useLogger(new JsonLogger());
/* global jest */
jest.setTimeout(30000);
13 changes: 9 additions & 4 deletions spec/DateFunctions.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { TestApplication } from './TestApplication';
import moment from 'moment';

describe('DateFunctions', () => {
/**
Expand Down Expand Up @@ -36,11 +37,13 @@ describe('DateFunctions', () => {
it('should use getDay()', async () => {
await app.executeInTestTranscaction(async (context) => {
let items = await context.model('Order')
.asQueryable().where('orderDate').getDay().equal(15).silent().getItems();
.asQueryable().where('orderDate').getDate().getDay().equal(15).silent().take(10).getItems();
expect(Array.isArray(items)).toBeTruthy();
expect(items.length).toBeGreaterThan(0);
for (const item of items) {
expect(item.orderDate.getDate()).toEqual(15);
const orderDate = item.orderDate;
const dayOfMonth = parseInt(moment.utc(orderDate).format('D'), 10);
expect(dayOfMonth).toEqual(15);
}
});
});
Expand All @@ -52,7 +55,8 @@ describe('DateFunctions', () => {
expect(Array.isArray(items)).toBeTruthy();
expect(items.length).toBeGreaterThan(0);
for (const item of items) {
expect(item.orderDate.getMonth()).toEqual(3);
const orderMonth = parseInt(moment.utc(item.orderDate).format('M'), 10);
expect(orderMonth).toEqual(4);
}
});
});
Expand All @@ -77,7 +81,8 @@ describe('DateFunctions', () => {
expect(Array.isArray(items)).toBeTruthy();
expect(items.length).toBeGreaterThan(0);
for (const item of items) {
expect(item.orderDate.getHours()).toEqual(14);
const hour = parseInt(moment.utc(item.orderDate).format('H'), 10);
expect(hour).toEqual(14);
}
});
});
Expand Down
56 changes: 56 additions & 0 deletions spec/QueryExpression.selectJson.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,4 +345,60 @@ describe('SqlFormatter', () => {
});
});

it('should use jsonObject in ad-hoc queries', async () => {
await app.executeInTestTranscaction(async (context) => {
const {viewAdapter: Orders} = context.model('Order');
const {viewAdapter: Customers} = context.model('Person');
const {viewAdapter: OrderStatusTypes} = context.model('OrderStatusType');
const q = new QueryExpression().select(
'id', 'orderedItem', 'orderStatus', 'orderDate'
).from(Orders).join(new QueryEntity(Customers).as('customers')).with(
new QueryExpression().where(
new QueryField('customer').from(Orders)
).equal(
new QueryField('id').from('customers')
)
).join(new QueryEntity(OrderStatusTypes).as('orderStatusTypes')).with(
new QueryExpression().where(
new QueryField('orderStatus').from(Orders)
).equal(
new QueryField('id').from('orderStatusTypes')
)
).where(new QueryField('description').from('customers')).equal('Eric Thomas');
const select = q.$select[Orders];
select.push({
customer: {
$jsonObject: [
'familyName',
new QueryField('familyName').from('customers'),
'givenName',
new QueryField('givenName').from('customers'),
]
}
}, {
orderStatus: {
$jsonObject: [
'name',
new QueryField('name').from('orderStatusTypes'),
'alternateName',
new QueryField('alternateName').from('orderStatusTypes'),
]
}
});
/**
* @type {Array<{id: number, orderedItem: number, orderDate: Date, orderStatus: { name: string, alternateName: string }, customer: {familyName: string, givenName: string}}>}
*/
const items = await context.db.executeAsync(q, []);
expect(items).toBeTruthy();
for (const item of items) {
expect(item.customer).toBeTruthy();
expect(item.customer.familyName).toEqual('Thomas');
expect(item.customer.givenName).toEqual('Eric');
expect(item.orderStatus).toBeTruthy();
expect(item.orderStatus.name).toBeTruthy();
}

});
});

});
Binary file modified spec/db/local.db
Binary file not shown.

0 comments on commit 219a109

Please sign in to comment.