From 232af1dc38c53f202cdee4febff57af90f6354eb Mon Sep 17 00:00:00 2001 From: Nathan Yam Date: Wed, 7 Jun 2017 23:32:26 +1000 Subject: [PATCH 01/10] Add basic URL routing tests --- requirements.txt | 3 +++ tests/test_nyaa.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 tests/test_nyaa.py diff --git a/requirements.txt b/requirements.txt index 705ed83..3530ea2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -28,11 +28,14 @@ mysqlclient==1.3.10 orderedset==2.0 packaging==16.8 passlib==1.7.1 +progressbar2==3.20.0 progressbar33==2.4 +py==1.4.34 pycodestyle==2.3.1 pycparser==2.17 PyMySQL==0.7.11 pyparsing==2.2.0 +pytest==3.1.1 python-dateutil==2.6.0 python-editor==1.0.3 python-utils==2.1.0 diff --git a/tests/test_nyaa.py b/tests/test_nyaa.py new file mode 100644 index 0000000..9e50380 --- /dev/null +++ b/tests/test_nyaa.py @@ -0,0 +1,53 @@ +import os +import unittest +import tempfile +import nyaa + + +class NyaaTestCase(unittest.TestCase): + + 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_empty_db(self): + rv = self.app.get('/') + assert b'Browse :: Nyaa' in rv.data + assert b'Guest' in rv.data + + def test_upload_url(self): + rv = self.app.get('/upload') + assert b'Upload Torrent' in rv.data + assert b'You are not logged in, and are uploading anonymously.' in rv.data + + def test_rules_url(self): + rv = self.app.get('/rules') + assert b'Site Rules' in rv.data + + def test_help_url(self): + rv = self.app.get('/help') + assert b'Using the Site' in rv.data + + def test_rss_url(self): + rv = self.app.get('/?page=rss') + assert b'/xmlns/nyaa' in rv.data + + def test_login_url(self): + rv = self.app.get('/login') + assert b'Username or email address' in rv.data + + def test_registry(self): + rv = self.app.get('/register') + assert b'Username' in rv.data + assert b'Password' in rv.data + + +if __name__ == '__main__': + unittest.main() From ffd87f32d241b74f4afd7e32a05c8b25025dead3 Mon Sep 17 00:00:00 2001 From: Nathan Yam Date: Wed, 7 Jun 2017 23:36:44 +1000 Subject: [PATCH 02/10] Add test section in readme section --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 7ef3410..2265042 100644 --- a/README.md +++ b/README.md @@ -108,3 +108,8 @@ However, take note that binglog is not necessary for simple ES testing and devel You're done! The script should now be feeding updates from the database to Elasticsearch. Take note, however, that the specified ES index refresh interval is 30 seconds, which may feel like a long time on local development. Feel free to adjust it or [poke Elasticsearch yourself!](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-refresh.html) + +### Running Tests +We have some basic tests that check if each page can render correctly. To run the tests: +- Make sure that you are in the python virtual environment. +- Run `python -m pytest tests/` while in the repository directory. From 64098dae49f990509cb442b33c57ad1eacfd4ef1 Mon Sep 17 00:00:00 2001 From: Nathan Yam Date: Wed, 7 Jun 2017 23:39:02 +1000 Subject: [PATCH 03/10] Rename test method better --- tests/test_nyaa.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_nyaa.py b/tests/test_nyaa.py index 9e50380..e3736ce 100644 --- a/tests/test_nyaa.py +++ b/tests/test_nyaa.py @@ -17,7 +17,7 @@ class NyaaTestCase(unittest.TestCase): os.close(self.db) os.unlink(nyaa.app.config['DATABASE']) - def test_empty_db(self): + def test_index_url(self): rv = self.app.get('/') assert b'Browse :: Nyaa' in rv.data assert b'Guest' in rv.data From 751378221f259a48eb7f9a5d8fd4f2b98823bbad Mon Sep 17 00:00:00 2001 From: Kfir Hadas Date: Wed, 7 Jun 2017 19:25:34 +0300 Subject: [PATCH 04/10] Add tests to Travis, fix lint script --- .travis.yml | 20 ++++++++++++++++---- lint.sh | 2 ++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index f5fcb2b..3915f25 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,17 +2,29 @@ language: python python: "3.6" -dist: xenial -sudo: false +dist: trusty +sudo: required matrix: fast_finish: true cache: pip -install: pip install --upgrade pycodestyle +services: + mysql -script: ./lint.sh --check +before_install: + - mysql -u root -e 'CREATE DATABASE nyaav2 DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;' + +install: + - pip install -r requirements.txt + - sed "s/mysql:\/\/test:test123@/mysql:\/\/root:@/" config.example.py > config.py + - python db_create.py + - ./db_migrate.py stamp head + +script: + - python -m pytest tests/ + - ./lint.sh --check notifications: email: false diff --git a/lint.sh b/lint.sh index 2152765..46c1660 100755 --- a/lint.sh +++ b/lint.sh @@ -49,3 +49,5 @@ if [[ ${action} == check_lint ]]; then echo "The code requires some changes." fi fi + +if [[ ${result} -ne 0 ]]; then exit 1; fi From 323780dd10ee9f8ce2c9f4f0d6da73195ba947a7 Mon Sep 17 00:00:00 2001 From: Kfir Hadas Date: Wed, 7 Jun 2017 19:32:02 +0300 Subject: [PATCH 05/10] Remove progressbar2 --- requirements.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 3530ea2..de79b62 100644 --- a/requirements.txt +++ b/requirements.txt @@ -28,7 +28,6 @@ mysqlclient==1.3.10 orderedset==2.0 packaging==16.8 passlib==1.7.1 -progressbar2==3.20.0 progressbar33==2.4 py==1.4.34 pycodestyle==2.3.1 From 2246f019eaf6d1e4ff0217adc06312832c10797d Mon Sep 17 00:00:00 2001 From: Kfir Hadas Date: Wed, 7 Jun 2017 19:32:45 +0300 Subject: [PATCH 06/10] Use spaces --- tests/test_nyaa.py | 68 +++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/tests/test_nyaa.py b/tests/test_nyaa.py index e3736ce..478a4c0 100644 --- a/tests/test_nyaa.py +++ b/tests/test_nyaa.py @@ -6,48 +6,48 @@ import nyaa class NyaaTestCase(unittest.TestCase): - 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 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 tearDown(self): + os.close(self.db) + os.unlink(nyaa.app.config['DATABASE']) - def test_index_url(self): - rv = self.app.get('/') - assert b'Browse :: Nyaa' in rv.data - assert b'Guest' in rv.data + def test_index_url(self): + rv = self.app.get('/') + assert b'Browse :: Nyaa' in rv.data + assert b'Guest' in rv.data - def test_upload_url(self): - rv = self.app.get('/upload') - assert b'Upload Torrent' in rv.data - assert b'You are not logged in, and are uploading anonymously.' in rv.data + def test_upload_url(self): + rv = self.app.get('/upload') + assert b'Upload Torrent' in rv.data + assert b'You are not logged in, and are uploading anonymously.' in rv.data - def test_rules_url(self): - rv = self.app.get('/rules') - assert b'Site Rules' in rv.data + def test_rules_url(self): + rv = self.app.get('/rules') + assert b'Site Rules' in rv.data - def test_help_url(self): - rv = self.app.get('/help') - assert b'Using the Site' in rv.data + def test_help_url(self): + rv = self.app.get('/help') + assert b'Using the Site' in rv.data - def test_rss_url(self): - rv = self.app.get('/?page=rss') - assert b'/xmlns/nyaa' in rv.data + def test_rss_url(self): + rv = self.app.get('/?page=rss') + assert b'/xmlns/nyaa' in rv.data - def test_login_url(self): - rv = self.app.get('/login') - assert b'Username or email address' in rv.data + def test_login_url(self): + rv = self.app.get('/login') + assert b'Username or email address' in rv.data - def test_registry(self): - rv = self.app.get('/register') - assert b'Username' in rv.data - assert b'Password' in rv.data + def test_registry(self): + rv = self.app.get('/register') + assert b'Username' in rv.data + assert b'Password' in rv.data if __name__ == '__main__': - unittest.main() + unittest.main() From 6f526de5ea5ac2102b59b0fdd9bdba08f5b7038c Mon Sep 17 00:00:00 2001 From: Kfir Hadas Date: Wed, 7 Jun 2017 19:38:19 +0300 Subject: [PATCH 07/10] Lint --- nyaa/api_handler.py | 6 +++++- utils/api_info.py | 3 ++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/nyaa/api_handler.py b/nyaa/api_handler.py index 7e17745..bbbd845 100644 --- a/nyaa/api_handler.py +++ b/nyaa/api_handler.py @@ -322,7 +322,11 @@ def v2_api_info(torrent_id_or_hash): 'information': torrent.information, 'description': torrent.description, - 'stats': {'seeders': torrent.stats.seed_count, 'leechers': torrent.stats.leech_count, 'downloads': torrent.stats.download_count}, + 'stats': { + 'seeders': torrent.stats.seed_count, + 'leechers': torrent.stats.leech_count, + 'downloads': torrent.stats.download_count + }, 'filesize': torrent.filesize, 'files': files, diff --git a/utils/api_info.py b/utils/api_info.py index acff5d7..2f3de9e 100755 --- a/utils/api_info.py +++ b/utils/api_info.py @@ -112,7 +112,8 @@ if __name__ == '__main__': exit(1) else: formatted_filesize = easy_file_size(response.get('filesize', 0)) - flag_info = ', '.join(n+': '+_as_yes_no(response['is_'+n.lower()]) for n in FLAG_NAMES) + flag_info = ', '.join( + n + ': ' + _as_yes_no(response['is_' + n.lower()]) for n in FLAG_NAMES) info_str = INFO_TEMPLATE.format(formatted_filesize=formatted_filesize, flag_info=flag_info, **response) From e8e8c27994f6b80d305e89df907ce3e1af84ff0e Mon Sep 17 00:00:00 2001 From: Kfir Hadas Date: Wed, 7 Jun 2017 19:44:14 +0300 Subject: [PATCH 08/10] Update README.md Add Travis build status badge, move running tests next to code quality --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 2265042..ac9667f 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# NyaaV2 +# NyaaV2 [![Build Status](https://travis-ci.org/nyaadevs/nyaa.svg?branch=master)](https://travis-ci.org/nyaadevs/nyaa) ## Setting up for development This project uses Python 3.6. There are features used that do not exist in 3.5, so make sure to use Python 3.6. @@ -10,6 +10,11 @@ It's not impossible to run Nyaa on Windows, but this guide doesn't focus on that - You may also use `./lint.sh -c` to see a list of warnings/problems instead of having `lint.sh` making modifications for you - Other than PEP8, try to keep your code clean and easy to understand, as well. It's only polite! +### Running Tests +We have some basic tests that check if each page can render correctly. To run the tests: +- Make sure that you are in the python virtual environment. +- Run `python -m pytest tests/` while in the repository directory. + ### Setting up Pyenv pyenv eases the use of different Python versions, and as not all Linux distros offer 3.6 packages, it's right up our alley. - Install dependencies https://github.com/pyenv/pyenv/wiki/Common-build-problems @@ -108,8 +113,3 @@ However, take note that binglog is not necessary for simple ES testing and devel You're done! The script should now be feeding updates from the database to Elasticsearch. Take note, however, that the specified ES index refresh interval is 30 seconds, which may feel like a long time on local development. Feel free to adjust it or [poke Elasticsearch yourself!](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-refresh.html) - -### Running Tests -We have some basic tests that check if each page can render correctly. To run the tests: -- Make sure that you are in the python virtual environment. -- Run `python -m pytest tests/` while in the repository directory. From 95a6c6c799711b9c7026f3494da3ad15b3328f55 Mon Sep 17 00:00:00 2001 From: Kfir Hadas Date: Thu, 8 Jun 2017 04:04:20 +0300 Subject: [PATCH 09/10] Amend method name to reflect actual reg URL test Cherry-picked 3bb9517f2a61a00fd1443d94ffd11f1f34d33ee6 --- tests/test_nyaa.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_nyaa.py b/tests/test_nyaa.py index 478a4c0..9a81330 100644 --- a/tests/test_nyaa.py +++ b/tests/test_nyaa.py @@ -43,7 +43,7 @@ class NyaaTestCase(unittest.TestCase): rv = self.app.get('/login') assert b'Username or email address' in rv.data - def test_registry(self): + def test_registration_url(self): rv = self.app.get('/register') assert b'Username' in rv.data assert b'Password' in rv.data From 16e72a2a9c886287300240b183013a1a953057ed Mon Sep 17 00:00:00 2001 From: Kfir Hadas Date: Thu, 8 Jun 2017 04:13:46 +0300 Subject: [PATCH 10/10] set noqa to ignore a single error --- nyaa/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nyaa/__init__.py b/nyaa/__init__.py index 9ef9bfb..cb406aa 100644 --- a/nyaa/__init__.py +++ b/nyaa/__init__.py @@ -70,4 +70,4 @@ assets = Environment(app) # output='style.css', depends='**/*.scss') # assets.register('style_all', css) -from nyaa import routes # noqa +from nyaa import routes # noqa E402