Skip to content

Commit

Permalink
tests for main.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Audiosutras committed Nov 13, 2023
1 parent 5b21c70 commit d5e4b81
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/getdat/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
EBOOK_ERROR_MSG = "Please provide your search to continue."
8 changes: 6 additions & 2 deletions src/getdat/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import click
import webbrowser
from .utils import AnnasEbook, print_help
from .constants import EBOOK_ERROR_MSG

@click.group(epilog='Check out our docs at https://github.com/Audiosutras/getdat for more details')
def cli():
Expand Down Expand Up @@ -33,9 +34,12 @@ def cinema():
)
@click.argument('q', nargs=-1)
def ebook(q, ext, output_dir):
"""Search for a particular ebook using Anna's Archive"""
"""Search for a particular ebook using Anna's Archive
ex: getdat ebook <Search>
"""
if not q:
print_help("Please provide your search to continue.")
print_help(EBOOK_ERROR_MSG)
ebook = AnnasEbook(q=q, ext=ext, output_dir=output_dir)
ebook.run()

69 changes: 61 additions & 8 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from src import getdat
from src.getdat.main import cinema, ebook
from src.getdat.utils import AnnasEbook
from src.getdat.constants import EBOOK_ERROR_MSG

class TestCinema:
runner = CliRunner()
Expand All @@ -19,21 +20,54 @@ def test_cinema_launches_browser(self, mocker):
class TestEbook:
runner = CliRunner()

def test_ebook_no_args_print_help(self, mocker):
print_help = mocker.patch('getdat.utils.print_help')
def get_help_text(self):
help_text = self.runner.invoke(ebook, "--help")
return help_text.output

def test_no_args_print_help(self, mocker):
ebook_run_method = mocker.patch.object(AnnasEbook, 'run')
results = self.runner.invoke(ebook)
# assert error message echoed
assert EBOOK_ERROR_MSG in results.output
# assert help text prompt shown
assert self.get_help_text() in results.output
ebook_run_method.assert_not_called()

def test_no_args_only_ext_option_print_help(self, mocker):
ebook_run_method = mocker.patch.object(AnnasEbook, 'run')
results = self.runner.invoke(ebook, "--ext=epub")
# assert error message echoed
assert EBOOK_ERROR_MSG in results.output
# assert help text prompt shown
assert self.get_help_text() in results.output
ebook_run_method.assert_not_called()

def test_no_args_only_output_dir_option_print_help(self, mocker):
ebook_run_method = mocker.patch.object(AnnasEbook, 'run')
results = self.runner.invoke(ebook, "--output_dir=~/books")
# assert error message echoed
assert EBOOK_ERROR_MSG in results.output
# assert help text prompt shown
assert self.get_help_text() in results.output
ebook_run_method.assert_not_called()

def test_no_args_only_options_print_help(self, mocker):
ebook_run_method = mocker.patch.object(AnnasEbook, 'run')
self.runner.invoke(ebook, " ")
# print_help.assert_called_once()
results = self.runner.invoke(ebook, "--ext=pdf --output_dir=~/books")
# assert error message echoed
assert EBOOK_ERROR_MSG in results.output
# assert help text prompt shown
assert self.get_help_text() in results.output
ebook_run_method.assert_not_called()

def test_ebook_with_1_search_arg(self, mocker):
def test_1_search_arg_ebook_run(self, mocker):
ebook_run_method = mocker.patch.object(AnnasEbook, 'run')
self.runner.invoke(
ebook, "Commentary on the Explanation of the Beautiful Names of Allah"
)
ebook_run_method.assert_called_once()

def test_ebook_with_many_search_arg(self, mocker):
def test_many_search_arg_ebook_run(self, mocker):
ebook_run_method = mocker.patch.object(AnnasEbook, 'run')
self.runner.invoke(
ebook, [
Expand All @@ -50,8 +84,27 @@ def test_ebook_with_many_search_arg(self, mocker):
]
)
ebook_run_method.assert_called_once()


def test_search_arg_ext_option_ebook_run(self, mocker):
ebook_run_method = mocker.patch.object(AnnasEbook, 'run')
self.runner.invoke(
ebook,
"Commentary on the Explanation of the Beautiful Names of Allah --ext=epub"
)
ebook_run_method.assert_called_once()


def test_search_arg_output_dir_option_ebook_run(self, mocker):
ebook_run_method = mocker.patch.object(AnnasEbook, 'run')
self.runner.invoke(
ebook,
"Commentary on the Explanation of the Beautiful Names of Allah --output_dir=~/books/"
)
ebook_run_method.assert_called_once()

def test_search_arg_options_ebook_run(self, mocker):
ebook_run_method = mocker.patch.object(AnnasEbook, 'run')
self.runner.invoke(
ebook,
"Commentary on the Explanation of the Beautiful Names of Allah --ext=epub --output_dir=~/books/"
)
ebook_run_method.assert_called_once()

0 comments on commit d5e4b81

Please sign in to comment.