1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
import re
import pytest
from sqlalchemy import Column
from sqlalchemy import MetaData
from sqlalchemy import Table
from sqlalchemy.sql import func
from sqlalchemy.sql import insert
from sqlalchemy.sql import text
from geoalchemy2.exc import ArgumentError
from geoalchemy2.types import Geography
from geoalchemy2.types import Geometry
from geoalchemy2.types import Raster
from . import select
def eq_sql(a, b):
a = re.sub(r"[\n\t]", "", str(a))
assert a == b
@pytest.fixture
def geometry_table():
table = Table("table", MetaData(), Column("geom", Geometry))
return table
@pytest.fixture
def geography_table():
table = Table("table", MetaData(), Column("geom", Geography))
return table
@pytest.fixture
def raster_table():
table = Table("table", MetaData(), Column("rast", Raster))
return table
class TestGeometry:
def test_get_col_spec(self):
g = Geometry(srid=900913)
assert g.get_col_spec() == "geometry(GEOMETRY,900913)"
def test_get_col_spec_no_srid(self):
g = Geometry(srid=None)
assert g.get_col_spec() == "geometry(GEOMETRY,-1)"
def test_get_col_spec_invalid_srid(self):
with pytest.raises(ArgumentError) as e:
g = Geometry(srid="foo")
g.get_col_spec()
assert str(e.value) == "srid must be convertible to an integer"
def test_get_col_spec_no_typmod(self):
g = Geometry(geometry_type=None)
assert g.get_col_spec() == "geometry"
def test_check_ctor_args_bad_srid(self):
with pytest.raises(ArgumentError):
Geometry(srid="foo")
def test_get_col_spec_geometryzm(self):
g = Geometry(geometry_type="GEOMETRYZM", srid=900913)
assert g.get_col_spec() == "geometry(GEOMETRYZM,900913)"
def test_get_col_spec_geometryz(self):
g = Geometry(geometry_type="GEOMETRYZ", srid=900913)
assert g.get_col_spec() == "geometry(GEOMETRYZ,900913)"
def test_get_col_spec_geometrym(self):
g = Geometry(geometry_type="GEOMETRYM", srid=900913)
assert g.get_col_spec() == "geometry(GEOMETRYM,900913)"
def test_check_ctor_args_srid_not_enforced(self):
with pytest.warns(UserWarning):
Geometry(geometry_type=None, srid=4326)
def test_check_ctor_args_use_typmod_nullable(self):
with pytest.raises(
ArgumentError,
match='The "nullable" and "use_typmod" arguments can not be used together',
):
Geometry(use_typmod=True, nullable=False)
def test_column_expression(self, geometry_table):
s = select([geometry_table.c.geom])
eq_sql(s, 'SELECT ST_AsEWKB("table".geom) AS geom FROM "table"')
def test_select_bind_expression(self, geometry_table):
s = select([text("foo")]).where(geometry_table.c.geom == "POINT(1 2)")
eq_sql(
s,
'SELECT foo FROM "table" WHERE "table".geom = ST_GeomFromEWKT(:geom_1)',
)
assert s.compile().params == {"geom_1": "POINT(1 2)"}
def test_insert_bind_expression(self, geometry_table):
i = insert(geometry_table).values(geom="POINT(1 2)")
eq_sql(i, 'INSERT INTO "table" (geom) VALUES (ST_GeomFromEWKT(:geom))')
assert i.compile().params == {"geom": "POINT(1 2)"}
def test_function_call(self, geometry_table):
s = select([geometry_table.c.geom.ST_Buffer(2)])
eq_sql(
s,
'SELECT ST_AsEWKB(ST_Buffer("table".geom, :ST_Buffer_2)) AS "ST_Buffer_1" FROM "table"',
)
def test_non_ST_function_call(self, geometry_table):
with pytest.raises(AttributeError):
geometry_table.c.geom.Buffer(2)
def test_subquery(self, geometry_table):
# test for geometry columns not delivered to the result
# http://hg.sqlalchemy.org/sqlalchemy/rev/f1efb20c6d61
s = select([geometry_table]).alias("name").select()
eq_sql(
s,
"SELECT ST_AsEWKB(name.geom) AS geom FROM "
'(SELECT "table".geom AS geom FROM "table") AS name',
)
class TestGeography:
def test_get_col_spec(self):
g = Geography(srid=900913)
assert g.get_col_spec() == "geography(GEOMETRY,900913)"
def test_get_col_spec_no_typmod(self):
g = Geography(geometry_type=None)
assert g.get_col_spec() == "geography"
def test_column_expression(self, geography_table):
s = select([geography_table.c.geom])
eq_sql(s, 'SELECT ST_AsBinary("table".geom) AS geom FROM "table"')
def test_select_bind_expression(self, geography_table):
s = select([text("foo")]).where(geography_table.c.geom == "POINT(1 2)")
eq_sql(
s,
'SELECT foo FROM "table" WHERE "table".geom = ST_GeogFromText(:geom_1)',
)
assert s.compile().params == {"geom_1": "POINT(1 2)"}
def test_insert_bind_expression(self, geography_table):
i = insert(geography_table).values(geom="POINT(1 2)")
eq_sql(i, 'INSERT INTO "table" (geom) VALUES (ST_GeogFromText(:geom))')
assert i.compile().params == {"geom": "POINT(1 2)"}
def test_function_call(self, geography_table):
s = select([geography_table.c.geom.ST_Buffer(2)])
eq_sql(
s,
'SELECT ST_AsEWKB(ST_Buffer("table".geom, :ST_Buffer_2)) AS "ST_Buffer_1" FROM "table"',
)
def test_non_ST_function_call(self, geography_table):
with pytest.raises(AttributeError):
geography_table.c.geom.Buffer(2)
def test_subquery(self, geography_table):
# test for geography columns not delivered to the result
# http://hg.sqlalchemy.org/sqlalchemy/rev/f1efb20c6d61
s = select([geography_table]).alias("name").select()
eq_sql(
s,
"SELECT ST_AsBinary(name.geom) AS geom FROM "
'(SELECT "table".geom AS geom FROM "table") AS name',
)
class TestPoint:
def test_get_col_spec(self):
g = Geometry(geometry_type="POINT", srid=900913)
assert g.get_col_spec() == "geometry(POINT,900913)"
class TestCurve:
def test_get_col_spec(self):
g = Geometry(geometry_type="CURVE", srid=900913)
assert g.get_col_spec() == "geometry(CURVE,900913)"
class TestLineString:
def test_get_col_spec(self):
g = Geometry(geometry_type="LINESTRING", srid=900913)
assert g.get_col_spec() == "geometry(LINESTRING,900913)"
class TestPolygon:
def test_get_col_spec(self):
g = Geometry(geometry_type="POLYGON", srid=900913)
assert g.get_col_spec() == "geometry(POLYGON,900913)"
class TestMultiPoint:
def test_get_col_spec(self):
g = Geometry(geometry_type="MULTIPOINT", srid=900913)
assert g.get_col_spec() == "geometry(MULTIPOINT,900913)"
class TestMultiLineString:
def test_get_col_spec(self):
g = Geometry(geometry_type="MULTILINESTRING", srid=900913)
assert g.get_col_spec() == "geometry(MULTILINESTRING,900913)"
class TestMultiPolygon:
def test_get_col_spec(self):
g = Geometry(geometry_type="MULTIPOLYGON", srid=900913)
assert g.get_col_spec() == "geometry(MULTIPOLYGON,900913)"
class TestGeometryCollection:
def test_get_col_spec(self):
g = Geometry(geometry_type="GEOMETRYCOLLECTION", srid=900913)
assert g.get_col_spec() == "geometry(GEOMETRYCOLLECTION,900913)"
class TestRaster:
def test_get_col_spec(self):
r = Raster()
assert r.get_col_spec() == "raster"
def test_column_expression(self, raster_table):
s = select([raster_table.c.rast])
eq_sql(s, 'SELECT raster("table".rast) AS rast FROM "table"')
def test_insert_bind_expression(self, raster_table):
i = insert(raster_table).values(rast=b"\x01\x02")
eq_sql(i, 'INSERT INTO "table" (rast) VALUES (raster(:rast))')
assert i.compile().params == {"rast": b"\x01\x02"}
def test_function_call(self, raster_table):
s = select([raster_table.c.rast.ST_Height()])
eq_sql(s, 'SELECT ST_Height("table".rast) AS "ST_Height_1" FROM "table"')
def test_non_ST_function_call(self, raster_table):
with pytest.raises(AttributeError):
raster_table.c.geom.Height()
class TestCompositeType:
def test_ST_Dump(self, geography_table):
s = select([func.ST_Dump(geography_table.c.geom).geom.label("geom")])
eq_sql(s, 'SELECT ST_AsEWKB((ST_Dump("table".geom)).geom) AS geom FROM "table"')
|