fixup for datetime parser from Matt Lawrence (mattlaw)
[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/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 => 10;
31
32 DBICTest::Schema->load_classes( 'Casecheck' );
33 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
34
35 # Check that datetime_parser returns correctly before we explicitly connect.
36 SKIP: {
37     eval { require DateTime::Format::Pg };
38     skip "DateTime::Format::Pg required", 2 if $@;
39
40     my $store = ref $schema->storage;
41     is($store, 'DBIx::Class::Storage::DBI', 'Started with generic storage');
42
43     my $parser = $schema->storage->datetime_parser;
44     is( $parser, 'DateTime::Format::Pg', 'datetime_parser is as expected');
45 }
46
47 my $dbh = $schema->storage->dbh;
48 $schema->source("Artist")->name("testschema.artist");
49 $dbh->do("CREATE SCHEMA testschema;");
50 $dbh->do("CREATE TABLE testschema.artist (artistid serial PRIMARY KEY, name VARCHAR(100), charfield CHAR(10));");
51 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');
52
53 # This is in Core now, but it's here just to test that it doesn't break
54 $schema->class('Artist')->load_components('PK::Auto');
55
56 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
57
58 is($new->artistid, 1, "Auto-PK worked");
59
60 $new = $schema->resultset('Artist')->create({ name => 'bar' });
61
62 is($new->artistid, 2, "Auto-PK worked");
63
64 my $test_type_info = {
65     'artistid' => {
66         'data_type' => 'integer',
67         'is_nullable' => 0,
68         'size' => 4,
69     },
70     'name' => {
71         'data_type' => 'character varying',
72         'is_nullable' => 1,
73         'size' => 100,
74         'default_value' => undef,
75     },
76     'charfield' => {
77         'data_type' => 'character',
78         'is_nullable' => 1,
79         'size' => 10,
80         'default_value' => undef,
81     },
82 };
83
84
85 my $type_info = $schema->storage->columns_info_for('testschema.artist');
86 my $artistid_defval = delete $type_info->{artistid}->{default_value};
87 like($artistid_defval,
88      qr/^nextval\('([^\.]*\.){0,1}artist_artistid_seq'::(?:text|regclass)\)/,
89      'columns_info_for - sequence matches Pg get_autoinc_seq expectations');
90 is_deeply($type_info, $test_type_info,
91           'columns_info_for - column data types');
92
93 my $name_info = $schema->source('Casecheck')->column_info( 'name' );
94 is( $name_info->{size}, 1, "Case sensitive matching info for 'name'" );
95
96 my $NAME_info = $schema->source('Casecheck')->column_info( 'NAME' );
97 is( $NAME_info->{size}, 2, "Case sensitive matching info for 'NAME'" );
98
99 my $uc_name_info = $schema->source('Casecheck')->column_info( 'uc_name' );
100 is( $uc_name_info->{size}, 3, "Case insensitive matching info for 'uc_name'" );
101
102 END {
103     if($dbh) {
104         $dbh->do("DROP TABLE testschema.artist;");
105         $dbh->do("DROP TABLE testschema.casecheck;");
106         $dbh->do("DROP SCHEMA testschema;");
107     }
108 }
109