def test_too_long_excerpt(self): """ test that we shorten an excerpt that is too long appropriately """ test_string = ( u"Here is a note about الاستحسان and it is very long - more than the desirable length of 100" u" characters - indeed this should show up but it should trim the characters around in" u" order to show the selected text in bold" ) test_result = { "content": { "notes": test_string, } } srp = SearchResultProcessor(test_result, u"الاستحسان") test_string_compare = SearchResultProcessor.decorate_matches(test_string, u"الاستحسان") excerpt = srp.excerpt self.assertNotEqual(excerpt, test_string_compare) self.assertIn(u"note about <b>الاستحسان</b> and it is", excerpt) test_string = ( u"Here is a note about stuff and it is very long - more than the desirable length of 100" u" characters - indeed this should show up but it should trim the الاستحسان characters around in" u" order to show the selected text in bold" ) test_result = { "content": { "notes": test_string, } } srp = SearchResultProcessor(test_result, u"الاستحسان") test_string_compare = SearchResultProcessor.decorate_matches(test_string, u"الاستحسان") excerpt = srp.excerpt self.assertNotEqual(excerpt, test_string_compare) self.assertIn(u"should trim the <b>الاستحسان</b> characters around", excerpt)
def test_find_matches(self): """ test finding matches """ words = ["hello"] strings = [ "hello there", "goodbye", "Sail away to say HELLO", ] matches = SearchResultProcessor.find_matches(strings, words, 100) self.assertEqual(matches, [strings[0], strings[2]]) words = ["hello", "there"] strings = [ "hello there", "goodbye", "Sail away to say HELLO", ] matches = SearchResultProcessor.find_matches(strings, words, 100) self.assertEqual(matches, [strings[0], strings[2]]) words = ["hello", "there"] strings = [ "hello there", "goodbye there", "Sail away to say HELLO", ] matches = SearchResultProcessor.find_matches(strings, words, 100) self.assertEqual(matches, strings) words = ["goodbye there", "goodbye", "there"] strings = [ "goodbye", "goodbye there", "Sail away to say GOODBYE", ] matches = SearchResultProcessor.find_matches(strings, words, 100) self.assertEqual(matches, strings) words = ["none of these are present"] strings = [ "goodbye", "goodbye there", "Sail away to say GOODBYE", ] matches = SearchResultProcessor.find_matches(strings, words, 100) self.assertEqual(len(matches), 0)
def test_property_error(self): """ result should be removed from list if there is an error in the handler properties """ test_result = { "not_course": "asdasda", "not_id": "rthrthretht" } new_result = SearchResultProcessor.process_result(test_result, "fake search pattern", None) self.assertIsNone(new_result)
def test_strings_in_dictionary(self): """ Test finding strings within dictionary item """ test_dict = { "a": "This is a string that should show up" } get_strings = SearchResultProcessor.strings_in_dictionary(test_dict) self.assertEqual(len(get_strings), 1) self.assertEqual(get_strings[0], test_dict["a"]) test_dict.update({ "b": "This is another string that should show up" }) get_strings = SearchResultProcessor.strings_in_dictionary(test_dict) self.assertEqual(len(get_strings), 2) self.assertEqual(get_strings[0], test_dict["a"]) self.assertEqual(get_strings[1], test_dict["b"]) test_dict.update({ "CASCADE": { "z": "This one should be found too" } }) get_strings = SearchResultProcessor.strings_in_dictionary(test_dict) self.assertEqual(len(get_strings), 3) self.assertEqual(get_strings[0], test_dict["a"]) self.assertEqual(get_strings[1], test_dict["b"]) self.assertEqual(get_strings[2], test_dict["CASCADE"]["z"]) test_dict.update({ "DEEP": { "DEEPER": { "STILL_GOING": { "MORE": { "here": "And here, again and again" } } } } }) get_strings = SearchResultProcessor.strings_in_dictionary(test_dict) self.assertEqual(len(get_strings), 4) self.assertEqual(get_strings[0], test_dict["a"]) self.assertEqual(get_strings[1], test_dict["b"]) self.assertEqual(get_strings[2], test_dict["CASCADE"]["z"]) self.assertEqual(get_strings[3], test_dict["DEEP"]["DEEPER"]["STILL_GOING"]["MORE"]["here"])
def test_removal(self): """ make sure that the override of should remove let's the application prevent access to a result """ test_result = { "course": "remove_course", "id": "remove_id", "remove_me": True } new_result = SearchResultProcessor.process_result(test_result, "fake search pattern", None) self.assertIsNone(new_result)
def test_too_long_find_matches(self): """ make sure that we keep the expert snippets short enough """ words = ["edx", "afterward"] strings = [ ("Here is a note about edx and it is very long - more than the desirable length of 100 characters" " - indeed this should show up"), "This matches too but comes afterward", ] matches = SearchResultProcessor.find_matches(strings, words, 100) self.assertEqual(len(matches), 1)
def test_additional_property(self): """ make sure the addition properties are returned """ test_result = { "course": "testmetestme", "id": "herestheid" } new_result = SearchResultProcessor.process_result(test_result, "fake search pattern", None) self.assertEqual(new_result, test_result) self.assertEqual(test_result["url"], "/courses/testmetestme/jump_to/herestheid") self.assertIsNone(test_result["excerpt"]) self.assertEqual(test_result["additional_property"], "Should have an extra value")
def test_excerpt_front(self): """ test that we process correctly when match is at the front of the excerpt """ test_result = { "content": { "notes": "Dog - match upon first word", } } srp = SearchResultProcessor(test_result, "dog") self.assertEqual(srp.excerpt, "<b>Dog</b> - match upon first word") test_result = { "content": { "notes": ("Dog - match upon first word " "The long and winding road " "That leads to your door " "Will never disappear ..."), } } srp = SearchResultProcessor(test_result, "dog") self.assertEqual(srp.excerpt[0:34], "<b>Dog</b> - match upon first word")
def test_strings_in_dictionary(self): """ Test finding strings within dictionary item """ test_dict = {"a": "This is a string that should show up"} get_strings = SearchResultProcessor.strings_in_dictionary(test_dict) self.assertEqual(len(get_strings), 1) self.assertEqual(get_strings[0], test_dict["a"]) test_dict.update({"b": "This is another string that should show up"}) get_strings = SearchResultProcessor.strings_in_dictionary(test_dict) self.assertEqual(len(get_strings), 2) self.assertIn(test_dict["a"], get_strings) self.assertIn(test_dict["b"], get_strings) test_dict.update({"CASCADE": {"z": "This one should be found too"}}) get_strings = SearchResultProcessor.strings_in_dictionary(test_dict) self.assertEqual(len(get_strings), 3) self.assertIn(test_dict["a"], get_strings) self.assertIn(test_dict["b"], get_strings) self.assertIn(test_dict["CASCADE"]["z"], get_strings) test_dict.update({ "DEEP": { "DEEPER": { "STILL_GOING": { "MORE": { "here": "And here, again and again" } } } } }) get_strings = SearchResultProcessor.strings_in_dictionary(test_dict) self.assertEqual(len(get_strings), 4) self.assertIn(test_dict["a"], get_strings) self.assertIn(test_dict["b"], get_strings) self.assertIn(test_dict["CASCADE"]["z"], get_strings) self.assertIn( test_dict["DEEP"]["DEEPER"]["STILL_GOING"]["MORE"]["here"], get_strings)
def test_excerpt_back(self): """ test that we process correctly when match is at the end of the excerpt """ test_result = { "content": { "notes": "Match upon last word - Dog", } } srp = SearchResultProcessor(test_result, "dog") self.assertEqual(srp.excerpt, "Match upon last word - <b>Dog</b>") test_result = { "content": { "notes": ( "The long and winding road " "That leads to your door ..." "... Yeah, yeah, yeah, yeah " "Match upon last word - Dog" ), } } srp = SearchResultProcessor(test_result, "dog") self.assertEqual(srp.excerpt[-33:], "Match upon last word - <b>Dog</b>")
def test_excerpt_quoted(self, search_phrase, expected_excerpt): test_result = { "content": { "notes": ("The long and winding road " "That leads to your door " "Will never disappear " "I've seen that road before " "It always leads me here " "यह एक हिंदी में लिखा हुआ वाक्य है| इसको search करें| " "Lead me to you door " "The wild and windy night " "That the rain washed away "), } } srp = SearchResultProcessor(test_result, search_phrase) self.assertIn(expected_excerpt, srp.excerpt)
def test_property_error(self): """ result should be removed from list if there is an error in the handler properties """ test_result = {"not_course": "asdasda", "not_id": "rthrthretht"} new_result = SearchResultProcessor.process_result( test_result, "fake search pattern", None) self.assertIsNone(new_result)