def setUp(self): tmp_dir = tempfile.mkdtemp() data_format = get_data_format('application/json') self.complex_out = ComplexOutput(identifier="complexinput", workdir=tmp_dir, data_format=data_format, supported_formats=[data_format], mode=MODE.NONE) self.complex_out_nc = ComplexOutput( identifier="netcdf", workdir=tmp_dir, data_format=get_data_format('application/x-netcdf'), supported_formats=[get_data_format('application/x-netcdf')], mode=MODE.NONE) self.data = json.dumps({ 'a': 1, 'unicodé': u'éîïç', }) self.ncfile = os.path.join(DATA_DIR, 'netcdf', 'time.nc') self.test_fn = os.path.join(self.complex_out.workdir, 'test.json') with open(self.test_fn, 'w') as f: f.write(self.data)
def test_build_output_name(self): storage = FileStorageBuilder().build() output = ComplexOutput('testme', 'Test', supported_formats=[FORMATS.TEXT], workdir=self.tmp_dir) output.data = "Hello World!" output_name, suffix = _build_output_name(output) self.assertEqual(output.file, self.tmp_dir + '/input.txt') self.assertEqual(output_name, 'input.txt') self.assertEqual(suffix, '.txt')
def test_write(self): configuration.CONFIG.set('server', 'outputpath', self.tmp_dir) configuration.CONFIG.set('server', 'outputurl', 'file://' + self.tmp_dir) storage = FileStorageBuilder().build() output = ComplexOutput('testme', 'Test', supported_formats=[FORMATS.TEXT], workdir=self.tmp_dir) output.data = "Hello World!" url = storage.write(output.data, 'foo.txt') self.assertEqual(url, 'file://' + self.tmp_dir + '/foo.txt') with open(self.tmp_dir + '/foo.txt') as f: self.assertEqual(f.read(), "Hello World!")
def test_store(self): configuration.CONFIG.set('server', 'outputpath', self.tmp_dir) storage = FileStorageBuilder().build() output = ComplexOutput('testme', 'Test', supported_formats=[FORMATS.TEXT], workdir=self.tmp_dir) output.data = "Hello World!" store_type, store_str, url = storage.store(output) self.assertEqual(store_type, STORE_TYPE.PATH) self.assertEqual(store_str, 'input.txt') with open(self.tmp_dir + '/' + store_str) as f: self.assertEqual(f.read(), "Hello World!")
def setUp(self): tmp_dir = tempfile.mkdtemp() data_format = get_data_format('application/json') self.complex_out = ComplexOutput(identifier="complexinput", workdir=tmp_dir, data_format=data_format, supported_formats=[data_format])
class ComplexOutputTest(unittest.TestCase): """ComplexOutput test cases""" def setUp(self): tmp_dir = tempfile.mkdtemp() data_format = get_data_format('application/json') self.complex_out = ComplexOutput(identifier="complexinput", workdir=tmp_dir, data_format=data_format, supported_formats=[data_format], mode=MODE.NONE) self.data = json.dumps({'a': 1, 'unicodé': u'éîïç', }) self.test_fn = os.path.join(self.complex_out.workdir, 'test.json') with open(self.test_fn, 'w') as f: f.write(self.data) def test_contruct(self): self.assertIsInstance(self.complex_out, ComplexOutput) def test_data_format(self): self.assertIsInstance(self.complex_out.data_format, Format) def test_storage(self): class Storage(object): pass storage = Storage() self.complex_out.store = storage self.assertEqual(self.complex_out.store, storage) def test_validator(self): self.assertEqual(self.complex_out.validator, get_validator('application/json')) def test_file_handler(self): self.complex_out.file = self.test_fn self.assertEqual(self.complex_out.data, self.data) if PY2: self.assertEqual(self.complex_out.stream.read(), self.data) else: with self.complex_out.stream as s: self.assertEqual(s.read(), bytes(self.data, encoding='utf8')) with open(urlparse(self.complex_out.url).path) as f: self.assertEqual(f.read(), self.data) def test_data_handler(self): self.complex_out.data = self.data with open(self.complex_out.file) as f: self.assertEqual(f.read(), self.data) def test_url_handler(self): wfsResource = 'http://demo.mapserver.org/cgi-bin/wfs?' \ 'service=WFS&version=1.1.0&' \ 'request=GetFeature&' \ 'typename=continents&maxfeatures=2' self.complex_out.url = wfsResource storage = FileStorage() self.complex_out.storage = storage url = self.complex_out.get_url() self.assertEqual('file', urlparse(url).scheme)
def test_store(self, uploadData): configuration.CONFIG.set('s3', 'bucket', 'notrealbucket') configuration.CONFIG.set('s3', 'prefix', 'wps') storage = S3StorageBuilder().build() output = ComplexOutput('testme', 'Test', supported_formats=[FORMATS.TEXT], workdir=self.tmp_dir) output.data = "Hello World!" store_type, filename, url = storage.store(output) called_args = uploadData.call_args[0] self.assertEqual(store_type, STORE_TYPE.S3) self.assertEqual(filename, 'wps/input.txt') self.assertEqual(uploadData.call_count, 1) self.assertEqual(called_args[1], 'wps/input.txt') self.assertEqual(called_args[2], {'ContentType': 'text/plain'})
def test_url(self): configuration.CONFIG.set('server', 'outputpath', self.tmp_dir) configuration.CONFIG.set('server', 'outputurl', 'file://' + self.tmp_dir) storage = FileStorageBuilder().build() output = ComplexOutput('testme', 'Test', supported_formats=[FORMATS.TEXT], workdir=self.tmp_dir) output.data = "Hello World!" url = storage.url(output) self.assertEqual('file://' + self.tmp_dir + '/input.txt', url) file_name = 'test.txt' url = storage.url(file_name) self.assertEqual('file://' + self.tmp_dir + '/test.txt', url)
def setUp(self): tmp_dir = tempfile.mkdtemp() data_format = get_data_format('application/json') self.complex_out = ComplexOutput(identifier="complexinput", workdir=tmp_dir, data_format=data_format, supported_formats=[data_format], mode=MODE.NONE) self.complex_out_nc = ComplexOutput(identifier="netcdf", workdir=tmp_dir, data_format=get_data_format('application/x-netcdf'), supported_formats=[get_data_format('application/x-netcdf')], mode=MODE.NONE) self.data = json.dumps({'a': 1, 'unicodé': u'éîïç', }) self.ncfile = os.path.join(DATA_DIR, 'netcdf', 'time.nc') self.test_fn = os.path.join(self.complex_out.workdir, 'test.json') with open(self.test_fn, 'w') as f: f.write(self.data)
class ComplexOutputTest(unittest.TestCase): """ComplexOutput test cases""" def setUp(self): tmp_dir = tempfile.mkdtemp() data_format = get_data_format('application/json') self.complex_out = ComplexOutput(identifier="complexinput", workdir=tmp_dir, data_format=data_format, supported_formats=[data_format], mode=MODE.NONE) self.complex_out_nc = ComplexOutput(identifier="netcdf", workdir=tmp_dir, data_format=get_data_format('application/x-netcdf'), supported_formats=[get_data_format('application/x-netcdf')], mode=MODE.NONE) self.data = json.dumps({'a': 1, 'unicodé': u'éîïç', }) self.ncfile = os.path.join(DATA_DIR, 'netcdf', 'time.nc') self.test_fn = os.path.join(self.complex_out.workdir, 'test.json') with open(self.test_fn, 'w') as f: f.write(self.data) def test_contruct(self): self.assertIsInstance(self.complex_out, ComplexOutput) def test_data_format(self): self.assertIsInstance(self.complex_out.data_format, Format) def test_storage(self): class Storage(object): pass storage = Storage() self.complex_out.store = storage self.assertEqual(self.complex_out.store, storage) def test_validator(self): self.assertEqual(self.complex_out.validator, get_validator('application/json')) self.assertEqual(self.complex_out_nc.validator, get_validator('application/x-netcdf')) def test_file_handler(self): self.complex_out.file = self.test_fn self.assertEqual(self.complex_out.data, self.data) if PY2: self.assertEqual(self.complex_out.stream.read(), self.data) else: with self.complex_out.stream as s: self.assertEqual(s.read(), bytes(self.data, encoding='utf8')) with open(urlparse(self.complex_out.url).path) as f: self.assertEqual(f.read(), self.data) def test_file_handler_netcdf(self): self.complex_out_nc.file = self.ncfile data = self.complex_out_nc.base64 def test_data_handler(self): self.complex_out.data = self.data with open(self.complex_out.file) as f: self.assertEqual(f.read(), self.data) def test_base64(self): self.complex_out.data = self.data b = self.complex_out.base64 if PY2: self.assertEqual(base64.b64decode(b), self.data) else: self.assertEqual(base64.b64decode(b).decode(), self.data) def test_url_handler(self): wfsResource = 'http://demo.mapserver.org/cgi-bin/wfs?' \ 'service=WFS&version=1.1.0&' \ 'request=GetFeature&' \ 'typename=continents&maxfeatures=2' self.complex_out.url = wfsResource storage = FileStorage() self.complex_out.storage = storage url = self.complex_out.get_url() self.assertEqual('file', urlparse(url).scheme)