def test_daytext(get_data_method, hmonths, hmain): m = martyrology.Martyrology() for lang in ('fr', 'la'): result = m.daytext(datetime.date.today(), lang) assert isinstance(result, martyrology.TextResult) assert mock.call(lang) in get_data_method.call_args_list assert result.last_sentence == martyrology.Martyrology._last_line[lang] assert result.matching_line is None assert get_data_method.call_count == 2 assert hmain.call_count == 2 assert hmonths.__getitem__.call_count == 2 result = m.daytext(datetime.date.today(), lang, 2) assert result.matching_line == 2 # 2 or 3 of November result = m.daytext(datetime.date(2014, 11, 3), 'fr') get_data_method().__getitem__.assert_called_with('faithful_departed') result = m.daytext(datetime.date(2017, 11, 2), 'fr') get_data_method().__getitem__.assert_called_with('faithful_departed') # leap year for day in range(25, 30): result = m.daytext(datetime.date(2000, 2, day), 'la') get_data_method().__getitem__('data').__getitem__( 1).__getitem__.assert_called_with(day - 2) for day in range(25, 29): result = m.daytext(datetime.date(2001, 2, day), 'la') get_data_method().__getitem__('data').__getitem__( 1).__getitem__.assert_called_with(day - 1)
def test_get_data(patch_pickle): m = martyrology.Martyrology() lang = 'fr' m._get_data(lang) patch_pickle.Unpickler.assert_any_call assert patch_pickle.Unpickler.call_count == 1 m._get_data(lang) assert patch_pickle.Unpickler.call_count == 1
def test_raw_kw(mock_matcher, get_data_method): matcher_instance = mock_matcher() matcher_instance.is_score_low.return_value = False matcher_instance.fuzzer.return_value = 1 lines = [ 'a', 'ba', 'caa', 'daaa', 'bbaaa', 'ccaaaa', ] get_data_method('lang').__getitem__.return_value = [ [['a', 'ba', 'caa']], [['daaa', 'bbaaa', 'ccaaaa']], ] tokens = ['token1', 'token2'] lang = 'ja' m = martyrology.Martyrology() res = m._raw_kw(tokens, lang) mock_matcher.assert_called_with(tokens, lang) get_data_method.assert_called_with(lang) assert get_data_method().__getitem__.call_args_list == [mock.call('data')] assert matcher_instance.fuzzer.call_count == 6 assert matcher_instance.fuzzer.call_args_list == [ mock.call(elt) for elt in lines ] assert matcher_instance.is_score_low.call_count == 1 get_data_method('lang').__getitem__.return_value = [[['a', 'ba', 'caa']], [[ 'daaa', 'bbaaa', 'ccaaaa' ]], [['c', 'b', 'j']], [['h', 'b', 'r', 't']]] matcher_instance.fuzzer = lambda x: len(x) res = m._raw_kw(tokens, lang, max_nb_returned=3) assert res[0].ratio == 6 and res[1].ratio == 3 assert len(res) == 3 res = m._raw_kw(tokens, lang, min_ratio=3) assert len(res) == 2 assert res[0].ratio >= res[1].ratio >= 3 def is_score_low(x=[2]): x[0] -= 1 return x[0] matcher_instance.is_score_low = is_score_low matcher_instance.fuzzer = mock.MagicMock() matcher_instance.fuzzer.return_value = 0.85 res = m._raw_kw(tokens, lang) assert matcher_instance.splitter.call_count == 1 assert matcher_instance.fuzzer.call_count == 13 * 2
def test_kw(_raw_kw, daytext): item = mock.MagicMock() item.month = 1 item.day = 10 item.matching_line = 2 _raw_kw.return_value = [item] m = martyrology.Martyrology() tokens = ['token1', 'token2'] lang = 'ja' m.kw(tokens, lang, year=2000) _raw_kw.assert_called_once_with(tokens, lang, -1, .80) daytext.assert_called_once_with(datetime.date(2000, item.month, item.day), lang, item.matching_line)
def test_init(listdir): listdir.return_value = [ "fr_roman_martyrology_{}.pkl".format(year) for year in range(1950, 1970) ] + [ "en_roman_martyrology_{}.pkl".format(year) for year in range(1950, 1970) ] + [ "la_roman_martyrology_{}.pkl".format(year) for year in range(1950, 1970) ] file_path = martyrology.file_path m = martyrology.Martyrology() assert m._data == { lang: file_path + 'data/' + '{}_roman_martyrology_1962.pkl'.format(lang) for lang in ('fr', 'en', 'la') } m = martyrology.Martyrology(1965) assert m._data == { lang: file_path + 'data/' + '{}_roman_martyrology_1965.pkl'.format(lang) for lang in ('fr', 'en', 'la') }
import sys from .forms import * # Create your views here. chemin = os.path.dirname(os.path.abspath(__file__)) programme = os.path.abspath(chemin + '/../..') sys.path.append(programme) import annus import adjutoria import exporter import martyrology import officia martyrology_instance = martyrology.Martyrology() host = os.environ.get("THEHOST","localhost:8000") liturgical_calendar = annus.LiturgicalCalendar(proper='roman') s=os.environ.get("SECURE",'') if s is None: s='s' #load raw widgets fpath = os.path.abspath(chemin + "/../spill/static/spill") + "/" unformated_widgets = {} for filename in ("widget_day","widget_day_mobile"): with open(fpath + filename) as f: unformated_widgets[filename] = f.read() def home(request, recherche_mot_clef=RechercheMotClef(None),recherche_simple=RechercheSimple(None),mois_entier=MoisEntier(None),mois_seul=False,
class DisplayMartyrology(QW.QTextEdit, translation.SuperTranslator): """Class that displays the roman martyrology""" martyrology = martyrology_module.Martyrology() def __init__(self, parent): QW.QTextEdit.__init__(self) translation.SuperTranslator.__init__(self) translation.SuperTranslator.retranslateUI(self) self.parent = parent # main window self.initUI() def __call__(self, start: datetime.date, end=None, kw=None, max_result=5, rate=80, span=None): """Function called to display martyrology start must be an int if used with kw. Lang is found in locale if kw is not None, it is a list of keywords max_result and rate are used only with keywords span is used to change the state of main window. If set, it is a string: day, week, month, year, arbitrary""" if kw is not None: text, title, state = self._kw_search(kw, start, max_result, rate) else: text, title, state = self._date_search(start, end, span) # setting display self.setHtml(text) self.parent.setWindowTitle(title) self.parent.setCentralWidget(self, type="martyrology", **state) def _kw_search(self, kw: list, year: int, max_result: int, rate: 80) -> tuple: """Handles the keyword research. max_result determines the max number of texts returned. rate determines the minimum matching rate. return the text and the title to set return the state of the main window""" lang = self.parent.lang rate = rate / 100 results = self.martyrology.kw(kw, lang, max_nb_returned=max_result, min_ratio=rate, year=year) title = " ".join(kw) text = "" for elt in results: text += self._format_text(elt, highlight=True) # changing main window state state = {"kw": True, "year": year, "data": results} return text, title, state def _date_search(self, start: datetime.date, end=None, span=None) -> tuple: """Function that set the text with a date research. return the text and the title to set. span: look to docstring of __call__ method.""" lang = self.parent.lang if end is None: title = self._title + self.localizedDate(day=start) end = start else: title = "{}{} -> {}".format(self._title, self.localizedDate(day=start), self.localizedDate(day=end)) text = '' # text which will be displayed data = [] # list of all the texts for each day requested cursor = start while cursor <= end: data.append(self.martyrology.daytext(cursor, lang)) text += self._format_text(data[-1]) cursor += datetime.timedelta(1) #changing main window state self.parent.state(type="martyrology", kw=False, start=start, end=end, data=data, span=span) state = { "kw": False, "start": start, "end": end, "data": data, "span": span } return text, title, state def _format_text(self, daytext: martyrology_module.TextResult, highlight=False) -> str: """Format text, which is an entry of the roman martyrology for a specific day. highlight can be set to highlight daytext.matching_line""" double_nline = "<br><br>" black_line = "<hr />" paragraph = '<p>{}</p>' text = "" text += "<h1>{}</h1>".format(daytext.title) for i, line in enumerate(daytext.main): if i == daytext.matching_line: line = "<b>{}</b>".format(line) text += paragraph.format(line) text += paragraph.format(daytext.last_sentence) text += black_line return text def initUI(self): """Launch user interface""" self.setReadOnly(True) def retranslateUI(self): """Retranslate UI""" self._title = _("DisplayMartyrology", "Theochrone - Roman Martyrology - ")
def test_credits(get_data_method): m = martyrology.Martyrology() lang = 'ja' m.credits(lang) assert mock.call(lang) in get_data_method.call_args_list assert mock.call('credits') in get_data_method().__getitem__.call_args_list