def __init__(self, datas, log_level, html_elem, html_elem_attr, lang, freeze): """A section contains a dictionary of data, a writer using NlgTools, and a text""" self.__dict__ = datas.__dict__.copy() self.__nlg = NlgTools(html_elem, html_elem_attr, log_level=log_level, lang=lang, freeze=freeze) self.__text = list()
class TestFreeText: nlg = NlgTools() free_text = nlg.free_text def test_empty_string(self): assert self.free_text("") == "" def test_none_string(self): assert self.free_text(None) == "" def test_empty_list(self): assert self.free_text([]) == "" def test_list_elem(self): assert self.free_text(["elem", "elem"]) == "elem elem" def test_list_empty_elem(self): assert self.free_text(["elem", "", "elem"]) == "elem elem" def test_mix_list_elem(self): assert self.free_text(["elem", "elem"], "elem") == "elem elem elem" def test_mix_none_empty_list(self): assert (self.free_text(["elem", "", None], "elem", None, [""], "", "elem") == "elem elem elem")
class Section: def __init__(self, datas, log_level, html_elem, html_elem_attr, lang, freeze): """A section contains a dictionary of data, a writer using NlgTools, and a text""" self.__dict__ = datas.__dict__.copy() self.__nlg = NlgTools(html_elem, html_elem_attr, log_level=log_level, lang=lang, freeze=freeze) self.__text = list() def __str__(self): return lxml.html.tostring(self.__nlg.html, pretty_print=True, encoding="utf-8").decode("utf-8") @property def html(self): """HTML getter""" return self.__nlg.html @property def text(self): return " ".join([elem for elem in self.__text if elem is not None]) @text.setter def text(self, value): if isinstance(value, str): self.__text += [value] else: f = lambda l: sum( ([x] if type(x) not in [list, tuple] else f(x) for x in l), []) self.__text += f(value) def write(self): """Uses NlgTools to write the text""" self.__nlg.write_text(self.text) self.__text = list() @property def tools(self): return self.__nlg
class TestAddTags: nlg = NlgTools() add_tag = nlg.add_tag iter_elems = nlg.iter_elems @pytest.mark.parametrize( "tag, expected", [ (None, "tag None doesn't exist in html"), ("b", "<b></b>"), ("p", "<p></p>"), ("div", "<div></div>"), ("DIV", "<div></div>"), ("a", "<a></a>"), ("u", "<u></u>"), ("test", "tag test doesn't exist in html") ], ) def test_empty_tag(self, tag, expected): assert self.add_tag(tag) == expected @pytest.mark.parametrize( "tag, text, expected", [ (None, "elem", "tag None doesn't exist in html"), ("b", "elem", "<b>elem</b>"), ("p", "elem", "<p>elem</p>"), ("div", "elem", "<div>elem</div>"), ("DIV", "elem", "<div>elem</div>"), ("a", "elem", "<a>elem</a>"), ("u", "elem", "<u>elem</u>"), ("test", "elem", "tag test doesn't exist in html") ], ) def test_tag_with_text(self, tag, text, expected): assert self.add_tag(tag, text) == expected @pytest.mark.parametrize( "tag, text, _class, expected", [ (None, "elem", "a_class", "tag None doesn't exist in html"), ("b", "elem", "a_class", "<b class=\"a_class\">elem</b>"), ("p", "elem", "a_class", "<p class=\"a_class\">elem</p>"), ("div", "elem", "a_class", "<div class=\"a_class\">elem</div>"), ("DIV", "elem", "a_class", "<div class=\"a_class\">elem</div>"), ("a", "elem", "a_class", "<a class=\"a_class\">elem</a>"), ("u", "elem", "a_class", "<u class=\"a_class\">elem</u>"), ("test", "elem", "a_class", "tag test doesn't exist in html") ], ) def test_tag_with_text_and_class(self, tag, text, _class, expected): assert self.add_tag(tag, text, _class=_class) == expected @pytest.mark.parametrize( "tag, text, _class, expected", [ (None, "elem", "a_class", "tag None doesn't exist in html"), ("b", "elem", "a_class", "<b other=\"test\" class=\"a_class\">elem</b>"), ("p", "elem", "a_class", "<p other=\"test\" class=\"a_class\">elem</p>"), ("div", "elem", "a_class", "<div other=\"test\" class=\"a_class\">elem</div>"), ("DIV", "elem", "a_class", "<div other=\"test\" class=\"a_class\">elem</div>"), ("a", "elem", "a_class", "<a other=\"test\" class=\"a_class\">elem</a>"), ("u", "elem", "a_class", "<u other=\"test\" class=\"a_class\">elem</u>"), ("test", "elem", "a_class", "tag test doesn't exist in html") ], ) def test_tag_with_text_class_and_other(self, tag, text, _class, expected): assert self.add_tag(tag, text, _class=_class, other="test") == expected def test_tag_encapsuled(self): assert self.add_tag("div", self.add_tag("p", self.add_tag("b", ))) == "<div><p><b></b></p></div>" def test_iter_tags(self): assert self.iter_elems(self.add_tag("b", [t for t in ["test", "iter", "tag"]])) == "<b>test</b> <b>iter</b> <b>tag</b>" def test_iter_tags_2(self): assert self.iter_elems([self.add_tag("b", t) for t in ["test", "iter", "tag"]]) == "<b>test</b> <b>iter</b> <b>tag</b>"
class TestIterElem: nlg = NlgTools() iter_elems = nlg.iter_elems @pytest.mark.parametrize( "input", [ None, [], [""], [None], [[]], [[""]], [["", ""], ""], [[None], None], [[None, ""], "", None], ], ) def test_empty(self, input): assert self.iter_elems(input) == "" @pytest.mark.parametrize( "input", [ None, [], [""], [None], [[]], [[""]], [["", ""], ""], [[None], None], [[None, ""], "", None], ], ) def test_empty_begin_with(self, input): assert self.iter_elems(input, IteratorConstructor(begin_w="begin")) == "" @pytest.mark.parametrize( "input", [ None, [], [""], [None], [[]], [[""]], [["", ""], ""], [[None], None], [[None, ""], "", None], ], ) def test_empty_end_with(self, input): assert self.iter_elems(input, IteratorConstructor(end_w="end")) == "" @pytest.mark.parametrize( "input", [ [], [""], [None], [[]], [[""]], [["", ""], ""], [[None], None], [[None, ""], "", None], ], ) def test_none_text_empty(self, input): assert self.iter_elems(input, IteratorConstructor(text_if_empty_list="empty")) == "empty" def test_single_sentence(self, list_elem): assert self.iter_elems([[e for e in list_elem]]) == "elem1 , elem2 et elem3" def test_bullet_points(self, list_elem): text = self.iter_elems([[e for e in list_elem]], IteratorConstructor(nb_elem_bullet=0)) assert text == "<ul><li>Elem1</li><li>Elem2</li><li>Elem3</li></ul>" def test_bullet_points_no_capitalize(self, list_elem): text = self.iter_elems([[e for e in list_elem]], IteratorConstructor(nb_elem_bullet=0, capitalize_bullets=False)) assert text == "<ul><li>elem1</li><li>elem2</li><li>elem3</li></ul>" def test_long_list_elem(self, long_list_elem): text = self.iter_elems([[e for e in long_list_elem]], ) assert text == "elem1 , elem2 , elem3 , elem4 , elem5 et elem6" def test_bullet_long_list_elem(self, long_list_elem): text = self.iter_elems([[e for e in long_list_elem]], IteratorConstructor(nb_elem_bullet=0)) assert text == "<ul><li>Elem1</li><li>Elem2</li><li>Elem3</li><li>Elem4</li><li>Elem5</li><li>Elem6</li></ul>" def test_long_list_limit_elem(self, long_list_elem): text = self.iter_elems([[e for e in long_list_elem]], IteratorConstructor(max_elem=4)) assert text == "elem1 , elem2 , elem3 et elem4" def test_bullet_long_list_limit_elem(self, long_list_elem): text = self.iter_elems([[e for e in long_list_elem]], IteratorConstructor(nb_elem_bullet=0, max_elem=4)) assert text == "<ul><li>Elem1</li><li>Elem2</li><li>Elem3</li><li>Elem4</li></ul>" def test_long_list_limit_no_more_bullet_elem(self, long_list_elem): text = self.iter_elems([[e for e in long_list_elem]], IteratorConstructor(nb_elem_bullet=3, max_elem=2)) assert text == "elem1 et elem2" def test_long_list_with_empty_elem(self, long_list_with_empty_elem): text = self.iter_elems([[e for e in long_list_with_empty_elem]], IteratorConstructor(max_elem=4)) assert text == "elem1 , elem2 , elem4 et elem6" def test_bullet_long_list_with_empty_elem(self, long_list_with_empty_elem): text = self.iter_elems([[e for e in long_list_with_empty_elem]], IteratorConstructor(nb_elem_bullet=0)) assert text == "<ul><li>Elem1</li><li>Elem2</li><li>Elem4</li><li>Elem6</li></ul>" def test_complete_bullet(self, long_list_elem, long_list_with_empty_elem): text = self.iter_elems([ [e for e in long_list_elem], [e for e in long_list_with_empty_elem] ], IteratorConstructor(begin_w="start", nb_elem_bullet=0, end_w="end")) assert text == "start <ul><li>Elem1 elem1</li><li>Elem2 elem2</li><li>Elem3</li><li>Elem4 elem4</li>" \ "<li>Elem5</li><li>Elem6 elem6</li></ul> end" def test_complete(self, long_list_elem, long_list_with_empty_elem): text = self.iter_elems([ [e for e in long_list_elem], [e for e in long_list_with_empty_elem] ], IteratorConstructor(begin_w="start", end_w="end")) assert text == "start elem1 elem1 , elem2 elem2 , elem3 , elem4 elem4 , elem5 et elem6 elem6 end"
class TestNumbers: nlg = NlgTools() nlg_num = nlg.number @pytest.mark.parametrize( "input, expected", [ (1, "1"), (1.0, "1.0"), (1.00, "1.0"), (1.002, "1.002"), (-15, "-15"), (178, "178"), (245.489, "245.489"), (245.48944897, "245.48944897"), (-245.48944897, "-245.48944897"), (245.400000000, "245.4"), (12354, "12 354"), (12354789, "12 354 789"), (-12354789, "-12 354 789"), (21657845.400000000, "21 657 845.4"), (21657845.41564865, "21 657 845.41564865"), ], ) def test_number(self, input, expected): assert post_treatment_with_numbers_fr(input) == expected @pytest.mark.parametrize( "input, expected", [ (1, "1€"), (1.0, "1.0€"), (1.00, "1.0€"), (1.002, "1.002€"), (-15, "-15€"), (178, "178€"), (245.489, "245.489€"), (245.48944897, "245.48944897€"), (-245.48944897, "-245.48944897€"), (245.400000000, "245.4€"), (12354, "12 354€"), (12354789, "12 354 789€"), (-12354789, "-12 354 789€"), (21657845.400000000, "21 657 845.4€"), (21657845.41564865, "21 657 845.41564865€"), ], ) def test_number_short(self, input, expected): assert post_treatment_with_numbers_fr(input, short="€") == expected @pytest.mark.parametrize( "input, expected", [ (1, "1"), (1.0, "1_0"), (1.00, "1_0"), (1.002, "1_002"), (-15, "-15"), (178, "178"), (245.489, "245_489"), (245.48944897, "245_48944897"), (-245.48944897, "-245_48944897"), (245.400000000, "245_4"), (12354, "12 354"), (12354789, "12 354 789"), (-12354789, "-12 354 789"), (21657845.400000000, "21 657 845_4"), (21657845.41564865, "21 657 845_41564865"), ], ) def test_number_sep(self, input, expected): assert post_treatment_with_numbers_fr(input, sep="_") == expected @pytest.mark.parametrize( "input, expected", [ (1, "1"), (1.0, "1.0"), (1.00, "1.0"), (1.002, "1.002"), (-15, "-15"), (178, "178"), (245.489, "245.489"), (245.48944897, "245.48944897"), (-245.48944897, "-245.48944897"), (245.400000000, "245.4"), (12354, "12'354"), (12354789, "12'354'789"), (-12354789, "-12'354'789"), (21657845.400000000, "21'657'845.4"), (21657845.41564865, "21'657'845.41564865"), ], ) def test_number_mile_sep(self, input, expected): assert post_treatment_with_numbers_fr(input, mile_sep="'") == expected @pytest.mark.parametrize( "input, expected", [ (1, "1"), (1.0, "1"), (1.00, "1"), (1.002, "1"), (-15, "-15"), (178, "178"), (245.489, "245.49"), (245.48944897, "245.49"), (-245.48944897, "-245.49"), (245.400000000, "245.4"), (12354, "12 354"), (12354789, "12 354 789"), (-12354789, "-12 354 789"), (21657845.400000000, "21 657 845.4"), (21657845.41564865, "21 657 845.42"), ], ) def test_number_dec(self, input, expected): assert post_treatment_with_numbers_fr(input, dec=2) == expected @pytest.mark.parametrize( "input, expected", [ (1, "+1"), (1.0, "+1.0"), (1.00, "+1.0"), (1.002, "+1.002"), (-15, "-15"), (178, "+178"), (245.489, "+245.489"), (245.48944897, "+245.48944897"), (-245.48944897, "-245.48944897"), (245.400000000, "+245.4"), (12354, "+12 354"), (12354789, "+12 354 789"), (-12354789, "-12 354 789"), (21657845.400000000, "+21 657 845.4"), (21657845.41564865, "+21 657 845.41564865"), ], ) def test_number_force_sign(self, input, expected): assert post_treatment_with_numbers_fr(input, force_sign=True) == expected @pytest.mark.parametrize( "input, expected", [ (1, "1.00"), (1.0, "1.00"), (1.00, "1.00"), (1.100, "1.10"), (1.002, "1.00"), (-15, "-15.00"), (178, "178.00"), (245.489, "245.49"), (245.48944897, "245.49"), (-245.48944897, "-245.49"), (245.400000000, "245.40"), (12354, "12 354.00"), (12354789, "12 354 789.00"), (-12354789, "-12 354 789.00"), (21657845.400000000, "21 657 845.40"), (21657845.41564865, "21 657 845.42"), ], ) def test_number_without_remove_trailing_zeros(self, input, expected): assert post_treatment_with_numbers_fr( input, dec=2, remove_trailing_zeros=False) == expected @pytest.mark.parametrize( "input, expected", [ (1, "0k€"), (1.0, "0k€"), (1.00, "0k€"), (1.002, "0k€"), (-15, "-0,01k€"), (178, "0,18k€"), (245.489, "0,25k€"), (245.48944897, "0,25k€"), (-245.48944897, "-0,25k€"), (245.400000000, "0,25k€"), (12354, "12,35k€"), (10000, "10k€"), (12354789, "12'354,79k€"), (-12354789, "-12'354,79k€"), (21657845.400000000, "21'657,85k€"), (21657845.41564865, "21'657,85k€"), ], ) def test_number_multi_params(self, input, expected): assert post_treatment_with_numbers_fr(input / 1000, short="k€", mile_sep="'", sep=",", dec=2) == expected
def test_synonym_before_treatment(self, input): nlg = NlgTools() nlg_syn = nlg.synonym assert nlg_syn(input) == "*1*"