mirror of
https://github.com/davegallant/rfd.git
synced 2025-08-05 16:23:40 +00:00
Add integration tests (#79)
Adds integration tests in pytest to detect breaking changes.
This commit is contained in:
20
Makefile
20
Makefile
@@ -32,29 +32,15 @@ lint:
|
||||
.PHONY: lint
|
||||
|
||||
## test: Run all unit tests
|
||||
test: tmp/.tests-passed.sentinel
|
||||
test:
|
||||
> pytest -vvv tests
|
||||
.PHONY: test
|
||||
|
||||
## examples: Run basic commands
|
||||
examples: tmp/.tests-passed.sentinel
|
||||
> rfd --version
|
||||
> rfd threads >/dev/null
|
||||
> rfd threads --sort-by score >/dev/null
|
||||
> rfd search 'pizza' >/dev/null
|
||||
> rfd search '(coffee|starbucks)' >/dev/null
|
||||
.PHONY: examples
|
||||
|
||||
# Tests - re-ran if any file under src has been changed since tmp/.tests-passed.sentinel was last touched
|
||||
tmp/.tests-passed.sentinel: $(shell find ${SRC} -type f)
|
||||
> mkdir -p $(@D)
|
||||
> pytest -v
|
||||
> touch $@
|
||||
|
||||
## pr: Run pre-commit, lint and test
|
||||
pr: precommit lint test
|
||||
.PHONY: pr
|
||||
|
||||
ci: lint test examples
|
||||
ci: lint test
|
||||
.PHONY: ci
|
||||
|
||||
## help: Print this help message
|
||||
|
37
rfd/cli.py
37
rfd/cli.py
@@ -7,11 +7,11 @@ import sys
|
||||
import click
|
||||
from colorama import init, Fore, Style
|
||||
from .api import get_threads, get_posts
|
||||
from .threads import parse_threads, search_threads, sort_threads
|
||||
from .threads import parse_threads, search_threads, sort_threads, generate_thread_output
|
||||
from .scores import get_vote_color
|
||||
from .__version__ import version as current_version
|
||||
|
||||
init()
|
||||
print()
|
||||
|
||||
logging.getLogger()
|
||||
logging.getLogger().setLevel(logging.INFO)
|
||||
@@ -27,14 +27,6 @@ def get_terminal_width():
|
||||
return int(columns)
|
||||
|
||||
|
||||
def get_vote_color(score):
|
||||
if score > 0:
|
||||
return Fore.GREEN + " [+" + str(score) + "] "
|
||||
if score < 0:
|
||||
return Fore.RED + " [" + str(score) + "] "
|
||||
return Fore.BLUE + " [" + str(score) + "] "
|
||||
|
||||
|
||||
def print_version(ctx, value):
|
||||
if not value or ctx.resilient_parsing:
|
||||
return
|
||||
@@ -42,31 +34,6 @@ def print_version(ctx, value):
|
||||
ctx.exit()
|
||||
|
||||
|
||||
def generate_thread_output(_threads):
|
||||
for count, thread in enumerate(_threads, 1):
|
||||
output = ""
|
||||
dealer = thread.dealer_name
|
||||
if dealer and dealer is not None:
|
||||
dealer = "[" + dealer + "] "
|
||||
else:
|
||||
dealer = ""
|
||||
output += (
|
||||
" "
|
||||
+ str(count)
|
||||
+ "."
|
||||
+ get_vote_color(thread.score)
|
||||
+ Fore.RESET
|
||||
+ "%s%s" % (dealer, thread.title)
|
||||
+ Fore.LIGHTYELLOW_EX
|
||||
+ " (%d views)" % thread.views
|
||||
+ Fore.RESET
|
||||
)
|
||||
output += Fore.BLUE + " {}".format(thread.url)
|
||||
output += Style.RESET_ALL
|
||||
output += "\n\n"
|
||||
yield output
|
||||
|
||||
|
||||
@click.group(invoke_without_command=True)
|
||||
@click.option(
|
||||
"-v",
|
||||
|
@@ -1,3 +1,6 @@
|
||||
from colorama import Fore
|
||||
|
||||
|
||||
def calculate_score(post):
|
||||
"""Calculate either topic or post score. If votes cannot be retrieved, the score is 0.
|
||||
|
||||
@@ -16,3 +19,11 @@ def calculate_score(post):
|
||||
pass
|
||||
|
||||
return score
|
||||
|
||||
|
||||
def get_vote_color(score):
|
||||
if score > 0:
|
||||
return Fore.GREEN + " [+" + str(score) + "] "
|
||||
if score < 0:
|
||||
return Fore.RED + " [" + str(score) + "] "
|
||||
return Fore.BLUE + " [" + str(score) + "] "
|
||||
|
@@ -1,6 +1,7 @@
|
||||
import re
|
||||
from colorama import Fore, Style
|
||||
from . import API_BASE_URL
|
||||
from .scores import calculate_score
|
||||
from .scores import calculate_score, get_vote_color
|
||||
|
||||
# pylint: disable=old-style-class
|
||||
class Thread:
|
||||
@@ -74,3 +75,28 @@ def search_threads(threads, regex):
|
||||
deal.dealer_name and regexp.search(deal.dealer_name.lower())
|
||||
):
|
||||
yield deal
|
||||
|
||||
|
||||
def generate_thread_output(threads):
|
||||
for count, thread in enumerate(threads, 1):
|
||||
output = ""
|
||||
dealer = thread.dealer_name
|
||||
if dealer and dealer is not None:
|
||||
dealer = "[" + dealer + "] "
|
||||
else:
|
||||
dealer = ""
|
||||
output += (
|
||||
" "
|
||||
+ str(count)
|
||||
+ "."
|
||||
+ get_vote_color(thread.score)
|
||||
+ Fore.RESET
|
||||
+ "%s%s" % (dealer, thread.title)
|
||||
+ Fore.LIGHTYELLOW_EX
|
||||
+ " (%d views)" % thread.views
|
||||
+ Fore.RESET
|
||||
)
|
||||
output += Fore.BLUE + " {}".format(thread.url)
|
||||
output += Style.RESET_ALL
|
||||
output += "\n\n"
|
||||
yield output
|
||||
|
25
tests/integration/test_cli.py
Normal file
25
tests/integration/test_cli.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from subprocess import Popen, PIPE
|
||||
import pytest
|
||||
|
||||
|
||||
def run_cli(args):
|
||||
cmd = ["python", "-m", "rfd"] + args.split()
|
||||
p = Popen(cmd, stdout=PIPE)
|
||||
stdout, _ = p.communicate()
|
||||
assert p.returncode == 0
|
||||
return stdout
|
||||
|
||||
|
||||
def test_version():
|
||||
stdout = run_cli("--version")
|
||||
assert b"rfd v" in stdout
|
||||
|
||||
|
||||
@pytest.mark.parametrize("args", ["", "--sort-by score"])
|
||||
def test_threads(args):
|
||||
run_cli("threads " + args)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("args", ["'pizza'", "'(coffee|starbucks)'"])
|
||||
def test_search(args):
|
||||
run_cli("search " + args)
|
Reference in New Issue
Block a user