hopefully fixup pg tests (thanks to zby, all bugs mine)
[dbsrgits/DBIx-Class.git] / t / 72pg.t
1 use strict;
2 use warnings;  
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7
8 {
9   package DBICTest::Schema::Casecheck;
10
11   use strict;
12   use warnings;
13   use base 'DBIx::Class';
14
15   __PACKAGE__->load_components(qw/PK::Auto Core/);
16   __PACKAGE__->table('casecheck');
17   __PACKAGE__->add_columns(qw/id name NAME uc_name/);
18   __PACKAGE__->set_primary_key('id');
19
20 }
21
22 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
23
24 #warn "$dsn $user $pass";
25
26 plan skip_all => 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test'
27  . ' (note: creates and drops tables named artist and casecheck!)' unless ($dsn && $user);
28
29 plan tests => 8;
30
31 DBICTest::Schema->load_classes( 'Casecheck' );
32 DBICTest::Schema->compose_connection('PgTest' => $dsn, $user, $pass);
33
34 my $dbh = PgTest->schema->storage->dbh;
35 PgTest->schema->source("Artist")->name("testschema.artist");
36 $dbh->do("CREATE SCHEMA testschema;");
37 $dbh->do("CREATE TABLE testschema.artist (artistid serial PRIMARY KEY, name VARCHAR(100), charfield CHAR(10));");
38 ok ( $dbh->do('CREATE TABLE testschema.casecheck (id serial PRIMARY KEY, "name" VARCHAR(1), "NAME" VARCHAR(2), "UC_NAME" VARCHAR(3));'), 'Creation of casecheck table');
39
40 PgTest::Artist->load_components('PK::Auto');
41
42 my $new = PgTest::Artist->create({ name => 'foo' });
43
44 is($new->artistid, 1, "Auto-PK worked");
45
46 $new = PgTest::Artist->create({ name => 'bar' });
47
48 is($new->artistid, 2, "Auto-PK worked");
49
50 my $test_type_info = {
51     'artistid' => {
52         'data_type' => 'integer',
53         'is_nullable' => 0,
54         'size' => 4,
55     },
56     'name' => {
57         'data_type' => 'character varying',
58         'is_nullable' => 1,
59         'size' => 100,
60         'default_value' => undef,
61     },
62     'charfield' => {
63         'data_type' => 'character',
64         'is_nullable' => 1,
65         'size' => 10,
66         'default_value' => undef,
67     },
68 };
69
70
71 my $type_info = PgTest->schema->storage->columns_info_for('testschema.artist');
72 my $artistid_defval = delete $type_info->{artistid}->{default_value};
73 like($artistid_defval,
74      qr/^nextval\('([^\.]*\.){0,1}artist_artistid_seq'::(?:text|regclass)\)/,
75      'columns_info_for - sequence matches Pg get_autoinc_seq expectations');
76 is_deeply($type_info, $test_type_info,
77           'columns_info_for - column data types');
78
79 my $name_info = PgTest::Casecheck->column_info( 'name' );
80 is( $name_info->{size}, 1, "Case sensitive matching info for 'name'" );
81
82 my $NAME_info = PgTest::Casecheck->column_info( 'NAME' );
83 is( $NAME_info->{size}, 2, "Case sensitive matching info for 'NAME'" );
84
85 my $uc_name_info = PgTest::Casecheck->column_info( 'uc_name' );
86 is( $uc_name_info->{size}, 3, "Case insensitive matching info for 'uc_name'" );
87
88 $dbh->do("DROP TABLE testschema.artist;");
89 $dbh->do("DROP TABLE testschema.casecheck;");
90 $dbh->do("DROP SCHEMA testschema;");
91