def test_lookup(svg_lib, ngw_env, ngw_webtest_app): svg_marker_library = ngw_env.svg_marker_library file_storage = ngw_env.file_storage def lookup_marker(name): return svg_marker_library.lookup(name, library=svg_lib) def filename(fileobj): return file_storage.filename(fileobj, makedirs=False) marker1 = svg_lib.find_svg_marker('marker1') marker2 = svg_lib.find_svg_marker('marker2') assert lookup_marker('marker1') == filename(marker1.fileobj) assert lookup_marker('marker2') == filename(marker2.fileobj) assert lookup_marker('marker3') == path.join(FOLDER1, 'marker3.svg') svg_marker_library.cache.clear() with transaction.manager: DBSession.delete(marker1) assert lookup_marker('marker1') == path.join(FOLDER1, 'marker1.svg') assert lookup_marker('marker2') == filename(marker2.fileobj) svg_marker_library.cache.clear() with svg_marker_library.options.override({'path': [FOLDER1]}): assert lookup_marker('marker1') == path.join(FOLDER1, 'marker1.svg')
def vector_layer_id(ngw_resource_group): with transaction.manager: obj = VectorLayer( parent_id=ngw_resource_group, display_name='vector_layer', owner_user=User.by_keyname('administrator'), srs=SRS.filter_by(id=3857).one(), tbl_uuid=uuid4().hex, ).persist() geojson = { 'type': 'FeatureCollection', 'features': get_features_for_orderby_test() } dsource = ogr.Open(json.dumps(geojson)) layer = dsource.GetLayer(0) obj.setup_from_ogr(layer) obj.load_from_ogr(layer) DBSession.flush() DBSession.expunge(obj) yield obj.id with transaction.manager: DBSession.delete(VectorLayer.filter_by(id=obj.id).one())
def type_layer(ngw_resource_group): with transaction.manager: vl_type = VectorLayer( parent_id=ngw_resource_group, display_name='type', owner_user=User.by_keyname('administrator'), srs=SRS.filter_by(id=3857).one(), tbl_uuid=uuid4().hex, ).persist() import nextgisweb.vector_layer.test path = Path( nextgisweb.vector_layer.test.__file__).parent / 'data/type.geojson' ogrds = ogr.Open(str(path)) ogrlayer = ogrds.GetLayer(0) vl_type.setup_from_ogr(ogrlayer) vl_type.load_from_ogr(ogrlayer) DBSession.flush() DBSession.expunge(vl_type) yield vl_type.id with transaction.manager: DBSession.delete(VectorLayer.filter_by(id=vl_type.id).one())
def test_storage_estimate_all(ngw_env, ngw_resource_group, ngw_webtest_app, ngw_auth_administrator): with transaction.manager: res = vector_layer('test-vector-layer', ngw_resource_group) DBSession.flush() DBSession.expunge(res) feature = dict(geom='POINT (0 0)') ngw_webtest_app.post_json('/api/resource/%d/feature/' % res.id, feature) content = 'some-content' resp = ngw_webtest_app.put('/api/component/file_upload/', content) file_upload = resp.json ngw_webtest_app.post_json( '/api/resource/%d/feature/%d/attachment/' % (res.id, 1), dict(file_upload=file_upload)) ngw_webtest_app.post('/api/component/pyramid/estimate_storage', status=200) sleep(0.05) # Give a chance to start a thread and acquire the lock with transaction.manager: # Wait estimation end DBSession.execute(SQL_LOCK) cur = ngw_env.core.query_storage( dict(resource_id=lambda col: col == res.id)) assert FeatureAttachmentData.identity in cur assert cur[FeatureAttachmentData.identity]['data_volume'] == len(content) with transaction.manager: DBSession.delete(res)
def vector_layer_id(ngw_resource_group): with transaction.manager: obj = VectorLayer( parent_id=ngw_resource_group, display_name='vector_layer', owner_user=User.by_keyname('administrator'), srs=SRS.filter_by(id=3857).one(), tbl_uuid=uuid4().hex, ).persist() geojson = { "type": "FeatureCollection", "name": "polygon_extent", "crs": {"type": "name", "properties": {"name": "urn:ogc:def:crs:EPSG::3857"}}, "features": [ { "type": "Feature", "properties": {"name": "west"}, "geometry": { "type": "Polygon", "coordinates": [[ [5542180, 8799167], [6191082, 7551279], [4668659, 7126998], [5542180, 8799167], ]], }, }, { "type": "Feature", "properties": {"name": "east"}, "geometry": { "type": "Polygon", "coordinates": [[ [15100999, 10396463], [16498633, 10546209], [16673337, 9223449], [15175872, 8948913], [15100999, 10396463], ]], }, }, ], } dsource = ogr.Open(json.dumps(geojson)) layer = dsource.GetLayer(0) obj.setup_from_ogr(layer) obj.load_from_ogr(layer) DBSession.flush() DBSession.expunge(obj) yield obj.id with transaction.manager: DBSession.delete(VectorLayer.filter_by(id=obj.id).one())
def vlayer_id(ngw_resource_group): with transaction.manager: res_vl = VectorLayer( parent_id=ngw_resource_group, display_name='test_cyrillic', owner_user=User.by_keyname('administrator'), srs=SRS.filter_by(id=3857).one(), tbl_uuid=uuid4().hex, ).persist() geojson = { 'type': 'FeatureCollection', 'crs': { 'type': 'name', 'properties': { 'name': 'urn:ogc:def:crs:EPSG::3857' } }, 'features': [{ 'type': 'Feature', 'properties': { 'field1': 1, 'поле2': 'значение1', '!field3': '!@#$%^&*()значение1' }, 'geometry': { 'type': 'Point', 'coordinates': [0, 0] } }, { 'type': 'Feature', 'properties': { 'field1': 2, 'поле2': 'значение2', '!field3': '!@#$%^&*()значение2' }, 'geometry': { 'type': 'Point', 'coordinates': [10, 10] } }] } dsource = ogr.Open(json.dumps(geojson)) layer = dsource.GetLayer(0) res_vl.setup_from_ogr(layer) res_vl.load_from_ogr(layer) DBSession.flush() DBSession.expunge(res_vl) yield res_vl.id with transaction.manager: DBSession.delete(VectorLayer.filter_by(id=res_vl.id).one())
def ngw_resource_group(ngw_env): with transaction.manager: res = ResourceGroup( parent_id=0, owner_user=User.by_keyname('administrator'), display_name='Test resource group ({})'.format(datetime.now().isoformat()), ).persist() yield res.id with transaction.manager: DBSession.delete(ResourceGroup.filter_by(id=res.id).one())
def vector_layer_id(ngw_resource_group): with transaction.manager: obj = VectorLayer( parent_id=ngw_resource_group, display_name='vector_layer', owner_user=User.by_keyname('administrator'), srs=SRS.filter_by(id=3857).one(), tbl_uuid=uuid4().hex, ).persist() geojson = { 'type': 'FeatureCollection', 'crs': { 'type': 'name', 'properties': { 'name': 'urn:ogc:def:crs:EPSG::3857' } }, 'features': [{ 'type': 'Feature', 'properties': { 'price': -1, 'name': 'feature1' }, 'geometry': { 'type': 'Point', 'coordinates': [0, 0] } }, { 'type': 'Feature', 'properties': { 'price': -2, 'name': 'feature2' }, 'geometry': { 'type': 'Point', 'coordinates': [0, 0] } }] } dsource = ogr.Open(json.dumps(geojson)) layer = dsource.GetLayer(0) obj.setup_from_ogr(layer) obj.load_from_ogr(layer) DBSession.flush() DBSession.expunge(obj) yield obj.id with transaction.manager: DBSession.delete(VectorLayer.filter_by(id=obj.id).one())
def webmap(ngw_env, ngw_resource_group): with transaction.manager: obj = WebMap(parent_id=ngw_resource_group, display_name=__name__, owner_user=User.by_keyname('administrator'), root_item=WebMapItem(item_type='root')).persist() DBSession.flush() DBSession.expunge(obj) yield obj with transaction.manager: DBSession.delete(WebMap.filter_by(id=obj.id).one())
def webmap_with_items(ngw_resource_group, fixt_layers_styles): with transaction.manager: webmap = WebMap(parent_id=ngw_resource_group, display_name=__name__, owner_user=User.by_keyname('administrator'), root_item=WebMapItem(item_type='root')) webmap.from_dict(make_webmap_items(fixt_layers_styles)) webmap.persist() yield webmap, fixt_layers_styles with transaction.manager: DBSession.delete(WebMap.filter_by(id=webmap.id).one())
def srs_msk23_id(): with transaction.manager: obj = SRS( display_name="MSK23", wkt= 'PROJCS["МСК 23 зона 1",GEOGCS["GCS_Pulkovo_1942",DATUM["Pulkovo_1942",SPHEROID["Krassowsky_1940",6378245.0,298.3]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",1300000.0],PARAMETER["False_Northing",-4511057.628],PARAMETER["Central_Meridian",37.98333333333],PARAMETER["Scale_Factor",1.0],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]]', # NOQA: E501 ).persist() DBSession.flush() DBSession.expunge(obj) yield obj.id with transaction.manager: DBSession.delete(SRS.filter_by(id=obj.id).one())
def rstyle_id(ngw_env, rlayer_id): with transaction.manager: obj = RasterStyle( parent_id=rlayer_id, display_name='test-wms-rstyle', owner_user=User.by_keyname('administrator')).persist() DBSession.flush() DBSession.expunge(obj) yield obj.id with transaction.manager: DBSession.delete(RasterStyle.filter_by(id=obj.id).one())
def resource_id(ngw_resource_group): with transaction.manager: res = ResourceGroup( parent_id=ngw_resource_group, display_name='test-resource-group', owner_user=User.by_keyname('administrator')).persist() DBSession.flush() DBSession.expunge(res) yield res.id with transaction.manager: DBSession.delete(res)
def svg_lib(ngw_env, ngw_resource_group): with transaction.manager: svg_lib = SVGMarkerLibrary( parent_id=ngw_resource_group, display_name='test_marker_lib', owner_user=User.by_keyname('administrator')).persist() DBSession.flush() fileobj1 = FileObj(component='svg_marker_library').persist() fileobj2 = FileObj(component='svg_marker_library').persist() SVGMarker(svg_marker_library_id=svg_lib.id, fileobj=fileobj1, name='marker1').persist() marker2 = SVGMarker(svg_marker_library_id=svg_lib.id, fileobj=fileobj2, name='marker2').persist() DBSession.flush() yield svg_lib with transaction.manager: DBSession.delete(marker2) DBSession.delete(fileobj1) DBSession.delete(fileobj2) DBSession.delete(svg_lib)
def service_id(ngw_resource_group): with transaction.manager: res_vl = VectorLayer( parent_id=ngw_resource_group, display_name='test_vector_layer', owner_user=User.by_keyname('administrator'), srs=SRS.filter_by(id=3857).one(), tbl_uuid=six.text_type(uuid4().hex), ).persist() geojson = { 'type': 'FeatureCollection', 'crs': {'type': 'name', 'properties': {'name': 'urn:ogc:def:crs:EPSG::3857'}}, 'features': [{ 'type': 'Feature', 'properties': {'name': 'feature1'}, 'geometry': {'type': 'Point', 'coordinates': [0, 0]} }, { 'type': 'Feature', 'properties': {'price': -1}, 'geometry': {'type': 'Point', 'coordinates': [10, 10]} }] } dsource = ogr.Open(json.dumps(geojson)) layer = dsource.GetLayer(0) res_vl.setup_from_ogr(layer, lambda x: x) res_vl.load_from_ogr(layer, lambda x: x) DBSession.flush() res_wfs = WFSService( parent_id=ngw_resource_group, display_name='test_wfsserver_service', owner_user=User.by_keyname('administrator'), ).persist() res_wfs.layers.append(WFSLayer( resource=res_vl, keyname='test', display_name='test', maxfeatures=1000, )) DBSession.flush() DBSession.expunge(res_vl) DBSession.expunge(res_wfs) yield res_wfs.id with transaction.manager: DBSession.delete(VectorLayer.filter_by(id=res_vl.id).one()) DBSession.delete(WFSService.filter_by(id=res_wfs.id).one())
def user_with_webmap(ngw_env, ngw_resource_group): with transaction.manager: user = User( keyname=TEST_USER_KEYNAME, display_name='Test User', ).persist() test_root_resource = ResourceGroup.filter_by(id=0).one() append_acl(test_root_resource, 'allow', user, 'resource', 'read', ResourceGroup.identity) test_resource_group = ResourceGroup.filter_by( id=ngw_resource_group).one() append_acl(test_resource_group, 'allow', user, 'resource', 'read', ResourceGroup.identity) webmap = WebMap(parent_id=ngw_resource_group, display_name=__name__, owner_user=User.by_keyname(TEST_USER_KEYNAME), root_item=WebMapItem(item_type='root')).persist() append_acl(webmap, 'allow', user, 'resource', 'read', WebMap.identity) append_acl(webmap, 'allow', user, 'webmap', 'annotation_read', WebMap.identity) append_acl(webmap, 'allow', user, 'webmap', 'annotation_write', WebMap.identity) webmap.persist() user_admin_id = User.by_keyname('administrator').id make_annotation(webmap, public=True, user_id=user_admin_id) make_annotation(webmap, public=False, user_id=user_admin_id) make_annotation(webmap, public=True, user_id=user.id) make_annotation(webmap, public=False, user_id=user.id) DBSession.flush() yield user, webmap with transaction.manager: user = User.filter_by(keyname=TEST_USER_KEYNAME).one() webmap = WebMap.filter_by(owner_user_id=user.id).one() DBSession.query(ResourceACLRule).filter( ResourceACLRule.principal_id == user.id).delete() DBSession.query(User).filter( User.keyname == TEST_USER_KEYNAME).delete() DBSession.query(WebMapAnnotation).filter( WebMapAnnotation.webmap_id == webmap.id).delete() DBSession.delete(WebMap.filter_by(owner_user_id=user.id).one())
def fixt_layers_styles(ngw_env, ngw_resource_group): layers_styles_ = [] with transaction.manager: for i in range(count_layers_created): layer_id, style_id = make_layer_style(ngw_resource_group, i) draw_order_position = count_layers_created - i layers_styles_.append((layer_id, style_id, draw_order_position)) yield layers_styles_ with transaction.manager: for layer_id, style_id, draw_order_position in layers_styles_: DBSession.delete(RasterStyle.filter_by(id=style_id).one()) DBSession.delete(RasterLayer.filter_by(id=layer_id).one())
def resource(ngw_resource_group): with transaction.manager: obj = ResourceGroup( parent_id=ngw_resource_group, display_name='Test Юникод Symbols ~%', keyname='Test-Keyname', owner_user=User.by_keyname('administrator'), ).persist() DBSession.flush() DBSession.expunge(obj) yield obj with transaction.manager: DBSession.delete(ResourceGroup.filter_by(id=obj.id).one())
def connection_id(ngw_resource_group, wfs_service_path): with transaction.manager: admin = User.by_keyname('administrator') obj = WFSConnection(parent_id=ngw_resource_group, display_name='wfs_connection', owner_user=admin, path=wfs_service_path, username='******', password='******', version='2.0.2').persist() yield obj.id with transaction.manager: DBSession.delete(WFSConnection.filter_by(id=obj.id).one())
def test_resource_storage(ngw_env, ngw_resource_group, ngw_webtest_app, ngw_auth_administrator): reserve_storage = ngw_env.core.reserve_storage with transaction.manager: res1 = vector_layer('test-resource-1', ngw_resource_group) res2 = vector_layer('test-resource-2', ngw_resource_group) reserve_storage('test_comp', TestKOD1, resource=res1, value_data_volume=100) reserve_storage('test_comp', TestKOD2, resource=res1, value_data_volume=10) reserve_storage('test_comp', TestKOD1, resource=res2, value_data_volume=200) DBSession.flush() DBSession.expunge(res1) DBSession.expunge(res2) resp = ngw_webtest_app.get('/api/resource/%d/volume' % res1.id, status=200) assert resp.json['volume'] == 110 resp = ngw_webtest_app.get('/api/resource/%d/volume' % res2.id, status=200) assert resp.json['volume'] == 200 cur = ngw_env.core.query_storage() assert cur['']['data_volume'] == 310 assert cur[TestKOD1.identity]['data_volume'] == 300 with transaction.manager: DBSession.delete(res1) cur = ngw_env.core.query_storage() assert cur['']['data_volume'] == 200 with transaction.manager: DBSession.delete(res2) cur = ngw_env.core.query_storage() assert cur['']['data_volume'] == 0
def service(ngw_resource_group): with transaction.manager: res_vl = VectorLayer( parent_id=ngw_resource_group, display_name='test_vector_layer', owner_user=User.by_keyname('administrator'), srs=SRS.filter_by(id=3857).one(), tbl_uuid=six.text_type(uuid4().hex), ).persist() dsource = type_geojson_dataset() layer = dsource.GetLayer(0) res_vl.setup_from_ogr(layer, lambda x: x) res_vl.load_from_ogr(layer, lambda x: x) DBSession.flush() res_wfs = WFSService( parent_id=ngw_resource_group, display_name='test_wfsserver_service', owner_user=User.by_keyname('administrator'), ).persist() res_wfs.layers.append( WFSLayer( resource=res_vl, keyname='test', display_name='test', maxfeatures=1000, )) DBSession.flush() DBSession.expunge(res_vl) DBSession.expunge(res_wfs) yield res_wfs.id with transaction.manager: DBSession.delete(VectorLayer.filter_by(id=res_vl.id).one()) DBSession.delete(WFSService.filter_by(id=res_wfs.id).one())
def rlayer_id(ngw_env, ngw_resource_group): with transaction.manager: obj = RasterLayer(parent_id=ngw_resource_group, display_name='test-wms-rlayer', owner_user=User.by_keyname('administrator'), srs=SRS.filter_by(id=3857).one()).persist() import nextgisweb.raster_layer.test path = Path( nextgisweb.raster_layer.test.__file__).parent / 'data/rounds.tif' obj.load_file(str(path), ngw_env) DBSession.flush() DBSession.expunge(obj) yield obj.id with transaction.manager: DBSession.delete(RasterLayer.filter_by(id=obj.id).one())
def service_id(ngw_resource_group, rstyle_id): with transaction.manager: obj = Service(parent_id=ngw_resource_group, display_name='test-wms-service', owner_user=User.by_keyname('administrator')).persist() DBSession.flush() obj.layers.append( Layer(resource_id=rstyle_id, keyname='test_rounds', display_name='test-rounds')) DBSession.flush() DBSession.expunge(obj) yield obj.id with transaction.manager: DBSession.delete(Service.filter_by(id=obj.id).one())
def layer_id(ngw_resource_group, connection_id): with transaction.manager: admin = User.by_keyname('administrator') obj = WFSLayer(parent_id=ngw_resource_group, display_name='wfs_layer', owner_user=admin, srs_id=3857, connection_id=connection_id, layer_name='type', column_geom='geom', geometry_srid=3857, geometry_type='POINT').persist() DBSession.flush() obj.setup() yield obj.id with transaction.manager: DBSession.delete(WFSLayer.filter_by(id=obj.id).one())
def raster_layer_id(ngw_env, ngw_resource_group): with transaction.manager: obj = RasterLayer( parent_id=ngw_resource_group, display_name="raster_layer.test:export", owner_user=User.by_keyname("administrator"), srs=SRS.filter_by(id=3857).one(), ).persist() obj.load_file( os.path.join( os.path.split(__file__)[0], "data", "sochi-aster-colorized.tif"), ngw_env) DBSession.flush() DBSession.expunge(obj) yield obj.id with transaction.manager: DBSession.delete(RasterLayer.filter_by(id=obj.id).one())
def frtc(ngw_resource_group): with transaction.manager: layer = RasterLayer( parent_id=ngw_resource_group, display_name='test-render-layer', owner_user=User.by_keyname('administrator'), srs=SRS.filter_by(id=3857).one(), xsize=100, ysize=100, dtype='Byte', band_count=3, ).persist() style = RasterStyle( parent=layer, display_name='test-render-style', owner_user=User.by_keyname('administrator'), ).persist() result = ResourceTileCache(resource=style, ).persist() result.async_writing = True DBSession.flush() result.initialize() yield result with transaction.manager: DBSession.delete( ResourceTileCache.filter_by(resource_id=result.resource_id).one()) DBSession.delete(RasterStyle.filter_by(id=style.id).one()) DBSession.delete(RasterLayer.filter_by(id=layer.id).one())
def test_postgis_sync(txn): obj = SRS(wkt=WKT_ESPG_4326, display_name='') obj.persist() DBSession.flush() assert obj.id >= SRID_LOCAL qpg = db.text('SELECT srtext FROM spatial_ref_sys WHERE srid = :id') srtext, = DBSession.connection().execute(qpg, id=obj.id).fetchone() assert obj.wkt == srtext obj.wkt = WKT_ESPG_3857 DBSession.flush() srtext, = DBSession.connection().execute(qpg, id=obj.id).fetchone() assert obj.wkt == srtext DBSession.delete(obj) DBSession.flush() assert DBSession.connection().execute(qpg, id=obj.id).fetchone() is None
def frtc(ngw_resource_group): with transaction.manager: vector_layer = VectorLayer(parent_id=ngw_resource_group, display_name='from_fields', owner_user=User.by_keyname('administrator'), geometry_type='POINT', srs=SRS.filter_by(id=3857).one(), tbl_uuid=six.text_type( uuid4().hex)).persist() vector_layer.setup_from_fields([]) result = ResourceTileCache(resource=vector_layer, ).persist() result.async_writing = True DBSession.flush() result.initialize() yield result with transaction.manager: DBSession.delete( ResourceTileCache.filter_by(resource_id=result.resource_id).one()) DBSession.delete(VectorLayer.filter_by(id=vector_layer.id).one())
def service_id(vlayer_id, ngw_resource_group): with transaction.manager: res_wfs = WFSService( parent_id=ngw_resource_group, display_name='test_cyrillic_service', owner_user=User.by_keyname('administrator'), ).persist() res_wfs.layers.append( WFSLayer( resource_id=vlayer_id, keyname='test', display_name='test', maxfeatures=1000, )) DBSession.flush() DBSession.expunge(res_wfs) yield res_wfs.id with transaction.manager: DBSession.delete(WFSService.filter_by(id=res_wfs.id).one())
def wfs_service_path(ngw_resource_group, ngw_httptest_app): with transaction.manager: vl_type = VectorLayer( parent_id=ngw_resource_group, display_name='type', owner_user=User.by_keyname('administrator'), srs=SRS.filter_by(id=3857).one(), tbl_uuid=six.text_type(uuid4().hex), ).persist() dsource = type_geojson_dataset('type.geojson') layer = dsource.GetLayer(0) vl_type.setup_from_ogr(layer, lambda x: x) vl_type.load_from_ogr(layer, lambda x: x) DBSession.flush() # NOTE: GDAL doesn't support time fields in GML / WFS. It completely breaks # XSD schema parsing. Delete the time field to pass tests. DBSession.delete(vl_type.field_by_keyname('time')) wfs_service = WFSService( parent_id=ngw_resource_group, display_name='test_wfsserver_service', owner_user=User.by_keyname('administrator'), ).persist() wfs_service.layers.append( WFS_Service_Layer(resource=vl_type, keyname='type', display_name='type', maxfeatures=1000)) DBSession.flush() DBSession.expunge(vl_type) DBSession.expunge(wfs_service) path = '{}/api/resource/{}/wfs'.format(ngw_httptest_app.base_url, wfs_service.id) yield path with transaction.manager: DBSession.delete(VectorLayer.filter_by(id=vl_type.id).one()) DBSession.delete(WFSService.filter_by(id=wfs_service.id).one())