def setup(self): spes_geogr_1 = """\ OOOO OOJO OOOO""" self.spes_geogr_2 = """\ OOOO OJJO OOOO""" self.spes_island = Island(spes_geogr_1) self.i = Island() self.loc = (0, 0) stnd_herb = Herbivore(island=self.i, loc=self.loc) stnd_carn = Carnivore(island=self.i, loc=self.loc) self.stnd_a_list = [stnd_herb, stnd_carn] self.herb_w_0 = Herbivore(island=self.i, loc=self.loc, weight=0) self.herb_w_5 = Herbivore(island=self.i, loc=self.loc, weight=5) self.herb_w_200 = Herbivore(island=self.i, loc=self.loc, weight=200) self.carn_w_0 = Carnivore(island=self.i, loc=self.loc, weight=0) self.carn_w_5 = Carnivore(island=self.i, loc=self.loc, weight=5) self.carn_w_7 = Carnivore(island=self.i, loc=self.loc, weight=7) self.a_list_w_0 = [self.herb_w_0, self.carn_w_0] self.a_list_w_5 = [self.herb_w_5, self.carn_w_5]
def test_get_num_same_species_carn(self): """Test to show that get_num_same_species return is number of same species(carn) """ i = Island() h_1 = Carnivore(i, self.loc) h_2 = Carnivore(i, self.loc) assert h_1.get_num_same_species(self.loc) == 2
def test_birth_adds_to_num_carn_on_loc(self, mocker): """Test to show that birth method adds new pop to carn_pop list when birth occurs """ mocker.patch('random.random', return_value=0) loc = (2, 7) i_sim = Island() a_sim_1 = Carnivore(i_sim, loc, weight=100) a_sim_2 = Carnivore(i_sim, loc, weight=100) a_sim_1.birth() assert i_sim.get_num_carn_on_loc(loc) == 3
def test_get_all_carn_list(self): """Tests that the get all carnivores list method returns a list that is equal in length and contains the same elements as a list containing all the carnivores on the map. """ i = Island() carn_list = [ Carnivore(i, (2, 7)), Carnivore(i, (1, 8)), Carnivore(i, (2, 1)) ] all_carns = i.get_all_carn_list() assert len(all_carns) == len(carn_list) for carn in carn_list: assert carn in all_carns
def test_relative_abundance_gives_output_carn(self): """Test to show that relative_abundance returns output for herb """ i = Island() jungle_loc = (2, 7) h = Herbivore(i, jungle_loc, weight=50) c = Carnivore(i, jungle_loc) self_calculated_abundance = 0.5 assert c.relative_abundance(jungle_loc) == self_calculated_abundance
def test_kill_herb_false_if_fitness_too_low(self): """Test to show that kill_herb returns False if the Carnivore-fitness is too low """ i = Island() loc = (2, 7) c = Carnivore(i, loc, weight=0) h = Herbivore(i, loc, weight=100) assert not c.kill_herb(h)
def test_death_occurs_if_fitness_is_0(self): """Test to show that animal will die if fitness is 0 """ loc = (2, 7) i = Island() h_sim = Herbivore(i, loc, weight=0) c_sim = Carnivore(i, loc, weight=0) assert h_sim.death() assert c_sim.death()
def test_get_relevant_fodder_carn(self): """Test to show that get_relevant_fodder returns fodder for carn, which is equal to the herb_weight """ i = Island() c = Carnivore(i, self.loc) herb_weight = 50 h = Herbivore(i, self.loc, weight=herb_weight) assert c.get_relevant_fodder(self.loc) == herb_weight
def test_get_carn_list_on_loc(self): """Tests if get carn list on loc method retrieves carn list attribute of cell on locatrion. """ i = Island() loc = (1, 8) for _ in range(3): Carnivore(i, loc) carn_list_manually = i.island_dict[loc].carn_pop_list assert i.get_carn_list_on_loc(loc) == carn_list_manually
def test_kill_herb_true_if_DeltaPhiMax_is_low(self): """Test to show that kill_herb returns True if DeltaPhiMax is set to 0. """ i = Island() loc = (2, 7) c = Carnivore(i, loc, weight=100) h = Herbivore(i, loc, weight=0) c.param_changer({"DeltaPhiMax": 0}) assert c.kill_herb(h)
def test_kill_herb_true_if_prob_1(self, mocker): """Test to show that kill_herb returns True if the mocker return value is set to lowest """ mocker.patch('random.random', return_value=0) i = Island() loc = (2, 7) c = Carnivore(i, loc, weight=100) h = Herbivore(i, loc, weight=0) assert c.kill_herb(h)
def test_probabilities_returns_prob_list(self): """Test to show that probabilities returns probability_list based on formula """ i = Island() loc = (3, 7) c = Carnivore(i, loc) loc_list = c.get_potential_coordinates() prob_list = [0.25, 0.25, 0.25, 0.25] assert c.probabilities(loc_list) == prob_list
def test_can_birth_occur_with_2_carnivores_with_1_prob(self, mocker): """Test to show that 2 carnivores will give birth if the probability is mocked to the lowest possible value. """ mocker.patch('random.random', return_value=0) loc = (2, 7) i_sim = Island() a_sim = Carnivore(i_sim, loc, weight=100) for _ in range(2): i_sim.add_pop_on_loc(loc, a_sim) assert a_sim.can_birth_occur()
def test_death_does_not_occur_if_prob_is_0(self, mocker): """Test to show that death will not occur if we mock the probability to highest return value """ mocker.patch('random.random', return_value=1) loc = (2, 7) i_sim = Island() h_sim = Herbivore(i_sim, loc) c_sim = Carnivore(i_sim, loc) assert not h_sim.death() assert not c_sim.death()
def test_death_occurs_if_prob_is_1(self, mocker): """Test to show that death will occur if we mock the probability to lowest return value """ mocker.patch('random.random', return_value=0) loc = (2, 7) i_sim = Island() h_sim = Herbivore(i_sim, loc) c_sim = Carnivore(i_sim, loc) assert h_sim.death() assert c_sim.death()
def test_feed_gains_weight(self, mocker): """Test to show that feed gains weight when a Herbivore is killed by setting the mocked value from kill_herb to True """ mocker.return_value = True i = Island() loc = (2, 7) h = Herbivore(i, loc, weight=40) c = Carnivore(i, loc, weight=100) old_weight = c.weight c.feed() new_weight = old_weight + (h.weight * c.parameters["beta"]) assert c.weight == new_weight
def test_get_num_animals_on_loc(self): """Tests if the get animals on location method can return the number of animals placed out on the map. """ i = Island() num_herb = 4 num_carn = 3 loc = (2, 7) for _ in range(num_herb): Herbivore(i, loc) for _ in range(num_carn): Carnivore(i, loc) assert i.get_num_herb_on_loc(loc) == num_herb assert i.get_num_carn_on_loc(loc) == num_carn
def test_sort_all_animals_by_fitness(self): """Tests if the sort all animals by fitness method sorts a sample of animals placed on a location by fitness. """ i = Island() loc = (2, 7) for _ in range(10): Herbivore(i, loc) Carnivore(i, loc) local_herb_list = i.get_herb_list_on_loc(loc) local_carn_list = i.get_carn_list_on_loc(loc) local_herb_list.sort(key=lambda herb: herb.fitness, reverse=True) local_carn_list.sort(key=lambda carn: carn.fitness, reverse=True) i.sort_all_animals_by_fitness() assert i.get_herb_list_on_loc(loc) == local_herb_list assert i.get_carn_list_on_loc(loc) == local_carn_list
def test_add_and_remove_pop_on_locs(self): """Tests if the add and remove pop on loc methods can add and remove a sample of animals in the pop lists. """ i = Island() loc_1 = (1, 8) loc_2 = (2, 7) animal_list = [Herbivore(i, loc_1), Carnivore(i, loc_1)] for animal in animal_list: new_cell = i.island_dict[loc_2] old_cell = i.island_dict[loc_1] i.add_pop_on_loc(loc_2, animal) i.remove_pop_on_loc(loc_1, animal) if animal.__class__.__name__ == "Herbivore": assert animal in new_cell.herb_pop_list assert animal not in old_cell.herb_pop_list elif animal.__class__.__name__ == "Carnivore": assert animal in new_cell.carn_pop_list assert animal not in old_cell.carn_pop_list
def test_appetite_checker_gives_weight(self): """Test to show that appetite_checker returns the weight that the Carnivore eats """ i = Island() loc = (2, 7) c = Carnivore(i, loc) e_weight_1 = 30 d_weight_1 = 50 last_kill_1 = 100 e_weight_2 = 30 d_weight_2 = 50 last_kill_2 = 20 e_weight_3 = 30 d_weight_3 = 50 last_kill_3 = 5 assert c.appetite_checker(e_weight_1, d_weight_1, last_kill_1) == 20 assert c.appetite_checker(e_weight_2, d_weight_2, last_kill_2) == 20 assert c.appetite_checker(e_weight_3, d_weight_3, last_kill_3) == 5