source: trunk/t/DNSTest.pm@ 961

Last change on this file since 961 was 961, checked in by Kris Deugau, 8 hours ago

/trunk

Polish up a little boilerplate in t/DNSTest.pm for easier debugging of tests
See #88

  • Property svn:keywords set to Date Rev Author Id
File size: 2.6 KB
Line 
1# -*- Perl -*-
2
3# Base class for DNSDB.pm unit testing
4# Sets up DB connection and repopulates it for a consistent test reference
5##
6# $Id: DNSTest.pm 961 2025-12-31 16:31:52Z kdeugau $
7# Copyright 2025 Kris Deugau <kdeugau@deepnet.cx>
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
21##
22
23package DNSTest;
24use strict;
25use warnings;
26use Exporter;
27use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
28@ISA = qw(Exporter);
29@EXPORT = qw($dnsdb $dbh);
30
31use Test::More;
32
33use lib '.';
34use DNSDB;
35
36our $dnsdb;
37our $dbh;
38our $debug;
39
40sub new {
41 my $this = shift;
42 my $class = ref($this) || $this;
43 my %args = @_;
44
45 $dnsdb = new DNSDB(
46 dbhost => 'localhost',
47 dbname => 'dnstest',
48 dbuser => 'dnstest',
49 dbpass => 'dnstestpwd',
50 );
51 ok( defined $dnsdb );
52 ok( $dnsdb->isa('DNSDB') );
53
54 $debug = 1 if $args{debug};
55
56 ## Prepare the DB
57 # Check that we aren't in an obviously production DB before blowing it away.
58 # A DB instantiated for these tests should NEVER have more than a handful of domains and maybe 20-30 records.
59
60 $dbh = $dnsdb->{dbh};
61
62 my ($dcount) = $dbh->selectrow_array("SELECT count(*) FROM domains");
63 BAIL_OUT("# DB looks like it may not be a test DB, found $dcount > 10 domains!\n")
64 if $dcount > 10;
65 cmp_ok( $dcount, '<=', 10, "domain count ($dcount): looks like a test DB" );
66
67 my ($rcount) = $dbh->selectrow_array("SELECT count(*) FROM records");
68 BAIL_OUT("# DB looks like it may not be a test DB, found $rcount > 30 records!\n")
69 if $rcount > 50;
70 cmp_ok( $rcount, '<=', 50, "record ($rcount): looks like a test DB" );
71
72 # drop all tables etc
73 $ENV{PGPASSWORD} = $dnsdb->{dbpass};
74# neither diag or note seem to suppress output from qx
75 my $dropdata = qx( psql -h $dnsdb->{dbhost} -U $dnsdb->{dbuser} $dnsdb->{dbname} 2>&1 < t/dropall.sql );
76 diag( $dropdata ) if $debug;
77 # load some standard test data
78 my $reload = qx( psql -h $dnsdb->{dbhost} -U $dnsdb->{dbuser} $dnsdb->{dbname} 2>&1 < t/dns-unitbase.sql );
79 diag( $reload ) if $debug;
80 undef $ENV{PGPASSWORD};
81} # new()
82
83sub DESTROY {
84 $dnsdb->finish;
85}
86
871;
Note: See TracBrowser for help on using the repository browser.