Fix and test resource_diff().

This commit is contained in:
Daniel Nichter
2013-03-27 11:12:38 -06:00
parent 13e9b5d7c7
commit afa6533f43
3 changed files with 26 additions and 5 deletions

View File

@@ -1466,6 +1466,7 @@ no Lmo;
{ {
package Percona::WebAPI::Util; package Percona::WebAPI::Util;
use JSON;
use Digest::MD5 qw(md5_hex); use Digest::MD5 qw(md5_hex);
use Percona::WebAPI::Representation; use Percona::WebAPI::Representation;
@@ -1478,8 +1479,12 @@ sub resource_diff {
my ($x, $y) = @_; my ($x, $y) = @_;
return 0 if !$x && !$y; return 0 if !$x && !$y;
return 1 if ($x && !$y) || (!$x && $y); return 1 if ($x && !$y) || (!$x && $y);
return md5_hex(Percona::WebAPI::Representation::as_json($x)) my $json = JSON->new->canonical([1]); # avoid hash key sort diffs
ne md5_hex(Percona::WebAPI::Representation::as_json($y)); my $x_hex = md5_hex(
Percona::WebAPI::Representation::as_json($x, json => $json));
my $y_hex = md5_hex(
Percona::WebAPI::Representation::as_json($y, json => $json));
return $x_hex eq $y_hex ? 0 : 1;
} }
1; 1;

View File

@@ -20,6 +20,7 @@
{ {
package Percona::WebAPI::Util; package Percona::WebAPI::Util;
use JSON;
use Digest::MD5 qw(md5_hex); use Digest::MD5 qw(md5_hex);
use Percona::WebAPI::Representation; use Percona::WebAPI::Representation;
@@ -32,8 +33,12 @@ sub resource_diff {
my ($x, $y) = @_; my ($x, $y) = @_;
return 0 if !$x && !$y; return 0 if !$x && !$y;
return 1 if ($x && !$y) || (!$x && $y); return 1 if ($x && !$y) || (!$x && $y);
return md5_hex(Percona::WebAPI::Representation::as_json($x)) my $json = JSON->new->canonical([1]); # avoid hash key sort diffs
ne md5_hex(Percona::WebAPI::Representation::as_json($y)); my $x_hex = md5_hex(
Percona::WebAPI::Representation::as_json($x, json => $json));
my $y_hex = md5_hex(
Percona::WebAPI::Representation::as_json($y, json => $json));
return $x_hex eq $y_hex ? 0 : 1;
} }
1; 1;

View File

@@ -45,7 +45,18 @@ $y->options->{spool} = '/var/lib/spool';
is( is(
resource_diff($x, $y), resource_diff($x, $y),
1, 1,
"Diff" "Big diff in 1 attrib"
);
# Restore this...
$y->options->{spool} = '/var/spool';
# Change this...
$y->options->{ts} = '101';
is(
resource_diff($x, $y),
1,
"Small diff in 1 attrib"
); );
# ############################################################################# # #############################################################################