Add integration tests (#79)

Adds integration tests in pytest to detect breaking changes.
This commit is contained in:
Dave Gallant
2020-08-15 20:55:10 -04:00
committed by GitHub
parent f664cbd9c6
commit cfd7e01e43
5 changed files with 68 additions and 53 deletions

View File

@@ -32,29 +32,15 @@ lint:
.PHONY: lint .PHONY: lint
## test: Run all unit tests ## test: Run all unit tests
test: tmp/.tests-passed.sentinel test:
> pytest -vvv tests
.PHONY: test .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: Run pre-commit, lint and test
pr: precommit lint test pr: precommit lint test
.PHONY: pr .PHONY: pr
ci: lint test examples ci: lint test
.PHONY: ci .PHONY: ci
## help: Print this help message ## help: Print this help message

View File

@@ -7,11 +7,11 @@ import sys
import click import click
from colorama import init, Fore, Style from colorama import init, Fore, Style
from .api import get_threads, get_posts 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 from .__version__ import version as current_version
init() init()
print()
logging.getLogger() logging.getLogger()
logging.getLogger().setLevel(logging.INFO) logging.getLogger().setLevel(logging.INFO)
@@ -27,14 +27,6 @@ def get_terminal_width():
return int(columns) 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): def print_version(ctx, value):
if not value or ctx.resilient_parsing: if not value or ctx.resilient_parsing:
return return
@@ -42,31 +34,6 @@ def print_version(ctx, value):
ctx.exit() 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.group(invoke_without_command=True)
@click.option( @click.option(
"-v", "-v",

View File

@@ -1,3 +1,6 @@
from colorama import Fore
def calculate_score(post): def calculate_score(post):
"""Calculate either topic or post score. If votes cannot be retrieved, the score is 0. """Calculate either topic or post score. If votes cannot be retrieved, the score is 0.
@@ -16,3 +19,11 @@ def calculate_score(post):
pass pass
return score 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) + "] "

View File

@@ -1,6 +1,7 @@
import re import re
from colorama import Fore, Style
from . import API_BASE_URL from . import API_BASE_URL
from .scores import calculate_score from .scores import calculate_score, get_vote_color
# pylint: disable=old-style-class # pylint: disable=old-style-class
class Thread: class Thread:
@@ -74,3 +75,28 @@ def search_threads(threads, regex):
deal.dealer_name and regexp.search(deal.dealer_name.lower()) deal.dealer_name and regexp.search(deal.dealer_name.lower())
): ):
yield deal 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

View 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)