Skip to content

Commit efd59d2

Browse files
committed
chore: better trovesearch tests, factories
1 parent af289b4 commit efd59d2

File tree

8 files changed

+361
-100
lines changed

8 files changed

+361
-100
lines changed

tests/share/search/__init__.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,25 @@
33
from typing import Iterable
44
from unittest import mock
55

6-
from share.search import index_strategy
7-
86

97
@contextlib.contextmanager
10-
def patch_index_strategies(strategies: Iterable[index_strategy.IndexStrategy]):
8+
def patch_index_strategies(strategies: Iterable):
9+
from share.search import index_strategy
1110
with mock.patch.object(index_strategy, '_AvailableStrategies', new=enum.Enum(
1211
'_AvailableStrategies', [
1312
(_strategy.strategy_name, _strategy)
1413
for _strategy in strategies
1514
],
1615
)):
1716
yield
17+
18+
19+
@contextlib.contextmanager
20+
def patch_index_strategy(strategy):
21+
from share.search import index_strategy as _module_to_patch
22+
with (
23+
mock.patch.object(_module_to_patch, 'all_strategy_names', return_value=frozenset([strategy.strategy_name])),
24+
mock.patch.object(_module_to_patch, 'each_strategy', return_value=[strategy]),
25+
mock.patch.object(_module_to_patch, 'get_strategy', return_value=strategy),
26+
):
27+
yield

tests/share/search/index_strategy/_common_trovesearch_tests.py

Lines changed: 61 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
create_indexcard,
1616
update_indexcard_content,
1717
create_supplement,
18+
index_indexcards,
1819
)
1920

2021
from ._with_real_services import RealElasticTestCase
@@ -30,7 +31,7 @@ def setUp(self):
3031
def test_for_smoke_without_daemon(self):
3132
_indexcard = self._create_indexcard(
3233
focus_iri=BLARG.hello,
33-
rdf_tripledict={BLARG.hello: {RDFS.label: {rdf.literal('hello')}}},
34+
rdf_twopledict={RDFS.label: {rdf.literal('hello')}},
3435
)
3536
_messages_chunk = messages.MessagesChunk(
3637
messages.MessageType.UPDATE_INDEXCARD,
@@ -44,7 +45,7 @@ def test_for_smoke_without_daemon(self):
4445
def test_for_smoke_with_daemon(self):
4546
_indexcard = self._create_indexcard(
4647
focus_iri=BLARG.hello,
47-
rdf_tripledict={BLARG.hello: {RDFS.label: {rdf.literal('hello')}}},
48+
rdf_twopledict={RDFS.label: {rdf.literal('hello')}},
4849
)
4950
_messages_chunk = messages.MessagesChunk(
5051
messages.MessageType.UPDATE_INDEXCARD,
@@ -78,11 +79,9 @@ def test_cardsearch_after_deletion(self):
7879
def test_cardsearch_after_updates(self):
7980
_cards = self._fill_test_data_for_querying()
8081
self._update_indexcard_content(_cards[BLARG.c], BLARG.c, {
81-
BLARG.c: {
82-
RDF.type: {BLARG.Thing},
83-
DCTERMS.subject: {BLARG.subj_ac, BLARG.subj_c}, # subj_bc removed; subj_c added
84-
DCTERMS.title: {rdf.literal('cccc')},
85-
},
82+
RDF.type: {BLARG.Thing},
83+
DCTERMS.subject: {BLARG.subj_ac, BLARG.subj_c}, # subj_bc removed; subj_c added
84+
DCTERMS.title: {rdf.literal('cccc')},
8685
})
8786
self._index_indexcards([_cards[BLARG.c]])
8887
_cases = [
@@ -112,11 +111,9 @@ def test_cardsearch_pagination(self):
112111
_focus_iri = BLARG[f'i{_i}']
113112
_expected_iris.add(_focus_iri)
114113
_cards.append(self._create_indexcard(_focus_iri, {
115-
_focus_iri: {
116-
RDF.type: {BLARG.Thing},
117-
DCTERMS.title: {rdf.literal(f'card #{_i}')},
118-
DCTERMS.created: {rdf.literal(_start_date + timedelta(weeks=_i, days=_i))},
119-
},
114+
RDF.type: {BLARG.Thing},
115+
DCTERMS.title: {rdf.literal(f'card #{_i}')},
116+
DCTERMS.created: {rdf.literal(_start_date + timedelta(weeks=_i, days=_i))},
120117
}))
121118
self._index_indexcards(_cards)
122119
# gather all pages results:
@@ -187,12 +184,10 @@ def test_valuesearch_after_deletion(self):
187184
def test_valuesearch_after_updates(self):
188185
_cards = self._fill_test_data_for_querying()
189186
self._update_indexcard_content(_cards[BLARG.c], BLARG.c, {
190-
BLARG.c: {
191-
RDF.type: {BLARG.Thing},
192-
DCTERMS.creator: {BLARG.someone_new}, # someone_else removed; someone_new added
193-
DCTERMS.subject: {BLARG.subj_ac, BLARG.subj_c, BLARG.subj_new}, # subj_bc removed; subj_new added
194-
DCTERMS.title: {rdf.literal('cccc')},
195-
},
187+
RDF.type: {BLARG.Thing},
188+
DCTERMS.creator: {BLARG.someone_new}, # someone_else removed; someone_new added
189+
DCTERMS.subject: {BLARG.subj_ac, BLARG.subj_c, BLARG.subj_new}, # subj_bc removed; subj_new added
190+
DCTERMS.title: {rdf.literal('cccc')},
196191
})
197192
self._index_indexcards([_cards[BLARG.c]])
198193
_cases = [
@@ -239,16 +234,15 @@ def _assert_valuesearch_values(self, queryparams, expected_values):
239234

240235
def _fill_test_data_for_querying(self):
241236
_card_a = self._create_indexcard(BLARG.a, {
242-
BLARG.a: {
243-
RDF.type: {BLARG.Thing},
244-
OWL.sameAs: {BLARG.a_same, BLARG.a_same2},
245-
DCTERMS.created: {rdf.literal(date(1999, 12, 31))},
246-
DCTERMS.creator: {BLARG.someone},
247-
DCTERMS.title: {rdf.literal('aaaa')},
248-
DCTERMS.subject: {BLARG.subj_ac, BLARG.subj_a},
249-
DCTERMS.references: {BLARG.b, BLARG.c},
250-
DCTERMS.description: {rdf.literal('This place is not a place of honor... no highly esteemed deed is commemorated here... nothing valued is here.', language='en')},
251-
},
237+
RDF.type: {BLARG.Thing},
238+
OWL.sameAs: {BLARG.a_same, BLARG.a_same2},
239+
DCTERMS.created: {rdf.literal(date(1999, 12, 31))},
240+
DCTERMS.creator: {BLARG.someone},
241+
DCTERMS.title: {rdf.literal('aaaa')},
242+
DCTERMS.subject: {BLARG.subj_ac, BLARG.subj_a},
243+
DCTERMS.references: {BLARG.b, BLARG.c},
244+
DCTERMS.description: {rdf.literal('This place is not a place of honor... no highly esteemed deed is commemorated here... nothing valued is here.', language='en')},
245+
}, rdf_tripledict={
252246
BLARG.someone: {
253247
FOAF.name: {rdf.literal('some one')},
254248
},
@@ -265,16 +259,15 @@ def _fill_test_data_for_querying(self):
265259
},
266260
})
267261
_card_b = self._create_indexcard(BLARG.b, {
268-
BLARG.b: {
269-
RDF.type: {BLARG.Thing},
270-
OWL.sameAs: {BLARG.b_same},
271-
DCTERMS.created: {rdf.literal(date(2012, 12, 31))},
272-
DCTERMS.creator: {BLARG.someone},
273-
DCTERMS.title: {rdf.literal('bbbb')},
274-
DCTERMS.subject: {BLARG.subj_b, BLARG.subj_bc},
275-
DCTERMS.references: {BLARG.c},
276-
DCTERMS.description: {rdf.literal('What is here was dangerous and repulsive to us. This message is a warning about danger. ', language='en')},
277-
},
262+
RDF.type: {BLARG.Thing},
263+
OWL.sameAs: {BLARG.b_same},
264+
DCTERMS.created: {rdf.literal(date(2012, 12, 31))},
265+
DCTERMS.creator: {BLARG.someone},
266+
DCTERMS.title: {rdf.literal('bbbb')},
267+
DCTERMS.subject: {BLARG.subj_b, BLARG.subj_bc},
268+
DCTERMS.references: {BLARG.c},
269+
DCTERMS.description: {rdf.literal('What is here was dangerous and repulsive to us. This message is a warning about danger. ', language='en')},
270+
}, rdf_tripledict={
278271
BLARG.someone: {
279272
FOAF.name: {rdf.literal('some one')},
280273
},
@@ -285,44 +278,37 @@ def _fill_test_data_for_querying(self):
285278
},
286279
})
287280
_card_c = self._create_indexcard(BLARG.c, {
288-
BLARG.c: {
289-
RDF.type: {BLARG.Thing},
290-
DCTERMS.created: {rdf.literal(date(2024, 12, 31))},
291-
DCTERMS.creator: {BLARG.someone_else},
292-
DCTERMS.title: {rdf.literal('cccc')},
293-
DCTERMS.subject: {
294-
BLARG['subj_ac/'], # this one has an extra trailing slash
295-
BLARG.subj_bc,
296-
BLARG.subj_c,
297-
},
298-
DCTERMS.description: {rdf.literal('The danger is unleashed only if you substantially disturb this place physically. This place is best shunned and left uninhabited.', language='en')},
281+
RDF.type: {BLARG.Thing},
282+
DCTERMS.created: {rdf.literal(date(2024, 12, 31))},
283+
DCTERMS.creator: {BLARG.someone_else},
284+
DCTERMS.title: {rdf.literal('cccc')},
285+
DCTERMS.subject: {
286+
BLARG['subj_ac/'], # this one has an extra trailing slash
287+
BLARG.subj_bc,
288+
BLARG.subj_c,
299289
},
290+
DCTERMS.description: {rdf.literal('The danger is unleashed only if you substantially disturb this place physically. This place is best shunned and left uninhabited.', language='en')},
291+
}, rdf_tripledict={
300292
BLARG.someone_else: {
301293
FOAF.name: {rdf.literal('some one else')},
302294
},
303295
})
304296
create_supplement(_card_a, BLARG.a, {
305-
BLARG.a: {
306-
DCTERMS.replaces: {BLARG.a_past},
307-
DCAT.servesDataset: {
308-
rdf.blanknode({DCAT.spatialResolutionInMeters: {rdf.literal(10)}}),
309-
},
297+
DCTERMS.replaces: {BLARG.a_past},
298+
DCAT.servesDataset: {
299+
rdf.blanknode({DCAT.spatialResolutionInMeters: {rdf.literal(10)}}),
310300
},
311301
})
312302
create_supplement(_card_b, BLARG.b, {
313-
BLARG.b: {
314-
DCTERMS.replaces: {BLARG.b_past},
315-
DCAT.servesDataset: {
316-
rdf.blanknode({DCAT.spatialResolutionInMeters: {rdf.literal(7)}}),
317-
},
303+
DCTERMS.replaces: {BLARG.b_past},
304+
DCAT.servesDataset: {
305+
rdf.blanknode({DCAT.spatialResolutionInMeters: {rdf.literal(7)}}),
318306
},
319307
})
320308
create_supplement(_card_c, BLARG.c, {
321-
BLARG.c: {
322-
DCTERMS.replaces: {BLARG.c_past},
323-
DCAT.servesDataset: {
324-
rdf.blanknode({DCAT.spatialResolutionInMeters: {rdf.literal(333)}}),
325-
},
309+
DCTERMS.replaces: {BLARG.c_past},
310+
DCAT.servesDataset: {
311+
rdf.blanknode({DCAT.spatialResolutionInMeters: {rdf.literal(333)}}),
326312
},
327313
})
328314
_cards = {
@@ -608,30 +594,28 @@ def valuesearch_sameas_cases(self):
608594
{BLARG.subj_ac, BLARG.subj_a, BLARG.subj_c, BLARG.subj_bc},
609595
)
610596

611-
def _create_indexcard(self, focus_iri: str, rdf_tripledict: rdf.RdfTripleDictionary) -> trove_db.Indexcard:
612-
_indexcard = create_indexcard(focus_iri, rdf_tripledict, (TROVE['derive/osfmap_json'],))
597+
def _create_indexcard(
598+
self,
599+
focus_iri: str,
600+
rdf_twopledict: rdf.RdfTwopleDictionary | None = None,
601+
rdf_tripledict: rdf.RdfTripleDictionary | None = None,
602+
) -> trove_db.Indexcard:
603+
_indexcard = create_indexcard(focus_iri, rdf_twopledict, rdf_tripledict, (TROVE['derive/osfmap_json'],))
613604
self._indexcard_focus_by_uuid[str(_indexcard.uuid)] = focus_iri
614605
return _indexcard
615606

616607
def _update_indexcard_content(
617608
self,
618609
indexcard: trove_db.Indexcard,
619610
focus_iri: str,
620-
rdf_tripledict: rdf.RdfTripleDictionary,
611+
rdf_twopledict: rdf.RdfTwopleDictionary | None = None,
612+
rdf_tripledict: rdf.RdfTripleDictionary | None = None,
621613
) -> None:
622-
update_indexcard_content(indexcard, focus_iri, rdf_tripledict)
614+
update_indexcard_content(indexcard, focus_iri, rdf_twopledict, rdf_tripledict)
623615
self._indexcard_focus_by_uuid[str(indexcard.uuid)] = focus_iri
624616

625617
def _index_indexcards(self, indexcards: Iterable[trove_db.Indexcard]):
626-
_messages_chunk = messages.MessagesChunk(
627-
messages.MessageType.UPDATE_INDEXCARD,
628-
[_indexcard.id for _indexcard in indexcards],
629-
)
630-
self.assertTrue(all(
631-
_response.is_done
632-
for _response in self.index_strategy.pls_handle_messages_chunk(_messages_chunk)
633-
))
634-
self.index_strategy.pls_refresh()
618+
index_indexcards(self.index_strategy, indexcards)
635619

636620
def _delete_indexcards(self, indexcards: Iterable[trove_db.Indexcard]):
637621
for _indexcard in indexcards:

tests/share/search/index_strategy/_with_real_services.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import abc
12
import contextlib
23
from unittest import mock
34

@@ -8,25 +9,29 @@
89
from share.search.daemon import IndexerDaemonControl
910
from share.search.index_messenger import IndexMessenger
1011
from share.search import index_strategy
11-
from tests.share.search import patch_index_strategies
12+
from tests.share.search import patch_index_strategy
1213

1314

1415
# base class for testing IndexStrategy subclasses with actual elasticsearch.
1516
# (using TransactionTestCase so there's NOT a transaction wrapping each test
1617
# and IndexerDaemon can use a separate db connection from a separate thread)
17-
class RealElasticTestCase(TransactionTestCase):
18+
class RealElasticTestCase(TransactionTestCase, abc.ABC):
1819
serialized_rollback = True # for TransactionTestCase; restore db after
1920

20-
# required for subclasses
21+
@abc.abstractmethod
2122
def get_index_strategy(self) -> index_strategy.IndexStrategy:
23+
'''return an IndexStrategy instance that will be tested
24+
25+
override in subclasses to reuse these tests
26+
'''
2227
raise NotImplementedError(f'{self.__class__} must implement `get_index_strategy`')
2328

2429
def setUp(self):
2530
super().setUp()
2631
self.enterContext(mock.patch('share.models.core._setup_user_token_and_groups'))
2732
self.index_strategy = self.get_index_strategy()
2833
self.index_strategy.pls_teardown() # in case it already exists
29-
self.enterContext(patch_index_strategies([self.index_strategy]))
34+
self.enterContext(patch_index_strategy(self.index_strategy))
3035
self.index_messenger = IndexMessenger(
3136
celery_app=celery_app,
3237
index_strategys=[self.index_strategy],

tests/share/search/index_strategy/test_sharev2_elastic5.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@ def setUp(self):
2424
self.__indexcard = create_indexcard(
2525
BLARG.hello,
2626
{
27-
BLARG.hello: {
28-
RDF.type: {SHAREv2.CreativeWork},
29-
DCTERMS.title: {rdf.literal('hello', language='en')},
30-
},
27+
RDF.type: {SHAREv2.CreativeWork},
28+
DCTERMS.title: {rdf.literal('hello', language='en')},
3129
},
3230
deriver_iris=[SHAREv2.sharev2_elastic],
3331
)

tests/share/search/index_strategy/test_sharev2_elastic8.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ def setUp(self):
1717
self.__indexcard = create_indexcard(
1818
BLARG.hello,
1919
{
20-
BLARG.hello: {
21-
RDF.type: {SHAREv2.CreativeWork},
22-
DCTERMS.title: {rdf.literal('hello', language='en')},
23-
},
20+
RDF.type: {SHAREv2.CreativeWork},
21+
DCTERMS.title: {rdf.literal('hello', language='en')},
2422
},
2523
deriver_iris=[SHAREv2.sharev2_elastic],
2624
)

0 commit comments

Comments
 (0)