Repository navigation
Expand file tree
/
Copy pathtest_index.py
More file actions
582 lines (473 loc) · 24.6 KB
/
Copy pathtest_index.py
File metadata and controls
582 lines (473 loc) · 24.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# ScanCode is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/nexB/scancode-toolkit for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#
import os
import pytest
from commoncode import fileutils
from commoncode.testcase import FileBasedTesting
from licensedcode import index
from licensedcode import match_seq
from licensedcode import models
from licensedcode.legalese import build_dictionary_from_iterable
from licensedcode.query import Query
from licensedcode.spans import Span
from licensedcode.tracing import get_texts
from licensedcode_test_utils import mini_legalese
from licensedcode_test_utils import create_rule_from_text_file_and_expression
from licensedcode_test_utils import create_rule_from_text_and_expression
def MiniLicenseIndex(*args, **kwargs):
return index.LicenseIndex(*args, _legalese=mini_legalese, **kwargs)
TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
class IndexTesting(FileBasedTesting):
test_data_dir = TEST_DATA_DIR
def get_test_rules(self, rule_data_dir):
"""
Return a list of simple Rules built from plain text files.
"""
rule_data_dir = self.get_test_loc(rule_data_dir)
text_files = sorted(os.listdir(rule_data_dir))
rules = []
for text_file in text_files:
expr = fileutils.file_base_name(text_file)
rule = create_rule_from_text_file_and_expression(
text_file=os.path.join(rule_data_dir, text_file),
license_expression=expr,
)
rules.append(rule)
return rules
class TestIndexing(IndexTesting):
def test_index_structures(self):
# rule text, unique low/high len, low/high len
test_rules = [
(u'a one a two a three licensed.', (4, 1, 4, 1)),
(u'a four a five a six licensed.', (4, 1, 4, 1)),
(u'one two three four five gpl', (6, 0, 6, 0)),
(u'The rose is a rose mit', (4, 0, 5, 0)),
(u'The license is GPL', (4, 1, 4, 1)),
(u'The license is this GPL', (5, 1, 5, 1)),
(u'a license is a rose', (3, 1, 3, 1)),
(u'the gpl', (2, 0, 2, 0)),
(u'the mit', (2, 0, 2, 0)),
(u'the bsd', (2, 0, 2, 0)),
(u'the lgpl', (2, 0, 2, 0)),
]
idx = MiniLicenseIndex()
rules = [create_rule_from_text_and_expression(text=t[0]) for t in test_rules]
idx._add_rules(rules, _legalese=mini_legalese,)
assert idx.len_legalese == 40
expected_lengths = [r[1] for r in test_rules]
results = [
(rule.length_unique, rule.high_length_unique,
rule.length, rule.high_length) for rule in rules]
assert results == expected_lengths
expected = set([
'bsd',
'five',
'four',
'gpl',
'is',
'lgpl',
'mit',
'one',
'rose',
'six',
'the',
'this',
'three',
'two'])
xdict = {key for key, val in idx.dictionary.items() if val >= idx.len_legalese}
assert xdict == expected
xtbi = sorted([
'one',
'two',
'three',
'four',
'five',
'six',
'gpl',
'the',
'rose',
'is',
'mit',
'this',
'bsd',
'lgpl'])
assert sorted([t for i, t in enumerate(idx.tokens_by_tid) if i >= idx.len_legalese]) == xtbi
def test_index_structures_with__add_rules(self):
base = self.get_test_loc('index/tokens_count')
keys = sorted(os.listdir(base))
idx = MiniLicenseIndex()
rules = []
for key in keys:
rules.append(create_rule_from_text_file_and_expression(
text_file=os.path.join(base, key),
license_expression='gpl-2.0',
))
idx._add_rules(rules, _legalese=mini_legalese)
assert idx.len_legalese == 40
expected = set([
'all',
'allowed',
'and',
'any',
'for',
'is',
'redistribution',
'thing',
'yes'])
xdict = {key for key, val in idx.dictionary.items() if val >= idx.len_legalese}
assert xdict == expected
xtbi = sorted([
'all',
'allowed',
'and',
'any',
'for',
'is',
'redistribution',
'thing',
'yes'
])
assert sorted([t for i, t in enumerate(idx.tokens_by_tid) if i >= idx.len_legalese]) == xtbi
expected_msets_by_rid = [
{u'redistribution': 1},
{u'is': 1, u'redistribution': 1, u'yes': 1},
{u'allowed': 1, u'is': 1, u'redistribution': 1, u'yes': 1},
{u'allowed': 1, u'for': 1, u'is': 1, u'redistribution': 1, u'yes': 1},
{u'all': 1, u'allowed': 1, u'for': 1, u'is': 1, u'redistribution': 1},
{u'all': 1,
u'allowed': 1,
u'and': 1,
u'any': 1,
u'is': 1,
u'redistribution': 1,
u'thing': 1},
{u'is': 1, u'redistribution': 1},
{u'allowed': 1, u'is': 1, u'redistribution': 1},
{u'allowed': 1, u'for': 1, u'is': 1, u'redistribution': 1},
{u'all': 1, u'allowed': 1, u'is': 1, u'redistribution': 1, u'yes': 1},
{u'all': 1, u'allowed': 1, u'and': 1, u'is': 1, u'redistribution': 1},
{u'all': 1, u'allowed': 1, u'is': 1, u'redistribution': 1},
{u'all': 1,
u'allowed': 1,
u'and': 1,
u'any': 1,
u'is': 1,
u'redistribution': 1}]
htmset = [{idx.tokens_by_tid[tok]: freq for (tok, freq) in tids_mset.items()}
for tids_mset in idx.msets_by_rid]
assert sorted([sorted(kv.items()) for kv in htmset]) == sorted([sorted(kv.items()) for kv in expected_msets_by_rid])
def test_index_fails_on_duplicated_rules(self):
rule_dir = self.get_test_loc('index/no_duplicated_rule')
try:
MiniLicenseIndex(models.load_rules(rule_dir))
self.fail('Exception on dupes not raised')
except index.DuplicateRuleError as e:
assert 'Duplicate rules' in str(e)
@pytest.mark.scanslow
def test_index_does_not_fail_on_rules_with_similar_normalized_names(self):
rules_dir = self.get_test_loc('index/similar_names/rules')
lics_dir = self.get_test_loc('index/similar_names/licenses')
rules = models.get_rules(licenses_data_dir=lics_dir, rules_data_dir=rules_dir)
index.LicenseIndex(rules)
@pytest.mark.scanslow
def test_index_rules_with_key_phrases_and_without_are_duplicates(self):
rules_dir = self.get_test_loc('index/duplicate-key-phrases/rules')
lics_dir = self.get_test_loc('index/duplicate-key-phrases/licenses')
rules = models.get_rules(licenses_data_dir=lics_dir, rules_data_dir=rules_dir)
try:
index.LicenseIndex(rules)
raise Exception("Exception not raised for duplicated rules")
except index.DuplicateRuleError as e:
assert str(e).startswith('Duplicate rules')
class TestMatchNoTemplates(IndexTesting):
test_data_dir = TEST_DATA_DIR
def test_match_exact_from_string_once(self):
rule_text = 'Redistribution and use in source and binary forms, with or without modification, are permitted'
idx = MiniLicenseIndex([create_rule_from_text_and_expression(text=rule_text, license_expression='bsd')])
querys = '''
The
Redistribution and use in source and binary forms, with or without modification, are permitted.
Always'''
result = idx.match(query_string=querys)
assert len(result) == 1
match = result[0]
qtext, itext = get_texts(match)
assert qtext == 'Redistribution and use in source and binary forms, with or without modification,\nare permitted.'
assert itext == 'redistribution and use in source and binary forms with or without modification\nare permitted'
assert match.qspan == Span(0, 13)
assert match.ispan == Span(0, 13)
def test_match_exact_from_string_twice_with_repeated_text(self):
_text = u'licensed under the GPL, licensed under the GPL'
# 0 1 2 3 4 5 6 7
license_expression = 'tst'
rule = models.Rule(license_expression=license_expression, text=_text)
idx = MiniLicenseIndex([rule])
querys = u'Hi licensed under the GPL, licensed under the GPL yes.'
# 0 1 2 3 4 5 6 7 8 9
result = idx.match(query_string=querys)
assert len(result) == 1
match = result[0]
qtext, itext = get_texts(match)
assert qtext == 'licensed under the GPL, licensed under the GPL'
assert itext == 'licensed under the gpl licensed under the gpl'
assert match.qspan == Span(0, 7)
assert match.ispan == Span(0, 7)
# match again to ensure that there are no state side effects
result = idx.match(query_string=querys)
assert len(result) == 1
match = result[0]
assert match.qspan == Span(0, 7)
assert match.ispan == Span(0, 7)
qtext, itext = get_texts(match)
assert qtext == u'licensed under the GPL, licensed under the GPL'
assert itext == u'licensed under the gpl licensed under the gpl'
def test_match_exact_with_junk_in_between_good_tokens(self):
_text = u'licensed under the GPL, licensed under the GPL'
license_expression = 'tst'
rule = models.Rule(license_expression=license_expression, text=_text)
idx = MiniLicenseIndex([rule])
querys = u'Hi licensed that under is the that GPL, licensed or under not the GPL by yes.'
result = idx.match(query_string=querys)
assert len(result) == 1
match = result[0]
qtext, itext = get_texts(match)
assert qtext == u'licensed [that] under [is] the [that] GPL, licensed [or] under [not] the GPL'
assert itext == u'licensed under the gpl licensed under the gpl'
def test_match_exact_from_file(self):
idx = MiniLicenseIndex(self.get_test_rules('index/mini'))
query_loc = self.get_test_loc('index/queryperfect-mini')
result = idx.match(location=query_loc)
assert len(result) == 1
match = result[0]
qtext, itext = get_texts(match)
assert qtext == 'Redistribution and use in source and binary forms, with or without modification,\nare permitted.'
assert itext == 'redistribution and use in source and binary forms with or without modification\nare permitted'
assert match.qspan == Span(0, 13)
assert match.ispan == Span(0, 13)
def test_match_multiple(self):
test_rules = self.get_test_rules('index/bsd')
idx = MiniLicenseIndex(test_rules)
query = self.get_test_loc('index/querysimple')
result = idx.match(location=query)
assert len(result) == 1
match = result[0]
assert match.qspan == Span(0, 211)
assert match.ispan == Span(0, 211)
def test_match_return_correct_offsets(self):
# notes: A is a stopword. This and that are not
_text = u'This GPL. A MIT. That LGPL.'
# 0 1 2 3 4 5
license_expression = 'tst'
rule = models.Rule(license_expression=license_expression, text=_text)
idx = MiniLicenseIndex([rule])
querys = u'some junk. this GPL. A MIT. that LGPL.'
# 0 1 2 3 4 5 6 7
result = idx.match(query_string=querys)
assert len(result) == 1
match = result[0]
qtext, itext = get_texts(match)
assert qtext == 'this GPL. A MIT. that LGPL.'
assert itext == 'this gpl mit that lgpl'
assert match.qspan == Span(0, 4)
assert match.ispan == Span(0, 4)
class TestMatchWithTemplates(IndexTesting):
test_data_dir = TEST_DATA_DIR
def test_match_with_template_and_multiple_rules(self):
test_rules = self.get_test_rules('index/bsd_templates',)
idx = MiniLicenseIndex(test_rules)
querys = u'''
Hello, what about this
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of nexB Inc. nor the names of its contributors may be
used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Goodbye
No part of match '''
result = idx.match(query_string=querys)
assert len(result) == 1
match = result[0]
assert match.matcher == match_seq.MATCH_SEQ
exp_qtext = u"""
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of [nexB] [Inc]. nor the names of its contributors may be
used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
""".split()
exp_itext = u"""
Redistribution and use in source and binary forms with or without
modification are permitted provided that the following conditions
are met
Redistributions of source code must retain the above copyright
notice this list of conditions and the following disclaimer
Redistributions in binary form must reproduce the above copyright
notice this list of conditions and the following disclaimer in the
documentation and or other materials provided with the distribution
Neither the name of nor the names of its contributors may be
used to endorse or promote products derived from this software
without specific prior written permission
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES INCLUDING BUT NOT
LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
PARTICULAR PURPOSE ARE DISCLAIMED IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT INDIRECT INCIDENTAL
SPECIAL EXEMPLARY OR CONSEQUENTIAL DAMAGES INCLUDING BUT NOT LIMITED
TO PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES LOSS OF USE DATA OR
PROFITS OR BUSINESS INTERRUPTION HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY WHETHER IN CONTRACT STRICT LIABILITY OR TORT INCLUDING
NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
""".lower().split()
qtext, itext = get_texts(match)
assert qtext.split() == exp_qtext
assert itext.split() == exp_itext
assert match.qspan == (Span(1, 72) | Span(74, 211))
assert match.ispan == Span(0, 209)
assert match.coverage() == 100
def test_match_to_indexed_template_with_few_tokens_around_gaps(self):
# Was failing when a gap in a template starts very close to the start of
# a rule tokens seq. We may still skip that, but we capture a large
# match anyway.
rule = create_rule_from_text_file_and_expression(text_file=self.get_test_loc('index/templates/idx.txt'),
license_expression='test')
legalese = build_dictionary_from_iterable(
set(mini_legalese) |
set(['permission', 'written', 'registered', 'derived', 'damage', 'due'])
)
idx = index.LicenseIndex([rule], _legalese=legalese)
query_loc = self.get_test_loc('index/templates/query.txt')
result = idx.match(location=query_loc)
assert len(result) == 1
match = result[0]
exp_qtext = u"""
All Rights Reserved.
Redistribution and use of this software and associated documentation
("Software"), with or without modification, are permitted provided
that the following conditions are met:
1. Redistributions of source code must retain copyright
statements and notices. Redistributions must also contain a
copy of this document.
2. Redistributions in binary form must reproduce the
above copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other
materials provided with the distribution.
3. The name "[groovy]" must not be used to endorse or promote
products derived from this Software without prior written
permission of [The] [Codehaus]. For written permission,
please contact [info]@[codehaus].[org].
4. Products derived from this Software may not be called "[groovy]"
nor may "[groovy]" appear in their names without prior written
permission of [The] [Codehaus]. "[groovy]" is a registered
trademark of [The] [Codehaus].
5. Due credit should be given to [The] [Codehaus] -
[http]://[groovy].[codehaus].[org]/
[THIS] [SOFTWARE] [IS] [PROVIDED] [BY] [THE] [CODEHAUS] AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
[THE] [CODEHAUS] OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE [POSSIBILITY] [OF] [SUCH] DAMAGE.
""".split()
exp_itext = u"""
All Rights Reserved
Redistribution and use of this software and associated documentation
Software with or without modification are permitted provided that
the following conditions are met
1 Redistributions of source code must retain copyright statements
and notices Redistributions must also contain copy of this
document
2 Redistributions in binary form must reproduce the above copyright
notice this list of conditions and the following disclaimer in the
documentation and or other materials provided with the distribution
3 The name must not be used to endorse or promote products
derived from this Software without prior written permission of
For written permission please contact
4 Products derived from this Software may not be called nor
may appear in their names without prior written permission of
is registered trademark of
5 Due credit should be given to
<THIS> <SOFTWARE> <IS> <PROVIDED> <BY>
AND CONTRIBUTORS AS IS AND ANY
EXPRESSED OR IMPLIED WARRANTIES INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR PARTICULAR
PURPOSE ARE DISCLAIMED IN NO EVENT SHALL OR ITS CONTRIBUTORS
BE LIABLE FOR ANY DIRECT INDIRECT INCIDENTAL SPECIAL EXEMPLARY OR
CONSEQUENTIAL DAMAGES INCLUDING BUT NOT LIMITED TO PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES LOSS OF USE DATA OR PROFITS OR BUSINESS
INTERRUPTION HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY WHETHER
IN CONTRACT STRICT LIABILITY OR TORT INCLUDING NEGLIGENCE OR
OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE EVEN IF
ADVISED OF THE DAMAGE
""".lower().split()
qtext, itext = get_texts(match)
assert qtext.split() == exp_qtext
assert itext.split() == exp_itext
assert match.coverage() > 97
assert match.matcher == match_seq.MATCH_SEQ
def test_match_with_templates_with_redundant_tokens_yield_single_exact_match(self):
from licensedcode_test_utils import query_tokens_with_unknowns # NOQA
_text = 'copyright reserved mit is license, copyright reserved mit is license'
# 0 1 2 3 4 5 6 7 8 9
license_expression = 'tst'
rule = models.Rule(license_expression=license_expression, text=_text)
idx = MiniLicenseIndex([rule])
querys = u'Hi my copyright reserved mit is license is the copyright reserved mit is license yes.'
# 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
qry = Query(query_string=querys, idx=idx)
# convert tid to actual token strings
tks_as_str = lambda tks: [None if tid is None else idx.tokens_by_tid[tid] for tid in tks]
expected = [None, None, u'copyright', u'reserved', u'mit', u'is', u'license', u'is', None, u'copyright', u'reserved', u'mit', u'is', u'license', None]
# 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
assert tks_as_str(query_tokens_with_unknowns(qry)) == expected
result = idx.match(query_string=querys)
assert len(result) == 1
match = result[0]
assert match.qspan == Span(0, 4) | Span(6, 10)
assert match.ispan == Span(0, 9)
assert match.coverage() == 100
qtext, itext = get_texts(match)
assert qtext == 'copyright reserved mit is license [is] [the] copyright reserved mit is license'
assert itext == 'copyright reserved mit is license copyright reserved mit is license'