"""Tests for template_renderer module.""" import pytest from htmlmin.minify import html_minify from notifications_delivery.utils import template_renderer def test_render_template_valid(mocker): """Test rendering a valid template.""" translation_mock = mocker.patch( 'notifications_delivery.utils.template_renderer.get_translations' ) template_name = '__test__' context = {'first_name': 'John', 'last_name': 'Doe'} user_locale = 'en' result = template_renderer.render_template(template_name, context, user_locale) translation_mock.assert_called_once_with(user_locale) assert result == html_minify('I am the test template. My name is John Doe.') def test_render_template_no_extension_file_not_found(mocker): """Test rendering when template file with no extension is not found.""" mocker.patch('notifications_delivery.utils.template_renderer.get_translations') template_name = 'i_dont_exists_on_any_extension' context = {'first_name': 'John', 'last_name': 'Doe'} user_locale = 'en' try: template_renderer.render_template(template_name, context, user_locale) except FileNotFoundError as e: err = "Template 'i_dont_exists_on_any_extension' not found with .jinja or .html extension" assert str(e) == err def test_datetimeformat(): """Test the datetimeformat.""" result = template_renderer.datetimeformat( value='2018-01-01', date_format='%m/%d') assert result == '01/01' def test_datetimeformat_with_ordinal_days(): """Test the datetimeformat.""" result = template_renderer.datetimeformat( value='2018-01-01', date_format='%b', has_ordinal=True) assert result == 'Jan 1st' def test_format_number_with_trillions(): """Test format_number with trillions.""" input_value = 1500000000000 expected_output = '1.5T' result = template_renderer.format_number(input_value) assert result == expected_output def test_format_number_with_billions(): """Test format_number with billions.""" input_value = 1500000000 expected_output = '1.5B' result = template_renderer.format_number(input_value) assert result == expected_output def test_format_number_with_millions(): """Test format_number with millions.""" input_value = 1500000 expected_output = '1.5M' result = template_renderer.format_number(input_value) assert result == expected_output def test_format_number_with_thousands(): """Test format_number with thousands.""" input_value = 1500 expected_output = '1.5K' result = template_renderer.format_number(input_value) assert result == expected_output def test_format_number_with_small_numbers(): """Test format_number with small numbers.""" input_value = 374.234 expected_output = '374' result = template_renderer.format_number(input_value) assert result == expected_output def test_replace_format(): """Replace Full Length with Album.""" release_format = 'FULL LENGTH' expected_output = 'ALBUM' result = template_renderer.replace_format(release_format) assert result == expected_output def test_replace_format_no_replacement(): """Replace Full Length with Album.""" release_format = 'SINGLE' expected_output = 'SINGLE' result = template_renderer.replace_format(release_format) assert result == expected_output def test_format_growth_percentage_positive(): """Test formatting growth percentage with a positive value.""" value = 2.4 result = template_renderer.format_growth_percentage(value) assert result == '+2%' def test_format_growth_percentage_negative(): """Test formatting growth percentage with a negative value.""" value = -2.4 result = template_renderer.format_growth_percentage(value) assert result == '-2%' def test_format_artist_names_less_three_values(): """Test formatting artist names with less than 3 values.""" values = ['artist_name_1', 'artist_name_2', 'artist_name_3'] result = template_renderer.format_artist_names(values) assert result == 'artist_name_1, artist_name_2, artist_name_3' def test_format_artist_names_more_three_values(): """Test formatting artist names with more than 3 values.""" values = [ 'artist_name_1', 'artist_name_2', 'artist_name_3', 'artist_name_4'] result = template_renderer.format_artist_names(values) assert result == 'artist_name_1, artist_name_2, artist_name_3...' def test_format_spike_playlist_artwork_with_replace(): """Test formatting url with heigh and width to replace.""" url = 'url/{w}x{h}cc.jpeg' result = template_renderer.format_spike_playlist_artwork(url, None) assert result == 'url/50x50cc.jpeg' def test_format_spike_playlist_artwork_without_replace(): """Test formatting url with no heigh and width to replace.""" url = 'url/image.jpeg' result = template_renderer.format_spike_playlist_artwork(url, None) assert result == 'url/image.jpeg' def test_format_spike_playlist_artwork_without_url_spotify(): """Test default url for spotify.""" url = '' result = template_renderer.format_spike_playlist_artwork(url, 'spotify') assert result == \ 'https://cdn.theorchard.io/web-icons/empty_artwork_spotify.png' def test_format_spike_playlist_artwork_without_url_apple(): """Test default url for apple.""" url = '' result = template_renderer.format_spike_playlist_artwork( url, 'apple music') assert result == \ 'https://cdn.theorchard.io/web-icons/empty_artwork_apple_music.png' @pytest.mark.parametrize( ('applications', 'expected_result'), ( pytest.param([], '', id='no applications'), pytest.param([{}], '', id='no url'), pytest.param([{'url': 'foo.bar'}], 'foo.bar', id='success'), ) ) def test_get_login_url(applications, expected_result): """Test get_login_url.""" result = template_renderer.get_login_url(applications) assert result == expected_result @pytest.mark.parametrize( ('identity', 'expected_result'), ( pytest.param({ 'name': 'Foo Bar' }, 'Foo Bar', id='just_name'), pytest.param({ 'name': 'Foo Bar', 'first_name': 'Roo', 'last_name': 'Baz' }, 'Foo Bar', id='name_and_first_last'), pytest.param({ 'name': '', 'first_name': 'Roo', 'last_name': 'Baz' }, 'Roo Baz', id='empty_name_and_first_last'), pytest.param({ 'first_name': 'Roo', 'last_name': 'Baz' }, 'Roo Baz', id='first_last'), pytest.param({ 'first_name': 'Roo' }, 'Roo', id='just_first'), pytest.param({ 'last_name': 'Baz' }, 'Baz', id='just_last'), pytest.param({}, 'Admin', id='no_name'), ) ) def test_get_name(identity, expected_result): """Test get_name.""" result = template_renderer.get_name(identity) assert result == expected_result