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")
def __init__(self, datas, lang, freeze, elem=None, elem_attr=None, **kwargs): super().__init__(datas) elem, elem_attr = self.read_old_attribs(elem, elem_attr, "child", **kwargs) self._nlg = NlgTools(lang, freeze, elem, elem_attr)
def __init__(self, datas, lang, freeze): super().__init__(datas) self._nlg = NlgTools(lang, freeze)
class TestIterElem: nlg = NlgTools(lang="en") iter_elems = nlg.enum @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, 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, end_w="end") == "" @pytest.mark.parametrize( "input", [ [], [""], [None], [[]], [[""]], [["", ""], ""], [[None], None], [[None, ""], "", None], ], ) def test_none_text_empty(self, input): assert self.iter_elems(input, 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 and elem3" def test_bullet_points(self, list_elem): text = self.iter_elems([[e for e in list_elem]], 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]], 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 and elem6" def test_bullet_long_list_elem(self, long_list_elem): text = self.iter_elems([[e for e in long_list_elem]], 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]], max_elem=4) assert text == "elem1 , elem2 , elem3 and elem4" def test_bullet_long_list_limit_elem(self, long_list_elem): text = self.iter_elems([[e for e in long_list_elem]], 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]], nb_elem_bullet=3, max_elem=2) assert text == "elem1 and 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]], max_elem=4) assert text == "elem1 , elem2 , elem4 and 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]], 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]], 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]], begin_w="start", end_w="end") assert text == "start elem1 elem1 , elem2 elem2 , elem3 , elem4 elem4 , elem5 and elem6 elem6 end"
class TestNumbers: nlg = NlgTools() nlg_num = nlg.nlg_num @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
class TestAddTags: nlg = NlgTools(lang="en") add_tag = nlg.add_tag iter_elems = nlg.enum @pytest.mark.parametrize( "tag, expected", [("b", "<b></b>"), ("p", "<p></p>"), ("div", "<div></div>"), ("DIV", "<div></div>"), ("a", "<a></a>"), ("u", "<u></u>")], ) def test_empty_tag(self, tag, expected): assert self.add_tag(tag) == expected @pytest.mark.parametrize( "tag, text, expected", [("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>")], ) def test_tag_with_text(self, tag, text, expected): assert self.add_tag(tag, text) == expected @pytest.mark.parametrize( "tag, text, _class, expected", [("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>")], ) 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", [("b", "elem", "a_class", "<b class=\"a_class\" other=\"test\">elem</b>"), ("p", "elem", "a_class", "<p class=\"a_class\" other=\"test\">elem</p>"), ("div", "elem", "a_class", "<div class=\"a_class\" other=\"test\">elem</div>"), ("DIV", "elem", "a_class", "<div class=\"a_class\" other=\"test\">elem</div>"), ("a", "elem", "a_class", "<a class=\"a_class\" other=\"test\">elem</a>"), ("u", "elem", "a_class", "<u class=\"a_class\" other=\"test\">elem</u>")], ) 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> and <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> and <b>tag</b>" @pytest.mark.parametrize( "tag, text, style, expected", [("b", "elem", "color:blue", "<b style=\"color:blue\">elem</b>")], ) def test_tag_with_styles(self, tag, text, style, expected): assert self.add_tag(tag, text, style=style) == expected
def test_synonym_before_treatment(self, input): nlg = NlgTools() nlg_syn = nlg.nlg_syn assert nlg_syn(input) == "*1*"
import lxml from CoreNLG.PredefObjects import TextVar from CoreNLG.NlgTools import NlgTools lang = 'fr' # change language to en / fr nlg = NlgTools(lang=lang) my_text = TextVar() data = { "2018": { "nbi": 20000, "gni": 3500, "quarters": { "Q1": { "nbi": 7000, "gni": 700 }, "Q2": { "nbi": 7000, "gni": 1500 }, "Q3": { "nbi": 6000, "gni": 1700 }, "Q4": { "nbi": 5000, "gni": 500 } }