1
0
Fork 0
mirror of https://gitlab.com/SIGBUS/nyaa.git synced 2024-12-22 16:29:59 +00:00
nyaa/tests/test_utils.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

116 lines
3.4 KiB
Python

import unittest
from collections import OrderedDict
from hashlib import sha1
from nyaa import utils
class TestUtils(unittest.TestCase):
def test_sha1_hash(self):
bencoded_test_data = b'd5:hello5:world7:numbersli1ei2eee'
self.assertEqual(
utils.sha1_hash(bencoded_test_data),
sha1(bencoded_test_data).digest())
def test_sorted_pathdict(self):
initial = {
'api_handler.py': 11805,
'routes.py': 34247,
'__init__.py': 6499,
'torrents.py': 11948,
'static': {
'img': {
'nyaa.png': 1200,
'sukebei.png': 1100,
},
'js': {
'main.js': 3000,
},
},
'search.py': 5148,
'models.py': 24293,
'templates': {
'upload.html': 3000,
'home.html': 1200,
'layout.html': 23000,
},
'utils.py': 14700,
}
expected = OrderedDict({
'static': OrderedDict({
'img': OrderedDict({
'nyaa.png': 1200,
'sukebei.png': 1100,
}),
'js': OrderedDict({
'main.js': 3000,
}),
}),
'templates': OrderedDict({
'home.html': 1200,
'layout.html': 23000,
'upload.html': 3000,
}),
'__init__.py': 6499,
'api_handler.py': 11805,
'models.py': 24293,
'routes.py': 34247,
'search.py': 5148,
'torrents.py': 11948,
'utils.py': 14700,
})
self.assertDictEqual(utils.sorted_pathdict(initial), expected)
@unittest.skip('Not yet implemented')
def test_cached_function(self):
# TODO: Test with a function that generates something random?
pass
def test_flatten_dict(self):
initial = OrderedDict({
'static': OrderedDict({
'img': OrderedDict({
'nyaa.png': 1200,
'sukebei.png': 1100,
}),
'js': OrderedDict({
'main.js': 3000,
}),
'favicon.ico': 1000,
}),
'templates': [
{'home.html': 1200},
{'layout.html': 23000},
{'upload.html': 3000},
],
'__init__.py': 6499,
'api_handler.py': 11805,
'models.py': 24293,
'routes.py': 34247,
'search.py': 5148,
'torrents.py': 11948,
'utils.py': 14700,
})
expected = {
'static/img/nyaa.png': 1200,
'static/img/sukebei.png': 1100,
'static/js/main.js': 3000,
'static/favicon.ico': 1000,
'templates/home.html': 1200,
'templates/layout.html': 23000,
'templates/upload.html': 3000,
'__init__.py': 6499,
'api_handler.py': 11805,
'models.py': 24293,
'routes.py': 34247,
'search.py': 5148,
'utils.py': 14700,
'torrents.py': 11948,
}
self.assertDictEqual(utils.flatten_dict(initial), expected)
if __name__ == '__main__':
unittest.main()