def test_empty(self): ''' Tests that an empty grid throws an exception of no sharks ''' client = SimulationClient('sqlite:///:memory:') grid = SimulationGrid(persistence=client, simulation_parameters=sim_config_empty) with pytest.raises(EndOfSimulatioError): grid._check_deads() with pytest.raises(EndOfSimulatioError): grid.play_turn()
def test_fish_only(self): ''' Test if non-empty, but fish only: the simulation should end Make two cases: (1) add manually the fish to empty grid (2) use the config with updated value for corresponding key ''' # No. 1 client = SimulationClient('sqlite:///:memory:') grid = SimulationGrid(persistence=client, simulation_parameters=sim_config_empty) a_list = [ (Animal.Fish, SquareGridCoordinate(x=1, y=1)), ] for t, c in a_list: client.init_animal(sim_id=grid._sid, current_turn=0, animal_type=t, coordinate=c) with pytest.raises(EndOfSimulatioError): grid._check_deads() with pytest.raises(EndOfSimulatioError): grid.play_turn() # repeat the same for No. 2 del client, grid, a_list sim_config_empty_local = copy.deepcopy(sim_config_empty) sim_config_empty_local['init_nb_fish'] = 1 client = SimulationClient('sqlite:///:memory:') grid = SimulationGrid(persistence=client, simulation_parameters=sim_config_empty_local) with pytest.raises(EndOfSimulatioError): grid._check_deads() with pytest.raises(EndOfSimulatioError): grid.play_turn()
def test_starving(self): client = SimulationClient('sqlite:///:memory:') grid = SimulationGrid(persistence=client, simulation_parameters=sim_config) turn_to_starve = sim_config['shark_starving'] # set turn to more... grid._sim_turn = turn_to_starve + 1 grid._check_deads() assert len( grid._persistence.get_animals_by_type(grid._sid, Animal.Shark)) == 0 # amend this test by adding further checks # after the first turn we should have killed the sharks with pytest.raises(EndOfSimulatioError): grid.check_simulation_ends() with pytest.raises(EndOfSimulatioError): grid._check_deads() with pytest.raises(EndOfSimulatioError): grid.play_turn()
Configuration file path. If specified, configuration file will be loaded from this path """) args = cmd_parser.parse_args() if args.config_path is not None: raise NotImplementedError( 'Code for directing to an alternative configuration' ' repository has not been implemented yet') # Load simulation configuration sim_config = read_simulation_config(args.config_name) # Instantiate client client = SimulationClient(get_database_string()) # display initial grid grid = SimulationGrid(persistence=client, simulation_parameters=sim_config) print( display_simple_grid(client.get_animals_df(grid._sid), grid_size=sim_config['grid_size'])) for turn in range(args.max_turn): start_time = current_milli_time() grid.play_turn() print(''.join(['*'] * sim_config['grid_size'] * 2)) print('Turn: {turn: ^{size}}'.format(turn=grid._sim_turn, size=sim_config['grid_size'])) print() print( display_simple_grid(grid.get_simulation_grid_data(), sim_config['grid_size'])) print() end_time = current_milli_time() print('Turn duration: {} ms'.format(end_time - start_time)) print()