File: db-object-changes-only-2.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 (439 lines) | stat: -rwxr-xr-x 9,046 bytes parent folder | download | duplicates (4)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#!/usr/bin/perl -w

use strict;

use Test::More tests => 2 + (71 * 4);

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

my $Include_Tables = '^(?:' . join('|', qw(rose_db_object_test)) . ')$';
$Include_Tables = qr($Include_Tables);

use Rose::DB::Object::Util qw(:all);

our $PG_HAS_CHKPASS;

my %Value =
(
  1  => 'varchar val',
  2  => 'char val',
  3  => 1,
  4  => 4.25,
  5  => '10111',
  6  => 78.25,
  7  => '1984-01-24',
  8  => '1999-05-20 03:04:05',
  9  => '2 years',
  10 => [ 5, 6 ],
  11 => 24,
  12 => '922337203685',
  13 => '1009539509',
);

my %Default =
(
  1  => 'varchar-def',
  2  => 'char def        ',
  3  => 1,
  4  => 1.25,
  5  => '00101',
  6  => 123.25,
  7  => '2001-02-03',
  8  => '2001-02-03 12:34:56',
  9  => '@ 2 months 5 days 3 seconds',
  10 => [ 3, 4 ],
  11 => 123,
  12 => '922337203685',
  13 => '1973-02',
);

my %Method =
(
  5  => 'to_Bin',
  7  => 'ymd',
  8  => sub { shift; shift->strftime('%Y-%m-%d %H:%M:%S') },
  9  => sub { shift->db->format_interval(shift) },
  13 => sub { shift; shift->strftime('%Y-%m') },
);

#
# Tests
#

foreach my $db_type (qw(pg mysql informix sqlite))
{
  unless(have_db($db_type))
  {
    SKIP: { skip("$db_type tests", 71) }
    next;
  }

  Rose::DB::Object::Metadata->unregister_all_classes;

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

  my $class_prefix = 'My' . ucfirst($db_type);

  my $loader = 
    Rose::DB::Object::Loader->new(
    db_class       => 'Rose::DB',
    class_prefix   => $class_prefix,
    include_tables => $Include_Tables);

  $loader->make_classes;

  my $class = $class_prefix . '::RoseDbObjectTest';

  $class->meta->replace_column(c13 => { type => 'epoch' });
  $class->meta->replace_column(c13d => { type => 'epoch', default => 99539509 });

  $class->meta->initialize(replace_existing => 1);
  #print $class->meta->perl_class_definition;

  add_nonpersistent_columns_and_methods($class);

  my $num_cols = 14;

  foreach my $n (1 .. $num_cols)
  {
    my $col = "c$n";
    my $def = "c${n}d";

    SKIP:
    {
      skip("column $n", 5)  unless($class->meta->column($col));
    }

    next  unless($class->meta->column($col));

    my $o = $class->new;
    $o->save;

    ok(!has_modified_columns($o), "has_modified_columns $col 1 - $db_type");

    $o->$col($Value{$n});

    is_deeply([ modified_column_names($o) ], [ $col ], "modified column $col 1 - $db_type");

    foreach my $n (14 .. $num_cols)
    {
      my $col = "c$n";
      my $def = "c${n}d";

      next  unless($class->meta->column($col));

      my $val = $o->$col();
      $val = $o->$def();
    }

    is_deeply([ modified_column_names($o) ], [ $col ], "modified column $col 2 - $db_type");      

    #local $Rose::DB::Object::Debug = 1;
    $o->update(changes_only => 1);
    #local $Rose::DB::Object::Debug = 0;

    modify_nonpersistent_column_values($o);

    ok(!has_modified_columns($o), "has_modified_columns $col 2 - $db_type");

    $o = $class->new(id => $o->id)->load;

    if(ref $Default{$n})
    {
      is_deeply(scalar $o->$def(), $Default{$n}, "check default $def 1 - $db_type");
    }
    else
    {
      my $method = $Method{$n};

      my $value;

      if(defined $method)
      {
        if(ref $method eq 'CODE')
        {
          $value = $method->($o, $o->$def());
        }
        else
        {
          $value = $o->$def()->$method();
        }
      }
      else
      {
        $value = $o->$def;
      }

      if($db_type eq 'mysql' && $n == 2 && $o->db->database_version < 5_000_000)
      {
        $value .= '        '; # MySQL < 5 seems to mess up CHAR fields
      }

      if(defined $Default{$n})
      {
        ok($value =~ /^\+?$Default{$n}$/, "check default $def 1 - $db_type");
      }
      else
      {
        is($value, $Default{$n}, "check default $def 1 - $db_type");
      }
    }

    if($n == 14 && $db_type eq 'pg')
    {
      ok($o->c14d_is('xyzzy'), "chkpass default - $db_type");
    }
    elsif($n == 13 &&  $db_type eq 'pg' && !$PG_HAS_CHKPASS)
    {
      ok(1, "no chkpass - $db_type");
    }
    elsif($n == 1 && $db_type ne 'pg')
    {
      ok(1, "chkpass skipped - $db_type");
    }
  }
}

BEGIN
{
  require 't/test-lib.pl';

  #
  # PostgreSQL
  #

  if(have_db('pg_admin'))
  {
    my $dbh = get_dbh('pg_admin');

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

    our $PG_HAS_CHKPASS = pg_has_chkpass();

    $dbh->do(<<"EOF");
CREATE TABLE rose_db_object_test
(
  id    SERIAL NOT NULL PRIMARY KEY,
  c1    VARCHAR(255),
  c1d   VARCHAR(255) DEFAULT 'varchar-def',
  c2    CHAR(16),
  c2d   CHAR(16) DEFAULT 'char def',
  c3    BOOLEAN,
  c3d   BOOLEAN DEFAULT 't',
  c4    FLOAT,
  c4d   FLOAT DEFAULT 1.25,
  c5    BIT(5),
  c5d   BIT(5) DEFAULT B'00101',
  c6    DECIMAL(10,2),
  c6d   DECIMAL(10,2) DEFAULT 123.25,
  c7    DATE,
  c7d   DATE DEFAULT '2001-02-03',
  c8    TIMESTAMP,
  c8d   TIMESTAMP DEFAULT '2001-02-03 12:34:56',
  c9    INTERVAL(6),
  c9d   INTERVAL(6) DEFAULT '2 months 5 days 3 seconds',
  c10   INT[],
  c10d  INT[] DEFAULT '{3,4}',
  c11   INT,
  c11d  INT DEFAULT 123,
  c12   BIGINT,
  c12d  BIGINT DEFAULT 922337203685,
  c13   INT,
  c13d  INT DEFAULT 99539509

  @{[ $PG_HAS_CHKPASS ? q(, c14 CHKPASS, c14d CHKPASS DEFAULT 'xyzzy') : '' ]}
)
EOF

    $dbh->disconnect;
  }

  #
  # MySQL
  #

  my $db_version;

  eval
  {
    my $dbh = get_dbh('mysql_admin');

    local $dbh->{'RaiseError'} = 0;
    local $dbh->{'PrintError'} = 0;
    $dbh->do('DROP TABLE rose_db_object_test');
  };

  if(have_db('mysql_admin'))
  {
    my $db  = get_db('mysql_admin');
    my $dbh = $db->retain_dbh;

    my $bool_columns = ($db->database_version >= 5_000_000) ?
        qq(c3    BOOLEAN,\n  c3d   BOOLEAN DEFAULT 1,) : '';

    $dbh->do(<<"EOF");
CREATE TABLE rose_db_object_test
(
  id    INT AUTO_INCREMENT PRIMARY KEY,
  c1    VARCHAR(255),
  c1d   VARCHAR(255) DEFAULT 'varchar-def',
  c2    CHAR(16),
  c2d   CHAR(16) DEFAULT 'char def',
  $bool_columns
  c4    FLOAT,
  c4d   FLOAT DEFAULT 1.25,
  c6    DECIMAL(10,2),
  c6d   DECIMAL(10,2) DEFAULT 123.25,
  c7    DATE,
  c7d   DATE DEFAULT '2001-02-03',
  c8    DATETIME,
  c8d   DATETIME DEFAULT '2001-02-03 12:34:56',
  c11   INT,
  c11d  INT DEFAULT 123,
  c12   BIGINT,
  c12d  BIGINT DEFAULT 922337203685,
  c13   INT,
  c13d  INT DEFAULT 99539509
)
EOF

    $dbh->disconnect;
  }

  #
  # Informix
  #

  if(have_db('informix_admin'))
  {
    my $dbh = get_dbh('informix_admin');

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

    $dbh->do(<<"EOF");
CREATE TABLE rose_db_object_test
(
  id    SERIAL NOT NULL PRIMARY KEY,
  c1    VARCHAR(255),
  c1d   VARCHAR(255) DEFAULT 'varchar-def',
  c2    CHAR(16),
  c2d   CHAR(16) DEFAULT 'char def',
  c3    BOOLEAN,
  c3d   BOOLEAN DEFAULT 't',
  c6    DECIMAL(10,2),
  c6d   DECIMAL(10,2) DEFAULT 123.25,
  c7    DATE,
  c7d   DATE DEFAULT '02/03/2001',
  -- DBD::Informix can't handle this default value, apparently...
  -- c8    DATETIME YEAR TO SECOND,
  -- c8d   DATETIME YEAR TO SECOND DEFAULT DATETIME(2001-02-03 12:34:56) YEAR TO SECOND,
  c11   INT,
  c11d  INT DEFAULT 123,
  c12   INT8,
  c12d  INT8 DEFAULT 922337203685,
  c13   INT,
  c13d  INT DEFAULT 99539509
)
EOF

    $dbh->disconnect;
  }

  #
  # SQLite
  #

  if(have_db('sqlite_admin'))
  {
    my $dbh = get_dbh('sqlite_admin');

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

    $dbh->do(<<"EOF");
CREATE TABLE rose_db_object_test
(
  id    INTEGER PRIMARY KEY AUTOINCREMENT,
  c1    VARCHAR(255),
  c1d   VARCHAR(255) DEFAULT 'varchar-def',
  c2    CHAR(16),
  c2d   CHAR(16) DEFAULT 'char def',
  c3    BOOLEAN,
  c3d   BOOLEAN DEFAULT 1,
  c4    REAL,
  c4d   REAL DEFAULT '1.25',
  c6    REAL,
  c6d   REAL DEFAULT '123.25',
  c7    DATE,
  c7d   DATE DEFAULT '2001-02-03',
  c8    DATETIME,
  c8d   DATETIME DEFAULT '2001-02-03 12:34:56',
  c11   INT,
  c11d  INT DEFAULT 123,
  c12   BIGINT,
  c12d  BIGINT DEFAULT 922337203685,
  c13   INT,
  c13d  INT DEFAULT 99539509
)
EOF

    $dbh->disconnect;
  }
}

END
{
  # Delete test tables

  if(have_db('pg_admin'))
  {
    my $dbh = get_dbh('pg_admin');

    $dbh->do('DROP TABLE rose_db_object_test');

    $dbh->disconnect;
  }

  if(have_db('mysql_admin'))
  {
    my $dbh = get_dbh('mysql_admin');

    $dbh->do('DROP TABLE rose_db_object_test');

    $dbh->disconnect;
  }

  if(have_db('informix_admin'))
  {
    my $dbh = get_dbh('informix_admin');

    $dbh->do('DROP TABLE rose_db_object_test');

    $dbh->disconnect;
  }

  if(have_db('sqlite_admin'))
  {
    my $dbh = get_dbh('sqlite_admin');

    $dbh->do('DROP TABLE rose_db_object_test');

    $dbh->disconnect;
  }
}