File: spot-check-10.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 (577 lines) | stat: -rwxr-xr-x 13,504 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
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
#!/usr/bin/perl -w

use strict;

use Test::More tests => 2 + (6 * 1);

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

our %Have;

#
# Tests
#

#$Rose::DB::Object::Manager::Debug = 1;

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

  next  unless($Have{$db_type});

  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(offering_levels offering_sequences offerings employee employer) ],
      include_map_class_relationships => 1);

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

  my $employer_class  = $class_prefix . '::Employer';
  my $offering_class = $class_prefix . '::Offering';
  my $offering_sequence_class = $class_prefix . '::OfferingSequence';
  my $offering_level_class    = $class_prefix . '::OfferingLevel';

  $employer_class->meta->column('data')->lazy(1);
  $employer_class->meta->column('data')->make_methods(replace_existing => 1);

  Rose::DB::Object::Manager->update_objects(
    set   => { name => { sql => 'upper(name)' } }, 
    where => [ name => 'Default Employer' ],
    object_class => $employer_class);

  Rose::DB::Object::Manager->update_objects(
    set   => { name => \q(name || 'x') }, 
    where => [ name => 'DEFAULT EMPLOYER' ],
    object_class => $employer_class);

  my $employer = $employer_class->new(company_code => 'TEST', data => "\0\1x\2\3");

  my @offerings =
  (
    {
      sort_order => 1,
      years      => 05,
      offering_sequences => 
      [
        {
          offering_levels => 
          [
            {
              catalog_level => 'C',
              catalog_code  => 'HZNO04',
            }
          ],
          sequence_number => 0,
          years           => 05,
          eid             => ''
        }
      ]
    },
    {
      sort_order => 2,
      years      => 10,
      offering_sequences => 
      [
        {
          offering_levels => 
          [
            {
              catalog_level => 'E',
              catalog_code  => 'HZNO04',
            }
          ],
          sequence_number => 0,
          years           => 10,
          eid             => ''
        }
      ]
    },
    {
      sort_order => 3,
      years      => 15,
      offering_sequences => 
      [
        {
          offering_levels => 
          [
            {
              catalog_level => 'H',
              catalog_code  => 'HZNO04',
            }
          ],
          sequence_number => 0,
          years           => 15,
          eid             => '',
        }
      ]
    },
    {
      sort_order => 5,
      years      => 25,
      offering_sequences => 
      [
        {
          offering_levels => 
          [
            {
              catalog_level => 'P',
              catalog_code  => 'HZNO04',
            }
          ],
          sequence_number => 0,
          years           => 25,
          eid             => '',
        }
      ]
    }
  );

  $employer->add_offerings(\@offerings);

  #$Rose::DB::Object::Debug = 1;
  #$Rose::DB::Object::Manager::Debug = 1;
  #$DB::single = 1;

  $employer->save;

  $employer = $employer_class->new(company_code => 'TEST')->load;

  is($employer->{'data'}, undef, "lazy bytea 1 - $db_type");
  is($employer->data, "\0\1x\2\3", "lazy bytea 2 - $db_type");
  $employer->data("\0\4x\3\1");

  $employer->save;

  my $employers = 
    Rose::DB::Object::Manager->get_objects(
      object_class => $employer_class,
      sort_by      => 'name');

  is_deeply([ map { scalar $_->Rose::DB::Object::Helpers::column_value_pairs } @$employers ],
  [
    {
      'name' => '',
      'company_code' => 'TEST',
      'data' => "\0\4x\3\1",
    },
    {
      'name' => 'DEFAULT EMPLOYERx',
      'company_code' => '',
      'data' => undef,
    }
  ],
  "employer check - $db_type");

  my $offerings = 
    Rose::DB::Object::Manager->get_objects(
      object_class => $offering_class,
      sort_by      => [ 'company_code', 'sort_order', 'years' ]);

  is_deeply([ map { scalar $_->Rose::DB::Object::Helpers::column_value_pairs } @$offerings ],
   [
     {
       'browse' => 1,
       'discrete_sequences' => 1,
       'sort_order' => '0',
       'years' => '05',
       'eid' => '',
       'company_code' => ''
     },
     {
       'browse' => 1,
       'discrete_sequences' => 1,
       'sort_order' => '0',
       'years' => '10',
       'eid' => '',
       'company_code' => ''
     },
     {
       'browse' => 1,
       'discrete_sequences' => 1,
       'sort_order' => '0',
       'years' => '15',
       'eid' => '',
       'company_code' => ''
     },
     {
       'browse' => 1,
       'discrete_sequences' => 1,
       'sort_order' => '1',
       'years' => '5',
       'eid' => '',
       'company_code' => 'TEST'
     },
     {
       'browse' => 1,
       'discrete_sequences' => 1,
       'sort_order' => '2',
       'years' => '10',
       'eid' => '',
       'company_code' => 'TEST'
     },
     {
       'browse' => 1,
       'discrete_sequences' => 1,
       'sort_order' => '3',
       'years' => '15',
       'eid' => '',
       'company_code' => 'TEST'
     },
     {
       'browse' => 1,
       'discrete_sequences' => 1,
       'sort_order' => '5',
       'years' => '25',
       'eid' => '',
       'company_code' => 'TEST'
     }
   ],
   "offering check - $db_type");

  my $offering_sequences = 
    Rose::DB::Object::Manager->get_objects(
      object_class => $offering_sequence_class,
      sort_by      => [ 'company_code', 'years' ]);

  is_deeply([ map { scalar $_->Rose::DB::Object::Helpers::column_value_pairs } @$offering_sequences ],
   [
     {
       'browse' => 1,
       'sequence_number' => '0',
       'years' => '05',
       'eid' => '',
       'company_code' => ''
     },
     {
       'browse' => 1,
       'sequence_number' => '0',
       'years' => '10',
       'eid' => '',
       'company_code' => ''
     },
     {
       'browse' => 1,
       'sequence_number' => '0',
       'years' => '15',
       'eid' => '',
       'company_code' => ''
     },
     {
       'browse' => 1,
       'sequence_number' => '0',
       'years' => '10',
       'eid' => '',
       'company_code' => 'TEST'
     },
     {
       'browse' => 1,
       'sequence_number' => '0',
       'years' => '15',
       'eid' => '',
       'company_code' => 'TEST'
     },
     {
       'browse' => 1,
       'sequence_number' => '0',
       'years' => '25',
       'eid' => '',
       'company_code' => 'TEST'
     },
     {
       'browse' => 1,
       'sequence_number' => '0',
       'years' => '5',
       'eid' => '',
       'company_code' => 'TEST'
     }
  ],
  "offering sequence check - $db_type");

  my $offering_levels = 
    Rose::DB::Object::Manager->get_objects(
      object_class => $offering_level_class,
      sort_by      => [ 'company_code', 'years' ]);

  is_deeply([ map { scalar $_->Rose::DB::Object::Helpers::column_value_pairs } @$offering_levels ],
   [
     {
       'browse' => 1,
       'sequence_number' => '0',
       'catalog_level' => 'E',
       'years' => '10',
       'eid' => '',
       'catalog_code' => 'HZNO04',
       'company_code' => 'TEST'
     },
     {
       'browse' => 1,
       'sequence_number' => '0',
       'catalog_level' => 'H',
       'years' => '15',
       'eid' => '',
       'catalog_code' => 'HZNO04',
       'company_code' => 'TEST'
     },
     {
       'browse' => 1,
       'sequence_number' => '0',
       'catalog_level' => 'P',
       'years' => '25',
       'eid' => '',
       'catalog_code' => 'HZNO04',
       'company_code' => 'TEST'
     },
     {
       'browse' => 1,
       'sequence_number' => '0',
       'catalog_level' => 'C',
       'years' => '5',
       'eid' => '',
       'catalog_code' => 'HZNO04',
       'company_code' => 'TEST'
     }
   ],
   "offering level check - $db_type");
}

BEGIN
{
  our %Have;

  #
  # PostgreSQL
  #

  my $dbh;

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

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

    # Drop existing tables and create schema, ignoring errors
    {
      local $dbh->{'RaiseError'} = 0;
      local $dbh->{'PrintError'} = 0;

      $dbh->do('DROP TABLE offering_levels CASCADE');
      $dbh->do('DROP TABLE offering_sequences CASCADE');
      $dbh->do('DROP TABLE offerings CASCADE');
      $dbh->do('DROP TABLE employee CASCADE');
      $dbh->do('DROP TABLE employer CASCADE');
    }

    my @sql =
    (
      <<"EOF",
CREATE OR REPLACE FUNCTION add_default_employee() RETURNS "trigger"
    AS '
BEGIN
    IF NEW.company_code IS NOT NULL THEN
        INSERT INTO
            employee (company_code, eid)
        VALUES (
            NEW.company_code,
            ''''
        );
    END IF;
    RETURN NEW;
END;
' LANGUAGE 'plpgsql';
EOF

      <<"EOF",
CREATE TABLE employer
(
  company_code  VARCHAR(6) DEFAULT '' NOT NULL PRIMARY KEY,
  name          VARCHAR(128) DEFAULT '' NOT NULL,
  data          BYTEA
)
EOF

      <<"EOF",
CREATE TRIGGER employer_default_employee
    AFTER INSERT ON employer
    FOR EACH ROW 
    EXECUTE PROCEDURE add_default_employee()
EOF

      <<"EOF",
CREATE TABLE employee 
(
  company_code VARCHAR(6) DEFAULT '' NOT NULL,
  eid          VARCHAR(9) DEFAULT '' NOT NULL,
  first_name   VARCHAR(15) DEFAULT '' NOT NULL,
  last_name    VARCHAR(25) DEFAULT '' NOT NULL,

  PRIMARY KEY (company_code, eid)
);
EOF

      <<"EOF",
ALTER TABLE employee ADD CONSTRAINT fk_employee_company_code 
  FOREIGN KEY (company_code) REFERENCES employer(company_code)
  ON UPDATE CASCADE ON DELETE RESTRICT
EOF

      <<"EOF",
INSERT INTO employer (company_code, name) VALUES ('', 'Default Employer')
EOF

      <<"EOF",
CREATE TABLE offerings 
(
  company_code        VARCHAR(6) DEFAULT '' NOT NULL,
  eid                 VARCHAR(9) DEFAULT '' NOT NULL,
  years               VARCHAR(2) DEFAULT '' NOT NULL,
  sort_order          SMALLINT DEFAULT 0 NOT NULL,
  browse              BOOLEAN DEFAULT true NOT NULL,
  discrete_sequences  BOOLEAN DEFAULT true NOT NULL,

  PRIMARY KEY (company_code, eid, years)
)
EOF

      <<"EOF",
ALTER TABLE offerings ADD CONSTRAINT fk_offering_employee 
  FOREIGN KEY (company_code, eid) REFERENCES employee(company_code, eid) 
  ON UPDATE CASCADE ON DELETE CASCADE
EOF

      <<"EOF",
ALTER TABLE offerings ADD CONSTRAINT fk_offering_company_code 
  FOREIGN KEY (company_code) REFERENCES employer(company_code) 
  ON UPDATE CASCADE ON DELETE CASCADE
EOF

      <<"EOF",
INSERT INTO offerings (company_code, eid, years) VALUES ('','','05')
EOF

      <<"EOF",
INSERT INTO offerings (company_code, eid, years) VALUES ('','','10')
EOF

      <<"EOF",
INSERT INTO offerings (company_code, eid, years) VALUES ('','','15')
EOF

      <<"EOF",
CREATE TABLE offering_sequences 
(
  company_code     VARCHAR(6) DEFAULT '' NOT NULL,
  eid              VARCHAR(9) DEFAULT '' NOT NULL,
  years            VARCHAR(2) DEFAULT '' NOT NULL,
  sequence_number  SMALLINT DEFAULT 0 NOT NULL,
  browse           BOOLEAN DEFAULT true NOT NULL,

  PRIMARY KEY (company_code, eid, years, sequence_number)
)
EOF

      <<"EOF",
ALTER TABLE offering_sequences ADD CONSTRAINT fk_offering_sequences 
  FOREIGN KEY (company_code, eid, years) REFERENCES offerings(company_code, eid, years) 
  ON UPDATE CASCADE ON DELETE CASCADE
EOF

      <<"EOF",
INSERT INTO offering_sequences (company_code, eid, years, sequence_number) VALUES ('','','05',0)
EOF

      <<"EOF",
INSERT INTO offering_sequences (company_code, eid, years, sequence_number) VALUES ('','','10',0)
EOF

      <<"EOF",
INSERT INTO offering_sequences (company_code, eid, years, sequence_number) VALUES ('','','15',0)
EOF

      <<"EOF",
CREATE TABLE offering_levels 
(
  company_code    VARCHAR(6) DEFAULT '' NOT NULL,
  eid             VARCHAR(9) DEFAULT '' NOT NULL,
  years           VARCHAR(2) DEFAULT '' NOT NULL,
  sequence_number SMALLINT DEFAULT 0 NOT NULL,
  catalog_code    VARCHAR(6) DEFAULT '' NOT NULL,
  catalog_level   VARCHAR(2) DEFAULT '' NOT NULL,
  browse          BOOLEAN DEFAULT true NOT NULL,

  PRIMARY KEY (company_code, eid, years, sequence_number, catalog_code, catalog_level)
)
EOF

      <<"EOF",
ALTER TABLE offering_levels ADD CONSTRAINT fk_offering_levels 
  FOREIGN KEY (company_code, eid, years, sequence_number) 
  REFERENCES offering_sequences(company_code, eid, years, sequence_number) 
  ON UPDATE CASCADE ON DELETE CASCADE
EOF
    );

    foreach my $sql (@sql)
    {
      local $dbh->{'PrintError'} = 0;
      eval { $dbh->do($sql) };

      if($@)
      {
        warn $@  unless($@ =~ /language "plpgsql" does not exist/);
        $Have{'pg'} = 0;
        $Have{'pg_with_schema'} = 0;
        last;
      }
    }

    $dbh->disconnect;
  }

}

END
{
  # Delete test table

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

    $dbh->do('DROP TABLE offering_levels CASCADE');
    $dbh->do('DROP TABLE offering_sequences CASCADE');
    $dbh->do('DROP TABLE offerings CASCADE');
    $dbh->do('DROP TABLE employee CASCADE');
    $dbh->do('DROP TABLE employer CASCADE');

    $dbh->disconnect;
  }
}