DBIO-PostgreSQL-PostGIS

 view release on metacpan or  search on metacpan

t/36-empty-collection-live.t  view on Meta::CPAN

use strict;
use warnings;
use Test::More;
use JSON::PP ();

# Live read of empty geometries (karr k19) and of GEOMETRYCOLLECTIONs
# (karr k24).
#
# An empty geometry read from the database used to say it was not empty
# and to be "LINESTRING()" or "POINT(NaN NaN)"; a collection lost the types
# of its members and was "GEOMETRYCOLLECTION()". Rows are written with
# plain SQL (ST_GeomFromText), read through the inflater, and then held
# against what the server says about the same row:
#
#  - is_empty against ST_IsEmpty;
#  - wkt against ST_AsText, where the two print the same text (they do not
#    for MULTIPOINT, which PostGIS prints with a parenthesis around every
#    point, and the dimension marker is told apart by the space the server
#    puts before the parenthesis);
#  - the WKT and EWKT the object builds are read by the server as the very
#    geometry the row holds, bit for bit (ST_AsEWKB), which is what makes
#    them usable as the argument of a spatial helper;
#  - to_geojson against ST_AsGeoJSON, where the server prints all of it;
#  - bbox against ST_XMin ... ST_YMax;
#  - the object written to another row stores the same EWKB.
#
# Gated on DBIO_TEST_PG_DSN; skips quietly without a PostGIS-capable DB.

my ($dsn, $user, $pass) =
  @ENV{map { "DBIO_TEST_PG_$_" } qw(DSN USER PASS)};
$dsn  //= $ENV{DBIO_TEST_PG_EXT_DSN};
$user //= $ENV{DBIO_TEST_PG_EXT_USER};
$pass //= $ENV{DBIO_TEST_PG_EXT_PASS};

plan skip_all => 'DBIO_TEST_PG_DSN not set' unless $dsn;

eval { require DBI; require DBD::Pg; 1 }
  or plan skip_all => 'DBI / DBD::Pg not available';

my $dbh = eval {
  DBI->connect($dsn, $user, $pass,
    { RaiseError => 1, PrintError => 0, AutoCommit => 1 });
};
plan skip_all => "Cannot connect: $@" unless $dbh;

eval { $dbh->do('CREATE EXTENSION IF NOT EXISTS postgis') };
plan skip_all => "PostGIS extension not available: $@" if $@;

{
  package My::EmptyColl::Schema::Result::Shape;
  use base 'DBIO::Core';
  __PACKAGE__->load_components(qw( PostgreSQL::PostGIS ));
  __PACKAGE__->table('dbio_postgis_test_empty_coll');
  __PACKAGE__->add_columns(
    id   => { data_type => 'integer', is_auto_increment => 1 },
    name => { data_type => 'text' },
    geom => { data_type => 'geometry', is_nullable => 1 },
  );
  __PACKAGE__->set_primary_key('id');

  package My::EmptyColl::Schema;
  use base 'DBIO::Schema';
  __PACKAGE__->load_components('PostgreSQL', 'PostgreSQL::PostGIS');
  __PACKAGE__->register_class(Shape => 'My::EmptyColl::Schema::Result::Shape');
}

my $schema = My::EmptyColl::Schema->connect(sub { $dbh });

# An untyped column: it takes every geometry type and dimension.
eval { $dbh->do('DROP TABLE IF EXISTS dbio_postgis_test_empty_coll') };



( run in 1.049 second using v1.01-cache-2.11-cpan-60884a8df76 )