File: spot-check-07.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 (163 lines) | stat: -rwxr-xr-x 3,322 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
#!/usr/bin/perl -w

use strict;

use Test::More tests => 4;

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

our %Have;

#
# Tests
#

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

  next  unless($Have{$db_type});

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

  my $class = ucfirst($db_type) . 'Foo';

  my $o =
    $class->new(name => 'f1: ' . localtime(),
                children =>
                [
                  { name => 'c1: ' . localtime() },
                  { name => 'c2: ' . localtime() },
                ]);

  $o->save;

  my $p = $class->new(id => $o->id);
  ok($p->load, "parent - $db_type");

  my $c1 = $class->new(id => $o->children->[0]->id);
  ok($c1->load, "child 1 - $db_type");

  my $c2 = $class->new(id => $o->children->[1]->id);
  ok($c2->load, "child 2 - $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_foo');
      $dbh->do('DROP TABLE rose_db_object_test_foo_parent');
    }
  };

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

    $dbh->do(<<"EOF");
CREATE TABLE rose_db_object_test_foo
(
  id    INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  name  VARCHAR(255) NOT NULL,

  UNIQUE(name)
)
EOF

    $dbh->do(<<"EOF");
CREATE TABLE rose_db_object_test_foo_parent
(
  parent_id INT UNSIGNED NOT NULL REFERENCES rose_db_object_test_foo (id),
  child_id  INT UNSIGNED NOT NULL REFERENCES rose_db_object_test_foo (id),

  PRIMARY KEY(parent_id, child_id)
)
EOF

    $dbh->disconnect;

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

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

    MysqlFoo->meta->table('rose_db_object_test_foo');
    MysqlFoo->meta->columns(qw(id name));
    MysqlFoo->meta->primary_key_columns('id');
    MysqlFoo->meta->add_unique_key('name');
    MysqlFoo->meta->relationships
    (
      parents => 
      { 
        type      => 'many to many',
        map_class => 'MysqlFooParent',
        map_from  => 'child',
        map_to    => 'parent',
      },

      children =>
      {
        type      => 'many to many',
        map_class => 'MysqlFooParent',
        map_from  => 'parent',
        map_to    => 'child',
      },
    );

    MysqlFoo->meta->initialize;

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

    MysqlFooParent->meta->table('rose_db_object_test_foo_parent');
    MysqlFooParent->meta->columns(qw(parent_id child_id));
    MysqlFooParent->meta->primary_key_columns(qw(parent_id child_id));

    MysqlFooParent->meta->foreign_keys
    (
      parent => { class => 'MysqlFoo', key_columns => { parent_id => 'id' } },
      child  => { class => 'MysqlFoo', key_columns => { child_id  => 'id' } },
    );

    MysqlFooParent->meta->initialize;
  }
}

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_foo');
    $dbh->do('DROP TABLE rose_db_object_test_foo_parent');
    $dbh->disconnect;
  }
}