Skip to content

Instantly share code, notes, and snippets.

@LevBravE
Created May 3, 2020 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LevBravE/e393830e7895db9ed048601855e2039d to your computer and use it in GitHub Desktop.
Save LevBravE/e393830e7895db9ed048601855e2039d to your computer and use it in GitHub Desktop.
import pytest
import sys
import solution
class TestAggregatorsBase:
@pytest.fixture
def base_class(self):
return None
@pytest.fixture
def base_obj(self, base_class):
return base_class()
@pytest.fixture
def odd_child_class(self):
return None
@pytest.fixture
def odd_child_obj(self, odd_child_class):
return odd_child_class()
@pytest.fixture
def even_child_class(self):
return None
@pytest.fixture
def even_child_obj(self, even_child_class):
return even_child_class()
@pytest.fixture
def square_child_class(self):
return None
@pytest.fixture
def square_child_obj(self, square_child_class):
return square_child_class()
@pytest.fixture
def cube_child_class(self):
return None
@pytest.fixture
def cube_child_obj(self, cube_child_class):
return cube_child_class()
@pytest.fixture
def aggregation_method(self):
raise NotImplementedError
def test_base_defined(self, base_class):
if base_class is None:
raise NotImplementedError
def test_base_has_methods(self, base_obj, aggregation_method):
assert hasattr(base_obj, 'condition')
assert hasattr(base_obj, aggregation_method)
def test_condition_base(self, base_obj):
"""
Condition for base class is always true
"""
for n in range(1, 100):
assert base_obj.condition(n)
def test_aggregation_base(self, base_obj, aggregation_method):
for n_min in range(1, 6):
for n_max in range(n_min, 11):
aggregation_result = getattr(base_obj, aggregation_method)(n_min, n_max)
if aggregation_method == "count":
assert aggregation_result == n_max - n_min + 1
elif aggregation_method == "minimum":
assert aggregation_result == n_min
elif aggregation_method == "maximum":
assert aggregation_result == n_max
else:
raise Exception("Unknown aggregation_method: {}".format(aggregation_method))
def test_inheritance(self,
base_class,
odd_child_class,
even_child_class,
square_child_class,
cube_child_class):
if odd_child_class is not None:
assert issubclass(odd_child_class, base_class)
if even_child_class is not None:
assert issubclass(even_child_class, base_class)
if square_child_class is not None:
assert issubclass(square_child_class, base_class)
if cube_child_class is not None:
assert issubclass(cube_child_class, base_class)
def test_condition_odd_child(self, odd_child_class):
if odd_child_class is not None:
obj = odd_child_class()
for i in [1, 3, 5, 7, 9]:
assert obj.condition(i)
for j in [2, 4, 6, 8, 10]:
assert not obj.condition(j)
def test_condition_even_child(self, even_child_class):
if even_child_class is not None:
obj = even_child_class()
for i in [1, 3, 5, 7, 9]:
assert not obj.condition(i)
for j in [2, 4, 6, 8, 10]:
assert obj.condition(j)
def test_condition_square_child(self, square_child_class):
if square_child_class is not None:
obj = square_child_class()
for i in [1, 4, 9]:
assert obj.condition(i)
for j in [2, 3, 5, 7, 8]:
assert not obj.condition(j)
def test_condition_cube_child(self, cube_child_class):
if cube_child_class is not None:
obj = cube_child_class()
for i in [1, 8, 27, 64]:
assert obj.condition(i)
for j in [2, 4, 9]:
assert not obj.condition(j)
# virtual method
def test_aggregation_subclasses(self, *subclasses):
raise NotImplementedError
class Test1(TestAggregatorsBase):
@pytest.fixture
def base_class(self):
return solution.Counter
@pytest.fixture
def aggregation_method(self):
return 'count'
@pytest.fixture
def odd_child_class(self):
return solution.OddCounter
@pytest.fixture
def square_child_class(self):
return solution.SquareCounter
def test_aggregation_subclasses(self, odd_child_obj, square_child_obj):
assert odd_child_obj.count(3, 9) == 4
assert square_child_obj.count(3, 9) == 2
class Test2(TestAggregatorsBase):
@pytest.fixture
def base_class(self):
return solution.Minimum
@pytest.fixture
def aggregation_method(self):
return 'minimum'
@pytest.fixture
def odd_child_class(self):
return solution.OddMinimum
@pytest.fixture
def cube_child_class(self):
return solution.CubeMinimum
def test_aggregation_subclasses(self, odd_child_obj, cube_child_obj):
assert odd_child_obj.minimum(4, 8) == 5
assert cube_child_obj.minimum(50, 100) == 64
class Test3(TestAggregatorsBase):
@pytest.fixture
def base_class(self):
return solution.Counter
@pytest.fixture
def aggregation_method(self):
return 'count'
@pytest.fixture
def even_child_class(self):
return solution.EvenCounter
@pytest.fixture
def cube_child_class(self):
return solution.CubeCounter
def test_aggregation_subclasses(self, even_child_obj, cube_child_obj):
assert even_child_obj.count(5, 12) == 4
assert cube_child_obj.count(1, 30) == 3
class Test4(TestAggregatorsBase):
@pytest.fixture
def base_class(self):
return solution.Maximum
@pytest.fixture
def aggregation_method(self):
return 'maximum'
@pytest.fixture
def odd_child_class(self):
return solution.OddMaximum
@pytest.fixture
def square_child_class(self):
return solution.SquareMaximum
def test_aggregation_subclasses(self, odd_child_obj, square_child_obj):
assert odd_child_obj.maximum(1, 10) == 9
assert odd_child_obj.maximum(1, 9) == 9
assert square_child_obj.maximum(1, 30) == 25
class Test5(TestAggregatorsBase):
@pytest.fixture
def base_class(self):
return solution.Minimum
@pytest.fixture
def aggregation_method(self):
return 'minimum'
@pytest.fixture
def even_child_class(self):
return solution.EvenMinimum
@pytest.fixture
def square_child_class(self):
return solution.SquareMinimum
def test_aggregation_subclasses(self, even_child_obj, square_child_obj):
assert even_child_obj.minimum(1, 10) == 2
assert even_child_obj.minimum(2, 10) == 2
assert square_child_obj.minimum(5, 20) == 9
class Test6(TestAggregatorsBase):
@pytest.fixture
def base_class(self):
return solution.Maximum
@pytest.fixture
def aggregation_method(self):
return 'maximum'
@pytest.fixture
def even_child_class(self):
return solution.EvenMaximum
@pytest.fixture
def cube_child_class(self):
return solution.CubeMaximum
def test_aggregation_subclasses(self, even_child_obj, cube_child_obj):
assert even_child_obj.maximum(1, 10) == 10
assert even_child_obj.maximum(1, 11) == 10
assert cube_child_obj.maximum(10, 30) == 27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment