diff options
-rw-r--r-- | archaeological_context_records/tests.py | 27 | ||||
-rw-r--r-- | archaeological_finds/tests.py | 57 | ||||
-rw-r--r-- | archaeological_operations/tests.py | 127 | ||||
-rw-r--r-- | ishtar_common/tests.py | 18 |
4 files changed, 155 insertions, 74 deletions
diff --git a/archaeological_context_records/tests.py b/archaeological_context_records/tests.py index 0ffe04c3a..cd547bc07 100644 --- a/archaeological_context_records/tests.py +++ b/archaeological_context_records/tests.py @@ -43,7 +43,7 @@ from ishtar_common.tests import WizardTest, WizardTestFormData as FormData, \ create_superuser, create_user, TestCase, AutocompleteTestBase, AcItem, \ CONTEXT_RECORD_FIXTURES, CONTEXT_RECORD_TOWNS_FIXTURES, \ OPERATION_TOWNS_FIXTURES, GenericSerializationTest, COMMON_FIXTURES, \ - WAREHOUSE_FIXTURES + WAREHOUSE_FIXTURES, SearchText from archaeological_operations.models import Operation from archaeological_operations.serializers import operation_serialization @@ -489,8 +489,9 @@ class ContextRecordTest(ContextRecordInit, TestCase): self.assertEqual(obj.datings.count(), 2) -class ContextRecordSearchTest(ContextRecordInit, TestCase): +class ContextRecordSearchTest(ContextRecordInit, TestCase, SearchText): fixtures = CONTEXT_RECORD_TOWNS_FIXTURES + SEARCH_URL = 'get-contextrecord' def setUp(self): IshtarSiteProfile.objects.create() @@ -618,7 +619,7 @@ class ContextRecordSearchTest(ContextRecordInit, TestCase): lines = [line for line in content.split('\n') if line] self.assertEqual(len(lines), 3) - def testUnitHierarchicSearch(self): + def test_unit_hierarchic_search(self): cr = self.context_records[0] c = Client() @@ -659,7 +660,16 @@ class ContextRecordSearchTest(ContextRecordInit, TestCase): content = response.content.decode() self.assertEqual(json.loads(content)['recordsTotal'], 1) - def testPeriodHierarchicSearch(self): + # test on text search + material_key = str(pgettext_lazy("key for text search", 'unit-type')) + result = [ + ('{}="{}"'.format(material_key, str(neg)), 1), + ('{}="{}"'.format(material_key, str(dest)), 0), + ('{}="{}"'.format(material_key, str(su)), 1), + ] + self._test_search(c, result, context="Text unit type search") + + def test_period_hierarchic_search(self): cr = self.context_records[0] c = Client() @@ -700,6 +710,15 @@ class ContextRecordSearchTest(ContextRecordInit, TestCase): content = response.content.decode() self.assertEqual(json.loads(content)['recordsTotal'], 1) + # test on text search + period_key = str(pgettext_lazy("key for text search", 'datings-period')) + result = [ + ('{}="{}"'.format(period_key, str(final_neo)), 1), + ('{}="{}"'.format(period_key, str(recent_neo)), 0), + ('{}="{}"'.format(period_key, str(neo)), 1), + ] + self._test_search(c, result, context="Text period search") + class ContextRecordPermissionTest(ContextRecordInit, TestCase): fixtures = CONTEXT_RECORD_TOWNS_FIXTURES diff --git a/archaeological_finds/tests.py b/archaeological_finds/tests.py index ab27a5b01..55006c4ca 100644 --- a/archaeological_finds/tests.py +++ b/archaeological_finds/tests.py @@ -52,7 +52,7 @@ from ishtar_common import forms_common from ishtar_common.tests import WizardTest, WizardTestFormData as FormData, \ TestCase, create_user, create_superuser, AutocompleteTestBase, AcItem, \ FIND_FIXTURES, FIND_TOWNS_FIXTURES, WAREHOUSE_FIXTURES, \ - COMMON_FIXTURES, GenericSerializationTest + COMMON_FIXTURES, GenericSerializationTest, SearchText from archaeological_operations.tests import ImportTest, create_operation from archaeological_context_records.tests import ContextRecordInit @@ -841,9 +841,10 @@ class FindTest(FindInit, TestCase): pk=self.base_finds[0].pk).count(), 0) -class FindSearchTest(FindInit, TestCase): +class FindSearchTest(FindInit, TestCase, SearchText): fixtures = WAREHOUSE_FIXTURES model = models.Find + SEARCH_URL = 'get-find' def setUp(self): self.create_finds(force=True) @@ -896,6 +897,15 @@ class FindSearchTest(FindInit, TestCase): content = response.content.decode() self.assertEqual(json.loads(content)['recordsTotal'], 1) + # test on text search + material_key = str(pgettext_lazy("key for text search", 'material')) + result = [ + ('{}="{}"'.format(material_key, str(iron_metal)), 1), + ('{}="{}"'.format(material_key, str(not_iron_metal)), 0), + ('{}="{}"'.format(material_key, str(metal)), 1), + ] + self._test_search(c, result, context="Text material search") + def test_pinned_search(self): c = Client() c.login(username=self.username, password=self.password) @@ -962,6 +972,15 @@ class FindSearchTest(FindInit, TestCase): self.assertEqual( json.loads(response.content.decode())['recordsTotal'], 1) + # test on text search + period_key = str(pgettext_lazy("key for text search", 'datings-period')) + result = [ + ('{}="{}"'.format(period_key, str(final_neo)), 1), + ('{}="{}"'.format(period_key, str(recent_neo)), 0), + ('{}="{}"'.format(period_key, str(neo)), 1), + ] + self._test_search(c, result, context="Text period search") + def test_conservatory_state_hierarchic_search(self): find = self.finds[0] c = Client() @@ -1005,6 +1024,15 @@ class FindSearchTest(FindInit, TestCase): content = response.content.decode() self.assertEqual(json.loads(content)['recordsTotal'], 1) + # test on text search + key = str(pgettext_lazy("key for text search", 'conservatory')) + result = [ + ('{}="{}"'.format(key, str(cs2)), 1), + ('{}="{}"'.format(key, str(cs3)), 0), + ('{}="{}"'.format(key, str(cs1)), 1), + ] + self._test_search(c, result, context="Text period search") + def test_image_search(self): c = Client() c.login(username=self.username, password=self.password) @@ -1026,19 +1054,6 @@ class FindSearchTest(FindInit, TestCase): res = json.loads(response.content.decode()) self.assertEqual(res['recordsTotal'], 1) - def _test_search(self, client, result, context=""): - for q, expected_result in result: - search = {'search_vector': q} - response = client.get(reverse('get-find'), search) - self.assertEqual(response.status_code, 200) - res = json.loads(response.content.decode()) - msg = "{} result(s) where expected for search: {} - found {}" \ - "".format(expected_result, q, res['recordsTotal']) - if context: - msg = context + " - " + msg - self.assertEqual(res['recordsTotal'], expected_result, - msg=msg) - def test_search_with_callable(self): find = self.finds[0] find2 = self.finds[1] @@ -1048,8 +1063,8 @@ class FindSearchTest(FindInit, TestCase): loan_key = str(pgettext_lazy("key for text search", 'loan')) result = [ - (u'{}="{}"'.format(loan_key, str(_(u"Yes"))), 0), - (u'{}="{}"'.format(loan_key, str(_(u"No"))), 0), + ('{}="{}"'.format(loan_key, str(_("Yes"))), 0), + ('{}="{}"'.format(loan_key, str(_("No"))), 0), ] self._test_search(c, result, context="No container defined") @@ -1142,11 +1157,11 @@ class FindSearchTest(FindInit, TestCase): 'reference-division')) + "-" result = [ - (u'{}="{}"'.format(ref_div_key + "salle", "B2"), 1), - (u'{}="{}"'.format(ref_div_key + "etagere", "A5"), 2), - (u'{}="{}" {}="{}"'.format( + ('{}="{}"'.format(ref_div_key + "salle", "B2"), 1), + ('{}="{}"'.format(ref_div_key + "etagere", "A5"), 2), + ('{}="{}" {}="{}"'.format( ref_div_key + "salle", "B2", ref_div_key + "etagere", "A5"), 1), - (u'{}="{}" {}="{}"'.format( + ('{}="{}" {}="{}"'.format( ref_div_key + "salle", "B*", ref_div_key + "etagere", "A5"), 2), ] c.login(username=self.username, password=self.password) diff --git a/archaeological_operations/tests.py b/archaeological_operations/tests.py index bb8c02c78..7b93c7d5e 100644 --- a/archaeological_operations/tests.py +++ b/archaeological_operations/tests.py @@ -55,7 +55,7 @@ from ishtar_common import forms_common from ishtar_common.tests import WizardTest, WizardTestFormData as FormData, \ create_superuser, create_user, TestCase, OPERATION_FIXTURES, \ AutocompleteTestBase, AcItem, OPERATION_TOWNS_FIXTURES, FILE_FIXTURES, \ - COMMON_FIXTURES, GenericSerializationTest, WAREHOUSE_FIXTURES + COMMON_FIXTURES, GenericSerializationTest, WAREHOUSE_FIXTURES, SearchText from ishtar_common.serializers import restore_serialized @@ -1898,8 +1898,9 @@ class CustomFormTest(TestCase, OperationInitTest): ) -class OperationSearchTest(TestCase, OperationInitTest): +class OperationSearchTest(TestCase, OperationInitTest, SearchText): fixtures = FILE_FIXTURES + SEARCH_URL = 'get-operation' def setUp(self): IshtarSiteProfile.objects.get_or_create( @@ -2027,20 +2028,6 @@ class OperationSearchTest(TestCase, OperationInitTest): result = json.loads(response.content.decode()) self.assertEqual(result['recordsTotal'], 1) - def _test_search(self, c, term, query_string, number, name=""): - q = term - if query_string: - q = u'{}="{}"'.format(term, query_string) - search = {'search_vector': q} - response = c.get(reverse('get-operation'), search) - self.assertEqual(response.status_code, 200) - result = json.loads(response.content.decode()) - if not result: - result = {"recordsTotal": 0} - self.assertEqual(result['recordsTotal'], number, - u"{} - {} - {} result(s) expected got {}".format( - name, q, number, result['recordsTotal'])) - def test_facet_search_vector(self): ope1 = self.operations[0] ope2 = self.operations[1] @@ -2071,49 +2058,76 @@ class OperationSearchTest(TestCase, OperationInitTest): villa = models.RemainType.objects.get(txt_idx='villa') ope1.remains.add(villa) - search_period_q = str(pgettext("key for text search", u"period")) - self._test_search(c, search_period_q, final_neo.label, 1, "Simple") + search_period_q = str(pgettext("key for text search", "period")) + result = [ + ('{}="{}"'.format(search_period_q, str(final_neo)), 1), + ] + self._test_search(c, result, context="Simple period") - search_year_q = str(pgettext("key for text search", u"year")) - self._test_search(c, search_year_q, "2042", 1, "Integer") + search_year_q = str(pgettext("key for text search", "year")) + result = [ + ('{}="{}"'.format(search_year_q, "2042"), 1), + ] + self._test_search(c, result, context="Integer") - self._test_search(c, search_year_q, '2042";"2020', 2, "Many integer") + result = [ + ('{}="{}"'.format(search_year_q, '2042";"2020'), 2), + ] + self._test_search(c, result, context="Many integer") - search_town_q = str(pgettext("key for text search", u"town")) + search_town_q = str(pgettext("key for text search", "town")) town = Town.objects.get(pk=town.pk) - self._test_search(c, search_town_q, town.cached_label, 1, - "String search with parenthesis and minus") - - self._test_search(c, search_period_q, neo.label, 2, "Hierarchic") - - self._test_search(c, u'-{}="{}"'.format(search_period_q, neo.label), "", - 1, "Exclude") + result = [ + ('{}="{}"'.format(search_town_q, town.cached_label), 1), + ] + self._test_search(c, result, + context="String search with parenthesis and minus") + result = [ + ('{}="{}"'.format(search_period_q, str(neo)), 2), + ] + self._test_search(c, result, context="Hierarchic period") - self._test_search(c, search_period_q, - u'{}";"{}'.format(neo.label, gallo.label), - 3, "OR") + result = [ + ('-{}="{}"'.format(search_period_q, str(neo)), 1), + ] + self._test_search(c, result, context="Period exclude") - self._test_search( - c, u'{}="{}" {}="{}"'.format(search_period_q, neo.label, - search_period_q, gallo.label), "", 3, - "OR alt syntax") + result = [ + ('{}="{}"'.format(search_period_q, + '{}";"{}'.format(neo.label, gallo.label)), 3), + ('{}="{}" {}="{}"'.format(search_period_q, neo.label, + search_period_q, gallo.label), 3), + ] + self._test_search(c, result, context="Period OR") # open search '*' + result = [ + ('{}="{}*"'.format(search_period_q, neo.label[:3]), 2), + ] + self._test_search(c, result, context="Period open search") + """ self._test_search(c, search_period_q, u'{}*'.format(neo.label[:3]), 2, "Open search") + """ # non hierarchic search - search_remain_q = str(pgettext("key for text search", u"remain")) - self._test_search(c, search_remain_q, villa.label, - 1, "Non hierarchic search") + search_remain_q = str(pgettext("key for text search", "remain")) + result = [ + ('{}="{}"'.format(search_remain_q, str(villa)), 1), + ] + self._test_search(c, result, + context="Non hierarchic remain search") # boolean search - search_open_q = str(pgettext("key for text search", u"is-open")) - self._test_search(c, search_open_q, u"Yes", 2, "Boolean") + search_open_q = str(pgettext("key for text search", "is-open")) + result = [ + ('{}="{}"'.format(search_open_q, str(_("Yes"))), 2), + ] + self._test_search(c, result, context="Boolean search") def test_mixed_search_vector(self): operation_1 = models.Operation.objects.get(pk=self.operations[0].pk) - operation_1.common_name = u"Opération : Château de Fougères" + operation_1.common_name = "Opération : Château de Fougères" operation_1.year = 2042 operation_1.save() @@ -2149,7 +2163,7 @@ class OperationSearchTest(TestCase, OperationInitTest): reltype_key, rel2.name) } response = c.get(reverse('get-operation'), search) - # no result when no authentification + # no result when no authentication self.assertTrue(not json.loads(response.content.decode())) c.login(username=self.username, password=self.password) response = c.get(reverse('get-operation'), search) @@ -2163,9 +2177,12 @@ class OperationSearchTest(TestCase, OperationInitTest): lbl = "aha = take on me | take me on" ope.common_name = lbl ope.save() - search_name_q = str(pgettext("key for text search", u"name")) - self._test_search(c, search_name_q, lbl, 1, - "Facet search with = and | characters") + search_name_q = str(pgettext("key for text search", "name")) + result = [ + ('{}="{}"'.format(search_name_q, lbl), 1), + ] + self._test_search(c, result, + context="Facet search with = and | characters") def test_search_with_asterisk_inside_names(self): c = Client() @@ -2174,10 +2191,13 @@ class OperationSearchTest(TestCase, OperationInitTest): ope_type = ope.operation_type ope_type.label = 'label*with*asterisk' ope_type.save() - search_name_q = str(pgettext("key for text search", u"type")) + search_name_q = str(pgettext("key for text search", "type")) nb = models.Operation.objects.filter(operation_type=ope_type).count() - self._test_search(c, search_name_q, ope_type.label, nb, - "Facet search with * characters") + result = [ + ('{}="{}"'.format(search_name_q, ope_type.label), nb), + ] + self._test_search(c, result, + context="Facet search with * characters") def test_hierarchic_search(self): ope = self.operations[1] @@ -2216,6 +2236,15 @@ class OperationSearchTest(TestCase, OperationInitTest): self.assertEqual(json.loads(response.content.decode())['recordsTotal'], 1) + # test on text search + period_key = str(pgettext_lazy("key for text search", 'period')) + result = [ + ('{}="{}"'.format(period_key, str(final_neo)), 1), + ('{}="{}"'.format(period_key, str(recent_neo)), 0), + ('{}="{}"'.format(period_key, str(neo)), 1), + ] + self._test_search(c, result, context="Text period search") + def test_town_search(self): c = Client() c.login(username=self.username, password=self.password) diff --git a/ishtar_common/tests.py b/ishtar_common/tests.py index 1d211c91b..122f29d92 100644 --- a/ishtar_common/tests.py +++ b/ishtar_common/tests.py @@ -236,6 +236,24 @@ class UtilsTest(TestCase): self.assertEqual(response.status_code, 302) +class SearchText: + SEARCH_URL = '' + + def _test_search(self, client, result, context=""): + assert self.SEARCH_URL + for q, expected_result in result: + search = {'search_vector': q} + response = client.get(reverse(self.SEARCH_URL), search) + self.assertEqual(response.status_code, 200) + res = json.loads(response.content.decode()) + msg = "{} result(s) where expected for search: {} - found {}" \ + "".format(expected_result, q, res['recordsTotal']) + if context: + msg = context + " - " + msg + self.assertEqual(res['recordsTotal'], expected_result, + msg=msg) + + class CommandsTestCase(TestCase): fixtures = [settings.ROOT_PATH + '../ishtar_common/fixtures/test_towns.json'] |