|
5 | 5 |
|
6 | 6 | import {expect} from '@loopback/testlab';
|
7 | 7 | import {
|
8 |
| - FilterBuilder, |
9 |
| - Filter, |
10 |
| - Where, |
11 |
| - constrainFilter, |
12 |
| - constrainWhere, |
13 | 8 | constrainDataObject,
|
14 | 9 | constrainDataObjects,
|
| 10 | + constrainFilter, |
| 11 | + constrainWhere, |
15 | 12 | Entity,
|
| 13 | + Filter, |
| 14 | + FilterBuilder, |
| 15 | + Where, |
16 | 16 | } from '../../..';
|
17 | 17 |
|
18 | 18 | describe('constraint utility functions', () => {
|
@@ -87,24 +87,58 @@ describe('constraint utility functions', () => {
|
87 | 87 | });
|
88 | 88 | });
|
89 | 89 |
|
90 |
| - it('throws error when the query changes field in constrain', () => { |
| 90 | + it('throws error when the query changes field in constraint', () => { |
91 | 91 | const input = new Order({id: 1, description: 'order 1'});
|
92 | 92 | const constraint: Partial<Order> = {id: 2};
|
93 | 93 | expect(() => {
|
94 | 94 | constrainDataObject(input, constraint);
|
95 | 95 | }).to.throwError(/Property "id" cannot be changed!/);
|
96 | 96 | });
|
97 | 97 |
|
| 98 | + it('allows constrained fields with the same values', () => { |
| 99 | + const input = new Order({id: 2, description: 'order 1'}); |
| 100 | + const constraint: Partial<Order> = {id: 2}; |
| 101 | + const result = constrainDataObject(input, constraint); |
| 102 | + expect(result).to.deepEqual( |
| 103 | + new Order({ |
| 104 | + id: 2, |
| 105 | + description: 'order 1', |
| 106 | + }), |
| 107 | + ); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe('constrainDataObjects', () => { |
98 | 112 | it('constrains array of data objects', () => {
|
99 | 113 | const input = [
|
100 |
| - new Order({id: 1, description: 'order 1'}), |
101 |
| - new Order({id: 2, description: 'order 2'}), |
| 114 | + new Order({description: 'order 1'}), |
| 115 | + new Order({description: 'order 2'}), |
102 | 116 | ];
|
103 | 117 | const constraint: Partial<Order> = {id: 3};
|
104 | 118 | const result = constrainDataObjects(input, constraint);
|
105 | 119 | expect(result[0]).to.containDeep(Object.assign({}, input[0], constraint));
|
106 | 120 | expect(result[1]).to.containDeep(Object.assign({}, input[1], constraint));
|
107 | 121 | });
|
| 122 | + |
| 123 | + it('throws error when the query changes field in constraint', () => { |
| 124 | + const input = [new Order({id: 1, description: 'order 1'})]; |
| 125 | + const constraint: Partial<Order> = {id: 2}; |
| 126 | + expect(() => { |
| 127 | + constrainDataObjects(input, constraint); |
| 128 | + }).to.throwError(/Property "id" cannot be changed!/); |
| 129 | + }); |
| 130 | + |
| 131 | + it('allows constrained fields with the same values', () => { |
| 132 | + const input = [new Order({id: 2, description: 'order 1'})]; |
| 133 | + const constraint: Partial<Order> = {id: 2}; |
| 134 | + const result = constrainDataObjects(input, constraint); |
| 135 | + expect(result).to.deepEqual([ |
| 136 | + new Order({ |
| 137 | + id: 2, |
| 138 | + description: 'order 1', |
| 139 | + }), |
| 140 | + ]); |
| 141 | + }); |
108 | 142 | });
|
109 | 143 |
|
110 | 144 | /*---------------HELPERS----------------*/
|
|
0 commit comments