class GeometryFileFieldTestCase(SimpleTestCase): def setUp(self): self.field = GeometryFileField() self.fp = SimpleUploadedFile( 'geom.json', json.dumps(_geom).encode('ascii')) self.fp.seek(0) def test_to_python(self): self.assertIsInstance(self.field.to_python(self.fp), OGRGeometry) fp = SimpleUploadedFile('empty.json', b'{}') self.assertRaises(forms.ValidationError, self.field.to_python, fp) def test_feature_to_python(self): feature = Feature(geometry=_geom) self.fp.write(str(feature).encode('ascii')) self.fp.seek(0) v = self.field.to_python(self.fp) self.assertIsInstance(v, OGRGeometry) def test_shapefile(self): base = 'dir/geofield.shp' path = default_storage.path(base) os.mkdir(os.path.dirname(path)) write_shp(path, _geom) b = io.BytesIO() with zipfile.ZipFile(b, 'w') as zf: for ext in ('dbf', 'prj', 'shp', 'shx'): fname = base.replace('shp', ext) with default_storage.open(fname) as fp: zf.writestr(fname, fp.read()) shutil.rmtree(os.path.dirname(path)) upfile = SimpleUploadedFile('geofield.zip', b.getvalue()) b.close() result = self.field.to_python(upfile) self.assertIsInstance(result, OGRGeometry) self.assertIsNotNone(result.srs) def test_zipfile(self): zfile = io.BytesIO() with zipfile.ZipFile(zfile, 'w') as zf: zf.writestr(self.fp.name, self.fp.read()) zfile.seek(0) upfile = SimpleUploadedFile('geofield.zip', zfile.read()) zfile.close() self.assertIsInstance(self.field.to_python(upfile), OGRGeometry) def tearDown(self): self.fp.close()
class GeometryFileFieldTestCase(SimpleTestCase): def setUp(self): self.field = GeometryFileField() self.fp = SimpleUploadedFile('geom.json', json.dumps(_geom).encode('ascii')) self.fp.seek(0) def test_to_python(self): self.assertIsInstance(self.field.to_python(self.fp), OGRGeometry) fp = SimpleUploadedFile('empty.json', b'{}') self.assertRaises(forms.ValidationError, self.field.to_python, fp) def test_feature_to_python(self): feature = Feature(geometry=_geom) self.fp.write(str(feature).encode('ascii')) self.fp.seek(0) v = self.field.to_python(self.fp) self.assertIsInstance(v, OGRGeometry) def test_shapefile(self): base = 'dir/geofield.shp' path = default_storage.path(base) os.mkdir(os.path.dirname(path)) write_shp(path, _geom) b = io.BytesIO() with zipfile.ZipFile(b, 'w') as zf: for ext in ('dbf', 'prj', 'shp', 'shx'): fname = base.replace('shp', ext) with default_storage.open(fname) as fp: zf.writestr(fname, fp.read()) shutil.rmtree(os.path.dirname(path)) upfile = SimpleUploadedFile('geofield.zip', b.getvalue()) b.close() result = self.field.to_python(upfile) self.assertIsInstance(result, OGRGeometry) self.assertIsNotNone(result.srs) def test_zipfile(self): zfile = io.BytesIO() with zipfile.ZipFile(zfile, 'w') as zf: zf.writestr(self.fp.name, self.fp.read()) zfile.seek(0) upfile = SimpleUploadedFile('geofield.zip', zfile.read()) zfile.close() self.assertIsInstance(self.field.to_python(upfile), OGRGeometry) def tearDown(self): self.fp.close()
class GeometryFileFieldTestCase(SimpleTestCase): def setUp(self): self.field = GeometryFileField() self.fp = default_storage.open('geofield.json', 'w+b') self.fp.write(json.dumps(_geom)) self.fp.seek(0) def test_to_python(self): self.assertIsInstance(self.field.to_python(self.fp), OGRGeometry) def test_zipfile(self): zfile = default_storage.open('geofield.zip', 'w+b') with zipfile.ZipFile(zfile, 'w') as zf: zf.write(self.fp.name) zfile.seek(0) self.assertIsInstance(self.field.to_python(zfile), OGRGeometry) zfile.close() def tearDown(self): self.fp.close() default_storage.delete(self.fp.name)
def setUp(self): self.field = GeometryFileField() self.fp = SimpleUploadedFile('geom.json', json.dumps(_geom).encode('ascii')) self.fp.seek(0)
def setUp(self): self.field = GeometryFileField() self.fp = SimpleUploadedFile( 'geom.json', json.dumps(_geom).encode('ascii')) self.fp.seek(0)
def setUp(self): self.field = GeometryFileField() self.fp = default_storage.open('geofield.json', 'w+b') self.fp.write(json.dumps(_geom)) self.fp.seek(0)