File: spot-check-14.t

package info (click to toggle)
librose-db-object-perl 1%3A0.815-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,408 kB
  • ctags: 2,365
  • sloc: perl: 79,670; sql: 28; makefile: 7
file content (124 lines) | stat: -rw-r--r-- 2,521 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/perl -w

use strict;

use Test::More tests => 2 + 6;

BEGIN 
{
  require 't/test-lib.pl';
  use_ok('Rose::DB::Object');
  use_ok('Rose::DB::Object::Loader');
}

our %Have;

foreach my $db_type (qw(pg))
{
  SKIP:
  {
    skip("$db_type tests", 6)  unless($Have{$db_type});
  }

  next  unless($Have{$db_type});

  Rose::DB::Object::Metadata->unregister_all_classes;
  Rose::DB->default_type($db_type);

  my $class_prefix = ucfirst($db_type);

  my $loader = 
    Rose::DB::Object::Loader->new(
      db           => Rose::DB->new,
      class_prefix => $class_prefix);

  my @classes = $loader->make_classes(include_tables => [ qw(rdbo_users rdbo_comments) ]);

  #foreach my $class (@classes)
  #{
  #  print $class->meta->perl_class_definition if($class->can('meta'));
  #}

  my $user_class    = $class_prefix . '::RdboUser';
  my $comment_class = $class_prefix . '::RdboComment';

  ok($user_class->meta->relationship('user1s'), "user1s rel - $db_type");
  ok($user_class->meta->relationship('user2s'), "user2s rel - $db_type");

  ok($comment_class->meta->foreign_key('user1'), "user1 fk - $db_type");
  ok($comment_class->meta->foreign_key('user2'), "user2 fk - $db_type");

  is($comment_class->meta->column('type')->type, 'enum', "enum - $db_type");
  is($comment_class->meta->column('type')->db_type, 'my_type', "custom type - $db_type");
}

BEGIN
{
  our %Have;

  #
  # Pg
  #

  my $dbh;

  eval
  {
    my $db = Rose::DB->new('pg_admin');
    $dbh = $db->retain_dbh or die Rose::DB->error;

    # Drop existing tables, ignoring errors
    {
      local $dbh->{'RaiseError'} = 0;
      local $dbh->{'PrintError'} = 0;
      $dbh->do('DROP TABLE rdbo_comments');
      $dbh->do('DROP TABLE rdbo_users');
      $dbh->do('DROP TYPE my_type');
    }
  };

  if(!$@ && $dbh)
  {
    $Have{'pg'} = 1;

    $dbh->do(<<"EOF");
CREATE TABLE rdbo_users
(
  id INT NOT NULL PRIMARY KEY
)
EOF

    $dbh->do(<<"EOF");
CREATE TYPE my_type AS ENUM ('foo', 'bar')
EOF

    $dbh->do(<<"EOF");
CREATE TABLE rdbo_comments
(
  id        SERIAL NOT NULL PRIMARY KEY,
  user1_id  INTEGER NOT NULL REFERENCES rdbo_users (id),
  user2_id  INTEGER NOT NULL REFERENCES rdbo_users (id),
  type      MY_TYPE
)
EOF

    $dbh->disconnect;
  }
}

END
{
  # Delete test tables

  if($Have{'pg'})
  {
    my $dbh = Rose::DB->new('pg_admin')->retain_dbh()
      or die Rose::DB->error;

    $dbh->do('DROP TABLE rdbo_comments');
    $dbh->do('DROP TABLE rdbo_users');
    $dbh->do('DROP TYPE my_type');

    $dbh->disconnect;
  }
}