File: TableSyncGroupBy.t

package info (click to toggle)
percona-toolkit 3.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 68,916 kB
  • sloc: perl: 241,287; sql: 22,868; sh: 19,746; javascript: 6,799; makefile: 353; awk: 38; python: 30; sed: 1
file content (122 lines) | stat: -rw-r--r-- 3,281 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/perl

BEGIN {
   die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n"
      unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH};
   unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib";
};

use strict;
use warnings FATAL => 'all';
use English qw(-no_match_vars);
use Test::More;
use Data::Dumper;

use TableSyncGroupBy;
use Quoter;
use MockSth;
use RowDiff;
use ChangeHandler;
use PerconaTest;

my $q = new Quoter();
my $tbl_struct = {
   type_for => { a => 'int', b => 'int', c => 'int' },
   col_posn => { a => 1, b => 2, c => 3 },
   is_col   => { a => 1, b => 2, c => 3 },
};
my @rows;

throws_ok(
   sub { new TableSyncGroupBy() },
   qr/I need a Quoter/,
   'Quoter required'
);
my $t = new TableSyncGroupBy(
   Quoter => $q,
);

my $ch = new ChangeHandler(
   Quoter    => $q,
   right_db  => 'test',
   right_tbl => 'foo',
   left_db   => 'test',
   left_tbl  => 'foo',
   replace   => 0,
   actions   => [ sub { push @rows, $_[0] }, ],
   queue     => 0,
   tbl_struct => $tbl_struct,
);

$t->prepare_to_sync(
   ChangeHandler => $ch,
   cols          => [qw(a b c)],
   tbl_struct    => $tbl_struct,
   buffer_in_mysql => 1,
);
is(
   $t->get_sql(
      where    => 'foo=1',
      database => 'test',
      table    => 'foo',
   ),
   'SELECT SQL_BUFFER_RESULT `a`, `b`, `c`, COUNT(*) AS __maatkit_count FROM `test`.`foo` '
      . 'WHERE foo=1 GROUP BY `a`, `b`, `c` ORDER BY `a`, `b`, `c`',
   'Got SQL with SQL_BUFFER_RESULT',
);

$t->prepare_to_sync(
   ChangeHandler => $ch,
   cols          => [qw(a b c)],
   tbl_struct    => $tbl_struct,
);
is(
   $t->get_sql(
      where    => 'foo=1',
      database => 'test',
      table    => 'foo',
   ),
   'SELECT `a`, `b`, `c`, COUNT(*) AS __maatkit_count FROM `test`.`foo` '
      . 'WHERE foo=1 GROUP BY `a`, `b`, `c` ORDER BY `a`, `b`, `c`',
   'Got SQL OK',
);

# Changed from undef to 0 due to r4802.
is( $t->done, 0, 'Not done yet' );

my $d = new RowDiff( dbh => 1 );
$d->compare_sets(
   left_sth => new MockSth(
      { a => 1, b => 2, c => 3, __maatkit_count => 4 },
      { a => 2, b => 2, c => 3, __maatkit_count => 4 },
      { a => 3, b => 2, c => 3, __maatkit_count => 2 },
      # { a => 4, b => 2, c => 3, __maatkit_count => 2 },
   ),
   right_sth => new MockSth(
      { a => 1, b => 2, c => 3, __maatkit_count => 3 },
      { a => 2, b => 2, c => 3, __maatkit_count => 6 },
      # { a => 3, b => 2, c => 3, __maatkit_count => 2 },
      { a => 4, b => 2, c => 3, __maatkit_count => 1 },
   ),
   syncer     => $t,
   tbl_struct => $tbl_struct,
);

is_deeply(
   \@rows,
   [
   "INSERT INTO `test`.`foo`(`a`, `b`, `c`) VALUES ('1', '2', '3')",
   "DELETE FROM `test`.`foo` WHERE `a`='2' AND `b`='2' AND `c`='3' LIMIT 1",
   "DELETE FROM `test`.`foo` WHERE `a`='2' AND `b`='2' AND `c`='3' LIMIT 1",
   "INSERT INTO `test`.`foo`(`a`, `b`, `c`) VALUES ('3', '2', '3')",
   "INSERT INTO `test`.`foo`(`a`, `b`, `c`) VALUES ('3', '2', '3')",
   "DELETE FROM `test`.`foo` WHERE `a`='4' AND `b`='2' AND `c`='3' LIMIT 1",
   ],
   'rows from handler',
) or diag(Dumper(\@rows));

# #############################################################################
# Done
# #############################################################################
done_testing;
exit;