From 4e9eb3263505efde314a508a74dde9425e05b0ef Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Sun, 26 Feb 2006 04:03:10 +0000 Subject: [PATCH] Added a new set of tests for checking the integrity of the WebGUI database. The first tests, Asset_diagnose.t checks that assetIds match in tables asset, assetData and each Asset's default table. Asset/Wobject/DataForm_diagnose.t adds tests for verifying DataForm collateral tables, DataForm_field and DataForm_tab. With a few small changes, the test could also cover the DataForm data tables, DataForm_entry and DataForm_entryData. The tests are optimized to do a quick preliminary test, and if this test fails, then an in-depth test is run. This second test will run a VERY VERY VERY long time, but will tell you exactly what's missing from your tables. The new tests require the module Test::Deep, for comparing data structures. --- docs/changelog/6.x.x.txt | 1 + sbin/testEnvironment.pl | 1 + t/Asset/Asset_diagnose.t | 72 +++++++++++++++++++++++++++++ t/Asset/Wobject/DataForm_diagnose.t | 53 +++++++++++++++++++++ 4 files changed, 127 insertions(+) create mode 100644 t/Asset/Asset_diagnose.t create mode 100644 t/Asset/Wobject/DataForm_diagnose.t diff --git a/docs/changelog/6.x.x.txt b/docs/changelog/6.x.x.txt index 9f1ec8482..5e43b245e 100644 --- a/docs/changelog/6.x.x.txt +++ b/docs/changelog/6.x.x.txt @@ -39,6 +39,7 @@ SiteMap feature. - [ 1433525 ] 6.9: Compilation errors - base36 removed from Utility.t because it no longer exists in WebGUI::Utility.pm + - Add tests that verify the integrity of the WebGUI Database. 6.8.8 - fix [ 1437186 ] 6.8.7 deploy DataForm package does not copy fields diff --git a/sbin/testEnvironment.pl b/sbin/testEnvironment.pl index 18183f9e9..2101d445c 100644 --- a/sbin/testEnvironment.pl +++ b/sbin/testEnvironment.pl @@ -47,6 +47,7 @@ checkModule("HTTP::Request",1.40); checkModule("HTTP::Headers",1.61); checkModule("Test::More",0.61,1); checkModule("Test::MockObject",1.02,1); +checkModule("Test::Deep",0.093,1); checkModule("Pod::Coverage",0.17,2); checkModule("Text::Balanced",1.95,1); checkModule("Digest::MD5",2.20); diff --git a/t/Asset/Asset_diagnose.t b/t/Asset/Asset_diagnose.t new file mode 100644 index 000000000..580deeed8 --- /dev/null +++ b/t/Asset/Asset_diagnose.t @@ -0,0 +1,72 @@ +#------------------------------------------------------------------- +# WebGUI is Copyright 2001-2006 Plain Black Corporation. +#------------------------------------------------------------------- +# Please read the legal notices (docs/legal.txt) and the license +# (docs/license.txt) that came with this distribution before using +# this software. +#------------------------------------------------------------------- +# http://www.plainblack.com info@plainblack.com +#------------------------------------------------------------------- + +use FindBin; +use strict; +use lib "$FindBin::Bin/../lib"; + +##The goal of this test is to look for orphaned assetIds across +##all assets in the Asset's main table, and the asset and assetData tables. + +use WebGUI::Test; +use WebGUI::Session; +use WebGUI::Utility; +use Test::More; # increment this value for each test you create +use Test::Deep; + +my $session = WebGUI::Test->session; + +my @assets = grep { !isIn($_, qw/WebGUI::Asset::FilePile/) } ( + @{ $session->config->get('assets') }, + @{ $session->config->get('utilityAssets') }, + @{ $session->config->get('assetContainers') }, +); + +my $numTests = scalar (2*@assets) + 2; +diag("Testing $numTests assets"); +plan tests => $numTests; + +my $assetIds = $session->db->buildArrayRef("select distinct(assetId) from asset order by assetId"); +my $assetDataIds = $session->db->buildArrayRef("select distinct(assetId) from assetData order by assetId"); + +##This is a quick test to see if details of mismatch are required +my $noDetails = ok(compare_arrays($assetIds, $assetDataIds), "Checking asset vs assetData"); + +SKIP: { + skip("No need for details", 1) if $noDetails; + ##This test takes a very, very long time. + cmp_bag($assetIds, $assetDataIds, "Checking asset vs assetData"); +} + +foreach my $asset ( @assets ) { + diag("Checking $asset"); + my $assetObj = WebGUI::Asset->newByPropertyHashRef($session, { className=>$asset }); + my $def = $asset->definition($session); + my $tableName = $def->[0]->{tableName}; + my $classIds = $session->db->buildArrayRef("select distinct(assetId) from asset where className=? order by assetId", [$asset]); + my $tableIds = $session->db->buildArrayRef(sprintf("select distinct(assetId) from %s order by assetId", $tableName)); + my $skipDetails = ok(compare_arrays($classIds, $tableIds), + sprintf("Comparing assetIds for %s",$asset) + ); + SKIP: { + skip("No details needed for $asset", 1) if $skipDetails; + cmp_bag($classIds, $tableIds, "Checking asset vs table for $asset"); + } +} + +sub compare_arrays { + my ($first, $second) = @_; + no warnings; + return 0 unless @$first == @$second; + for (my $i=0; $i <=@$first; $i++) { + return 0 if $first->[$i] ne $second->[$i]; + } + return 1; +} diff --git a/t/Asset/Wobject/DataForm_diagnose.t b/t/Asset/Wobject/DataForm_diagnose.t new file mode 100644 index 000000000..64d97f03f --- /dev/null +++ b/t/Asset/Wobject/DataForm_diagnose.t @@ -0,0 +1,53 @@ +#------------------------------------------------------------------- +# WebGUI is Copyright 2001-2006 Plain Black Corporation. +#------------------------------------------------------------------- +# Please read the legal notices (docs/legal.txt) and the license +# (docs/license.txt) that came with this distribution before using +# this software. +#------------------------------------------------------------------- +# http://www.plainblack.com info@plainblack.com +#------------------------------------------------------------------- + +use FindBin; +use strict; +use lib "$FindBin::Bin/../../lib"; + +##The goal of this test is to diagnose problems in DataForms. +## Orphaned DataForms with no Asset table entries +## + +use WebGUI::Test; +use WebGUI::Session; +use Test::More tests => 4; # increment this value for each test you create +use Test::Deep; + +my $one = [1, 2, 3, 4]; +my @two = (); + +my $session = WebGUI::Test->session; + +my $dataFormIds = $session->db->buildArrayRef("select asset.assetId, assetData.revisionDate from DataForm left join asset on asset.assetId=DataForm.assetId left join assetData on assetData.revisionDate=DataForm.revisionDate and assetData.assetId=DataForm.assetId where asset.state='published' and assetData.revisionDate=(SELECT max(revisionDate) from assetData where assetData.assetId=asset.assetId and (assetData.status='approved' or assetData.tagId=?)) order by assetData.title"); + +diag("Checking DataForm tables for orphaned assetIds"); +foreach my $table (qw/DataForm DataForm_field/) { + my $tableIds = $session->db->buildArrayRef(sprintf ("select distinct(assetId) from %s", $table)); + cmp_bag($dataFormIds, $tableIds, + sprintf("Orphaned assetIds in %s", $table)); +} + +diag("Checking DataForm_tab tables for orphaned assetIds"); +##DataForm_tab will have a subset of assetIds since not all DataForms have tabs. +foreach my $table (qw/DataForm_tab/) { + my $tableIds = $session->db->buildArrayRef(sprintf ("select distinct(assetId) from %s", $table)); + cmp_deeply($tableIds, subsetof(@{ $dataFormIds }), + sprintf("Orphaned assetIds in %s", $table)); +} + +diag("Checking DataForm tables for orphaned fieldIds"); +my $dataForm_fieldIds = $session->db->buildArrayRef("select distinct(DataForm_fieldId) from DataForm_field"); +foreach my $table (qw/DataForm_tab/) { + my $tableIds = $session->db->buildArrayRef(sprintf ("select distinct(assetId) from %s", $table)); + cmp_deeply($tableIds, subsetof(@{ $dataForm_fieldIds}), + sprintf("Orphaned fieldId in %s", $table)); +} +