|
| 1 | +// Copyright IBM Corp. 2015,2016. All Rights Reserved. |
| 2 | +// Node module: loopback-datasource-juggler |
| 3 | +// This file is licensed under the MIT License. |
| 4 | +// License text available at https://opensource.org/licenses/MIT |
| 5 | + |
| 6 | +'use strict'; |
| 7 | + |
| 8 | +const DataSource = require('..').DataSource; |
| 9 | +const should = require('should'); |
| 10 | + |
| 11 | +describe('Model.settings.allowExtendedOperators', () => { |
| 12 | + context('DAO.find()', () => { |
| 13 | + it('converts extended operators to string value by default', () => { |
| 14 | + const TestModel = createTestModel(); |
| 15 | + return TestModel.find(extendedQuery()).then((results) => { |
| 16 | + should(results[0].value).eql('[object Object]'); |
| 17 | + }); |
| 18 | + }); |
| 19 | + |
| 20 | + it('preserves extended operators wit allowExtendedOperators set', () => { |
| 21 | + const TestModel = createTestModel({allowExtendedOperators: true}); |
| 22 | + return TestModel.find(extendedQuery()).then((results) => { |
| 23 | + should(results[0].value).eql({$exists: true}); |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + function extendedQuery() { |
| 28 | + // datasource modifies the query, |
| 29 | + // we have to build a new object for each test |
| 30 | + return {where: {value: {$exists: true}}}; |
| 31 | + } |
| 32 | + }); |
| 33 | + |
| 34 | + function createTestModel(connectorSettings) { |
| 35 | + const ds = createTestDataSource(connectorSettings); |
| 36 | + return ds.createModel('TestModel', {value: String}); |
| 37 | + } |
| 38 | + |
| 39 | + function createTestDataSource(connectorSettings) { |
| 40 | + connectorSettings = connectorSettings || {}; |
| 41 | + connectorSettings.connector = { |
| 42 | + initialize: (dataSource, cb) => { |
| 43 | + dataSource.connector = new TestConnector(dataSource); |
| 44 | + }, |
| 45 | + }; |
| 46 | + |
| 47 | + return new DataSource(connectorSettings); |
| 48 | + } |
| 49 | + |
| 50 | + class TestConnector { |
| 51 | + constructor(dataSource) { |
| 52 | + } |
| 53 | + |
| 54 | + all(model, filter, options, callback) { |
| 55 | + // return the raw "value" query |
| 56 | + var instanceFound = { |
| 57 | + value: filter.where.value, |
| 58 | + }; |
| 59 | + callback(null, [instanceFound]); |
| 60 | + } |
| 61 | + } |
| 62 | +}); |
0 commit comments