=head1 Name

newdb


=head1 Ziel

Anlegen einer Test-Datenbank


=head1 Testfälle

Es wird getestet:


=head2 dbbuild

C<dbbuild> wird dreimal durchlaufen: einmal zum Anlegen der Tabellen,
einmal um die nextid-Werte aufzufüllen, und einmal um die
Datenbankbeschreibungen in den db_*-Tabellen abzulegen.


=head2

Exemplarisch wird das Vorhandensein eines Eintrags in C<db_tabelle>,
einer Tabelle und eines nextid-Eintrags geprüft.


=cut


package Test::newdb;
use utf8;
use warnings;
use strict; use warnings;
BEGIN { unshift(@INC,($ENV{'POPHOME'}||'@POPHOME@').'/lib')
			unless $ENV{'KUNDE_NO_PERLPATH'};
      }


my %muster;
BEGIN {
	# $muster{vorlage} = [ qw(rt_template adress_vorlage template_test) ];
	# next: deskriptortabellen
}

use Test::More tests => 13+4*(keys %muster); $|=1;
BEGIN { $ENV{'DbLocalCf'}=1; }
use Dbase::Test;

BEGIN {
	die "Problem: DB ist $ENV{'DBDATABASE'}!\n" if $ENV{'DBDATABASE'} !~ /^test_/;

	my $host=$ENV{'TESTHOST'} || "localhost";
	sys("mysql '-u$ENV{'DATAUSER'}' '-h$host' '-p$ENV{'DATAPASS'}' -e 'drop database /*!40100 if exists */ $ENV{'DBDATABASE'}'");
	sys("mysql '-u$ENV{'DATAUSER'}' '-h$host' '-p$ENV{'DATAPASS'}' -e 'drop database /*!40100 if exists */ $ENV{'OTRS'}'");

	sys("mysql '-u$ENV{'DATAUSER'}' '-h$host' '-p$ENV{'DATAPASS'}' -e 'create database $ENV{'DBDATABASE'}'");
	sys("mysql '-u$ENV{'DATAUSER'}' '-h$host' '-p$ENV{'DATAPASS'}' -e 'create database $ENV{'OTRS'}'");

	sys("mysql '-u$ENV{'DATAUSER'}' '-h$host' '-p$ENV{'DATAPASS'}' -e 'create table ticket(id int not null auto_increment primary key, tn int, subject char(100), valid_id tinyint)' $ENV{'OTRS'}");

	no warnings 'once';
	$Dbase::FastError = 1;
}

my %info;
BEGIN {
	open(IN,"POP-Datenbank") || die "Keine Datenbank";

	local $/=""; local $|=1;
	while(<IN>) {
    	next unless /^Tabelle:\s+(.*)$/m;
	    $info{$1} = $_;
	}
	close(IN);
}

use Dbase::Help;
use Fehler qw(ffehler fehler);

{
	no warnings 'once'; # ERR
	open(ERR,">&STDERR");
	open(STDERR,">/dev/null") if $ENV{'HARNESS_ACTIVE'};
	print STDERR "\nTest Fehlerreporting... dies ist KEIN";
	ok(!DoF("Bla Fasel Laber Gerede"), "Teste Fehlerreporting");
	open(STDERR,">&ERR");
}

okQ "select 1",1; ## Test ob es die Datenbank gibt
## wegen einem MySQL-bug kann eine gelöschte DB nämlich trotzdem noch
## existieren und sich dann natürlich auch nicht anlegen lassen ...

select(STDERR); $|=1;
select(STDOUT); $|=1;

chomp(my $path = `pwd`);
my $prefix = $ENV{'REAL_TEST'} ? "/usr/pop/bin" : "$path/bin";
my $dbbuild = (defined $DB::single)
	? $ENV{'REAL_TEST'}
		? "perl -d $prefix/dbbuild"
		: "perl -I$path/lib -d $prefix/dbbuild"
	: $ENV{'REAL_TEST'}
		? "$prefix/dbbuild >&2"
		: "perl -I$path/lib $prefix/dbbuild >&2";

my @act = (-g => "create", -i => "nextid", -d => "struct", -k => "foreignkeys");
while(@act) {
	my $k = shift @act;
	my $info = shift @act;
	ok(!system("$dbbuild -ti $k -f POP-Datenbank"),"database $info");
	while(my($name,$tables) = each %muster) {
		ok(!system("$dbbuild -ti $k -f POP-Datenbank -m $name ".join(" ",@$tables)),"$name $info");
	}
}

pass "Nee, die db_feld-Daten prüfen wir jetzt nicht.";

ffehler {
	Do "CREATE TABLE `quelle` (
		`kunde` int(11) NOT NULL default '0',
		`quelle` int(11) NOT NULL default '0',
		`datum` int(11) NOT NULL default '0',
		`ziel` tinyint(4) default NULL,
		`pakete` bigint(20) default NULL,
		`bytes` bigint(20) default NULL,
		`timestamp` timestamp NOT NULL,
		KEY `qkd` (`kunde`,`datum`),
		KEY `qqd` (`quelle`,`datum`),
		KEY `timestamp` (`timestamp`)
	) ENGINE=InnoDB";
	pass "Tabelle 'quelle'";
} sub {
	fail "Tabelle 'quelle': @_";
};

