From d96d4a8bb5e35dc835f72038a8bbb918bcba95e4 Mon Sep 17 00:00:00 2001 From: Samuel Henrique Date: Thu, 6 Jul 2017 12:32:27 -0300 Subject: [PATCH] More testing - Adds new tests of the new command engine - Regions and pokemon types and names are now case insensitive - Roll back some of the removed tests. --- filters/__init__.py | 22 +++++++++++++++++----- main.py | 11 +++++++---- test_filters.py | 29 +++++++++++++++++++++++++++++ test_main.py | 26 +++++++++++++++++++++++++- 4 files changed, 78 insertions(+), 10 deletions(-) create mode 100644 test_filters.py diff --git a/filters/__init__.py b/filters/__init__.py index a8565e9..cb97fdf 100644 --- a/filters/__init__.py +++ b/filters/__init__.py @@ -6,6 +6,7 @@ class Filter(Action): POKEMON_LIST = Database().get_all() filtered_list = [p for p in POKEMON_LIST] FILTERS = [] + EXAMPLE_VAL = None def matches(self, pokemon, value): raise NotImplementedError @@ -19,27 +20,38 @@ class Filter(Action): class NameFilter(Filter): - def matches(self, pokemon: Pokemon, value): + EXAMPLE_VAL = 'bulb' + + def matches(self, pokemon: Pokemon, value: str): return value in pokemon.get_name() class RegionFilter(Filter): - def matches(self, pokemon: Pokemon, value): + EXAMPLE_VAL = 'kanto' + + def matches(self, pokemon: Pokemon, value: str): return pokemon.get_region() == value class LightFilter(Filter): - def matches(self, pokemon: Pokemon, value): + EXAMPLE_VAL = 0.7 + + def matches(self, pokemon: Pokemon, value: float): return pokemon.get_dark_threshold() > value class DarkFilter(Filter): - def matches(self, pokemon: Pokemon, value): + EXAMPLE_VAL = 0.4 + + def matches(self, pokemon: Pokemon, value: float): return pokemon.get_dark_threshold() < value class TypeFilter(Filter): - def matches(self, pokemon: Pokemon, value): + EXAMPLE_VAL = 'water' + + def matches(self, pokemon: Pokemon, value: str): + value = value.lower() return value in (pokemon.get_pkmn_type(), pokemon.get_pkmn_type_secondary()) diff --git a/main.py b/main.py index 70dfaba..7ce3e87 100755 --- a/main.py +++ b/main.py @@ -63,11 +63,13 @@ def main(argv): 'various conditions' ) filtersGroup.add_argument( - '-n', '--name', help='Filter by pokemon which ' - 'name contains NAME', action=filters.NameFilter) + '-n', '--name', help='Filter by pokemon which name contains NAME', + action=filters.NameFilter, type=str.lower + ) filtersGroup.add_argument( '-r', '--region', help='Filter the pokemons by region', - action=filters.RegionFilter, choices=Database.REGIONS + action=filters.RegionFilter, choices=Database.REGIONS, + type=str.lower ) filtersGroup.add_argument( '-l', '--light', help='Filter out the pokemons darker then 0.xx', @@ -81,7 +83,8 @@ def main(argv): ) filtersGroup.add_argument( '-t', '--type', help='Filter the pokemons by type.', - action=filters.TypeFilter, choices=Database.POKEMON_TYPES + action=filters.TypeFilter, choices=Database.POKEMON_TYPES, + type=str.lower ) filtersGroup.add_argument( '-ne', '--no-extras', help='Excludes extra pokemons', diff --git a/test_filters.py b/test_filters.py new file mode 100644 index 0000000..800a177 --- /dev/null +++ b/test_filters.py @@ -0,0 +1,29 @@ +from filters import Filter +import pytest + + +def test_basic_loading(): + assert len(Filter.POKEMON_LIST) >= 493 + assert len(Filter.filtered_list) == len(Filter.POKEMON_LIST) + + +def test_filters_infrastructure(): + inst = Filter(None, None) + with pytest.raises(NotImplementedError): + inst.matches(None, None) + for fltr in Filter.FILTERS: + fltr = fltr(None, None) + filtered = [pkmn for pkmn in Filter.POKEMON_LIST + if fltr.matches(pkmn, fltr.EXAMPLE_VAL)] + assert len(filtered) < len(Filter.POKEMON_LIST) + + +if __name__ == '__main__': + # Test runner: Runs all functions whose name begins with `test_` + # locals() changes when trying to do this without the list comprehension!!! + name_funcs = [(n, f) for n, f in locals().items() if n.startswith('test_')] + for name, func in name_funcs: + if callable(func): + func() + else: + print(name + ' is not callable()!') diff --git a/test_main.py b/test_main.py index 5df0365..fb32182 100644 --- a/test_main.py +++ b/test_main.py @@ -10,7 +10,31 @@ from test_utils import region_dict import random db = Database() -print(len(db)) + + +def broken_test_no_args(capsys): + """ FIXME: Now the the main file accepts zero arguments """ + main([__file__]) + out, err = capsys.readouterr() + assert out.startswith("No command line arguments specified.") + + +def broken_test_three_args(capsys): + """ FIXME: Now the main file accepts way more then 3 arguments """ + main([__file__, 1, 2, 3]) + out, err = capsys.readouterr() + assert out.startswith("Invalid number of arguments.") + + +def broken_test_two_letters(capsys): + """ FIXME: The search argorhytm is now bultin the name filter """ + 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 test_extra(capsys):