def test_get_cell_type(self): """Tests if the get cell type method returns Jungle on a known jungle location. """ i = Island() jungle_loc = (2, 7) assert i.get_cell_type(jungle_loc) == "Jungle"
def test_get_fodder_on_loc(self): """Tests if get fodder on loc method retrieves fodder attribute of cell on location. """ i = Island() j_loc = (2, 7) fodder_on_loc = i.island_dict[j_loc].fodder assert i.get_fodder_on_loc(j_loc) == fodder_on_loc
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_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_param_changer(self): """Tests that the parameter changer method in the Island class changes the parameters in the Landscape class. """ i = Island() new_param = {"f_max": 700} landscape = "J" i._param_changer(landscape, new_param) assert i.island_dict[( 0, 0)].landscape_parameters[landscape]["f_max"] == 700
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_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_check_geo_string_not_surrounded_by_ocean(self): """Tests that check geo string method raises ValueError if the string is not surrounded by Ocean code. """ geo_string_1 = """\ OOOOOOOOOOOO OOOJJSSSSMMM OSSSSSJJJJMM """ with pytest.raises(ValueError): Island._check_geo_string(geo_string_1)
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_herb_eats_fodder_on_loc(self): """Tests if the fodder before eating is equal to the fodder after eating in addition to the amount eaten. """ i = Island() j_loc = (2, 7) herb_amount = 10 fodder_pre_eating = i.island_dict[j_loc].fodder i.herb_eats_fodder_on_loc(j_loc, herb_amount) fodder_post_eating = i.island_dict[j_loc].fodder assert fodder_pre_eating == fodder_post_eating + herb_amount
def test_geo_string_with_wrong_code_letters(self): """Test that island dict maker raises ValueError when it detects unknown landscape code. """ geo_string = """\ OOOO OSLO OOOO """ with pytest.raises(ValueError): Island._island_dict_maker(geo_string)
def test_get_total_herb_weight_on_loc(self): """Tests if the get total herbivore weight on location method returns the same weight as the weight of the herbivores times their number. """ i = Island() herb_weight = 50 num_herb = 5 loc = (2, 7) for _ in range(num_herb): Herbivore(i, loc, weight=herb_weight) assert i.get_total_herb_weight_on_loc(loc) == herb_weight * num_herb
def test_check_geo_string_not_rectangular(self): """Tests that the geo string checker raises ValueError if the geo string is non-rectangular """ geo_string_2 = """\ OOOOOOOOOOOO OOOJJSSSSMMOOO OOOOOOOOOOOOOOOO """ with pytest.raises(ValueError): Island._check_geo_string(geo_string_2)
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_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_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_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_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 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_fodder_annual_refill(self): """Tests that fodder annual refill method raises the fodder in all Jungle and Savannah cells. """ i = Island() for loc in i.island_dict: i.island_dict[loc].fodder = 0 i.fodder_annual_refill() for loc in i.island_dict: loc_type = i.island_dict[loc].__class__.__name__ loc_fodder = i.island_dict[loc].fodder if loc_type == "Jungle" or loc_type == "Savannah": assert loc_fodder > 0 else: assert loc_fodder == 0
def test_migration_removes_pop_from_loc_and_adds_to_odder(self, mocker): """Test to show that migration removes population from initial loc to the chosen destination when the mocked value for will_move is set to True """ mocker.return_value = True loc_with_one_place_to_move = (1, 1) i = Island(self.spes_geogr_2) h = Herbivore(i, loc_with_one_place_to_move) old_loc = h.get_loc() h.migrate() new_loc = h.get_loc() assert i.get_num_herb_on_loc(old_loc) == 0 assert i.get_num_herb_on_loc(new_loc) == 1 assert old_loc != new_loc
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_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_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_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_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_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_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_geo_string_works_with_upper_and_lower_case(self): """Test that the island dict maker method passes when geo string is both lower and upper case. """ geo_string = """\ OOOOOOOOoOOO OOOJJSSSsMMO OSSSsSJjJJMO OOOoOOOOoooO """ island_dict = Island._island_dict_maker(geo_string)