def test_topo_remove_herbivore():
    """Tests that an emigrating animal can be removed from its old cells
     animal list"""
    cell = topo.Topography()
    testherbi = animals.Herbivores()
    testlist = [animals.Herbivores() for _ in range(10)]
    cell.herbivore_list = testlist
    cell.add_animal(testherbi)
    cell.remove_animal(testherbi)
    assert testherbi not in cell.herbivore_list
def test_annual_death_0_fitness_random_cells(test_map):
    """Tests that the method annual_death works in a random accessible cell"""
    test_map.raster_model[(3, 4)].add_animal(ani.Herbivores(age=99, weight=0))
    test_map.raster_model[(11, 8)].add_animal(ani.Carnivores(age=99, weight=0))
    test_map._annual_death_all_cells()
    assert test_map.raster_model[(3, 4)].herbivore_list == []
    assert test_map.raster_model[(11, 8)].carnivore_list == []
def test_birth_weight_sd0():
    """
    Tests that the birth weight = herbivores.parameters["w_birth"],
    when sigma_birth is set to 0
    """
    ani.Herbivores.parameters["w_birth"] = 10
    ani.Herbivores.parameters["sigma_birth"] = 0
    assert ani.Herbivores._birth_weight(ani.Herbivores()) == 10
def test_will_migrate_50_chance():
    """Test that a herbivore will migrate around half the times when the
    probability = 0.5 """
    testanimal = ani.Herbivores(age=10, weight=10.0499)
    testanimal.set_parameters({"mu": 1})
    testlist = [testanimal._will_migrate() for _ in range(1000)]
    would_migrate = testlist.count(True)
    assert 450 < would_migrate < 550
def test_fitness_level_is_zero_when_weight_is_zero():
    """Tests that the fitness level is zero, when the weight is zero"""
    herbivore = ani.Herbivores()
    carnivore = ani.Carnivores()
    herbivore.weight = 0
    carnivore.weight = 0
    assert herbivore.fitness == 0
    assert carnivore.fitness == 0
def test_breed_low_weight():
    """Test that a herbivore with a low weight cannot breed"""
    herbivore = ani.Herbivores()
    herbivore.weight = 1
    cell = topo.Jungle()
    cell.add_animal(herbivore)
    herbivore.breed(cell, 100)
    assert len(cell.herbivore_list) == 1
def test_animal_with_fitness_level_0_dies():
    """Test that an animal with a very low fitness dies"""
    animal = animals.Herbivores(age=200, weight=1)
    cell = topo.Jungle()
    cell.add_animal(animal)
    animals.Herbivores.parameters["omega"] = 1
    cell.natural_death_all_animals_in_cell()
    animals.Herbivores.parameters["omega"] = 0.4
    assert len(cell.herbivore_list) == 0
def test_breed_certain_probability_all_in_cell():
    """Tests that all animals breed, when the breeding-probability = 1"""
    cell = topo.Jungle()
    for _ in range(100):
        cell.add_animal(animals.Herbivores(age=10, weight=100))
        cell.add_animal(animals.Carnivores(age=10, weight=100))
    cell.breed_all_animals_in_cell()
    assert len(cell.herbivore_list) == 200
    assert len(cell.carnivore_list) == 200
def test_annual_weight_decrease_age_up():
    """Tests that a herbivore loses weight and age up, when these commandos
    are run"""
    herbivore = ani.Herbivores()
    pre_decrease_weight = herbivore.weight
    herbivore.annual_metabolism()
    herbivore.age_up()
    assert herbivore.weight == pre_decrease_weight - (
        herbivore.parameters["eta"] * pre_decrease_weight)
    assert herbivore.age == 1
def test_breed_certain_prob_overweight_newborn():
    """Tests that a herbivore cannot give birth to a child with
    greater weight """
    herbivore = ani.Herbivores(weight=50)
    cell = topo.Jungle()
    cell.add_animal(herbivore)
    herbivore.parameters["xi"] = 100
    herbivore.breed(cell, 1000)
    herbivore.parameters["xi"] = 1.2
    assert len(cell.herbivore_list) == 1
def strong_vs_weak():
    """Creates a test herbivore and carnivore instance"""
    cell = topo.Desert()
    herbivore = ani.Herbivores()
    carnivore = ani.Carnivores()
    herbivore.weight, herbivore.age = 1, 100
    carnivore.weight, carnivore.age = 15, 52
    cell.add_animal(herbivore)
    cell.add_animal(carnivore)
    return herbivore, carnivore, cell
def test_set_parameters(bad_parameters):
    """Test if the non allowed parameters raises a value error. """
    carnivore = ani.Carnivores()
    herbivore = ani.Herbivores()
    with pytest.raises(ValueError):
        carnivore.set_parameters(bad_parameters)
    with pytest.raises(ValueError):
        herbivore.set_parameters(bad_parameters)
    carnivore.set_parameters({"w_birth": 6.0})
    assert carnivore.parameters["w_birth"] == 6.0
def test_feeding_carnivores_in_a_cell():
    """Tests if the most fit herbivore kills the least fit herbivore etc"""
    animals.Carnivores.set_parameters({"DeltaPhiMax": 0.1})
    jungle_cell = topo.Jungle()
    [jungle_cell.add_animal(animals.Herbivores()) for _ in range(10)]
    [jungle_cell.add_animal(animals.Carnivores(weight=80)) for _ in range(10)]
    assert jungle_cell.biomass_carnivores() == 800
    pre_feeding_herbi_biomass = jungle_cell.biomass_herbivores()
    jungle_cell.feed_carnivores_in_cell()
    assert pre_feeding_herbi_biomass > jungle_cell.biomass_herbivores()
def test_population_age_groups(test_map):
    """Tests that the method population_age_groups returns two arrays and two
    lists"""
    test_map.raster_model[(3, 4)].add_animal(ani.Herbivores(age=1, weight=10))
    test_map.raster_model[(3, 4)].add_animal(ani.Herbivores(age=4, weight=10))
    test_map.raster_model[(3, 4)].add_animal(ani.Herbivores(age=9, weight=10))
    test_map.raster_model[(3, 4)].add_animal(ani.Herbivores(age=14, weight=10))
    test_map.raster_model[(3, 4)].add_animal(ani.Herbivores(age=50, weight=10))
    test_map.raster_model[(3, 4)].add_animal(ani.Carnivores(age=1, weight=10))
    test_map.raster_model[(3, 4)].add_animal(ani.Carnivores(age=4, weight=10))
    test_map.raster_model[(3, 4)].add_animal(ani.Carnivores(age=9, weight=10))
    test_map.raster_model[(3, 4)].add_animal(ani.Carnivores(age=14, weight=10))
    test_map.raster_model[(3, 4)].add_animal(ani.Carnivores(age=50, weight=10))
    herb_list, carn_list, herb_mean_w_list, carn_mean_w_list = \
        test_map.population_biomass_age_groups()
    assert herb_list == [1, 1, 1, 1, 1]
    assert carn_list == [-1, -1, -1, -1, -1]
    assert herb_mean_w_list == [10, 10, 10, 10, 10]
    assert carn_mean_w_list == [-10, -10, -10, -10, -10]
def test_breed_certain_probability():
    """Test that a fit herbivore will give birth to another herbivore when the
    breeding probability = 1"""
    cell = topo.Jungle()
    herbivore = ani.Herbivores()
    herbivore.weight, herbivore.age = 80, 30
    cell.add_animal(herbivore)
    herbivore.breed(cell, 100)
    assert len(cell.herbivore_list) == 2
    assert isinstance(cell.herbivore_list[0], ani.Herbivores)
    assert isinstance(cell.herbivore_list[1], ani.Herbivores)
def test_ek_for_cell_9_herbs_carns_100_fodder(basic_jungle):
    """Tests that the ek formula for herbivores and carnivores are correct"""
    basic_jungle.fodder = 100
    for _ in range(9):
        basic_jungle.add_animal(animals.Herbivores(weight=10))
        basic_jungle.add_animal(animals.Carnivores())
    animals.Carnivores.parameters["F"] = 50
    ek_herbivores = basic_jungle.ek_for_cell("Herbivores")
    ek_carnivores = basic_jungle.ek_for_cell("Carnivores")
    assert ek_herbivores == 1
    assert ek_carnivores == 0.18
def test_biomass_food_chain(test_map):
    """Tests that the method biomass_food_chain returns a dictionary with the
    correct islands total biomass of fodder, herbivores and carnivores"""
    test_map.raster_model[(3, 4)].add_animal(ani.Herbivores(age=99, weight=10))
    test_map.raster_model[(3, 4)].add_animal(ani.Carnivores(age=1, weight=100))
    for cell in test_map.raster_model.values():
        if cell.is_accessible:
            cell.fodder = 0
    biomass_dict = test_map.biomass_food_chain()
    assert biomass_dict.get('biomass_fodder') == 0
    assert biomass_dict.get('biomass_herbs') == 10
    assert biomass_dict.get('biomass_carnivores') == 100
def test_remove_herbivore():
    """Test that a herbivore can be removed"""
    savanna_cell = topo.Savanna()
    test_herbivore = animals.Herbivores()
    savanna_cell.add_animal(test_herbivore)
    assert test_herbivore in savanna_cell.herbivore_list
    assert test_herbivore in animals.Animals.instances
    savanna_cell.remove_animal(test_herbivore)
    assert test_herbivore not in savanna_cell.herbivore_list
    assert test_herbivore in animals.Animals.instances
    animals.Animals.instances.remove(test_herbivore)
    assert test_herbivore not in animals.Animals.instances
def low_fitness_animals():
    """Creates some test instances with very low fitness"""
    jungle_cell = topo.Jungle()
    herbivore = animals.Herbivores()
    carnivore = animals.Carnivores()
    carnivore.weight, carnivore.age = 1, 1000
    herbivore.weight, herbivore.age = 1, 1000
    herbivore.parameters["omega"] = 1
    carnivore.parameters["omega"] = 1
    jungle_cell.add_animal(herbivore)
    jungle_cell.add_animal(carnivore)
    return jungle_cell
def test_breed_all_cells_certain_prob_random_cell(test_map):
    """Test that the method 'breed_all_cell' works in a random accessible
     cell"""
    for _ in range(100):
        test_map.raster_model[(3,
                               4)].add_animal(ani.Herbivores(age=0,
                                                             weight=100))
        test_map.raster_model[(11,
                               8)].add_animal(ani.Carnivores(age=0,
                                                             weight=100))
    test_map._breed_in_all_cells()
    assert len(test_map.raster_model[(3, 4)].herbivore_list) == 200
    assert len(test_map.raster_model[(11, 8)].carnivore_list) == 200
def test_breed_uncertain_probability():
    """Test a herbivore will give birth to around 400 kids, when it trys 1000
    times, when the probability for birth is around 0.4"""
    born = 0
    for _ in range(1000):
        cell = topo.Jungle()
        herbivore = ani.Herbivores()
        herbivore.weight, herbivore.age = 400, 10
        cell.add_animal(herbivore)
        herbivore.breed(cell, 3)
        if len(cell.herbivore_list) == 2:
            born += 1
    assert born > 350
    assert born < 450
def test_herbivore_grazing():
    """Test that the herbivore cannot gain weight when grazing in a desert,
    but gains weight when grazing in the jungle and savanna"""
    herbivore = ani.Herbivores()
    pre_eating_weight = herbivore.weight
    desert_cell = topo.Desert()
    herbivore.graze(desert_cell)
    assert herbivore.weight == pre_eating_weight
    jungle_cell = topo.Jungle()
    herbivore.graze(jungle_cell)
    assert herbivore.weight == pre_eating_weight + (
        herbivore.parameters["beta"] * herbivore.parameters["F"])
    after_jungle_graze = herbivore.weight
    savanna_cell = topo.Savanna()
    herbivore.graze(savanna_cell)
    assert herbivore.weight == after_jungle_graze + (
        herbivore.parameters["beta"] * herbivore.parameters["F"])
def test_feeding_herbivores_in_a_cell():
    """ Tests the method "test_feeding_herbivores_in_a_cell" where the
    least fittest animal shall in not be able to eat, due to overgrazing,
    and therefore keep the same weight after the graze commando"""
    jungle_cell = topo.Jungle()
    animals.Herbivores.parameters["sigma_birth"] = 1.5
    [jungle_cell.add_animal(animals.Herbivores()) for _ in range(81)]
    herbivore_fitness_sort = sorted(jungle_cell.herbivore_list,
                                    key=lambda herbi: herbi.fitness)
    assert herbivore_fitness_sort[0].fitness < \
        herbivore_fitness_sort[30].fitness
    least_fittest_herb = herbivore_fitness_sort[0]
    second_least_fittest_herb = herbivore_fitness_sort[1]
    least_fittest_weight = least_fittest_herb.weight
    second_least_fittest_weight = second_least_fittest_herb.weight
    jungle_cell.feed_herbivores_in_cell()
    assert least_fittest_weight == least_fittest_herb.weight
    assert second_least_fittest_weight != second_least_fittest_herb.weight
    # Test that the available fodder now are = 0, and that the increase_fodder
    # works, so the food level again = f_max
    assert jungle_cell.fodder == 0
    jungle_cell.increase_fodder()
    assert jungle_cell.fodder == 800
def test_topo_add_herbviore():
    """Tests that an immigrating animal can be added to the new cells
     animal list"""
    instance = topo.Topography()
    instance.add_animal(animals.Herbivores())
    assert len(instance.herbivore_list) == 1
def test_fitness_level_is_between_zero_and_one():
    """Test that the fitness level cannot be greater then 1"""
    herbivore = ani.Herbivores()
    herbivore.weight = 10000
    herbivore.age = 0
    assert herbivore.fitness <= 1
def certain_migration_prob_herb():
    """Creates a test herbivore instance"""
    testanimal = ani.Herbivores(age=2, weight=40)
    testanimal.set_parameters({"mu": 4})
    return testanimal
def test_what_cell_when_will_not_migrate(mock_ek):
    """Test that a herbivore never migrates when the mu-parameter = 0"""
    testanimal = ani.Herbivores()
    testanimal.parameters["mu"] = 0
    chosen_cell = testanimal.what_cell_to_migrate_to((10, 10), mock_ek)
    assert chosen_cell == (10, 10)