|
| 1 | +import { |
| 2 | + mapboxFilterToQueryFilter |
| 3 | +} from '../../../src/common/util/FilterCondition'; |
| 4 | + |
| 5 | +describe('FilterCondition', () => { |
| 6 | + describe('mapboxFilterToQueryFilter - SQL', () => { |
| 7 | + it('== 普通等值', () => { |
| 8 | + const filter = ['==', 'name', 'Tom']; |
| 9 | + const result = mapboxFilterToQueryFilter(filter, 'SQL'); |
| 10 | + expect(result).toBe("name == 'Tom'"); |
| 11 | + }); |
| 12 | + |
| 13 | + it('== null 转 IS NULL', () => { |
| 14 | + const filter = ['==', 'name', null]; |
| 15 | + const result = mapboxFilterToQueryFilter(filter, 'SQL'); |
| 16 | + expect(result).toBe('name IS NULL'); |
| 17 | + }); |
| 18 | + |
| 19 | + it('!= null 转 IS NOT NULL', () => { |
| 20 | + const filter = ['!=', 'name', null]; |
| 21 | + const result = mapboxFilterToQueryFilter(filter, 'SQL'); |
| 22 | + expect(result).toBe('name IS NOT NULL'); |
| 23 | + }); |
| 24 | + |
| 25 | + it('字符串单引号转义', () => { |
| 26 | + const filter = ['==', 'publisher', "O'Reilly"]; |
| 27 | + const result = mapboxFilterToQueryFilter(filter, 'SQL'); |
| 28 | + expect(result).toBe("publisher == 'O''Reilly'"); |
| 29 | + }); |
| 30 | + |
| 31 | + it('数值比较 >', () => { |
| 32 | + const filter = ['>', 'age', 18]; |
| 33 | + const result = mapboxFilterToQueryFilter(filter, 'SQL'); |
| 34 | + expect(result).toBe('age > 18'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('in 操作符', () => { |
| 38 | + const filter = ['in', 'status', 'A', 'B', 'C']; |
| 39 | + const result = mapboxFilterToQueryFilter(filter, 'SQL'); |
| 40 | + expect(result).toBe("status IN ('A', 'B', 'C')"); |
| 41 | + }); |
| 42 | + |
| 43 | + it('!in 操作符', () => { |
| 44 | + const filter = ['!in', 'status', 'A', 'B']; |
| 45 | + const result = mapboxFilterToQueryFilter(filter, 'SQL'); |
| 46 | + expect(result).toBe("status NOT IN ('A', 'B')"); |
| 47 | + }); |
| 48 | + |
| 49 | + it('all (AND)', () => { |
| 50 | + const filter = [ |
| 51 | + 'all', |
| 52 | + ['>', 'age', 18], |
| 53 | + ['==', 'gender', 'M'] |
| 54 | + ]; |
| 55 | + const result = mapboxFilterToQueryFilter(filter, 'SQL'); |
| 56 | + expect(result).toBe("age > 18 AND gender == 'M'"); |
| 57 | + }); |
| 58 | + |
| 59 | + it('any (OR)', () => { |
| 60 | + const filter = [ |
| 61 | + 'any', |
| 62 | + ['==', 'type', 'A'], |
| 63 | + ['==', 'type', 'B'] |
| 64 | + ]; |
| 65 | + const result = mapboxFilterToQueryFilter(filter, 'SQL'); |
| 66 | + expect(result).toBe("(type == 'A') OR (type == 'B')"); |
| 67 | + }); |
| 68 | + |
| 69 | + it('none (NOT AND)', () => { |
| 70 | + const filter = [ |
| 71 | + 'none', |
| 72 | + ['==', 'status', 'disabled'], |
| 73 | + ['<', 'age', 10] |
| 74 | + ]; |
| 75 | + const result = mapboxFilterToQueryFilter(filter, 'SQL'); |
| 76 | + expect(result).toBe("NOT (status == 'disabled') AND NOT (age < 10)"); |
| 77 | + }); |
| 78 | + |
| 79 | + it('非法 filter 返回空字符串', () => { |
| 80 | + const result = mapboxFilterToQueryFilter(['get', 'name'], 'SQL'); |
| 81 | + expect(result).toBe(''); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe('mapboxFilterToQueryFilter - XML', () => { |
| 86 | + it('== 普通等值', () => { |
| 87 | + const filter = ['==', 'name', 'Tom']; |
| 88 | + const result = mapboxFilterToQueryFilter(filter, 'XML'); |
| 89 | + expect(result).toContain('<fes:PropertyIsEqualTo>'); |
| 90 | + expect(result).toContain('<fes:ValueReference>name</fes:ValueReference>'); |
| 91 | + expect(result).toContain('<fes:Literal>Tom</fes:Literal>'); |
| 92 | + }); |
| 93 | + |
| 94 | + it('== null 转 PropertyIsNull', () => { |
| 95 | + const filter = ['==', 'name', null]; |
| 96 | + const result = mapboxFilterToQueryFilter(filter, 'XML'); |
| 97 | + expect(result).toContain('<fes:PropertyIsNull>'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('!= null 转 PropertyIsNotNull', () => { |
| 101 | + const filter = ['!=', 'name', null]; |
| 102 | + const result = mapboxFilterToQueryFilter(filter, 'XML'); |
| 103 | + expect(result).toContain('<fes:PropertyIsNotNull>'); |
| 104 | + }); |
| 105 | + |
| 106 | + it('in 转 Or', () => { |
| 107 | + const filter = ['in', 'type', 'A', 'B']; |
| 108 | + const result = mapboxFilterToQueryFilter(filter, 'XML'); |
| 109 | + expect(result).toContain('<fes:Or>'); |
| 110 | + const equalCount = (result.match(/<fes:PropertyIsEqualTo>/g) || []).length; |
| 111 | + expect(equalCount).toBe(2); |
| 112 | + }); |
| 113 | + it('!in 转 Not + Or', () => { |
| 114 | + const filter = ['!in', 'type', 'A', 'B']; |
| 115 | + const result = mapboxFilterToQueryFilter(filter, 'XML'); |
| 116 | + expect(result).toContain('<fes:Not>'); |
| 117 | + expect(result).toContain('<fes:Or>'); |
| 118 | + }); |
| 119 | + |
| 120 | + it('all 转 And', () => { |
| 121 | + const filter = [ |
| 122 | + 'all', |
| 123 | + ['>', 'age', 18], |
| 124 | + ['==', 'gender', 'M'] |
| 125 | + ]; |
| 126 | + const result = mapboxFilterToQueryFilter(filter, 'XML'); |
| 127 | + expect(result).toContain('<fes:And>'); |
| 128 | + }); |
| 129 | + |
| 130 | + it('any 转 Or', () => { |
| 131 | + const filter = [ |
| 132 | + 'any', |
| 133 | + ['==', 'type', 'A'], |
| 134 | + ['==', 'type', 'B'] |
| 135 | + ]; |
| 136 | + const result = mapboxFilterToQueryFilter(filter, 'XML'); |
| 137 | + expect(result).toContain('<fes:Or>'); |
| 138 | + }); |
| 139 | + |
| 140 | + it('none 使用反转比较', () => { |
| 141 | + const filter = [ |
| 142 | + 'none', |
| 143 | + ['==', 'status', 'disabled'], |
| 144 | + ['<', 'age', 10] |
| 145 | + ]; |
| 146 | + const result = mapboxFilterToQueryFilter(filter, 'XML'); |
| 147 | + // == → != , < → >= |
| 148 | + expect(result).toContain('PropertyIsNotEqualTo'); |
| 149 | + expect(result).toContain('PropertyIsGreaterThanOrEqualTo'); |
| 150 | + }); |
| 151 | + }); |
| 152 | +}); |
0 commit comments