def test_default_can_read(): F = DummyFormat('test', '', 'foobar') # Prepare files filename = join(DATA_PATH, 'data', 'spectra.foobar') filename_wrong_ext = join(DATA_PATH, 'data', 'spectra.notavalidext') # Test _can_read() assert F.can_read(Request(filename)) assert not F.can_read(Request(filename_wrong_ext))
def test_reader(): filename = join(DATA_PATH, 'data', 'spectra.foobar') F = MyFormat('test', '') # Test using reader n = 3 R = F.get_reader(Request(filename)) assert len(R) == n specs = [spec for spec in R] assert len(specs) == n for i in range(3): assert specs[i].spectrum[0, 0] == i assert specs[i].wavelength[0] == i assert specs[i].meta['index'] == i for i in range(3): assert R.get_meta_data(i)['index'] == i # Read next assert R.get_data(0).spectrum[0, 0] == 0 assert R.get_next_data().spectrum[0, 0] == 1 assert R.get_next_data().spectrum[0, 0] == 2 # Fail R._failmode = 1 with pytest.raises(ValueError, message="Meta data be a dict"): R.get_meta_data(0) R._failmode = 2 with raises(IndexError): [spec for spec in R]
def test_request_read_sources(): filename = join(DATA_PATH, 'data', 'spectra.foobar') R = Request(filename) first_bytes = R.firstbytes all_bytes = open(filename, 'rb').read() assert len(first_bytes) == 256 assert all_bytes.startswith(first_bytes)
def test_format_subclass(): filename = join(DATA_PATH, 'data', 'spectra.foobar') F = MyFormat('test', '') assert 'TEST DOCS' in F.doc # Get and check reader and write classes R = F.get_reader(Request(filename)) assert isinstance(R, MyFormat.Reader) assert R.format is F assert R.request.filename == filename
def test_get_reader(): filename = load_mzml_path() R = Request(filename) F = formats["MZML"] assert F.can_read(R) reader = F.get_reader(R) assert reader.get_length() == 531 spec = reader.get_data(0) assert spec.amplitudes.shape == (282,) assert spec.wavelength.shape == (282,) assert spec.meta['filename'] == basename(filename) assert spec.amplitudes[0] == pytest.approx(37.384331)
def test_dummy_format(): filename = join(DATA_PATH, 'data', 'spectra.foobar') R = Request(filename) F = formats['FOOBAR'] assert F.can_read(R) reader = F.get_reader(R) assert reader.get_length() == 1 assert reader.get_meta_data() == {} spec = reader.get_data(0) assert spec.amplitudes.shape == (1, 801) assert spec.wavelength.shape == (801, )
def test_format_context_manager(): filename = join(DATA_PATH, 'data', 'spectra.foobar') F = MyFormat('test', '') R = F.get_reader(Request(filename)) # Use as context manager with R: pass # Objects are now closed, cannot be used assert R.closed with pytest.raises(RuntimeError, message="I/O operation on closed Reader."): R.__enter__() with pytest.raises(RuntimeError, message="I/O operation on closed Reader."): R.get_data(0) R = F.get_reader(Request(filename)) ids = id(R) F._closed[:] = [] del R gc.collect() # Invoke __del__ assert ids == F._closed[0]
def test_format_selection(): filename = join(DATA_PATH, 'data', 'spectra.foobar') test_dir = mkdtemp() fname2 = join(test_dir, 'test.selectext1') fname3 = join(test_dir, 'test.haha') open(fname2, 'wb') open(fname3, 'wb') try: F = formats.search_read_format(Request(filename)) assert F is formats['DUMMY'] # Now with custom format format = MyFormat('test_selection', 'xx', 'selectext1') formats.add_format(format) assert '.selectext1' in fname2 F = formats.search_read_format(Request(fname2)) assert F is format assert '.haha' in fname3 F = formats.search_read_format(Request(fname3)) assert F is format finally: shutil.rmtree(test_dir)
def test_spc_format(): filename = load_spc_path() R = Request(filename) F = formats['SPC'] assert F.can_read(R) reader = F.get_reader(R) assert reader.get_length() == 1 assert reader.get_meta_data()['dat_fmt'] == 'x-y' spec = reader.get_data() assert spec.spectrum.shape == (1911, ) assert spec.wavelength.shape == (1911, ) spec = reader.get_data(0) assert spec.spectrum.shape == (1911, ) assert spec.wavelength.shape == (1911, )
def test_get_reader(filename, filename_cmp, format_spec, len_spectrum, spectrum_shape, wavelength_shape, first_value): R = Request(filename) F = formats[format_spec] assert F.can_read(R) reader = F.get_reader(R) assert reader.get_length() == len_spectrum spec = reader.get_data() if isinstance(spec, Spectrum): _check_get_spectra(spec, filename, filename_cmp, spectrum_shape, wavelength_shape) spec = reader.get_data(0) assert spec.amplitudes.size == spectrum_shape[-1] assert spec.wavelength.size == wavelength_shape[-1] assert spec.amplitudes[0] == pytest.approx(first_value)
def test_fsm_format(): filename = load_fsm_path() R = Request(filename) F = formats['FSM'] assert F.can_read(R) reader = F.get_reader(R) assert reader.get_length() == 7998 assert reader.get_meta_data()['signature'] == b'PEPE' spec = reader.get_data() assert spec.spectrum.shape == (7998, 1641) assert spec.wavelength.shape == (1641, ) spec = reader.get_data(0) assert spec.spectrum.shape == (1641, ) assert spec.wavelength.shape == (1641, ) assert spec.spectrum[0] == pytest.approx(38.656551)
def test_request_file_no_seek(): R = Request(File()) with raises(IOError): R.firstbytes
def test_request_error(type_error, msg, params): with pytest.raises(type_error, message=msg): Request(params)
def test_request(): filename = 'file://' + join(DATA_PATH, 'data', 'spectra.foobar') R = Request(filename) assert R._uri_type == core.request.URI_FILENAME assert R.get_local_filename() == filename[7:] file_obj = R.get_file() assert file_obj.name == filename[7:] filename = join(DATA_PATH, 'data', 'spectra.foobar') R = Request(filename) assert R._uri_type == core.request.URI_FILENAME assert R.get_local_filename() == filename file_obj = R.get_file() assert file_obj.name == filename filename = join(DATA_PATH, 'data', 'spectra.foobar') file_obj = open(filename, 'rb') R = Request(file_obj) assert R.get_local_filename() == filename assert R.get_file().name == filename shutil.copy(filename, expanduser('~/')) filename = '~/spectra.foobar' R = Request(filename) assert R.filename == expanduser('~/spectra.foobar').replace('/', sep) os.remove(expanduser(filename)) filename = join(DATA_PATH, 'data', 'spectra.foobar') R = Request(filename, some_kwarg='something') assert R.kwargs == {'some_kwarg': 'something'}