Skip to content

Commit 054f5e5

Browse files
committed
refactor: migrate tests for wfs search
1 parent 0acb8f8 commit 054f5e5

File tree

3 files changed

+139
-142
lines changed

3 files changed

+139
-142
lines changed

src/lib/getFeatures/wfs/buildWfsFilter.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,60 @@ export const buildWfsFilter = (
8787
inputs.map((input) => buildWfsFilterQuery(input, parameters)).join('') +
8888
'</wfs:GetFeature>'
8989
)
90+
91+
if (import.meta.vitest) {
92+
const { expect, test } = import.meta.vitest
93+
94+
const parameters: WfsParameters = {
95+
typeName: 'TyPeNaMe',
96+
epsg: 'EPSG:25832',
97+
featurePrefix: 'prefix',
98+
xmlns: 'example.com',
99+
maxFeatures: 999,
100+
patterns: [
101+
'{{gemarkung}} {{flur}} {{flstnrzae}}/{{flstnrnen}}, {{flstkennz}}',
102+
'{{gemarkung}} {{flur}} {{flstnrzae}}, {{flstkennz}}',
103+
'{{gemarkung}} {{flstnrzae}}/{{flstnrnen}}, {{flstkennz}}',
104+
'{{gemarkung}} {{flstnrzae}}, {{flstkennz}}',
105+
'{{flstkennz}}',
106+
],
107+
patternKeys: {
108+
gemarkung: '([^0-9]+)',
109+
flur: '([0-9]+)',
110+
flstnrzae: '([0-9]+)',
111+
flstnrnen: '([0-9]+)',
112+
flstkennz: '([0-9_]+)',
113+
},
114+
}
115+
116+
test('creates an empty search for an empty input', () => {
117+
expect(buildWfsFilter([], parameters)).toEqual(
118+
'<?xml version="1.0" encoding="UTF-8"?><wfs:GetFeature xmlns:wfs="http://www.opengis.net/wfs" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" service="WFS" version="1.1.0" xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.1.0/wfs.xsd" maxFeatures="999"></wfs:GetFeature>'
119+
)
120+
})
121+
test('creates a one-query search for a single match', () => {
122+
expect(buildWfsFilter([[['a', '5']]], parameters)).toEqual(
123+
'<?xml version="1.0" encoding="UTF-8"?><wfs:GetFeature xmlns:wfs="http://www.opengis.net/wfs" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" service="WFS" version="1.1.0" xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.1.0/wfs.xsd" maxFeatures="999"><wfs:Query typeName="prefix:TyPeNaMe" xmlns:prefix="example.com"><ogc:Filter xmlns:ogc="http://www.opengis.net/ogc"><ogc:PropertyIsLike wildCard="*" singleChar="." escapeChar="!"><ogc:PropertyName>prefix:a</ogc:PropertyName><ogc:Literal>5*</ogc:Literal></ogc:PropertyIsLike></ogc:Filter></wfs:Query></wfs:GetFeature>'
124+
)
125+
})
126+
test('creates a one-query and-ed search for a single match with multiple fields', () => {
127+
expect(
128+
buildWfsFilter(
129+
[
130+
[
131+
['a', '5'],
132+
['b', '3'],
133+
],
134+
],
135+
parameters
136+
)
137+
).toEqual(
138+
'<?xml version="1.0" encoding="UTF-8"?><wfs:GetFeature xmlns:wfs="http://www.opengis.net/wfs" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" service="WFS" version="1.1.0" xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.1.0/wfs.xsd" maxFeatures="999"><wfs:Query typeName="prefix:TyPeNaMe" xmlns:prefix="example.com"><ogc:Filter xmlns:ogc="http://www.opengis.net/ogc"><ogc:And><ogc:PropertyIsLike wildCard="*" singleChar="." escapeChar="!"><ogc:PropertyName>prefix:a</ogc:PropertyName><ogc:Literal>5*</ogc:Literal></ogc:PropertyIsLike><ogc:PropertyIsLike wildCard="*" singleChar="." escapeChar="!"><ogc:PropertyName>prefix:b</ogc:PropertyName><ogc:Literal>3*</ogc:Literal></ogc:PropertyIsLike></ogc:And></ogc:Filter></wfs:Query></wfs:GetFeature>'
139+
)
140+
})
141+
test('creates a multi-query search for a multiple matches', () => {
142+
expect(buildWfsFilter([[['a', '5']], [['b', '3']]], parameters)).toEqual(
143+
'<?xml version="1.0" encoding="UTF-8"?><wfs:GetFeature xmlns:wfs="http://www.opengis.net/wfs" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" service="WFS" version="1.1.0" xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.1.0/wfs.xsd" maxFeatures="999"><wfs:Query typeName="prefix:TyPeNaMe" xmlns:prefix="example.com"><ogc:Filter xmlns:ogc="http://www.opengis.net/ogc"><ogc:PropertyIsLike wildCard="*" singleChar="." escapeChar="!"><ogc:PropertyName>prefix:a</ogc:PropertyName><ogc:Literal>5*</ogc:Literal></ogc:PropertyIsLike></ogc:Filter></wfs:Query><wfs:Query typeName="prefix:TyPeNaMe" xmlns:prefix="example.com"><ogc:Filter xmlns:ogc="http://www.opengis.net/ogc"><ogc:PropertyIsLike wildCard="*" singleChar="." escapeChar="!"><ogc:PropertyName>prefix:b</ogc:PropertyName><ogc:Literal>3*</ogc:Literal></ogc:PropertyIsLike></ogc:Filter></wfs:Query></wfs:GetFeature>'
144+
)
145+
})
146+
}

src/lib/getFeatures/wfs/match.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,85 @@ export const match = (
146146
})
147147
return sortMatches(matches, patterns, uninterpretedCharacters)
148148
}
149+
150+
if (import.meta.vitest) {
151+
const { expect, test } = import.meta.vitest
152+
153+
// mock result from pattern.matchAll(/{{(.*?)}}/g) for test comparison
154+
const mockRegExpMatchArray = (find, inner, index, input, groups?) => {
155+
const regExpMatchArray: RegExpMatchArray = [find, inner]
156+
regExpMatchArray.index = index
157+
regExpMatchArray.input = input
158+
regExpMatchArray.groups = groups
159+
return regExpMatchArray
160+
}
161+
const patterns = [
162+
'{{gemarkung}} {{flur}} {{flstnrzae}}/{{flstnrnen}}, {{flstkennz}}',
163+
'{{gemarkung}} {{flur}} {{flstnrzae}}, {{flstkennz}}',
164+
'{{gemarkung}} {{flstnrzae}}/{{flstnrnen}}, {{flstkennz}}',
165+
'{{gemarkung}} {{flstnrzae}}, {{flstkennz}}',
166+
'{{flstkennz}}',
167+
]
168+
const patternKeys = {
169+
gemarkung: '([^0-9]+)',
170+
flur: '([0-9]+)',
171+
flstnrzae: '([0-9]+)',
172+
flstnrnen: '([0-9]+)',
173+
flstkennz: '([0-9_]+)',
174+
}
175+
176+
test('getBlocks parts pattern strings to Block[][]', () => {
177+
expect(getBlocks(patterns[0] as string)).toEqual([
178+
mockRegExpMatchArray('{{gemarkung}}', 'gemarkung', 0, patterns[0]),
179+
' ',
180+
mockRegExpMatchArray('{{flur}}', 'flur', 14, patterns[0]),
181+
' ',
182+
mockRegExpMatchArray('{{flstnrzae}}', 'flstnrzae', 23, patterns[0]),
183+
'/',
184+
mockRegExpMatchArray('{{flstnrnen}}', 'flstnrnen', 37, patterns[0]),
185+
', ',
186+
mockRegExpMatchArray('{{flstkennz}}', 'flstkennz', 52, patterns[0]),
187+
])
188+
})
189+
test('match builds full match groups', () => {
190+
expect(
191+
match(patterns, patternKeys, 'Musterhausen 12 3/4, 1234___')
192+
).toEqual([
193+
[
194+
['gemarkung', 'Musterhausen'],
195+
['flur', '12'],
196+
['flstnrzae', '3'],
197+
['flstnrnen', '4'],
198+
['flstkennz', '1234___'],
199+
],
200+
[
201+
['gemarkung', 'Musterhausen'],
202+
['flur', '12'],
203+
['flstnrzae', '3'],
204+
],
205+
[
206+
['gemarkung', 'Musterhausen'],
207+
['flstnrzae', '12'],
208+
['flstnrnen', '3'],
209+
],
210+
[
211+
['gemarkung', 'Musterhausen'],
212+
['flstnrzae', '12'],
213+
['flstkennz', '3'],
214+
],
215+
])
216+
})
217+
test('match manages to decide the best fit', () => {
218+
expect(match(patterns, patternKeys, '1234___')).toEqual([
219+
[
220+
['flur', '1234'],
221+
['flstkennz', '___'],
222+
],
223+
[
224+
['flstnrzae', '1234'],
225+
['flstkennz', '___'],
226+
],
227+
[['flstkennz', '1234___']],
228+
])
229+
})
230+
}

vue2/packages/lib/getFeatures/tests/wfs.spec.ts

Lines changed: 0 additions & 142 deletions
This file was deleted.

0 commit comments

Comments
 (0)