1
0
Fork 0
mirror of https://gitlab.com/SIGBUS/nyaa.git synced 2024-11-01 02:25:55 +00:00
nyaa/tests/test_filters.py
Kfir Hadas c466e76471 Fix RFC822 filters + More tests (#257)
* Make rfc822 filters compatible with Windows systems.

.strftime() is relative to the system it's being run on.
UNIX has '%s' for seconds since the EPOCH, Windows doesn't (ValueError).
Solution: use .timestamp() to achieve the same result on both platforms.
This also allows us to drop the float() around it, since it returns a float.

* Start testing filters

* Add placeholders for more tests

* Make 'tests' folder a Python package

Now you can run tests with just `pytest tests`

* Update readme and travis config

* Test timesince()

* Update and organize .gitignore

Deleted: (nothing)
Added: Coverage files, .idea\

* Test filter_truthy, category_name

* Tests for backend.py

* Tests for bencode.py

* Move (empty) test_models.py to tests package

* Tests for utils.py

* Fixes for flattenDict

* Change name to `flatten_dict`
* `newkey` was assigned but never used

* Add a helper class for testing

* Show coverage on Travis

(only Travis for now...)

* Remove IDE

* Use correct assert functions

* Update README.md
2017-07-07 16:14:37 -05:00

99 lines
3.5 KiB
Python

import unittest
import datetime
from email.utils import formatdate
from tests import NyaaTestCase
from nyaa.routes import (_jinja2_filter_rfc822, _jinja2_filter_rfc822_es, get_utc_timestamp,
get_display_time, timesince, filter_truthy, category_name)
class TestFilters(NyaaTestCase):
# def setUp(self):
# self.db, nyaa.app.config['DATABASE'] = tempfile.mkstemp()
# nyaa.app.config['TESTING'] = True
# self.app = nyaa.app.test_client()
# with nyaa.app.app_context():
# nyaa.db.create_all()
#
# def tearDown(self):
# os.close(self.db)
# os.unlink(nyaa.app.config['DATABASE'])
def test_filter_rfc822(self):
# test with timezone UTC
test_date = datetime.datetime(2017, 2, 15, 11, 15, 34, 100, datetime.timezone.utc)
self.assertEqual(_jinja2_filter_rfc822(test_date), 'Wed, 15 Feb 2017 11:15:34 -0000')
def test_filter_rfc822_es(self):
# test with local timezone
test_date_str = '2017-02-15T11:15:34'
# this is in order to get around local time zone issues
expected = formatdate(float(datetime.datetime(2017, 2, 15, 11, 15, 34, 100).timestamp()))
self.assertEqual(_jinja2_filter_rfc822_es(test_date_str), expected)
def test_get_utc_timestamp(self):
# test with local timezone
test_date_str = '2017-02-15T11:15:34'
self.assertEqual(get_utc_timestamp(test_date_str), 1487157334)
def test_get_display_time(self):
# test with local timezone
test_date_str = '2017-02-15T11:15:34'
self.assertEqual(get_display_time(test_date_str), '2017-02-15 11:15')
def test_timesince(self):
now = datetime.datetime.utcnow()
self.assertEqual(timesince(now), 'just now')
self.assertEqual(timesince(now - datetime.timedelta(seconds=5)), '5 seconds ago')
self.assertEqual(timesince(now - datetime.timedelta(minutes=1)), '1 minute ago')
self.assertEqual(
timesince(now - datetime.timedelta(minutes=38, seconds=43)), '38 minutes ago')
self.assertEqual(
timesince(now - datetime.timedelta(hours=2, minutes=38, seconds=51)), '2 hours ago')
bigger = now - datetime.timedelta(days=3)
self.assertEqual(timesince(bigger), bigger.strftime('%Y-%m-%d %H:%M UTC'))
@unittest.skip('Not yet implemented')
def test_static_cachebuster(self):
pass
@unittest.skip('Not yet implemented')
def test_modify_query(self):
pass
def test_filter_truthy(self):
my_list = [
True, False, # booleans
'hello!', '', # strings
1, 0, -1, # integers
1.0, 0.0, -1.0, # floats
['test'], [], # lists
{'marco': 'polo'}, {}, # dictionaries
None
]
expected_result = [
True,
'hello!',
1, -1,
1.0, -1.0,
['test'],
{'marco': 'polo'}
]
self.assertListEqual(filter_truthy(my_list), expected_result)
def test_category_name(self):
with self.app_context:
# Nyaa categories only
self.assertEqual(category_name('1_0'), 'Anime')
self.assertEqual(category_name('1_2'), 'Anime - English-translated')
# Unknown category ids
self.assertEqual(category_name('100_0'), '???')
self.assertEqual(category_name('1_100'), '???')
self.assertEqual(category_name('0_0'), '???')
if __name__ == '__main__':
unittest.main()