Bladeren bron

Add CLI and parser tests

theenglishway (time) 7 jaren geleden
bovenliggende
commit
8b53716bef
8 gewijzigde bestanden met toevoegingen van 90 en 44 verwijderingen
  1. 2 0
      setup.cfg
  2. 27 0
      tests/conftest.py
  3. 38 0
      tests/test_cli.py
  4. 12 0
      tests/test_parser.py
  5. 0 37
      tests/test_twhatter.py
  6. 2 3
      twhatter/__main__.py
  7. 8 3
      twhatter/cli.py
  8. 1 1
      twhatter/parser/__init__.py

+ 2 - 0
setup.cfg

@@ -75,3 +75,5 @@ console_scripts =
 [options.extras_require]
 test =
 	pytest
+markers =
+    send_request: mark a test as requiring to send a specific web request (slow).

+ 27 - 0
tests/conftest.py

@@ -0,0 +1,27 @@
+import pytest
+from click.testing import CliRunner
+from twhatter.api import ApiUser
+from bs4 import BeautifulSoup
+
+
+@pytest.fixture
+def cli_runner():
+    """Runner for Click"""
+    return CliRunner()
+
+
+@pytest.fixture(scope="session")
+def user():
+    return "the_english_way"
+
+
+@pytest.fixture(scope="session")
+def tweet_limit():
+    return 10
+
+
+@pytest.fixture(scope="session")
+def raw_html_user_initial_page(user):
+    a = ApiUser(user)
+    response = a.get_initial()
+    return BeautifulSoup(response.text, "lxml")

+ 38 - 0
tests/test_cli.py

@@ -0,0 +1,38 @@
+#!/usr/bin/env python
+# coding: utf-8
+
+"""Tests for `twhatter` package."""
+import pytest
+
+from twhatter import cli
+
+
+def test_command_line_interface(cli_runner):
+    """Test the CLI."""
+    result = cli_runner.invoke(cli.main)
+    assert result.exit_code == 0
+
+    help_result = cli_runner.invoke(cli.main, ['--help'])
+    assert help_result.exit_code == 0
+    assert '--help  Show this message and exit.' in help_result.output
+
+
+class TestOwn:
+    @pytest.mark.send_request
+    def test_no_limit(self, cli_runner, user):
+        result = cli_runner.invoke(cli.main, ['own', user])
+        assert result.exit_code == 0
+
+        lines = result.output.split('\n')[:-1]
+        assert 100 > len(lines) > 0
+
+        for l in lines:
+            assert "Tweet" in l
+
+    @pytest.mark.send_request
+    def test_limit(self, cli_runner, user, tweet_limit):
+        result = cli_runner.invoke(cli.main, ['own', user, '--limit', tweet_limit])
+        assert result.exit_code == 0
+
+        lines = result.output.split('\n')[:-1]
+        assert len(lines) == tweet_limit

+ 12 - 0
tests/test_parser.py

@@ -0,0 +1,12 @@
+from twhatter.parser import TweetList, Tweet
+
+
+class TestTweetList:
+    def test_len(self, raw_html_user_initial_page):
+        t_list = TweetList(raw_html_user_initial_page)
+        assert len(t_list) == 20
+
+    def test_iter(self, raw_html_user_initial_page):
+        t_list = TweetList(raw_html_user_initial_page)
+        for t in t_list:
+            assert isinstance(t, Tweet)

+ 0 - 37
tests/test_twhatter.py

@@ -1,37 +0,0 @@
-#!/usr/bin/env python
-# coding: utf-8
-
-"""Tests for `twhatter` package."""
-
-import pytest
-from click.testing import CliRunner
-
-from twhatter import twhatter
-from twhatter import cli
-
-
-@pytest.fixture
-def response():
-    """Sample pytest fixture.
-
-    See more at: http://doc.pytest.org/en/latest/fixture.html
-    """
-    # import requests
-    # return requests.get('https://github.com/audreyr/cookiecutter-pypackage')
-
-
-def test_content(response):
-    """Sample pytest test function with the pytest fixture as an argument."""
-    # from bs4 import BeautifulSoup
-    # assert 'GitHub' in BeautifulSoup(response.content).title.string
-
-
-def test_command_line_interface():
-    """Test the CLI."""
-    runner = CliRunner()
-    result = runner.invoke(cli.main)
-    assert result.exit_code == 0
-    assert 'twhatter.cli.main' in result.output
-    help_result = runner.invoke(cli.main, ['--help'])
-    assert help_result.exit_code == 0
-    assert '--help  Show this message and exit.' in help_result.output

+ 2 - 3
twhatter/__main__.py

@@ -1,9 +1,8 @@
 from twhatter.api import ApiUser
-from bs4 import BeautifulSoup
-from twhatter.parser import TweetList
 from twhatter.output import Print
 
-user="the_english_way"
+
+user = "the_english_way"
 a = ApiUser(user)
 
 for t in a.iter_tweets():

+ 8 - 3
twhatter/cli.py

@@ -13,13 +13,18 @@ def main():
 
 
 @main.command()
+@click.option('-l', '--limit', type=int, default=100, show_default=True)
 @click.argument('user')
-def own(user):
-    """Get all the user's own publications"""
+def own(user, limit):
+    """Get some user's Tweets"""
     a = ApiUser(user)
 
-    for t in a.iter_tweets():
+    for n, t in enumerate(a.iter_tweets()):
+        if n >= limit:
+            break
+
         click.echo(t)
 
+
 if __name__ == "__main__":
     main()

+ 1 - 1
twhatter/parser/__init__.py

@@ -1 +1 @@
-from .tweet import TweetList
+from .tweet import TweetList, Tweet