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