From 2e9c5964d5d3500932b901cb7f9008c380f39fd5 Mon Sep 17 00:00:00 2001 From: cclauss Date: Tue, 4 Jul 2017 08:01:39 +0200 Subject: [PATCH] test_main uses test_utils plus more tests --- test_main.py | 119 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 80 insertions(+), 39 deletions(-) diff --git a/test_main.py b/test_main.py index d1190c6..dd13e46 100644 --- a/test_main.py +++ b/test_main.py @@ -1,16 +1,14 @@ -#!/usr/bin/env python -m pytest --capture=sys +#!/usr/bin/env python3 + +# To run the tests, use: python3 -m pytest --capture=sys -from collections import namedtuple from database import Database from main import main +import pytest +from test_utils import region_dict -region_record = namedtuple('region_record', 'roman_number start end') -region_dict = {"kanto": region_record("I", 1, 151), - "johto": region_record("II", 152, 251), - "hoenn": region_record("III", 252, 386), - "sinnoh": region_record("IV", 387, 493), - "extra": region_record("", 494, 100000)} db = Database() +print(len(db)) def test_no_args(capsys): @@ -19,38 +17,81 @@ def test_no_args(capsys): assert out.startswith("No command line arguments specified.") -def test_len(): - # Database unfortunately makes db.__MAX_ID private :-( - __MAX_ID = 493 - assert len(db) == __MAX_ID + len(db.get_extra()) - - -def _test_region(region_name): - region_name = (region_name or 'extra').lower() - # Database unfortunately makes db.__get_region() private :-( - func = {"kanto": db.get_kanto, - "johto": db.get_johto, - "hoenn": db.get_hoenn, - "sinnoh": db.get_sinnoh, - "extra": db.get_extra}[region_name] - pokemon_list = func() - region_record = region_dict[region_name] - start = region_record.start - end = len(db) if region_name == "extra" else region_record.end - # make sure there are no missing pokemon - assert len(pokemon_list) == end - start + 1 - if region_name == "extra": - return - # make sure that all pokemon.id are in the ID range - assert all([start <= int(p.get_id()) <= end for p in pokemon_list]) - - -def test_regions(): - for region_name in region_dict: - _test_region(region_name) - - def test_three_args(capsys): main([__file__, 1, 2, 3]) out, err = capsys.readouterr() assert out.startswith("Invalid number of arguments.") + + +def test_two_letters(capsys): + main([__file__, 'bu']) + out, err = capsys.readouterr() + assert 'Butterfree' in out + # prefix search only + main([__file__, 'ut']) + out, err = capsys.readouterr() + assert 'butterfree' not in out.lower() + + +def failing_test_extra(capsys): # FIXME + main([__file__, 'extra']) + out, err = capsys.readouterr() + assert out.count('Castform') == 3, out + assert 'turtwig' not in out.lower() + + +def test_region_names(capsys): + main([__file__, 'regions']) + out, err = capsys.readouterr() + for region_name in region_dict: + assert region_name in out + + +def test_help(capsys): + main([__file__, 'help']) + out, err = capsys.readouterr() + assert 'Usage:' in out + main([__file__, '-h']) + out2, err = capsys.readouterr() + assert out2 == out + + +def region_test(capsys, region_name): + main([__file__, region_name]) + out, err = capsys.readouterr() + # matrix test of first pokemon name and last pokemon name from all regions + for name, region_info in region_dict.items(): + if name == 'extra': + continue + assert (region_info.first in out) == (name == region_name) + assert (region_info.last in out) == (name == region_name) + + +def test_kanto(capsys): + region_test(capsys, 'kanto') + + +def test_johto(capsys): + region_test(capsys, 'johto') + + +def test_hoenn(capsys): + region_test(capsys, 'hoenn') + + +def test_sinnoh(capsys): + region_test(capsys, 'sinnoh') + + +def test_all(capsys): + main([__file__, 'all']) + out, err = capsys.readouterr() + for region_info in region_dict.values(): + assert (region_info.first or '') in out # convert None --> '' + assert (region_info.last or '') in out # convert None --> '' + + +def test_question_mark(capsys): + main([__file__, '?']) + out, err = capsys.readouterr() + assert 'deprecated' in out