def test_process_heightfiles_with_gdal_with_no_files(mock_call): state = State() state.set_window(0, 7, 2, 1) status = HeightMapStatus(0, [], state) building.process_heightfiles_with_gdal(status) mock_call.assert_not_called() assert status.current_files == []
def test_add_file_when_success(mock_exists): state = State() result = state.add_height_file('mylittletestfile') mock_exists.assert_called() assert result == FileAddResult.SUCCESS assert 1 == len(state.height_files) assert path.abspath('mylittletestfile') in state.height_files
def test_add_file_when_already_added(mock_exists): state = State() state.add_height_file('mylittletestfile') result = state.add_height_file('mylittletestfile') assert result == FileAddResult.ALREADY_ADDED assert 1 == len(state.height_files) assert path.abspath('mylittletestfile') in state.height_files
def test_add_file_can_add_multiple(mock_exists): state = State() for i in range(10): result = state.add_osm_file('testfile{}'.format(i)) assert FileAddResult.SUCCESS == result assert 10 == len(state.osm_files) for i in range(10): assert path.abspath('testfile{}'.format(i)) in state.osm_files
def test_process_heightfiles_with_gdal(mock_call): state = State() state.set_window(0, 7, 2, 1) status = HeightMapStatus(0, ['test.txt'], state) building.process_heightfiles_with_gdal(status) outpath = path.join(building.BUILD_DIR, building.INTERMEDIATE_HEIGHT_FILENAME) expected_command = 'gdalwarp -tr 10 10 -te_srs EPSG:4326 -t_srs EPSG:3857 -r bilinear -te 0 1 2 7 test.txt {}'.format( outpath) mock_call.assert_called_once_with(expected_command, status, False) assert status.current_files == [outpath]
def test_insert_colors(): test_init_build() data = OSMData.load(get_resource_path('test_osm_terrains_input.xml')) state = State() state.add_area_color('meadow', 0, 255, 0) status = OSMStatus(0, ['asd'], state) status.osmdata = [data] building.insert_colors(status) outpath = path.join(building.BUILD_DIR, 'result.xml') data.save(outpath) assert_xml_equal(get_resource_path('test_osm_terrain_colors_expected.xml'), outpath)
def test_process_satellite_with_gdal(mock_call): state = State() state.set_window(0, 7, 2, 1) status = SatelliteStatus(0, ['test.tif', 'test2.tif'], state) building.process_satellite_with_gdal(status) outpath = path.join(building.FINALIZED_DIR, building.INTERMEDIATE_SATELLITE_FORMAT.format(0)) expected_command = 'gdalwarp -tr 10 10 -te_srs EPSG:4326 -t_srs EPSG:3857 -r bilinear -te 0 1 2 7 test.tif test2.tif {}'.format( outpath) mock_call.assert_called_once_with(expected_command, status, False) assert status.current_files == [outpath]
def test_check_projection_window(mock_forfile, mock_call): ginfo_for_testing = Gdalinfo() ginfo_for_testing.minX = -99 ginfo_for_testing.minY = -99 ginfo_for_testing.maxX = 99 ginfo_for_testing.maxY = 3 mock_forfile.return_value = ginfo_for_testing state = State() state.set_window(0, 7, 2, 1) status = HeightMapStatus(0, ['test.txt'], state) building.check_projection_window(status) mock_forfile.assert_called_once_with('test.txt') assert status.current_files == ['test.txt']
def test_translate_satellite_to_png(mock_call): state = State() status = SatelliteStatus(0, ['intermediate.tiff'], state) building.translate_satellite_to_png(status) outpath = path.join(building.FINALIZED_DIR, building.FINAL_SATELLITE_FORMAT.format(0)) expected_command = 'gdal_translate -of {} {} {}'.format( building.SATELLITE_OUTPUT_FORMAT, 'intermediate.tiff', outpath) mock_call.assert_called_once_with(expected_command, status, False) assert status.result_files == [outpath]
def test_from_dict(): state = State.from_dict({ 'height_files': ['foo', 'bar'], 'osm_files': ['hello', 'world'], 'window': { 'ulx': 1, 'uly': 2, 'lrx': 3, 'lry': 4 } }) assert ['foo', 'bar'] == state.height_files assert ['hello', 'world'] == state.osm_files assert (1, 2) == state.get_window_upper_left() assert (3, 4) == state.get_window_lower_right()
def test_window_string(): state = State() assert state.get_window_string() == '' state.set_window(-1, 4, 1, 2) nums = list(map(int, state.get_window_string().split(' '))) assert len(nums) == 4 assert nums[0] == -1 assert nums[1] == 4 assert nums[2] == 1 assert nums[3] == 2
def load_state(): if state_exists(): with open(state_path(), 'r') as infile: return State.from_dict(json.load(infile)) else: return init_state()
def test_add_file_when_doesnt_exist(mock_exists): state = State() result = state.add_osm_file('mylittletestfile') mock_exists.assert_called() assert result == FileAddResult.DOESNT_EXIST assert 0 == len(state.osm_files)
class TestCliUtil: def x_or_error_shows_error(self, function, mock_persistence_func, mock_echoes, call_contents): assert not function() mock_persistence_func.assert_called_once() mock_error = mock_echoes.error mock_error.assert_called() total_output = '' for call in mock_error.call_args_list: args, kwargs = call assert 0 == len(kwargs) assert 1 == len(args) total_output += ' ' + args[0] for piece in call_contents: assert piece in total_output def x_or_error_no_error(self, function, mock_persistence_func, mock_echoes, return_value): assert function() == return_value mock_persistence_func.assert_called_once() mock_error = mock_echoes.error mock_error.assert_not_called() @patch('mapcreator.persistence.init_state', side_effect=OSError('Whoops!')) def test_init_or_error_shows_error_when_unsuccessful( self, mock_init, mock_echoes): self.x_or_error_shows_error( cli_util.init_or_error, mock_init, mock_echoes, ['Unable to initialize project', str(OSError('Whoops!'))]) @patch('mapcreator.persistence.init_state', return_value='Success :-)') def test_init_or_error_doesnt_show_error_when_successful( self, mock_init, mock_echoes): self.x_or_error_no_error(cli_util.init_or_error, mock_init, mock_echoes, 'Success :-)') @patch('mapcreator.persistence.load_state', side_effect=OSError('Whoops!')) def test_load_or_error_shows_error_when_unsuccessful( self, mock_load, mock_echoes): self.x_or_error_shows_error( cli_util.load_or_error, mock_load, mock_echoes, [ 'Unable to load or initialize the project', str(OSError('Whoops!')) ]) @patch('mapcreator.persistence.load_state', return_value='Success :-)') def test_load_or_error_doesnt_show_error_when_successful( self, mock_load, mock_echoes): self.x_or_error_no_error(cli_util.load_or_error, mock_load, mock_echoes, 'Success :-)') @patch('mapcreator.persistence.save_state', side_effect=OSError('Whoops!')) def test_save_or_error_shows_error_when_unsuccessful( self, mock_save, mock_echoes): self.x_or_error_shows_error( lambda: cli_util.save_or_error('asd'), mock_save, mock_echoes, [ 'Unable to save changes', 'No changes done', 'What went wrong', str(OSError('Whoops!')) ]) @patch('mapcreator.persistence.save_state', return_value=True) def test_save_or_error_doesnt_show_error_when_successful( self, mock_save, mock_echoes): self.x_or_error_no_error(lambda: cli_util.save_or_error('Success :-)'), mock_save, mock_echoes, 'Success :-)') @patch('mapcreator.persistence.clear_state', side_effect=OSError('Whoops!')) def test_clear_or_error_shows_error_when_unsuccessful( self, mock_clear, mock_echoes): self.x_or_error_shows_error( cli_util.clear_or_error, mock_clear, mock_echoes, ['Unable to reset project', str(OSError('Whoops!'))]) @patch('mapcreator.persistence.clear_state', return_value=True) def test_clear_or_error_doesnt_show_error_when_successful( self, mock_clear, mock_echoes): self.x_or_error_no_error(cli_util.clear_or_error, mock_clear, mock_echoes, True) @patch('mapcreator.building.init_build', side_effect=OSError('Whoops!')) def test_build_init_or_error_shows_error_when_unsuccessful( self, mock_init, mock_echoes): self.x_or_error_shows_error( cli_util.build_init_or_error, mock_init, mock_echoes, ['Unable to initialize build', str(OSError('Whoops!'))]) @patch('mapcreator.building.init_build') def test_build_init_or_error_doesnt_show_error_when_successful( self, mock_init, mock_echoes): self.x_or_error_no_error(cli_util.build_init_or_error, mock_init, mock_echoes, True) @patch('mapcreator.building.cleanup', side_effect=OSError('Whoops!')) def test_build_clean_or_error_shows_error_when_unsuccessful( self, mock_clean, mock_echoes): self.x_or_error_shows_error( cli_util.build_clean_or_error, mock_clean, mock_echoes, ['Unable to clean', str(OSError('Whoops!'))]) @patch('mapcreator.building.cleanup') def test_build_clean_or_error_doesnt_show_error_when_successful( self, mock_clean, mock_echoes): self.x_or_error_no_error(cli_util.build_clean_or_error, mock_clean, mock_echoes, True) @patch('mapcreator.persistence.load_state', lambda: State()) @patch.object(mapcreator.state.State, 'add_height_file') @patch('mapcreator.persistence.save_state', side_effect=lambda state: state ) #Returns state, so save was successful def test_add_files(self, mock_save, mock_state, mock_echoes): mock_state.return_value = FileAddResult.SUCCESS cli_util.add_files(('test1.txt', 'test2.txt'), 'add_height_file') mock_state.assert_any_call('test1.txt') mock_state.assert_any_call('test2.txt') assert mock_state.call_count == 2 assert mock_save.call_count == 1 mock_echoes.success.assert_called() @patch('mapcreator.persistence.load_state', lambda: State()) @patch.object(mapcreator.state.State, 'add_osm_file') @patch('mapcreator.persistence.save_state') def test_add_files_no_files_exist(self, mock_save, mock_state, mock_echoes): mock_state.return_value = FileAddResult.DOESNT_EXIST cli_util.add_files(('test1.txt', 'test2.txt', 'test3.txt'), 'add_osm_file') mock_state.assert_any_call('test1.txt') mock_state.assert_any_call('test2.txt') mock_state.assert_any_call('test3.txt') assert mock_state.call_count == 3 assert mock_save.call_count == 0 # Don't save if there's no changes for i in range(1, 3): mock_echoes.error.assert_any_call( 'File "test{}.txt" doesn\'t exist!'.format(i)) @patch('mapcreator.persistence.load_state', lambda: State()) @patch.object(mapcreator.state.State, 'add_height_file') @patch('mapcreator.persistence.save_state') def test_add_files_all_already_added(self, mock_save, mock_state, mock_echoes): mock_state.return_value = FileAddResult.ALREADY_ADDED cli_util.add_files(('test3.txt', ), 'add_height_file') mock_state.assert_called_once_with('test3.txt') assert mock_save.call_count == 0 mock_echoes.warn.assert_any_call( 'test3.txt has already been added to this project') @patch('mapcreator.persistence.load_state', lambda: State()) @patch.object(mapcreator.state.State, 'add_osm_file') @patch('mapcreator.persistence.save_state') def test_add_files_some_files_ok(self, mock_save, mock_state, mock_echoes): def add_side_effect(filename): if filename in ('1', '11', '21'): return FileAddResult.SUCCESS else: return (FileAddResult.ALREADY_ADDED, FileAddResult.DOESNT_EXIST)[int(filename) % 2] mock_state.side_effect = add_side_effect files = [str(i) for i in range(25)] cli_util.add_files(files, 'add_osm_file') assert mock_state.call_count == len(files) assert mock_save.call_count == 1 mock_echoes.warn.assert_called_with( '3 files (out of 25) added to the project successfully') @patch('mapcreator.persistence.load_state', lambda: State()) @patch.object(mapcreator.state.State, 'clear_osm_files') @patch('mapcreator.persistence.save_state') def test_clear_files(self, mock_save, mock_state, mock_echoes): cli_util.clear_files('clear_osm_files', 'open street map') assert mock_state.call_count == 1 assert mock_save.call_count == 1 mock_echoes.success.assert_called_with( 'All open street map files cleared successfully!') def test_parse_color_wrong_number_of_args(self, mock_echoes): mock_error = mock_echoes.error assert not cli_util.parse_color("tag 123 221 9 77") mock_error.assert_called() def test_parse_color_invalid_number_format(self, mock_echoes): mock_error = mock_echoes.error assert not cli_util.parse_color("terrain 7 8 cheese") mock_error.assert_called() def test_parse_color(self, mock_echoes): mock_error = mock_echoes.error assert ["hello", 7, 88, 3] == cli_util.parse_color("hello 7 88 3") mock_error.assert_not_called() def test_validate_color_inivalid_color(self, mock_echoes): mock_error = mock_echoes.error assert not cli_util.validate_color(-11, 255, 13) mock_error.assert_called() mock_error.reset_mock() assert not cli_util.validate_color(0, 256, 13) mock_error.assert_called() mock_error.reset_mock() assert not cli_util.validate_color(6, 7, 999) mock_error.assert_called() def test_validate_color_valid_color(self, mock_echoes): mock_error = mock_echoes.error assert cli_util.validate_color(0, 255, 127) assert cli_util.validate_color(255, 44, 0) assert cli_util.validate_color(31, 0, 255) assert cli_util.validate_color(123, 221, 99) mock_error.assert_not_called() def test_validate_resolution_valid_reso(self, mock_echoes): mock_error = mock_echoes.error assert cli_util.validate_resolution(1, 0.5, 1000) assert cli_util.validate_resolution(0.05, 0.01, 0.05) assert cli_util.validate_resolution(700, 300, 800) mock_error.assert_not_called() def test_validate_resolution_invalid_reso(self, mock_echoes): mock_error = mock_echoes.error assert not cli_util.validate_resolution(1, 2, 1000) mock_error.assert_called() mock_error.reset_mock() assert not cli_util.validate_resolution(1000, 50, 999) mock_error.assert_called() mock_error.reset_mock() assert not cli_util.validate_resolution(0.05, 0.1, 0.5) mock_error.assert_called()
def test_has_window(): state = State() assert not state.has_window() state.set_window(1, 2, 3, 4) assert state.has_window()
def test_has_height_files(): state = State() assert not state.has_height_files() state.add_height_file('asd') assert state.has_height_files()
def init_state(): initial_state = State() save_state(initial_state) return initial_state
def test_check_projection_window_when_no_window(mock_call): status = HeightMapStatus(0, ['test.txt', 'test2.txt'], State()) building.check_projection_window(status) mock_call.assert_not_called() assert status.current_files == []
def test_init_state(): persistence.init_state() with open(path.join(persistence.STATE_DIR, persistence.STATE_FILE)) as f: content = json.load(f) assert content == State().__dict__
def test_has_satellite_system(): state = State() assert not state.has_satellite_system() state.set_satellite_system('EPSG:12345') assert state.has_satellite_system()
def test_has_height_system(): state = State() assert not state.has_height_system() state.set_height_system('EPSG:1234') assert state.has_height_system()