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
|
#!/usr/bin/env 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 tests => 27;
use PerconaTest;
use Sandbox;
require "$trunk/bin/pt-table-sync";
# Just for brevity.
sub _cmp {
return pt_table_sync::cmp_conflict_col(@_);
}
# These constants are from mk-table-sync, defined just
# before sub cmp_conflict_col().
use constant UPDATE_LEFT => -1;
use constant UPDATE_RIGHT => 1;
use constant UPDATE_NEITHER => 0; # neither value equals/matches
use constant FAILED_THRESHOLD => 2; # failed to exceed threshold
# #############################################################################
# Sanity checks.
# #############################################################################
throws_ok(
sub { pt_table_sync::cmp_conflict_col(1, 2, 'bad') },
qr/Invalid comparison: bad/,
'Dies on invalid comparison'
);
# ###########################################################################
# newest/oldest
# ###########################################################################
is(
_cmp('2009-12-01 12:00:00', '2009-12-01 12:00:00', 'newest'),
UPDATE_NEITHER,
'same datetime'
);
is(
_cmp('2009-12-01 12:00:00', '2009-12-01 12:00:011', 'newest'),
UPDATE_LEFT,
'newest datetime'
);
is(
_cmp('2009-12-01 12:00:00', '2009-12-01 12:00:011', 'oldest'),
UPDATE_RIGHT,
'oldest datetime'
);
is(
_cmp('2009-12-01 13:00:00', '2009-12-01 12:00:011', 'newest'),
UPDATE_RIGHT,
'newest datetime (reversed)'
);
is(
_cmp('2009-12-01 13:00:00', '2009-12-01 12:00:011', 'oldest'),
UPDATE_LEFT,
'oldest datetime (reversed)'
);
is(
_cmp('2009-12-01', '2009-12-02', 'newest'),
UPDATE_LEFT,
'newest date'
);
is(
_cmp('2009-12-01', '2009-12-02', 'oldest'),
UPDATE_RIGHT,
'oldest date'
);
is(
_cmp('12:00:00', '12:00:011', 'newest'),
UPDATE_LEFT,
'newest time'
);
is(
_cmp('12:00:00', '12:00:011', 'oldest'),
UPDATE_RIGHT,
'oldest time'
);
is(
_cmp('2009-12-01 12:00:00', '2009-12-01 12:05:00', 'newest', undef,
'5m'),
UPDATE_LEFT,
'newest datetime, threshold ok'
);
is(
_cmp('2009-12-01 12:00:00', '2009-12-01 12:05:00', 'newest', undef,
'6m'),
FAILED_THRESHOLD,
'newest datetime, failed threshold'
);
is(
_cmp('2009-12-01 12:00:00', '2009-12-01 12:05:00', 'oldest', undef,
'5m'),
UPDATE_RIGHT,
'oldest datetime, threshold ok'
);
is(
_cmp('2009-12-01 12:00:00', '2009-12-01 12:05:00', 'oldest', undef,
'6m'),
FAILED_THRESHOLD,
'oldest datetime, failed threshold'
);
is(
_cmp('2009-12-01', '2009-12-03', 'newest', undef,
'2d'),
UPDATE_LEFT,
'newest date, threshold ok'
);
is(
_cmp('2009-12-01', '2009-12-03', 'newest', undef,
'3d'),
FAILED_THRESHOLD,
'newest date, failed threshold'
);
# ###########################################################################
# greatest/least
# ###########################################################################
is(
_cmp(11, 11, 'greatest'),
UPDATE_NEITHER,
'same number'
);
is(
_cmp(11, 10, 'greatest'),
UPDATE_RIGHT,
'greatest'
);
is(
_cmp(11, 10, 'least'),
UPDATE_LEFT,
'least'
);
is(
_cmp(20, 10, 'least', undef, 10),
UPDATE_LEFT,
'least, threshold ok'
);
is(
_cmp(20, 10, 'least', undef, 11),
FAILED_THRESHOLD,
'least, failed threshold'
);
# #############################################################################
# equals
# #############################################################################
is(
_cmp('foo', 'bar', 'equals', 'foo'),
UPDATE_RIGHT,
'equals left, update right'
);
is(
_cmp('foo', 'bar', 'equals', 'bar'),
UPDATE_LEFT,
'equals right, update left'
);
is(
_cmp('foo', 'bar', 'equals', 'banana'),
UPDATE_NEITHER,
'equals neither'
);
# #############################################################################
# matches
# #############################################################################
is(
_cmp('foo', 'bar', 'matches', '^f..'),
UPDATE_RIGHT,
'matches left, update right'
);
is(
_cmp('foo', 'bar', 'matches', '.[ar]+$'),
UPDATE_LEFT,
'matches right, update left'
);
is(
_cmp('foo', 'bar', 'matches', '^foo.$'),
UPDATE_NEITHER,
'matches neither'
);
# #############################################################################
# Done.
# #############################################################################
exit;
|