Skip to content

Commit

Permalink
Validate where statement (#162)
Browse files Browse the repository at this point in the history
* validate where statement

* add link to issue
  • Loading branch information
kbarbounakis authored Sep 29, 2024
1 parent 58e5794 commit 388bf26
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
3 changes: 2 additions & 1 deletion data-queryable.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var _ = require('lodash');
var {TextUtils} = require('@themost/common');
var {DataMappingExtender} = require('./data-mapping-extensions');
var {DataAssociationMapping} = require('./types');
var {DataError} = require('@themost/common');
var {DataError, Args} = require('@themost/common');
var {QueryField} = require('@themost/query');
var {QueryEntity} = require('@themost/query');
var {QueryUtils} = require('@themost/query');
Expand Down Expand Up @@ -853,6 +853,7 @@ DataQueryable.prototype.prepare = function(useOr) {
});
*/
DataQueryable.prototype.where = function(attr) {
Args.check(this.query.$where == null, new Error('The where expression has already been initialized.'));
if (typeof attr === 'string' && /\//.test(attr)) {
this.query.where(DataAttributeResolver.prototype.resolveNestedAttribute.call(this, attr));
return this;
Expand Down
51 changes: 51 additions & 0 deletions spec/DataQueryable.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {DataModelFilterParser} from '../data-model-filter.parser';
import {TestApplication} from './TestApplication';
import {DataContext} from '../types';
import {resolve} from 'path';

describe('DataQueryable', () => {

let app: TestApplication;
let context: DataContext;
beforeAll((done) => {
app = new TestApplication(resolve(__dirname, 'test2'));
context = app.createContext();
return done();
});
afterAll(async () => {
await context.finalizeAsync();
await app.finalize();
});

/**
* @see https://github.com/themost-framework/data/issues/161
*/
it('should validate where statement', async () => {
const Orders = context.model('Order').silent();
const items = await Orders.where('orderStatus/alternateName').equal('OrderDelivered')
.orderByDescending('orderDate').take(10).getItems();
expect(items).toBeTruthy();
expect(items.length).toBeGreaterThan(0);
});

/**
* @see https://github.com/themost-framework/data/issues/161
*/
it('should validate where statement with error', async () => {
const Orders = context.model('Order').silent();
const q = Orders.where('orderStatus/alternateName').equal('OrderDelivered')
.orderByDescending('orderDate').take(10);
expect(() => q.where('orderedItem/category').equal('Laptops')).toThrow('The where expression has already been initialized.');
});

/**
* @see https://github.com/themost-framework/data/issues/161
*/
it('should validate where statement after prepare', async () => {
const Orders = context.model('Order').silent();
const q = Orders.where('orderStatus/alternateName').equal('OrderDelivered')
.orderByDescending('orderDate').take(10);
expect(() => q.prepare().where('orderedItem/category').equal('Laptops')).toBeTruthy();
});

});

0 comments on commit 388bf26

Please sign in to comment.