Skip to content

Commit

Permalink
Ignore case in postprocess_wildcards (#85)
Browse files Browse the repository at this point in the history
* #82 ignore case in postprocess_wildcards, remove reduntant postprocessing
* Add extra tests and fix postfix search. Refs #82

Co-authored-by: Emrys <roefemr@cronos.be>
  • Loading branch information
koenedaele and Emrys committed Jan 21, 2021
1 parent f51948a commit 18e07bf
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 15 deletions.
17 changes: 10 additions & 7 deletions pyramid_skosprovider/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,14 +280,17 @@ def get_conceptscheme_concepts(self):

return self._page_results(concepts)

def _postprocess_wildcards(self, concepts, label):
@staticmethod
def _postprocess_wildcards(concepts, label):
# We need to refine results further
if label.startswith('*') and label.endswith('*'):
concepts = [c for c in concepts if label[1:-1] in c['label']]
elif label.endswith('*'):
concepts = [c for c in concepts if c['label'].startswith(label[0:-1])]
elif label.startswith('*'):
concepts = [c for c in concepts if c['label'].endswith(label[1:])]
if label.startswith('*') and not label.endswith('*'):
concepts = [
c for c in concepts if c['label'].lower().endswith(label[1:].lower())
]
elif label.endswith('*') and not label.startswith('*'):
concepts = [
c for c in concepts if c['label'].lower().startswith(label[:-1].lower())
]
return concepts

def _get_sort_params(self):
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
'uri': 'http://python.com/trees/species',
'labels': [
{'type': 'prefLabel', 'language': 'en', 'label': 'Trees by species'},
{'type': 'altLabel', 'language': 'en', 'label': 'Trees by their species'},
{'type': 'prefLabel', 'language': 'nl', 'label': 'Bomen per soort'},
{'type': 'prefLabel', 'language': 'nl', 'label': 'b'}
],
Expand Down
35 changes: 32 additions & 3 deletions tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,13 +315,12 @@ def test_get_conceptschemes_trees_species_jsonld_url(self):
data2 = json.loads(res2.body.decode('utf-8'))
assert data == data2

def test_get_conceptscheme_concepts_search_dfs_label_star(self):
def test_get_conceptscheme_concepts_search_dfs_label_star_postfix(self):
res = self.testapp.get(
'/conceptschemes/TREES/c?language=nl-BE',
{
'type': 'concept',
'mode': 'dijitFilteringSelect',
'label': 'De *'
'label': 'de *'
},
{ascii_native_('Accept'): ascii_native_('application/json')}
)
Expand All @@ -331,6 +330,36 @@ def test_get_conceptscheme_concepts_search_dfs_label_star(self):
self.assertIsInstance(data, list)
self.assertEqual(2, len(data))

def test_get_conceptscheme_concepts_search_dfs_label_star_prefix(self):
res = self.testapp.get(
'/conceptschemes/TREES/c?language=en',
{
'mode': 'dijitFilteringSelect',
'label': '*nut'
},
{ascii_native_('Accept'): ascii_native_('application/json')}
)
self.assertEqual('200 OK', res.status)
self.assertIn('application/json', res.headers['Content-Type'])
data = json.loads(res.body.decode('utf-8'))
self.assertIsInstance(data, list)
self.assertEqual(1, len(data))

def test_get_conceptscheme_concepts_search_dfs_label_star_prepostfix(self):
res = self.testapp.get(
'/conceptschemes/TREES/c?language=nl-BE',
{
'mode': 'dijitFilteringSelect',
'label': '*Lariks*'
},
{ascii_native_('Accept'): ascii_native_('application/json')}
)
self.assertEqual('200 OK', res.status)
self.assertIn('application/json', res.headers['Content-Type'])
data = json.loads(res.body.decode('utf-8'))
self.assertIsInstance(data, list)
self.assertEqual(1, len(data))

def test_get_conceptscheme_concepts_search_dfs_all(self):
res = self.testapp.get(
'/conceptschemes/TREES/c',
Expand Down
4 changes: 2 additions & 2 deletions tests/test_renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def test_collection_adapter(self):
self.assertIn('label', collection)
self.assertIn('uri', collection)
self.assertEqual(collection['type'], 'collection')
self.assertEqual(len(collection['labels']), 3)
assert 4 == len(collection['labels'])
self._assert_is_labels(collection['labels'])
self.assertIn('notes', collection)
assert collection['infer_concept_relations'] is True
Expand Down Expand Up @@ -201,7 +201,7 @@ def test_json_collection(self):
assert isinstance(coll['label'], text_type)
assert coll['type'] == 'collection'
assert isinstance(coll['labels'], list)
assert len(coll['labels']) == 3
assert 4 == len(coll['labels'])
for l in coll['labels']:
assert 'label' in l
assert 'type' in l
Expand Down
38 changes: 35 additions & 3 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,16 +306,48 @@ def test_get_concepts_search_dfs_all(self):
self.assertIsInstance(concepts, list)
self.assertEqual(2, len(concepts))

def test_get_concepts_search_dfs_label_star(self):
def test_get_concepts_search_dfs_label_star_postfix(self):
request = self._get_dummy_request({
'type': 'concept',
'mode': 'dijitFilteringSelect',
'label': 'De *',
'label': 'soo*',
'language': 'nl-BE'
})
pv = self._get_provider_view(request)
concepts = pv.get_concepts()
self.assertIsInstance(concepts, list)
self.assertEqual(0, len(concepts))

def test_get_concepts_search_dfs_label_star_postfix_en(self):
request = self._get_dummy_request({
'mode': 'dijitFilteringSelect',
'label': 'The*',
'language': 'en'
})
pv = self._get_provider_view(request)
concepts = pv.get_concepts()
self.assertIsInstance(concepts, list)
self.assertEqual(2, len(concepts))

def test_get_concepts_search_dfs_label_star_prefix(self):
request = self._get_dummy_request({
'mode': 'dijitFilteringSelect',
'label': '*kastanje',
'language': 'nl-BE'
})
pv = self._get_provider_view(request)
concepts = pv.get_concepts()
self.assertIsInstance(concepts, list)
self.assertEqual(1, len(concepts))

def test_get_concepts_search_dfs_label_star_allfix(self):
request = self._get_dummy_request({
'mode': 'dijitFilteringSelect',
'label': '*ch*',
'language': 'en'
})
pv = self._get_provider_view(request)
concepts = pv.get_concepts()
self.assertIsInstance(concepts, list)
self.assertEqual(2, len(concepts))

def test_get_conceptscheme_concepts(self):
Expand Down

0 comments on commit 18e07bf

Please sign in to comment.