diff --git a/jest.setup.js b/jest.setup.js index 92d10b6..51d9b02 100644 --- a/jest.setup.js +++ b/jest.setup.js @@ -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); \ No newline at end of file diff --git a/spec/DateFunctions.spec.js b/spec/DateFunctions.spec.js index 4ae0885..979ba10 100644 --- a/spec/DateFunctions.spec.js +++ b/spec/DateFunctions.spec.js @@ -1,4 +1,5 @@ import { TestApplication } from './TestApplication'; +import moment from 'moment'; describe('DateFunctions', () => { /** @@ -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); } }); }); @@ -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); } }); }); @@ -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); } }); }); diff --git a/spec/QueryExpression.selectJson.spec.js b/spec/QueryExpression.selectJson.spec.js index 5f0ecbb..c980cb8 100644 --- a/spec/QueryExpression.selectJson.spec.js +++ b/spec/QueryExpression.selectJson.spec.js @@ -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(); + } + + }); + }); + }); diff --git a/spec/db/local.db b/spec/db/local.db index b5ec071..7f91614 100644 Binary files a/spec/db/local.db and b/spec/db/local.db differ