DBIx-Compare-ContentChecksum-mysql

 view release on metacpan or  search on metacpan

t/DBIx_Compare_ContentChecksum_mysql.t  view on Meta::CPAN

#! /usr/bin/perl -w

use strict;

use Test::More tests=>18;
use Test::Group;
use Test::Differences;

use DBI;

# 1
BEGIN {
	use_ok('DBIx::Compare::ContentChecksum::mysql');
}

my $user_name = 'test';
my $user_pass = '';
my $dsn1 = "DBI:mysql:test:localhost";
my $dsn2 = "DBI:mysql:test2:localhost";
my ($to_test,$oDB_Content,$dbh1,$dbh2);

eval {
	require DBD::mysql;
};
if ($@){
	diag("Skipping 17 tests: Could not create the test databases because the driver 'DBD::mysql' is not installed");
} else {
	$dbh1 = DBI->connect($dsn1, $user_name, $user_pass);
	$dbh2 = DBI->connect($dsn2, $user_name, $user_pass);
	if ($dbh1 && $dbh2 && create_test_db($dbh1) && create_test_db($dbh2)){
		$to_test = 1;
	} else {
		# because Test::Harness doesn't seem to want to output my skips!
		diag("Skipping 17 tests: Could not create the test databases");
	}
}

SKIP: {
	skip("Could not create the test databases", 17) unless ($to_test);

	#2
	test 'object init' => sub {
		ok($oDB_Content = compare_mysql_checksum->new($dbh1,$dbh2),'init');
		isa_ok($oDB_Content,'db_comparison','DBIx::Compare object');
		isa_ok($oDB_Content,'compare_mysql_checksum','DBIx::Compare::ContentChecksum::mysql object');
	};

	#3
	test 'dbh stuff' => sub {
		my ($dbh1b,$dbh2b) = $oDB_Content->get_dbh;
		isa_ok($dbh1b,'DBI::db','dbh1 after set');
		isa_ok($dbh2b,'DBI::db','dbh2 after set');
		ok(my @aNames = $oDB_Content->get_db_names,'get_db_names');
		eq_or_diff \@aNames,['test:localhost','test2:localhost'],'database names';
		cmp_ok($oDB_Content->get_db_driver,'eq','mysql','get_db_driver');
	};

	#4
	test 'group_concat_max_len' => sub {
		ok(my @aLengths = $oDB_Content->mysql_group_concat_max_len,'mysql_group_concat_max_len 1');
		eq_or_diff \@aLengths,[1024,1024],'mysql_group_concat_max_len default';
		cmp_ok($oDB_Content->group_concat_max_len,'==',1024,'group_concat_max_len at init');
		$oDB_Content->group_concat_max_len(2048);
		cmp_ok($oDB_Content->group_concat_max_len,'==',2048,'group_concat_max_len after set');
		ok(@aLengths = $oDB_Content->mysql_group_concat_max_len,'mysql_group_concat_max_len 2');
		eq_or_diff \@aLengths,[2048,2048],'mysql_group_concat_max_len set';
	};

	#5
	test 'table lists' => sub {
		ok(my @aTables = $oDB_Content->get_tables,'get_tables 1 & 2');
		eq_or_diff \@aTables,[['filter','fluorochrome','laser','protocol_type'],['filter','fluorochrome','laser','protocol_type']],'table lists';
		ok(my $aTables1 = $oDB_Content->get_tables,'get_tables 1');
		eq_or_diff $aTables1,$aTables[0],'tables vs tables1';
	};

	#6
	test 'primary keys' => sub {
		ok(my $keys = $oDB_Content->get_primary_keys('filter',$dbh1),'get_primary_keys');
		cmp_ok($keys,'eq','filter_id','primary key string');
		ok(my @aKeys = $oDB_Content->get_primary_keys('filter',$dbh1),'get_primary_keys');
		eq_or_diff \@aKeys,['filter_id'],'primary key list';
	};

	#7
	test 'row counts' => sub {
		cmp_ok($oDB_Content->row_count('protocol_type',$dbh1),'==',4,'row_count');
		cmp_ok($oDB_Content->row_count('filter',$dbh1),'==',3,'row_count');
		cmp_ok($oDB_Content->row_count('laser',$dbh1),'==',3,'row_count');
		cmp_ok($oDB_Content->row_count('fluorochrome',$dbh1),'==',3,'row_count');
	};

	#8
	test 'checksums' => sub {
		# a text field
		ok(my @aChecksum = $oDB_Content->field_checksum('protocol_type','description'),"field_checksum('protocol_type','description')");
		eq_or_diff \@aChecksum,['026c991aead235493031010d66f9b342d0126146e437c33a19881984529a57ef','026c991aead235493031010d66f9b342d0126146e437c33a19881984529a57ef'],'protocol_type.description checksums';
		
		# an int field
		ok(@aChecksum = $oDB_Content->field_checksum('filter','filter_id'),"field_checksum('filter','filter_id')");
		eq_or_diff \@aChecksum,['766cf85a89d87f5bca3c9b5793b456831a45ed8a388e4b644044d238cde0a9f4','766cf85a89d87f5bca3c9b5793b456831a45ed8a388e4b644044d238cde0a9f4'],'filter.filter_id checksums';

		# a varchar field
		ok(@aChecksum = $oDB_Content->field_checksum('laser','colour_name'),"field_checksum('laser','colour_name')");
		eq_or_diff \@aChecksum,['e235f3560a066a5d5bd51d2ebe81813ae18af807eb7a24cf8f796af719ddca1f','e235f3560a066a5d5bd51d2ebe81813ae18af807eb7a24cf8f796af719ddca1f'],'laser.colour_name checksums';

		# a varchar field returning NULL
		ok(@aChecksum = $oDB_Content->field_checksum('fluorochrome','manufacturer'),"field_checksum('fluorochrome','manufacturer')");
		#eq_or_diff \@aChecksum,[undef,undef],'fluorochrome.manufacturer';	# Test::Differences throws warning at this test
		is($aChecksum[0],undef,'fluorochrome.manufacturer');
		is($aChecksum[1],undef,'fluorochrome.manufacturer');
	};

	#9
	test 'the comparisons' => sub {
		cmp_ok($oDB_Content->compare_table_lists,'==',1,'compare_table_lists');
		cmp_ok($oDB_Content->compare_row_counts,'==',1,'compare_row_counts');
		cmp_ok($oDB_Content->compare_fields_checksum,'==',1,'compare_fields_checksum');
		
		cmp_ok($oDB_Content->compare,'==',1,'compare');	# just re-does the above
		
		ok(my $hDiffs = $oDB_Content->get_differences,'get_differences');
		eq_or_diff $hDiffs,{},'differences hashref';
	};

	#10
	test 'deep_compare' => sub {
		cmp_ok($oDB_Content->deep_compare,'==',1,'deep_compare');
	};

	### now make the two databases different ###		
	if (add_differences($dbh1)){
		$to_test = 1;
	} else {
		$to_test = undef;
		# because Test::Harness doesn't seem to want to output my skips!
		diag("Skipping 8 tests: Could not update the database");
	}

	SKIP: {
		skip("Could not update the database", 8) unless ($to_test);

		#11
		test 'object RE-init' => sub {
			ok($oDB_Content = compare_mysql_checksum->new($dbh1,$dbh2),'init');
			isa_ok($oDB_Content,'db_comparison','DBIx::Compare object');
			isa_ok($oDB_Content,'compare_mysql_checksum','DBIx::Compare::ContentChecksum::mysql object');
		};

		#12
		test 'no primary key in table extra' => sub {
			my $keys = $oDB_Content->get_primary_keys('extra',$dbh1);
			is($keys,undef,'primary key string');
			my @aKeys = $oDB_Content->get_primary_keys('extra',$dbh1);
			cmp_ok(@aKeys,'==',0,'primary key list');
		};

		#13
		test 're-examine databases' => sub {
			# table lists
			ok(my @aTables = $oDB_Content->get_tables,'get_tables 1 & 2');
			eq_or_diff \@aTables,[['extra','filter','fluorochrome','laser','protocol_type'],['filter','fluorochrome','laser','protocol_type']],'table lists';
			
			# extra row in filter
			cmp_ok($oDB_Content->row_count('filter',$dbh1),'==',4,'row_count');

			# different checksums for laser.colour_name
			ok(my @aChecksum = $oDB_Content->field_checksum('laser','colour_name'),"field_checksum('laser','colour_name')");
			eq_or_diff \@aChecksum,['9c4926911e466e889af52bb345859f1f3aa0245b2c384e908544a31c730995f0','e235f3560a066a5d5bd51d2ebe81813ae18af807eb7a24cf8f796af719ddca1f'],'laser.colour_name checksums';



( run in 0.743 second using v1.01-cache-2.11-cpan-6aa56a78535 )