Skip to content
Browse files

*** empty log message ***

git-svn-id: svn://svn.tt2.org/tt/Template2/trunk@54 d5a88997-0a34-4036-9ed2-92fb5d660d91
  • Loading branch information...
1 parent 9de6bd5 commit 48634a1fee43de0e0e4f615c8c5862a88ba5393c @abw committed Oct 9, 2000
Showing with 625 additions and 28 deletions.
  1. +2 −0 MANIFEST
  2. +121 −28 Makefile.PL
  3. +416 −0 t/dbi.t
  4. +86 −0 t/xpath.t
View
2 MANIFEST
@@ -31,6 +31,7 @@ lib/Template/Parser.pod
lib/Template/Plugin.pm
lib/Template/Plugin.pod
lib/Template/Plugin/CGI.pm
+lib/Template/Plugin/DBI.pm
lib/Template/Plugin/Datafile.pm
lib/Template/Plugin/Date.pm
lib/Template/Plugin/Dumper.pm
@@ -72,6 +73,7 @@ t/config.t
t/context.t
t/datafile.t
t/date.t
+t/dbi.t
t/directive.t
t/document.t
t/dom.t
View
149 Makefile.PL
@@ -4,43 +4,68 @@ select STDERR;
$| = 1;
select STDOUT;
-warn(<<EOF);
+my $DBI_CONFIG_FILE = 't/dbi_test.cfg';
- Template Toolkit Version 2.00 beta 5
- ------------------------------------
+print <<EOF;
+Template Toolkit Version 2.00
+-----------------------------
+EOF
+
+#------------------------------------------------------------------------
+# check for pre-version 2.00 installation and issue warning
+#------------------------------------------------------------------------
+
+eval "use Template";
+unless ($@ or $Template::VERSION =~ /^2/) {
+ warn(<<EOF);
+IMPORTANT NOTE:
+
+ You have version $Template::VERSION of the Template Toolkit installed.
- This is rock-solid and ready-to-rumble beta code, and is the final
- release candidate for Version 2.00 proper. Please read the README
- file and consult the Changes and TODO files for further
- information.
+ There are some minor incompatabilities between version 1.* and 2.*
+ of the Template Toolkit which you should be aware of. Installing
+ this version will overwrite your version $Template::VERSION files
+ unless you take measures to install one or the other version in a
+ different location (i.e. perl Makefile.PL LIB=/other/path).
+
+ Please consult the README and Changes file for further details.
+ Most of the changes are in the more obscure features and
+ directives so hopefully you will find the upgrade process fairly
+ painless.
EOF
+ exit unless prompt("Do you want to continue?", 'y') =~ /y/i;
+}
+
+#------------------------------------------------------------------------
+# detect additional modules required by plugins (just for fun)
+#------------------------------------------------------------------------
+print <<EOF;
+
+The Template Toolkit includes a number of plugin modules, some of which
+interface to external Perl modules available from CPAN. All the plugins
+will be installed regardless (so that they will work automatically if and
+when you install the relevant modules) but the tests will be skipped for
+plugins that require external modules not available on your system.
-eval "use Template";
-unless ($@) {
- warn(<<EOF) unless $Template::VERSION =~ /^2/;
- IMPORTANT NOTE:
- --------------
-
- Version $Template::VERSION of the Template Toolkit detected.
-
- If you install these modules you will totally screw up your
- Version $Template::VERSION installation. If you're ready to take
- the plunge and aren't relying too heavily on any of the "features"
- that have changed from version 1.xx to 2.xx then go ahead, be
- bold. See the README file for further details.
EOF
+
+foreach my $module (qw( XML::DOM XML::RSS XML::XPath DBI )) {
+ eval "use $module";
+ if ($@) {
+ print " $module: not found - skipping test\n";
+ }
+ else {
+ print " $module found - including test\n";
+ dbi_config() if $module eq 'DBI';
+ }
}
-# my $man3pods = {
-# map { ("docs/pod/$_.pod"
-# => "\$(INST_MAN3DIR)/Template::$_.\$(MAN3EXT)") }
-# qw( Base Config Constants Context Document Exception Filters
-# Iterator Parser Plugin Plugins Provider Service Stash Test )
-# };
-# $man3pods->{'docs/pod/Template.pod'} = '$(INST_MAN3DIR)/Template.$(MAN3EXT)';
+#------------------------------------------------------------------------
+# build options and write Makefile
+#------------------------------------------------------------------------
my %opts = (
'NAME' => 'Template',
@@ -49,7 +74,6 @@ my %opts = (
'EXE_FILES' => [ 'bin/tpage', 'bin/ttree' ],
'PMLIBDIRS' => [ 'lib' ],
'PREREQ_PM' => { 'AppConfig' => 1.52 },
-# 'MAN3PODS' => $man3pods,
'dist' => {
'COMPRESS' => 'gzip',
'SUFFIX' => 'gz',
@@ -66,6 +90,14 @@ if ($ExtUtils::MakeMaker::VERSION >= 5.43) {
WriteMakefile( %opts );
+#========================================================================
+
+
+#------------------------------------------------------------------------
+# build_docs()
+#
+# Echo the relevant incantation so that 'make dist' regenerates the
+# documentation from the template sources.
#------------------------------------------------------------------------
sub build_docs {
@@ -75,3 +107,64 @@ echo "Building documentation for version \$(VERSION)" ; \\
EOF
}
+
+#------------------------------------------------------------------------
+# dbi_config()
+#
+# Quiz the user for options related to running the DBI tests.
+#------------------------------------------------------------------------
+
+sub dbi_config {
+ my ($dsn, $user, $pass, $run);
+ $run = 1;
+
+ print "\nConfiguration for DBI tests\n";
+
+ if (prompt("- Do you want to run the DBI tests?\n" .
+ " It requires access to an existing test database.",
+ "y") =~ /y/i) {
+
+ my ($driver, $dbname);
+ my @drivers = DBI->available_drivers();
+ local $" = ', ';
+
+ my $default = (grep(/m.?sql/i, @drivers))[0];
+
+ print "- Please enter the driver name for the test database.\n";
+ print " The DBD drivers installed on your system are\n\n";
+ print " @drivers\n\n";
+
+ while (! $driver) {
+ $driver = prompt("- Enter driver name: ", $default);
+ print("! No such DBD driver\n"), undef $driver
+ unless grep(/^$driver$/, @drivers);
+ }
+
+ while (! $dbname) {
+ $dbname = prompt('- Enter database name: ', 'test');
+ }
+
+ $dsn = "dbi:$driver:$dbname";
+
+ $user = prompt('- Enter user name : ', '');
+ $pass = prompt('- Enter password : ', '');
+
+ $user = '' unless defined $user;
+ $pass = '' unless defined $pass;
+ }
+ else {
+ $run = 0;
+ }
+
+ print "- writing $DBI_CONFIG_FILE\n";
+ open(CFGFILE, ">$DBI_CONFIG_FILE") || die "$DBI_CONFIG_FILE: $!\n";
+ print CFGFILE <<EOF;
+\$run = $run;
+\$dsn = '$dsn';
+\$user = '$user';
+\$pass = '$pass';
+1;
+EOF
+ close(CFGFILE);
+}
+
View
416 t/dbi.t
@@ -0,0 +1,416 @@
+#============================================================= -*-perl-*-
+#
+# t/dbi.t
+#
+# Test script for the DBI plugin.
+#
+# The DBI plugin and test scripts were written by Simon Matthews
+# <sam@knowledgepool.com> with some minor modifications by Andy
+# Wardley <abw@kfs.org> for inclusion in the Template Toolkit from
+# version 2.00 onwards.
+#
+# This is free software; you can redistribute it and/or modify it
+# under the same terms as Perl itself.
+#
+# $Id$
+#
+#========================================================================
+
+use strict;
+use lib qw( . ./t ./blib/lib ../blib/lib );
+use vars qw( $DEBUG $run $dsn $user $pass );
+use Template::Test;
+
+$^W = 1;
+$DEBUG = 0;
+#$Template::Test::PRESERVE = 1;
+
+eval "use DBI";
+if ($@) {
+ print "1..0\n";
+ exit(0);
+}
+
+# load the configuration file created by Makefile.PL which defines
+# the $run, $dsn, $user and $pass variables.
+require 'dbi_test.cfg';
+unless ($run) {
+ print "1..0\n";
+ exit(0);
+}
+
+
+init_database($dsn, $user, $pass);
+
+my $dbh = DBI->connect($dsn, $user, $pass, { PrintError => 0 })
+ || die "DBI connect() failed: $DBI::errstr\n";
+
+my $vars = {
+ dbh => $dbh,
+ dsn => $dsn,
+ user => $user,
+ pass => $pass,
+};
+
+test_expect(\*DATA, undef, $vars);
+
+cleanup_database($dsn, $user, $pass);
+
+$dbh->disconnect();
+
+#------------------------------------------------------------------------
+# init_database($dsn, $user, $pass)
+#------------------------------------------------------------------------
+
+sub init_database {
+ my ($dsn, $user, $pass) = @_;
+
+ my $dbh = DBI->connect($dsn, $user, $pass)
+ || die "DBI connect() failed: $DBI::errstr\n";
+
+ # create some tables
+ sql_query($dbh, 'CREATE TABLE grp (
+ id Char(16),
+ name Char(32)
+ )');
+
+ sql_query($dbh, 'CREATE TABLE usr (
+ id Char(16),
+ name Char(32),
+ grp Char(16)
+ )');
+
+ # add some records to the 'grp' table
+ sql_query($dbh, "INSERT INTO grp
+ VALUES ('foo', 'The Foo Group')");
+ sql_query($dbh, "INSERT INTO grp
+ VALUES ('bar', 'The Bar Group')");
+
+ # add some records to the 'usr' table
+ sql_query($dbh, "INSERT INTO usr
+ VALUES ('abw', 'Andy Wardley', 'foo')");
+ sql_query($dbh, "INSERT INTO usr
+ VALUES ('sam', 'Simon Matthews', 'foo')");
+ sql_query($dbh, "INSERT INTO usr
+ VALUES ('hans', 'Hans von Lengerke', 'bar')");
+ sql_query($dbh, "INSERT INTO usr
+ VALUES ('mrp', 'Martin Portman', 'bar')");
+
+ $dbh->disconnect();
+}
+
+
+#------------------------------------------------------------------------
+# sql_query($dbh, $sql)
+#------------------------------------------------------------------------
+
+sub sql_query {
+ my ($dbh, $sql) = @_;
+
+ my $sth = $dbh->prepare($sql)
+ || warn "prepare() failed: $DBI::errstr\n";
+
+ $sth->execute()
+ || warn "execute() failed: $DBI::errstr\n";
+
+ $sth->finish();
+}
+
+
+#------------------------------------------------------------------------
+# cleanup_database($dsn, $user, $pass)
+#------------------------------------------------------------------------
+
+sub cleanup_database {
+ my ($dsn, $user, $pass) = @_;
+
+ my $dbh = DBI->connect($dsn, $user, $pass)
+ || die "DBI connect() failed: $DBI::errstr\n";
+
+ sql_query($dbh, 'DROP TABLE usr');
+ sql_query($dbh, 'DROP TABLE grp');
+
+ $dbh->disconnect();
+};
+
+
+#========================================================================
+
+__END__
+
+#------------------------------------------------------------------------
+# test plugin loading with implicit and explicit connect() method
+#------------------------------------------------------------------------
+
+-- test --
+[% USE DBI -%]
+[% DBI.connect(dsn, user, pass) -%]
+[% FOREACH user = DBI.query("SELECT name FROM usr WHERE id='abw'") -%]
+* [% user.name %]
+[% END %]
+
+-- expect --
+* Andy Wardley
+
+-- test --
+[% USE dbi -%]
+[% dbi.connect(dsn, user, pass) -%]
+[% FOREACH user = dbi.query("SELECT name FROM usr WHERE id='sam'") -%]
+* [% user.name %]
+[% END %]
+
+-- expect --
+* Simon Matthews
+
+-- test --
+[% USE db = DBI -%]
+[% db.connect(dsn, user, pass) -%]
+[% FOREACH user = db.query("SELECT name FROM usr WHERE id='hans'") -%]
+* [% user.name %]
+[% END %]
+
+-- expect --
+* Hans von Lengerke
+
+-- test --
+[% USE db = DBI(data_source => dsn, username => user, password => pass) -%]
+[% FOREACH user = db.query("SELECT name FROM usr WHERE id='mrp'") -%]
+* [% user.name %]
+[% END %]
+
+-- expect --
+* Martin Portman
+
+-- test --
+[% USE dbi -%]
+[% dbi.connect(dsn=dsn, user=user, pass=pass) -%]
+[% FOREACH user = dbi.query("SELECT name FROM usr WHERE id='abw'") -%]
+* [% user.name %]
+[% END %]
+
+-- expect --
+* Andy Wardley
+
+-- test --
+[% USE DBI -%]
+[% TRY;
+ DBI.query('blah blah');
+ CATCH;
+ error;
+ END
+%]
+-- expect --
+DBI error - no connection
+
+
+#------------------------------------------------------------------------
+# disconnect() and subsequent connect()
+#------------------------------------------------------------------------
+
+-- test --
+[% USE DBI(dsn, user, pass) -%]
+[% DBI.disconnect -%]
+[% TRY;
+ DBI.query('blah blah');
+ CATCH;
+ error;
+ END
+%]
+-- expect --
+DBI error - no connection
+
+-- test --
+[% USE DBI(dsn, user, pass) -%]
+[% DBI.disconnect -%]
+[% DBI.connect(dsn, user, pass) -%]
+[% FOREACH user = DBI.query("SELECT name FROM usr WHERE id='abw'") -%]
+* [% user.name %]
+[% END %]
+
+-- expect --
+* Andy Wardley
+
+
+#------------------------------------------------------------------------
+# validate 'loop' reference to iterator
+#------------------------------------------------------------------------
+
+-- test --
+[% USE dbi(dsn, user, pass) -%]
+[% FOREACH user = dbi.query('SELECT * FROM usr ORDER BY id') -%]
+[% loop.number %]: [% user.id %] - [% user.name %]
+[% END %]
+-- expect --
+1: abw - Andy Wardley
+2: hans - Hans von Lengerke
+3: mrp - Martin Portman
+4: sam - Simon Matthews
+
+-- test --
+# DBI plugin before TT 2.00 used 'count' instead of 'number'
+[% USE dbi(dsn, user, pass) -%]
+[% FOREACH user = dbi.query('SELECT * FROM usr ORDER BY id') -%]
+[% loop.count %]: [% user.id %] - [% user.name %]
+[% END %]
+-- expect --
+1: abw - Andy Wardley
+2: hans - Hans von Lengerke
+3: mrp - Martin Portman
+4: sam - Simon Matthews
+
+
+#------------------------------------------------------------------------
+# test 'loop' reference to iterator in nested queries
+#------------------------------------------------------------------------
+
+-- test --
+[% USE dbi(dsn, user, pass) -%]
+[% FOREACH group = dbi.query('SELECT * FROM grp
+ ORDER BY id') -%]
+Group [% loop.number %]: [% group.name %] ([% group.id %])
+[% FOREACH user = dbi.query("SELECT * FROM usr
+ WHERE grp='$group.id'
+ ORDER BY id") -%]
+ #[% loop.number %]: [% user.name %] ([% user.id %])
+[% END -%]
+[% END -%]
+
+-- expect --
+Group 1: The Bar Group (bar)
+ #1: Hans von Lengerke (hans)
+ #2: Martin Portman (mrp)
+Group 2: The Foo Group (foo)
+ #1: Andy Wardley (abw)
+ #2: Simon Matthews (sam)
+
+
+#------------------------------------------------------------------------
+# test do() to perform SQL queries without returning results
+#------------------------------------------------------------------------
+
+-- test --
+[% USE DBI(dsn, user, pass) -%]
+[% CALL DBI.do("INSERT INTO usr VALUES ('numb', 'Numb Nuts', 'bar')") -%]
+[% FOREACH user = DBI.query("SELECT * FROM usr
+ WHERE grp = 'bar'
+ ORDER BY id") -%]
+* [% user.name %] ([% user.id %])
+[% END %]
+
+-- expect --
+* Hans von Lengerke (hans)
+* Martin Portman (mrp)
+* Numb Nuts (numb)
+
+-- test --
+[% USE dbi(dsn, user, pass) -%]
+[% IF dbi.do("DELETE FROM usr WHERE id = 'numb'") -%]
+deleted the user
+[% ELSE -%]
+failed to delete the user
+[% END -%]
+[% FOREACH user = dbi.query("SELECT * FROM usr
+ WHERE grp = 'bar'
+ ORDER BY id") -%]
+* [% user.name %] ([% user.id %])
+[% END %]
+-- expect --
+deleted the user
+* Hans von Lengerke (hans)
+* Martin Portman (mrp)
+
+
+#------------------------------------------------------------------------
+# prepare()
+#------------------------------------------------------------------------
+
+-- test --
+[% USE dbi(dsn=dsn, user=user, pass=pass) -%]
+[% user_query = dbi.prepare('SELECT * FROM usr
+ WHERE grp = ?
+ ORDER BY id') -%]
+Foo:
+[% users = user_query.execute('foo') -%]
+[% FOREACH user = users -%]
+ #[% users.index %]: [% user.name %] ([% user.id %])
+[% END -%]
+
+Bar:
+[% users = user_query.execute('bar') -%]
+[% FOREACH user = users -%]
+ #[% users.index %]: [% user.name %] ([% user.id %])
+[% END -%]
+-- expect --
+Foo:
+ #0: Andy Wardley (abw)
+ #1: Simon Matthews (sam)
+
+Bar:
+ #0: Hans von Lengerke (hans)
+ #1: Martin Portman (mrp)
+
+
+-- test --
+[% USE dbi(dsn, user, pass) -%]
+[% group_query = dbi.prepare('SELECT * FROM grp
+ ORDER BY id') -%]
+[% user_query = dbi.prepare('SELECT * FROM usr
+ WHERE grp = ?
+ ORDER BY id') -%]
+[% groups = group_query.execute -%]
+[% FOREACH group = groups -%]
+Group [% groups.count %] : [% group.name %]
+[% users = user_query.execute(group.id) -%]
+[% FOREACH user = users -%]
+ User [% users.index %] : [% user.name %] ([% user.id %])
+[% END -%]
+[% END %]
+-- expect --
+Group 1 : The Bar Group
+ User 0 : Hans von Lengerke (hans)
+ User 1 : Martin Portman (mrp)
+Group 2 : The Foo Group
+ User 0 : Andy Wardley (abw)
+ User 1 : Simon Matthews (sam)
+
+
+-- test --
+[% USE dbi(dsn, user, pass) -%]
+[% CALL dbi.prepare('SELECT * FROM usr WHERE id = ?') -%]
+[% FOREACH uid = [ 'abw', 'sam' ] -%]
+===
+[% FOREACH user = dbi.execute(uid) -%]
+ * [% user.name %] ([% user.id %])
+[% END -%]
+===
+[% END %]
+
+-- expect --
+===
+ * Andy Wardley (abw)
+===
+===
+ * Simon Matthews (sam)
+===
+
+
+#------------------------------------------------------------------------
+# test that dbh can be passed as a named parameter and remains open
+#------------------------------------------------------------------------
+
+-- test --
+[% USE dbi(dbh => dbh) -%]
+[% FOREACH user = dbi.query("SELECT * FROM usr WHERE id = 'abw'") -%]
+* [% user.name %]
+[% END -%]
+[% dbi.connect(dsn, user, pass) -%]
+[% FOREACH user = dbi.query("SELECT * FROM usr WHERE id = 'abw'") -%]
+* [% user.name %]
+[% END -%]
+-- expect --
+* Andy Wardley
+* Andy Wardley
+
+
+
+
View
86 t/xpath.t
@@ -0,0 +1,86 @@
+#============================================================= -*-perl-*-
+#
+# t/xpath.t
+#
+# Test the XML::XPath plugin.
+#
+# Written by Andy Wardley <abw@kfs.org>
+#
+# Copyright (C) 1996-2000 Andy Wardley. All Rights Reserved.
+# Copyright (C) 1998-2000 Canon Research Centre Europe Ltd.
+#
+# This is free software; you can redistribute it and/or modify it
+# under the same terms as Perl itself.
+#
+# $Id$
+#
+#========================================================================
+
+use strict;
+use lib qw( lib ../lib );
+use Template;
+use Template::Test;
+use Cwd qw( abs_path );
+$^W = 1;
+
+eval "use XML::XPath";
+if ($@) {
+ print "1..0\n";
+ exit(0);
+}
+
+# account for script being run in distribution root or 't' directory
+my $file = abs_path( -d 't' ? 't/test/xml' : 'test/xml' );
+$file .= '/testfile.xml';
+
+test_expect(\*DATA, undef, { 'xmlfile' => $file });
+
+__END__
+-- test --
+[% TRY;
+ USE xpath = XML.XPath('no_such_file');
+ xpath.findvalue('/foo/bar');
+ CATCH;
+ error.info.split(':').0;
+ END
+%]
+-- expect --
+Couldn't open no_such_file
+
+-- test --
+[% USE xpath = XML.XPath(xmlfile) -%]
+[% FOREACH page = xpath.findnodes('/website/section/page') -%]
+page: [% page.getAttribute('title') %]
+[% END %]
+-- expect --
+page: The Foo Page
+page: The Bar Page
+page: The Baz Page
+
+
+-- test --
+[% xmltext = BLOCK %]
+<html>
+<body>
+<section id="foo">
+ This is the foo section, here is some <b>bold</b> text.
+</section>
+<section id="foo">
+ This is the bar section, here is some <i>italic</i> text
+</section>
+</body>
+</html>
+[% END -%]
+[% USE xpath = XML.XPath(xmltext) -%]
+...
+[% FOREACH section = xpath.findnodes('/html/body/section') -%]
+[% section.string_value %]
+[% END %]
+
+-- expect --
+...
+
+ This is the foo section, here is some bold text.
+
+
+ This is the bar section, here is some italic text

0 comments on commit 48634a1

Please sign in to comment.
Something went wrong with that request. Please try again.