def test_will_birth(self, mocker): """Tests that the will_birth method works correctly. The weights of herbivores and carnivores are set as needed to have the correct return object from the will_birth method. The mocker is used to give spesific values from random functions used in the module. """ h = Herb() c = Carn() h.weight = 0 c.weight = 0 return_object_herb = h.will_birth(10) return_object_carn = c.will_birth(10) assert return_object_herb is None assert return_object_carn is None mocker.patch("random.uniform", return_value=0) h = Herb() c = Carn() h.weight = 100 c.weight = 100 return_object_herb = h.will_birth(10000) return_object_carn = c.will_birth(10000) assert isinstance(return_object_herb, Herb) assert isinstance(return_object_carn, Carn)
def test_weightloss(self, mocker): """Tests that the weight is updated according to the weightloss method. The mocker is used to give spesific values from random functions used in the module. """ mocker.patch("numpy.random.normal", return_value=1) h = Herb() h.weightloss() assert h.weight == 1 - h.eta * 1 h.weight = 100000 c = Carn() c.weight = 100000 weight_list_herb = [] weight_list_carn = [] for _ in range(100): weight_list_herb.append(h.weight) weight_list_carn.append(c.weight) h.weightloss() c.weightloss() assert all([ weight1 > weight2 for weight1, weight2 in zip( weight_list_herb[:-1], weight_list_herb[1:]) ]), "The weight does not decrease in the list when weightloss is used" assert all([ weight1 > weight2 for weight1, weight2 in zip( weight_list_carn[:-1], weight_list_carn[1:]) ]), "The weight does not decrease in the list when weightloss is used"
def test_fitness_update(self, mocker): """Tests that the fitness gets updated for given values of weight. The mocker is used to give spesific values from random functions used in the module. """ h = Herb() c = Carn() h.weight = -3 c.weight = -2 h.fitness_update() c.fitness_update() assert h.fitness == 0 assert c.fitness == 0 mocker.patch("numpy.random.normal", return_value=0) h = Herb() c = Carn() h.fitness_update() c.fitness_update() assert h.fitness == 0 assert c.fitness == 0 mocker.patch("numpy.random.normal", return_value=1) h = Herb() c = Carn() h.fitness_update() c.fitness_update() assert h.fitness == pytest.approx( 1 / (1 + m.exp(h.phi_age * (h.a - h.a_half))) * 1 / (1 + m.exp(-h.phi_weight * (1 - h.w_half)))) assert c.fitness == pytest.approx( 1 / (1 + m.exp(c.phi_age * (c.a - c.a_half))) * 1 / (1 + m.exp(-c.phi_weight * (1 - c.w_half))))
def test_fitness_update(self, mocker): h = Herb() c = Carn() h.weight = -3 c.weight = -2 h.fitness_update() c.fitness_update() assert h.fitness == 0 assert c.fitness == 0 mocker.patch('numpy.random.normal', return_value=0) h = Herb() c = Carn() h.fitness_update() c.fitness_update() assert h.fitness == 0 assert c.fitness == 0 mocker.patch('numpy.random.normal', return_value=1) h = Herb() c = Carn() h.fitness_update() c.fitness_update() assert h.fitness == pytest.approx( 1 / (1 + m.exp(h.phi_age * (h.a - h.a_half))) * 1 / (1 + m.exp(-h.phi_weight * (1 - h.w_half)))) assert c.fitness == pytest.approx( 1 / (1 + m.exp(c.phi_age * (c.a - c.a_half))) * 1 / (1 + m.exp(-c.phi_weight * (1 - c.w_half))))
def test_will_birth(self, mocker): h = Herb() c = Carn() h.weight = 0 c.weight = 0 return_object_herb = h.will_birth(10) return_object_carn = c.will_birth(10) assert return_object_herb is None assert return_object_carn is None mocker.patch('random.uniform', return_value=0) h = Herb() c = Carn() h.weight = 100 c.weight = 100 return_object_herb = h.will_birth(10000) return_object_carn = c.will_birth(10000) assert isinstance(return_object_herb, Herb) assert isinstance(return_object_carn, Carn)
def test_weightloss(self, mocker): mocker.patch('numpy.random.normal', return_value=1) h = Herb() h.weightloss() assert h.weight == 1 - h.eta * 1 h.weight = 100000 c = Carn() c.weight = 100000 weight_list_herb = [] weight_list_carn = [] for _ in range(100): weight_list_herb.append(h.weight) weight_list_carn.append(c.weight) h.weightloss() c.weightloss() assert all([ weight1 > weight2 for weight1, weight2 in zip( weight_list_herb[:-1], weight_list_herb[1:]) ]) assert all([ weight1 > weight2 for weight1, weight2 in zip( weight_list_carn[:-1], weight_list_carn[1:]) ])