def test_db_stats(set_defaults, capsys): argv = ['', '--apiurl', 'http://example.com/yourls-api.php', 'db-stats'] db_stats = DBStats(total_links=200, total_clicks=5000) patch_argv = patch.object(sys, 'argv', argv) patch_db_stats = patch( 'yourls.core.YOURLSAPIMixin.db_stats', autospec=True, return_value=db_stats) with patch_argv, patch_db_stats as mock_db_stats: with pytest.raises(SystemExit): main() out, _ = capsys.readouterr() expected = format_dbstats(db_stats) + '\n' assert expected == out assert mock_db_stats.call_count == 1
def test_stats(set_defaults, capsys): argv = [ '', '--apiurl', 'http://example.com/yourls-api.php', 'stats', 'top', '3' ] links = [ ShortenedURL(shorturl='http://example.com/abcde', url='http://google.com', title='Google', date=datetime.datetime(2014, 9, 8, 20, 30, 17), ip='203.0.113.0', clicks=789, keyword=None), ShortenedURL(shorturl='http://example.com/abc45', url='https://www.bbc.co.uk/news', title='BBC News', date=datetime.datetime(2014, 12, 19, 16, 26, 39), ip='203.0.113.0', clicks=1364, keyword=None), ShortenedURL(shorturl='http://example.com/123de', url='https://www.youtube.com', title='YouTube', date=datetime.datetime(2015, 10, 9, 5, 46, 27), ip='203.0.113.0', clicks=27, keyword=None) ] stats = DBStats(total_links=200, total_clicks=5000) patch_argv = patch.object(sys, 'argv', argv) patch_stats = patch('yourls.core.YOURLSAPIMixin.stats', autospec=True, return_value=(links, stats)) with patch_argv, patch_stats as mock_stats: with pytest.raises(SystemExit): main() out, _ = capsys.readouterr() expected = format_dbstats(stats) + '\n' for link in links: expected += format_shorturl(link) + '\n' assert expected == out assert mock_stats.call_count == 1 argv = [ '', '--apiurl', 'http://example.com/yourls-api.php', 'stats', 'top', '3', '--simple' ] patch_argv = patch.object(sys, 'argv', argv) with patch_argv, patch_stats as mock_stats: with pytest.raises(SystemExit): main() out, _ = capsys.readouterr() expected = format_dbstats(stats) + '\n' expected += ("http://example.com/abcde\n" "http://example.com/abc45\n" "http://example.com/123de\n") assert expected == out assert mock_stats.call_count == 1
def test_stats(set_defaults, capsys): argv = ['', '--apiurl', 'http://example.com/yourls-api.php', 'stats', 'top', '3'] links = [ ShortenedURL( shorturl='http://example.com/abcde', url='http://google.com', title='Google', date=datetime.datetime(2014, 9, 8, 20, 30, 17), ip='203.0.113.0', clicks=789, keyword=None), ShortenedURL( shorturl='http://example.com/abc45', url='https://www.bbc.co.uk/news', title='BBC News', date=datetime.datetime(2014, 12, 19, 16, 26, 39), ip='203.0.113.0', clicks=1364, keyword=None), ShortenedURL( shorturl='http://example.com/123de', url='https://www.youtube.com', title='YouTube', date=datetime.datetime(2015, 10, 9, 5, 46, 27), ip='203.0.113.0', clicks=27, keyword=None) ] stats = DBStats(total_links=200, total_clicks=5000) patch_argv = patch.object(sys, 'argv', argv) patch_stats = patch( 'yourls.core.YOURLSAPIMixin.stats', autospec=True, return_value=(links, stats)) with patch_argv, patch_stats as mock_stats: with pytest.raises(SystemExit): main() out, _ = capsys.readouterr() expected = format_dbstats(stats) + '\n' for link in links: expected += format_shorturl(link) + '\n' assert expected == out assert mock_stats.call_count == 1 argv = ['', '--apiurl', 'http://example.com/yourls-api.php', 'stats', 'top', '3', '--simple'] patch_argv = patch.object(sys, 'argv', argv) with patch_argv, patch_stats as mock_stats: with pytest.raises(SystemExit): main() out, _ = capsys.readouterr() expected = format_dbstats(stats) + '\n' expected += ( "http://example.com/abcde\n" "http://example.com/abc45\n" "http://example.com/123de\n") assert expected == out assert mock_stats.call_count == 1