Beispiel #1
0
 def test_fitness_between_zero_and_one(self, example_properties_w_20):
     """
     Checks that find_fitness method calculates a fitness between 0 and 1.
     """
     animal = Animal(example_properties_w_20)
     animal.find_fitness()
     assert 0 <= animal.fitness <= 1
Beispiel #2
0
 def test_correct_prob_death(self, example_properties_w_20):
     """
     Asserts that prob_death calculates the correct probability of dying.
     """
     animal = Animal(example_properties_w_20)
     animal.find_fitness()
     assert animal.prob_death() == approx(0.0014929212599999687)
Beispiel #3
0
 def test_correct_fitness_calculated(self, example_properties_w_20):
     """
     Checks that find_fitness method calculates correct value of fitness.
     """
     animal = Animal(example_properties_w_20)
     animal.find_fitness()
     assert animal.fitness == approx(0.9983411986)
Beispiel #4
0
 def test_true_if_death_prob_is_zero(self, example_properties_w_40):
     """
     Assert that will_animal_live returns True if probability of dying
     is zero because of high fitness.
     """
     numpy.random.seed(1)
     animal = Animal(example_properties_w_40)
     assert animal.will_animal_live() is True
Beispiel #5
0
 def test_true_if_birth_prob_one(self, example_properties_w_40):
     """
     Tests the will_birth_take_place method by checking if True is returned
     when the probability of giving birth is 1.
     """
     animal = Animal(example_properties_w_40)
     animal.find_fitness()
     assert animal.will_birth_take_place(num_animals=6) is True
Beispiel #6
0
 def test_correct_prob_of_birth(self, example_properties_w_40):
     """
     Asserts that prob_give_birth calculates the correct probability of
     giving birth.
     """
     animal = Animal(example_properties_w_40)
     animal.find_fitness()
     assert animal.prob_give_birth(num_animals=6) == 1
Beispiel #7
0
 def test_correct_prob_of_moving(self, example_properties_w_20):
     """
     Asserts that prob_of_animal_moving calculates the correct probability
     of moving.
     """
     animal = Animal(example_properties_w_20)
     animal.find_fitness()
     assert animal.prob_of_animal_moving() == approx(0.3993364794)
Beispiel #8
0
 def test_is_animal_one_year_older(self, example_properties_w_20):
     """
     Checks that animal is one year older than last year after running
     make_animal_one_year_older method.
     """
     animal = Animal(example_properties_w_20)
     initial_age = animal.age
     animal.make_animal_one_year_older()
     assert animal.age - initial_age == 1
Beispiel #9
0
 def test_prob_of_birth_if_weight_less_than_limit(self,
                                                  example_properties_w_20):
     """
     Asserts that prob_give_birth returns a probability of zero if the
     mothers weight is less than the given limit.
     """
     animal = Animal(example_properties_w_20)
     animal.find_fitness()
     assert animal.prob_give_birth(num_animals=6) == 0
Beispiel #10
0
 def test_prob_of_birth_with_one_animal_in_cell(self,
                                                example_properties_w_20):
     """
     Asserts that if there is only one animal in the cell, then the
     prob_give_birth method returns a probability of zero.
     """
     animal = Animal(example_properties_w_20)
     animal.find_fitness()
     assert animal.prob_give_birth(num_animals=1) == 0
Beispiel #11
0
 def test_correct_bool_of_moving(self, example_properties_w_20, mocker):
     """
     Asserts that will_animal_move method returns True for a certain
     probability and a specific random number.
     """
     mocker.patch('numpy.random.random', return_value=0.1)
     animal = Animal(example_properties_w_20)
     animal.fitness = 1
     assert animal.will_animal_move() is True
Beispiel #12
0
 def test_fitness_zero_if_weight_zero(self, example_properties_w_20):
     """
     Checks that find_fitness method calculates a fitness of zero if
     weight of the animal is zero.
     """
     animal = Animal(example_properties_w_20)
     animal.weight = 0
     animal.find_fitness()
     assert animal.fitness == 0
Beispiel #13
0
 def test_has_animal_lost_weight(self, example_properties_w_20):
     """
     Checks that weight after weight loss is less than initial weight after
     running weight_loss method
     """
     animal = Animal(example_properties_w_20)
     initial_weight = animal.weight
     animal.weight_loss()
     assert animal.weight < initial_weight
Beispiel #14
0
 def test_false_if_rand_num_more_than_birth_prob(self, mocker):
     """
     Tests will_birth_take_place method by checking if False is returned
     when a random number larger than a specific probability is drawn.
     """
     test_properties_prob = {"species": "animal", "age": 63, "weight": 30}
     mocker.patch('numpy.random.random', return_value=0.95)
     animal = Animal(test_properties_prob)
     animal.find_fitness()
     assert animal.will_birth_take_place(num_animals=6) is not True
Beispiel #15
0
 def test_no_birth_if_baby_weight_is_zero(self, example_properties_w_40,
                                          mocker):
     """
     Tests birth_process method.
     Asserts that no baby is born if the baby's birth weight is
     equal to zero.
     """
     mocker.patch('numpy.random.normal', return_value=0)
     animal = Animal(example_properties_w_40)
     animal.find_fitness()
     assert animal.birth_process(num_animals=6) is None
Beispiel #16
0
 def test_no_birth_when_mothers_weight_small(self, example_properties_w_40,
                                             mocker):
     """
     Asserts that no baby is born from running birth_process
     when mother's weight is less than xi times baby's weight.
     """
     mocker.patch('numpy.random.normal', return_value=50)
     animal = Animal(example_properties_w_40)
     animal.find_fitness()
     baby_weight = animal.birth_process(num_animals=6)
     assert baby_weight is None
Beispiel #17
0
 def test_true_if_rand_num_more_than_death_prob(self,
                                                example_properties_w_20,
                                                mocker):
     """
     Asserts that will_animal_live returns True if the random number is
     larger than the death probability.
     """
     mocker.patch('numpy.random.random', return_value=0.5)
     animal = Animal(example_properties_w_20)
     animal.find_fitness()
     assert animal.will_animal_live() is True
Beispiel #18
0
 def test_has_animal_gained_weight_from_eating(self,
                                               example_properties_w_20):
     """
     Checks that herbivore has gained weight after running
     add_eaten_fodder_to_weight method.
     """
     animal = Animal(example_properties_w_20)
     test_fodder = 8
     initial_weight = animal.weight
     animal.add_eaten_fodder_to_weight(test_fodder)
     assert animal.weight > initial_weight
Beispiel #19
0
 def test_true_if_rand_num_less_than_birth_prob(self, mocker):
     """
     Tests will_birth_take_place method. Checks if True is returned when
     random number less than a specific probability is drawn.
     """
     test_properties_num_less = {
         "species": "animal",
         "age": 63,
         "weight": 30
     }
     mocker.patch('numpy.random.random', return_value=0.8)
     animal = Animal(test_properties_num_less)
     animal.find_fitness()
     assert animal.will_birth_take_place(num_animals=6) is True
Beispiel #20
0
 def test_baby_weight_returned_when_mothers_weight_large_enough(
         self, example_properties_w_40, mocker):
     """
     After running birth_process, we check that a birth took place by
     testing if baby's weight is a float of given value and that the baby's
     weight times xi was smaller than the mother's weight.
     """
     mocker.patch('numpy.random.normal', return_value=5.5)
     animal = Animal(example_properties_w_40)
     animal.find_fitness()
     baby_weight = animal.birth_process(num_animals=6)
     assert type(baby_weight) is float
     assert baby_weight == 5.5
     assert animal.weight > baby_weight * animal.params['xi']
Beispiel #21
0
 def test_mother_loses_weight_after_birth(self, example_properties_w_40,
                                          mocker):
     """
     Test birth_process method.
     Asserts that mother loses weight equal to xi * baby's weight after
     giving birth.
     """
     mocker.patch('numpy.random.normal', return_value=5.5)
     animal = Animal(example_properties_w_40)
     animal.find_fitness()
     initial_weight = animal.weight
     birth_weight = animal.birth_process(num_animals=6)
     assert animal.weight == initial_weight - \
         (animal.params['xi'] * birth_weight)
Beispiel #22
0
 def test_error_raised_from_weight_zero(self):
     """
     Tests that ValueError is raised if animal with weight zero is
     initialized.
     """
     properties_weight_zero = {"species": "animal", "age": 5, "weight": 0}
     with pytest.raises(ValueError):
         Animal(properties_weight_zero)
Beispiel #23
0
 def test_error_raised_from_negative_weight(self):
     """
     Tests that ValueError is raised if animal with negative weight is
     initialized.
     """
     properties_weight = {"species": "animal", "age": 5, "weight": -40}
     with pytest.raises(ValueError):
         Animal(properties_weight)
Beispiel #24
0
 def test_error_raised_from_invalid_age(self):
     """
     Tests that ValueError is raised if animal with negative age is
     initialized.
     """
     properties_age = {"species": "animal", "age": -5, "weight": 40}
     with pytest.raises(ValueError):
         Animal(properties_age)
Beispiel #25
0
 def test_constructor_animal(self, example_properties_w_20):
     """
     Checks that animal class can been initialized and that some parameters
     have been unpacked correctly.
     """
     animal = Animal(example_properties_w_20)
     assert animal.age == 5
     assert animal.params['a_half'] == 60
     assert animal.params['omega'] == 0.9
Beispiel #26
0
 def test_not_true_if_death_prob_is_one(self, example_properties_w_20):
     """
     Assert that will_animal_live does not return True if probability of
     dying is one.
     """
     animal = Animal(example_properties_w_20)
     animal.weight = 0
     animal.find_fitness()
     assert animal.will_animal_live() is not True
Beispiel #27
0
 def test_prob_death_is_one_if_fitness_zero(self, example_properties_w_20):
     """
     Asserts that the probability calculated from prob_death is one
     if fitness equals zero.
     """
     animal = Animal(example_properties_w_20)
     animal.weight = 0
     animal.find_fitness()
     assert animal.prob_death() == 1