From cfd7e01e43585d1c0525414ccd0932eea37ee15f Mon Sep 17 00:00:00 2001 From: Dave Gallant Date: Sat, 15 Aug 2020 20:55:10 -0400 Subject: [PATCH] Add integration tests (#79) Adds integration tests in pytest to detect breaking changes. --- Makefile | 20 +++---------------- rfd/cli.py | 37 ++--------------------------------- rfd/scores.py | 11 +++++++++++ rfd/threads.py | 28 +++++++++++++++++++++++++- tests/integration/test_cli.py | 25 +++++++++++++++++++++++ 5 files changed, 68 insertions(+), 53 deletions(-) create mode 100644 tests/integration/test_cli.py diff --git a/Makefile b/Makefile index 7ba1813..142cc25 100644 --- a/Makefile +++ b/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 diff --git a/rfd/cli.py b/rfd/cli.py index d0bccb9..d9fdaa6 100644 --- a/rfd/cli.py +++ b/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", diff --git a/rfd/scores.py b/rfd/scores.py index c7352a6..1ad317e 100644 --- a/rfd/scores.py +++ b/rfd/scores.py @@ -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) + "] " diff --git a/rfd/threads.py b/rfd/threads.py index 5c9f5c7..16145ee 100644 --- a/rfd/threads.py +++ b/rfd/threads.py @@ -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 diff --git a/tests/integration/test_cli.py b/tests/integration/test_cli.py new file mode 100644 index 0000000..c8d8963 --- /dev/null +++ b/tests/integration/test_cli.py @@ -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)