"""Maigret utils test functions""" import itertools import re from markupsafe import Markup from maigret.utils import ( CaseConverter, is_country_tag, enrich_link_str, URLMatcher, get_dict_ascii_tree, get_match_ratio, is_plausible_username, ) def test_case_convert_camel_to_snake(): a = 'SnakeCasedString' b = CaseConverter.camel_to_snake(a) assert b == 'snake_cased_string' def test_case_convert_snake_to_camel(): a = 'camel_cased_string' b = CaseConverter.snake_to_camel(a) assert b == 'camelCasedString' def test_case_convert_snake_to_title(): a = 'camel_cased_string' b = CaseConverter.snake_to_title(a) assert b == 'Camel cased string' def test_case_convert_camel_with_digits_to_snake(): a = 'ignore403' b = CaseConverter.camel_to_snake(a) assert b == 'ignore403' def test_is_country_tag(): assert is_country_tag('ru') is True assert is_country_tag('FR') is True assert is_country_tag('a1') is False assert is_country_tag('dating') is False assert is_country_tag('global') is True def test_is_country_tag_field_names_are_not_country_codes(): """The field names 'country' and 'locale' must not be mistaken for ISO codes. generate_report_context iterates ids_data and for keys 'country'/'locale' decides whether to use direct alpha_2 lookup (is_country_tag(v)) or fuzzy search. A previous bug passed the key name k instead of the value v, so is_country_tag('country') was always False and the direct lookup path was dead code. """ assert is_country_tag('country') is False assert is_country_tag('locale') is False # Values that should trigger the direct lookup assert is_country_tag('US') is True assert is_country_tag('ru') is True def test_enrich_link_str(): assert enrich_link_str('test') == 'test' assert ( enrich_link_str(' www.flickr.com/photos/alexaimephotography/') == 'www.flickr.com/photos/alexaimephotography/' ) def test_enrich_link_str_escapes_payload(): # markup inside a link must be escaped while the wrapper is preserved payload = 'http://evil.example/">' result = enrich_link_str(payload) assert isinstance(result, Markup) assert '