Spaces:
Sleeping
Sleeping
Download tests/test_reports.py from PRAnalytics/PR_Tool: direct link, hf CLI and curl.
- Browser
- Download file 1.86 kB
-
https://huggingface.co/spaces/PRAnalytics/PR_Tool/resolve/main/tests/test_reports.py
- Command line
-
hf download hf://spaces/PRAnalytics/PR_Tool/tests/test_reports.py
-
curl -L -o test_reports.py https://huggingface.co/spaces/PRAnalytics/PR_Tool/resolve/main/tests/test_reports.py
1.86 kB
| import unittest | |
| from reports import normalize, human_views, render | |
| class ReportTests(unittest.TestCase): | |
| def test_normalize(self): | |
| self.assertEqual(normalize('http://vk.ru/wall-123_456')['url'], 'https://vk.com/wall-123_456') | |
| self.assertEqual(normalize('https://telegram.me/s/Channel/123')['url'], 'https://t.me/channel/123') | |
| for link in ('https://t.me/c/123/456', 'https://vk.ru.evil.test/wall-123_456', 'https://vk.com/club123'): | |
| with self.assertRaises(ValueError): | |
| normalize(link) | |
| def test_format_without_zero(self): | |
| for value, expected in ((2000, '2K'), (10600, '10,6K'), (1000000, '1M'), (845, '0,8K'), (100, '0,1K'), (35, '0K'), (0, '0K')): | |
| self.assertEqual(human_views(value), expected) | |
| def test_errors_are_not_zero_views(self): | |
| first = dict(normalize('https://t.me/channel/1'), title='Имя 🗿', views=2000) | |
| second = dict(normalize('https://t.me/channel/2'), title='Имя', views=None, error='Нет счётчика') | |
| r = render([first, second]) | |
| self.assertIn('<b>2K</b>', r['html']) | |
| self.assertNotIn('🗿', r['html']) | |
| self.assertNotIn('0K', r['html']) | |
| self.assertIn('Неполный итог', r['html']) | |
| self.assertEqual(len(r['errors']), 1) | |
| def test_grouping_and_html_escaping(self): | |
| records = [dict(normalize('https://max.ru/channel/AaE-viFyB3E'), title='<script>alert(1)</script> Имя', views=1000), | |
| dict(normalize('https://max.ru/channel/AaE-viFyB3F'), title='Имя', views=2000)] | |
| r = render(records) | |
| self.assertIn('<h2>Макс</h2>', r['html']) | |
| self.assertIn('ещё</a>', r['html']) | |
| self.assertNotIn('<script>', r['html']) | |
| self.assertIn('<b>3K</b>', r['html']) | |
| if __name__ == '__main__': | |
| unittest.main() | |