File: spot-check-13.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 (179 lines) | stat: -rw-r--r-- 2,887 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
#!/usr/bin/perl -w

use strict;

use Test::More tests => 2;

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

our %Have;

#
# Test created by Jud
#

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

  next  unless($Have{$db_type});

  my $t1 = T1->new(id => 1)->save;
  my $t2 = T2->new(id => 1)->save;
  my $tt = T1T2Map->new;
  $tt->t1_id(1);
  $tt->t2_id(1);
  $tt->save;

  my @results = $t2->t1s;

  is(scalar @results, 1, "bigint keys - $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 t1_t2_map');
      $dbh->do('DROP TABLE t1');
      $dbh->do('DROP TABLE t2');
    }
  };

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

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

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

    $dbh->do(<<"EOF");
CREATE TABLE t1_t2_map
(
  t1_id BIGINT NOT NULL,
  t2_id BIGINT NOT NULL,

  PRIMARY KEY(t1_id, t2_id)
)
EOF

    $dbh->disconnect;

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

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

    __PACKAGE__->meta->setup
    (
      table   => 't1',
      columns =>
      [
        id => { type => 'bigint', not_null => 1, primary_key => 1 },
      ],

      relationships => 
      [
        related => 
        {
          type      => 'many to many',
          map_class => 'T1T2Map',
          map_from  => 't1',
          map_to    => 't2',
        },
      ],
    );

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

    __PACKAGE__->meta->setup
    (
      table   => 't1_t2_map',
      columns =>
      [
        t1_id => { type => 'bigint', not_null => 1 },
        t2_id => { type => 'bigint', not_null => 1 },
      ],

      primary_key_columns => ['t1_id', 't2_id'],

      foreign_keys => 
      [
        t1 => { class => 'T1' },
        t2 => { class => 'T2' },
      ],
    );

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

    __PACKAGE__->meta->setup
    (
      table   => 't2',
      columns =>
      [
        id => { type => 'bigint', not_null => 1, primary_key => 1 },
      ],

      relationships =>
      [
        t1s => 
        {
          type       => 'many to many',
          map_class  => 'T1T2Map',
          column_map => { node_id => 'id' },
        },
      ],
    );
  }
}

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 t1_t2_map');
    $dbh->do('DROP TABLE t1');
    $dbh->do('DROP TABLE t2');
    $dbh->disconnect;
  }
}