Esempio n. 1
0
 def test_position_functions(self):
     client = SimulationClient('sqlite:///:memory:')
     # init DB
     sid = client.init_simulation(**sim_config)
     # add a bunch of animals
     for t, c in animal_list:
         client.init_animal(sim_id=sid,
                            current_turn=0,
                            animal_type=t,
                            coordinate=c)
     # check animal in square
     coord = SquareGridCoordinate(x=1, y=3)
     animals_in_square = client.get_animal_in_position(sim_id=sid,
                                                       coordinate=coord)
     assert len(animals_in_square) == 1
     # kill that animal
     client.kill_animal(sim_id=sid, animal_ids=[animals_in_square[0].oid])
     animals_in_square = client.get_animal_in_position(sim_id=sid,
                                                       coordinate=coord)
     assert len(animals_in_square) == 0
     # adding a live one in coord
     client.init_animal(sim_id=sid,
                        current_turn=0,
                        animal_type=Animal.Fish,
                        coordinate=coord)
     animals_in_square = client.get_animal_in_position(sim_id=sid,
                                                       coordinate=coord,
                                                       live_only=False)
     assert len(animals_in_square) == 2
Esempio n. 2
0
 def test_animal_functions(self):
     client = SimulationClient('sqlite:///:memory:')
     # init DB
     sid = client.init_simulation(**sim_config)
     # add a bunch of animals
     for t, c in animal_list:
         client.init_animal(sim_id=sid,
                            current_turn=0,
                            animal_type=t,
                            coordinate=c)
     # retrieve animals
     animals_df = client.get_animals_df(sim_id=1)
     assert isinstance(animals_df, pd.DataFrame), 'results is a DataFrame'
     assert len(animals_df) == 8, 'should be only 9 animals'
     # update animals
     # create update dict
     update_dict = {
         oid: {
             'breed_count': 1,
             'last_breed': 5,
             'last_fed': 4
         }
         for oid in animals_df.oid.values
     }
     client.update_animals(sim_id=sid, update_dict=update_dict)
     animals_df = client.get_animals_df(sim_id=1)
     assert animals_df.breed_count.unique(
     )[0] == 1, 'Breed count should be one now'
     client.update_animals(sim_id=sid, update_dict={1: {'alive': False}})
     # this update should not work
     animal = client.get_animal(sim_id=sid, animal_id=1)
     assert animal.alive, 'Animal should still be alive'
     # but this should work
     client.kill_animal(sim_id=sid, animal_ids=[1, 2, 3])
     animal = client.get_animal(sim_id=sid, animal_id=1)
     assert not animal.alive, 'This time alive was updates'
     # a dead animal does not occupy a square
     assert not client.coordinate_is_occupied(
         sim_id=sid, coordinate=SquareGridCoordinate(1, 3))
     # test get animal by types
     shark_df = client.get_animals_by_type(sim_id=sid,
                                           animal_type=Animal.Shark)
     assert shark_df.animal_type.unique()[0] == Animal.Shark
Esempio n. 3
0
 def test_moving_animals(self):
     client = SimulationClient('sqlite:///:memory:')
     # init DB
     sid = client.init_simulation(**sim_config)
     # add a bunch of animals
     for v in animal_list:
         client.init_animal(sim_id=sid,
                            current_turn=0,
                            animal_type=v[0],
                            coordinate=v[1])
     # test move
     assert not client.coordinate_is_occupied(
         sim_id=sid, coordinate=SquareGridCoordinate(5, 3))
     client.move_animal(sim_id=sid,
                        animal_id=4,
                        new_position=SquareGridCoordinate(5, 3))
     assert client.coordinate_is_occupied(sim_id=sid,
                                          coordinate=SquareGridCoordinate(
                                              5, 3))
     # trying to move dead animal
     client.kill_animal(sim_id=sid, animal_ids=[3])
     with pytest.raises(ImpossibleAction):
         client.move_animal(sim_id=sid,
                            animal_id=3,
                            new_position=SquareGridCoordinate(3, 3))
     # trying to move to an already occupied square
     with pytest.raises(NonEmptyCoordinate):
         client.move_animal(sim_id=sid,
                            animal_id=5,
                            new_position=SquareGridCoordinate(5, 3))
     # but moving to a square occupied by a dead animal is possible
     assert not client.coordinate_is_occupied(
         sim_id=sid, coordinate=SquareGridCoordinate(3, 2))
     client.move_animal(sim_id=sid,
                        animal_id=1,
                        new_position=SquareGridCoordinate(3, 2))
     assert client.coordinate_is_occupied(sim_id=sid,
                                          coordinate=SquareGridCoordinate(
                                              3, 2))