def test_will_move_happens_if_prob_1(self, mocker): """Test to show that move will happen if we mock the probability to lowest value """ mocker.patch('random.random', return_value=0) a_sim = Herbivore(self.i, self.loc) assert a_sim.will_move()
def test_get_relevant_fodder_herb(self): """Test to show that get_relevant_fodder returns fodder for herb """ jungle_loc = (2, 7) a = Herbivore(self.i, jungle_loc) fodder = self.i.get_fodder_on_loc(jungle_loc) assert a.get_relevant_fodder(jungle_loc) == fodder
def test_probabilities_returns_none_surrounded_by_ocean(self): """Test to show probabilities returns none when surrounded by ocean """ loc = (1, 2) h = Herbivore(self.spes_island, loc) loc_list = h.get_potential_coordinates() assert h.probabilities(loc_list) is None
def test_will_move_does_not_happen_if_prob_0(self, mocker): """Test to show that move will not happen if we mock the probability to highest value """ mocker.patch('random.random', return_value=1) a_sim = Herbivore(self.i, self.loc) assert not a_sim.will_move()
def test_relative_abundance_gives_output_herb(self): """Test to show that relative_abundance returns output for herb """ i = Island() jungle_loc = (2, 7) h = Herbivore(i, jungle_loc) self_calculated_abundance = 40 assert h.relative_abundance(jungle_loc) == self_calculated_abundance
def test_propensity_return_value_desert(self): """Test to show that propensity returns a value based on a formula when placed in desert """ i = Island() des_loc = (5, 9) h = Herbivore(i, des_loc) assert h.propensity(des_loc) == 1
def test_get_potential_coordinates_gives_neighbour_coord(self): """Test to show that get_potential_coordinates returns the neighbouring coordinates """ loc = (2, 2) neighbour_coord = [(3, 2), (1, 2), (2, 3), (2, 1)] h = Herbivore(self.i, loc) assert h.get_potential_coordinates() == neighbour_coord
def test_total_propensity_surrounded_by_desert(self): """Test to show that total_propensity returns value based on formula when surrounded by desert """ loc = (6, 9) h = Herbivore(self.i, loc) loc_list = h.get_potential_coordinates() assert h.total_propensity(loc_list) == 4
def test_destination_is_none_surrounded_by_ocean(self): """Test to show that destination is none while surrounded by ocean """ loc = (1, 2) h = Herbivore(self.spes_island, loc) loc_list = h.get_potential_coordinates() assert h.destination(loc_list) is None
def test_can_birth_occur_with_1_herbivore(self): """Test to see that birth will not occur for only 1 animal """ loc = (2, 7) i_sim = Island() a_sim = Herbivore(i_sim, loc) i_sim.add_pop_on_loc(loc, a_sim) assert not a_sim.can_birth_occur()
def test_get_num_same_species_herb(self): """Test to show that get_num_same_species return is number of same species(herb) """ i = Island() h_1 = Herbivore(i, self.loc) h_2 = Herbivore(i, self.loc) assert h_1.get_num_same_species(self.loc) == 2
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_fodder_eaten_only_eat_available_fodder(self): """Test to show that fodder_eaten only returns the available fodder if the location contains less fodder than optimal fodder """ jungle_loc = (2, 7) Island._param_changer("J", {"f_max": 5}) i_sim = Island() s_1 = Herbivore(i_sim, jungle_loc) assert s_1.fodder_eaten() == 5
def test_feed_changes_fitness(self): """Test feed changes fitness """ loc = (2, 1) i = Island() i_sim = Herbivore(i, loc) start_fitness = i_sim.fitness i_sim.feed() changed_fitness = i_sim.fitness assert start_fitness != changed_fitness
def test_feed_raises_fitness(self): """Test to show that feed makes changes to fitness """ loc = (2, 7) i = Island() h = Herbivore(i, loc) old_fitness = h.fitness h.feed() new_fitness = h.fitness assert old_fitness < new_fitness
def test_propensity_0_if_mountain_or_ocean(self): """Test to show that propensity returns 0 if cell_type is mountain or ocean """ ocean_loc = (0, 0) mountain_loc = (1, 9) i = Island() h = Herbivore(i, mountain_loc) assert h.propensity(ocean_loc) == 0 assert h.propensity(mountain_loc) == 0
def test_can_birth_occur_with_2_herbivores_with_0_prob(self, mocker): """Test to show that 2 herbivores will not give birth if the probability is mocked to highest possible value. """ mocker.patch('random.random', return_value=1) loc = (2, 7) i_sim = Island() a_sim = Herbivore(i_sim, loc, weight=100) for _ in range(2): i_sim.add_pop_on_loc(loc, a_sim) assert not a_sim.can_birth_occur()
def test_destination_gives_coordinate(self, mocker): """Test to show that destination returns destination when mocked to a probability of 100% at different indexes """ mocker.return_value = [1, 0, 0, 0] i = Island() loc = (2, 8) h = Herbivore(i, loc) loc_list = h.get_potential_coordinates() assert h.destination(loc_list) == loc_list[0] mocker.return_value = [0, 1, 0, 0] assert h.destination(loc_list) == loc_list[1]
def test_annual_weight_loss_decreases_weight(self): """Test to show that annual_weight_loss decreases the weight of the animal """ loc = (2, 7) i = Island() a_sim = Herbivore(i, loc) initial_weight = a_sim.weight a_sim.annual_weight_loss() new_weight = a_sim.weight assert initial_weight > new_weight
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_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_birth_adds_to_num_herb_on_loc(self, mocker): """Test to show that birth method adds new pop to herb_pop list when birth occurs """ mocker.patch('random.random', return_value=0) loc = (2, 7) i_sim = Island() a_sim_1 = Herbivore(i_sim, loc, weight=100) a_sim_2 = Herbivore(i_sim, loc, weight=100) a_sim_1.birth() assert i_sim.get_num_herb_on_loc(loc) == 3
def test_birth_does_not_happen_if_prob_0(self, mocker): """Test to show that birth method does not add new pop if birth does not occur """ mocker.patch('random.random', return_value=1) loc = (2, 7) i_sim = Island() a_sim_1 = Herbivore(i_sim, loc, weight=100) a_sim_2 = Herbivore(i_sim, loc, weight=100) a_sim_1.birth() assert i_sim.get_num_herb_on_loc(loc) == 2
def test_get_all_herb_list(self): """Tests that the get all herbivores list method returns a list that is equal in length and contains the same elements as a list containing all the herbivores on the map. """ i = Island() herb_list = [ Herbivore(i, (2, 7)), Herbivore(i, (1, 8)), Herbivore(i, (2, 1)) ] all_herbs = i.get_all_herb_list() assert len(all_herbs) == len(herb_list) for herb in herb_list: assert herb in all_herbs
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_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_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_herb_list_on_loc(self): """Tests if get herb list on loc method retrieves herb list attribute of cell on locatrion. """ i = Island() loc = (1, 8) for _ in range(3): Herbivore(i, loc) herb_list_manually = i.island_dict[loc].herb_pop_list assert i.get_herb_list_on_loc(loc) == herb_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)