File: nested-joins.t

package info (click to toggle)
librose-db-object-perl 1%3A0.815-1%2Bdeb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,048 kB
  • sloc: perl: 79,670; sql: 28; makefile: 7
file content (193 lines) | stat: -rwxr-xr-x 3,819 bytes parent folder | download | duplicates (8)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/perl -w

use strict;

use Test::More tests => 3;

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

if(defined $ENV{'RDBO_NESTED_JOINS'} && Rose::DB::Object::Manager->can('default_nested_joins'))
{
  Rose::DB::Object::Manager->default_nested_joins($ENV{'RDBO_NESTED_JOINS'});
}

our %Have;

#
# Tests
#

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

  next  unless($Have{$db_type});

  Rose::DB->default_type($db_type);

  my $class = ucfirst($db_type) . '::A';

  my $as =
    Rose::DB::Object::Manager->get_objects(
      #debug => 1,
      object_class => $class,
      with_objects => [ 'bs.c' ]);

  is(scalar @$as, 2, "check count - $db_type");
}

BEGIN
{
  our %Have;

  #
  # MySQL
  #

  my $dbh;

  eval
  {
    my $db = Rose::DB->new('mysql_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 rose_db_object_test_b');
      $dbh->do('DROP TABLE rose_db_object_test_a');
      $dbh->do('DROP TABLE rose_db_object_test_c');
    }
  };

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

    $dbh->do(<<"EOF");
CREATE TABLE rose_db_object_test_a
(
  id   INT PRIMARY KEY, 
  name VARCHAR(255)
)
EOF

    $dbh->do(<<"EOF");
CREATE TABLE rose_db_object_test_c
(
  id   INT PRIMARY KEY,
  name VARCHAR(255));
EOF

    $dbh->do(<<"EOF");
CREATE TABLE rose_db_object_test_b 
(
  id   INT PRIMARY KEY,
  name VARCHAR(255),
  a_id INT NOT NULL REFERENCES a (id),
  c_id INT NOT NULL REFERENCES c (id)
)
EOF

    Rose::DB->default_type('mysql');

    package Mysql::A;
    our @ISA = qw(Rose::DB::Object);

    __PACKAGE__->meta->setup
    (
      table => 'rose_db_object_test_a',

      columns => [ qw(id name) ],

      relationships =>
      [
        bs =>
        {
          type => 'one to many',
          class => 'Mysql::B',
          column_map => { id => 'a_id' },
        },
      ],
    );

    package Mysql::B;
    our @ISA = qw(Rose::DB::Object);

    __PACKAGE__->meta->setup
    (
      table => 'rose_db_object_test_b',

      columns => 
      [
        id => { type => 'serial', primary_key => 1 },
        name => { type => 'varchar', length => 255 },
        a_id => { type => 'integer', not_null => 1 },
        c_id => { type => 'integer', not_null => 1 },
      ],

      foreign_keys =>
      [
        a =>
        {
          class => 'Mysql::A',
          key_columns => { a_id => 'id' },
        },

        c =>
        {
          class => 'Mysql::C',
          key_columns => { a_id => 'id' },
        }
      ],
    );

    package Mysql::C;
    our @ISA = qw(Rose::DB::Object);

    __PACKAGE__->meta->setup
    (
      table => 'rose_db_object_test_c',

      columns => 
      [
        id => { type => 'serial', primary_key => 1 },
        name => { type => 'varchar', length => 255 },
      ]
    );

    $dbh->do("insert into rose_db_object_test_a (id, name) values (1, 'one')");
    $dbh->do("insert into rose_db_object_test_a (id, name) values (2, 'two')");
    $dbh->do("insert into rose_db_object_test_c (id, name) values (1, 'c one')");
    $dbh->do("insert into rose_db_object_test_c (id, name) values (2, 'c two')");
    $dbh->do("insert into rose_db_object_test_b (id, name, a_id, c_id) values (1, 'b one', 1, 1)");

    $dbh->disconnect;
  }
}

END
{
  # Delete test tables

  if($Have{'mysql'})
  {
    # MySQL
    my $dbh = Rose::DB->new('mysql_admin')->retain_dbh()
      or die Rose::DB->error;

    $dbh->do('DROP TABLE rose_db_object_test_b');
    $dbh->do('DROP TABLE rose_db_object_test_a');
    $dbh->do('DROP TABLE rose_db_object_test_c');
    $dbh->disconnect;
  }
}