示例#1
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))
示例#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
示例#3
0
    def test_database_init(self):
        client = SimulationClient('sqlite:///:memory:')
        # init DB
        sid = client.init_simulation(**sim_config)
        assert len(
            client.get_all_simulations()) == 1, 'Should be only one simulation'
        assert isinstance(client.get_simulation(sid), Simulation)

        # check exception raised in init when adding animal on non-existent sim
        with pytest.raises(ValueError):
            client.init_animal(sim_id=10,
                               current_turn=0,
                               animal_type=Animal.Fish,
                               coordinate=SquareGridCoordinate(x=0, y=1))
        # init some animals
        client.init_animal(sim_id=sid,
                           current_turn=0,
                           animal_type=Animal.Fish,
                           coordinate=SquareGridCoordinate(x=0, y=1))
        assert client.coordinate_is_occupied(sim_id=sid,
                                             coordinate=SquareGridCoordinate(
                                                 x=0, y=1))

        # check exception for adding animal to an already occupied square
        with pytest.raises(NonEmptyCoordinate):
            client.init_animal(sim_id=sid,
                               current_turn=0,
                               animal_type=Animal.Fish,
                               coordinate=SquareGridCoordinate(x=0, y=1))
        # but should be fine in a new simulation
        sid_2 = client.init_simulation(**sim_config)
        client.init_animal(sim_id=sid_2,
                           current_turn=0,
                           animal_type=Animal.Fish,
                           coordinate=SquareGridCoordinate(x=0, y=1))

        # spawn outside the grid
        with pytest.raises(TopologyError):
            client.init_animal(sim_id=sid_2,
                               current_turn=0,
                               animal_type=Animal.Fish,
                               coordinate=SquareGridCoordinate(x=10, y=1))