1#!/usr/bin/perl -w
2
3use strict;
4
5use Test::More tests => 2 + 6;
6
7BEGIN
8{
9  require 't/test-lib.pl';
10  use_ok('Rose::DB::Object');
11  use_ok('Rose::DB::Object::Loader');
12}
13
14our %Have;
15
16foreach my $db_type (qw(pg))
17{
18  SKIP:
19  {
20    skip("$db_type tests", 6)  unless($Have{$db_type});
21  }
22
23  next  unless($Have{$db_type});
24
25  Rose::DB::Object::Metadata->unregister_all_classes;
26  Rose::DB->default_type($db_type);
27
28  my $class_prefix = ucfirst($db_type);
29
30  my $loader =
31    Rose::DB::Object::Loader->new(
32      db           => Rose::DB->new,
33      class_prefix => $class_prefix);
34
35  my @classes = $loader->make_classes(include_tables => [ qw(rdbo_users rdbo_comments) ]);
36
37  #foreach my $class (@classes)
38  #{
39  #  print $class->meta->perl_class_definition if($class->can('meta'));
40  #}
41
42  my $user_class    = $class_prefix . '::RdboUser';
43  my $comment_class = $class_prefix . '::RdboComment';
44
45  ok($user_class->meta->relationship('user1s'), "user1s rel - $db_type");
46  ok($user_class->meta->relationship('user2s'), "user2s rel - $db_type");
47
48  ok($comment_class->meta->foreign_key('user1'), "user1 fk - $db_type");
49  ok($comment_class->meta->foreign_key('user2'), "user2 fk - $db_type");
50
51  is($comment_class->meta->column('type')->type, 'enum', "enum - $db_type");
52  is($comment_class->meta->column('type')->db_type, 'my_type', "custom type - $db_type");
53}
54
55BEGIN
56{
57  our %Have;
58
59  #
60  # Pg
61  #
62
63  my $dbh;
64
65  eval
66  {
67    my $db = Rose::DB->new('pg_admin');
68    $dbh = $db->retain_dbh or die Rose::DB->error;
69
70    # Drop existing tables, ignoring errors
71    {
72      local $dbh->{'RaiseError'} = 0;
73      local $dbh->{'PrintError'} = 0;
74      $dbh->do('DROP TABLE rdbo_comments');
75      $dbh->do('DROP TABLE rdbo_users');
76      $dbh->do('DROP TYPE my_type');
77    }
78  };
79
80  if(!$@ && $dbh)
81  {
82    $Have{'pg'} = 1;
83
84    $dbh->do(<<"EOF");
85CREATE TABLE rdbo_users
86(
87  id INT NOT NULL PRIMARY KEY
88)
89EOF
90
91    $dbh->do(<<"EOF");
92CREATE TYPE my_type AS ENUM ('foo', 'bar')
93EOF
94
95    $dbh->do(<<"EOF");
96CREATE TABLE rdbo_comments
97(
98  id        SERIAL NOT NULL PRIMARY KEY,
99  user1_id  INTEGER NOT NULL REFERENCES rdbo_users (id),
100  user2_id  INTEGER NOT NULL REFERENCES rdbo_users (id),
101  type      MY_TYPE
102)
103EOF
104
105    $dbh->disconnect;
106  }
107}
108
109END
110{
111  # Delete test tables
112
113  if($Have{'pg'})
114  {
115    my $dbh = Rose::DB->new('pg_admin')->retain_dbh()
116      or die Rose::DB->error;
117
118    $dbh->do('DROP TABLE rdbo_comments');
119    $dbh->do('DROP TABLE rdbo_users');
120    $dbh->do('DROP TYPE my_type');
121
122    $dbh->disconnect;
123  }
124}
125