[Bast-commits] r3370 - in trunk/DBIx-Class-InflateColumn-IP: . t t/lib t/lib/DBICTest t/lib/DBICTest/Schema

ilmari at dev.catalyst.perl.org ilmari at dev.catalyst.perl.org
Thu May 24 03:26:04 GMT 2007


Author: ilmari
Date: 2007-05-24 03:26:03 +0100 (Thu, 24 May 2007)
New Revision: 3370

Added:
   trunk/DBIx-Class-InflateColumn-IP/t/01-ip.t
   trunk/DBIx-Class-InflateColumn-IP/t/lib/
   trunk/DBIx-Class-InflateColumn-IP/t/lib/DBICTest.pm
   trunk/DBIx-Class-InflateColumn-IP/t/lib/DBICTest/
   trunk/DBIx-Class-InflateColumn-IP/t/lib/DBICTest/Schema.pm
   trunk/DBIx-Class-InflateColumn-IP/t/lib/DBICTest/Schema/
   trunk/DBIx-Class-InflateColumn-IP/t/lib/DBICTest/Schema/Host.pm
   trunk/DBIx-Class-InflateColumn-IP/t/lib/sqlite.sql
Modified:
   trunk/DBIx-Class-InflateColumn-IP/
   trunk/DBIx-Class-InflateColumn-IP/MANIFEST
   trunk/DBIx-Class-InflateColumn-IP/MANIFEST.SKIP
   trunk/DBIx-Class-InflateColumn-IP/README
   trunk/DBIx-Class-InflateColumn-IP/t/
Log:
Add tests


Property changes on: trunk/DBIx-Class-InflateColumn-IP
___________________________________________________________________
Name: svn:ignore
   + RCS
CVS
*,v
.svn
blib
pm_to_blib
MakeMaker-*
Makefile
Makefile.old
Build.PL
Build.bat
*.db
blibdirs.ts
*.gz
*~
*.bak
TODO
META.yml
inc

Modified: trunk/DBIx-Class-InflateColumn-IP/MANIFEST
===================================================================
--- trunk/DBIx-Class-InflateColumn-IP/MANIFEST	2007-05-23 19:43:13 UTC (rev 3369)
+++ trunk/DBIx-Class-InflateColumn-IP/MANIFEST	2007-05-24 02:26:03 UTC (rev 3370)
@@ -1,11 +1,27 @@
+Changes
+inc/Module/AutoInstall.pm
+inc/Module/Install.pm
+inc/Module/Install/AutoInstall.pm
+inc/Module/Install/Base.pm
+inc/Module/Install/Can.pm
+inc/Module/Install/Fetch.pm
+inc/Module/Install/Include.pm
+inc/Module/Install/Makefile.pm
+inc/Module/Install/Metadata.pm
+inc/Module/Install/Win32.pm
+inc/Module/Install/WriteAll.pm
+lib/DBIx/Class/InflateColumn/IP.pm
 Makefile.PL
-Changes
 MANIFEST
 MANIFEST.SKIP
-META.yml # Will be created by "make dist"
+META.yml			# Will be created by "make dist"
 README
-lib/DBIx/Class/InflateColumn/IP.pm
 t/00-load.t
+t/01-ip.t
 t/boilerplate.t
+t/lib/DBICTest.pm
+t/lib/DBICTest/Schema.pm
+t/lib/DBICTest/Schema/Host.pm
+t/lib/sqlite.sql
 t/pod-coverage.t
 t/pod.t

Modified: trunk/DBIx-Class-InflateColumn-IP/MANIFEST.SKIP
===================================================================
--- trunk/DBIx-Class-InflateColumn-IP/MANIFEST.SKIP	2007-05-23 19:43:13 UTC (rev 3369)
+++ trunk/DBIx-Class-InflateColumn-IP/MANIFEST.SKIP	2007-05-24 02:26:03 UTC (rev 3370)
@@ -16,5 +16,6 @@
 ^blibdirs\.ts
 \.gz
 ~$
+\.bak$
 ^\.cvsignore
 TODO

Modified: trunk/DBIx-Class-InflateColumn-IP/README
===================================================================
--- trunk/DBIx-Class-InflateColumn-IP/README	2007-05-23 19:43:13 UTC (rev 3369)
+++ trunk/DBIx-Class-InflateColumn-IP/README	2007-05-24 02:26:03 UTC (rev 3370)
@@ -1,12 +1,12 @@
 NAME
     DBIx::Class::InflateColumn::IP - Auto-create NetAddr::IP objects from
-    integer columns.
+    columns.
 
 VERSION
     Version 0.01
 
 SYNOPSIS
-    Load this component and declare the relevant columns as IP addresses.
+    Load this component and declare integer columns as IP addresses.
 
         package Host;
         __PACKAGE__->load_components(qw/InflateColumn::IP/);
@@ -18,7 +18,7 @@
             }
         );
 
-    Then you can treat the specified column as a Data::Currency object.
+    Then you can treat the specified column as a NetAddr::IP object.
 
         print 'IP address: ', $host->ip_address->ip;
         print 'Address type: ', $host->ip_address->iptype;


Property changes on: trunk/DBIx-Class-InflateColumn-IP/t
___________________________________________________________________
Name: svn:ignore
   + var

Added: trunk/DBIx-Class-InflateColumn-IP/t/01-ip.t
===================================================================
--- trunk/DBIx-Class-InflateColumn-IP/t/01-ip.t	                        (rev 0)
+++ trunk/DBIx-Class-InflateColumn-IP/t/01-ip.t	2007-05-24 02:26:03 UTC (rev 3370)
@@ -0,0 +1,34 @@
+#!perl -T  
+use lib qw(t/lib);
+use DBICTest;
+use Test::More tests => 6;
+use NetAddr::IP;
+
+my $schema = DBICTest->init_schema();
+
+my $host_rs = $schema->resultset('Host');
+
+my $localhost = $host_rs->find('localhost');
+
+isa_ok($localhost->ip, 'NetAddr::IP', 'inflated to right class');
+is($localhost->ip, '127.0.0.1/32', 'address correctly inflated');
+
+TODO: {
+    local $TODO = "find by object doesn't work yet";
+
+    $localhost = $host_rs->find(NetAddr::IP->new('127.0.0.1'), { key => 'ip' });
+
+    ok($localhost, 'find by object returned a row');
+}
+
+SKIP: {
+    skip 'no object to check' => 1 unless $localhost;
+
+    is($localhost->hostname, 'localhost', 'right row found by object');
+}
+
+my $ip = NetAddr::IP->new('192.168.0.1');
+my $host = $host_rs->create({ hostname => 'foo', ip => $ip });
+
+isa_ok($host, 'DBICTest::Schema::Host', 'create with object');
+is($host->get_column('ip'), $ip->numeric, 'address correctly deflated');

Added: trunk/DBIx-Class-InflateColumn-IP/t/lib/DBICTest/Schema/Host.pm
===================================================================
--- trunk/DBIx-Class-InflateColumn-IP/t/lib/DBICTest/Schema/Host.pm	                        (rev 0)
+++ trunk/DBIx-Class-InflateColumn-IP/t/lib/DBICTest/Schema/Host.pm	2007-05-24 02:26:03 UTC (rev 3370)
@@ -0,0 +1,24 @@
+package # hide from PAUSE
+    DBICTest::Schema::Host;
+
+use base qw/DBIx::Class/;
+
+__PACKAGE__->load_components(qw/InflateColumn::IP Core/);
+__PACKAGE__->table('host');
+
+__PACKAGE__->add_columns(
+    hostname => {
+        data_type   => 'text',
+        is_nullable => 0,
+    },
+    ip => {
+        data_type   => 'integer',
+        is_nullable => 0,
+        is_ip       => 1,
+    }
+);
+
+__PACKAGE__->set_primary_key('hostname');
+__PACKAGE__->add_unique_constraint(ip => [ qw/ip/ ]);
+
+1;

Added: trunk/DBIx-Class-InflateColumn-IP/t/lib/DBICTest/Schema.pm
===================================================================
--- trunk/DBIx-Class-InflateColumn-IP/t/lib/DBICTest/Schema.pm	                        (rev 0)
+++ trunk/DBIx-Class-InflateColumn-IP/t/lib/DBICTest/Schema.pm	2007-05-24 02:26:03 UTC (rev 3370)
@@ -0,0 +1,8 @@
+package # hide from PAUSE
+    DBICTest::Schema;
+
+use base qw/DBIx::Class::Schema/;
+
+__PACKAGE__->load_classes(qw/Host/);
+
+1;

Added: trunk/DBIx-Class-InflateColumn-IP/t/lib/DBICTest.pm
===================================================================
--- trunk/DBIx-Class-InflateColumn-IP/t/lib/DBICTest.pm	                        (rev 0)
+++ trunk/DBIx-Class-InflateColumn-IP/t/lib/DBICTest.pm	2007-05-24 02:26:03 UTC (rev 3370)
@@ -0,0 +1,113 @@
+package # hide from PAUSE 
+    DBICTest;
+
+use strict;
+use warnings;
+use DBICTest::Schema;
+
+=head1 NAME
+
+DBICTest - Library to be used by DBIx::Class test scripts.
+
+=head1 SYNOPSIS
+
+  use lib qw(t/lib);
+  use DBICTest;
+  use Test::More;
+  
+  my $schema = DBICTest->init_schema();
+
+=head1 DESCRIPTION
+
+This module provides the basic utilities to write tests against 
+DBIx::Class.
+
+=head1 METHODS
+
+=head2 init_schema
+
+  my $schema = DBICTest->init_schema(
+    no_deploy=>1,
+    no_populate=>1,
+  );
+
+This method removes the test SQLite database in t/var/DBIxClass.db 
+and then creates a new, empty database.
+
+This method will call deploy_schema() by default, unless the 
+no_deploy flag is set.
+
+Also, by default, this method will call populate_schema() by 
+default, unless the no_deploy or no_populate flags are set.
+
+=cut
+
+sub init_schema {
+    my $self = shift;
+    my %args = @_;
+    my $db_file = "t/var/DBIxClass.db";
+
+    unlink($db_file) if -e $db_file;
+    unlink($db_file . "-journal") if -e $db_file . "-journal";
+    mkdir("t/var") unless -d "t/var";
+
+    my $dsn = $ENV{"DBICTEST_DSN"} || "dbi:SQLite:${db_file}";
+    my $dbuser = $ENV{"DBICTEST_DBUSER"} || '';
+    my $dbpass = $ENV{"DBICTEST_DBPASS"} || '';
+
+   my $schema = DBICTest::Schema->compose_connection('DBICTest' => $dsn, $dbuser, $dbpass);
+    $schema->storage->on_connect_do(['PRAGMA synchronous = OFF']);
+    if ( !$args{no_deploy} ) {
+        __PACKAGE__->deploy_schema( $schema );
+        __PACKAGE__->populate_schema( $schema ) if( !$args{no_populate} );
+    }
+    return $schema;
+}
+
+=head2 deploy_schema
+
+  DBICTest->deploy_schema( $schema );
+
+This method does one of two things to the schema.  It can either call 
+the experimental $schema->deploy() if the DBICTEST_SQLT_DEPLOY environment 
+variable is set, otherwise the default is to read in the t/lib/sqlite.sql 
+file and execute the SQL within. Either way you end up with a fresh set 
+of tables for testing.
+
+=cut
+
+sub deploy_schema {
+    my $self = shift;
+    my $schema = shift;
+
+    if ($ENV{"DBICTEST_SQLT_DEPLOY"}) {
+        return $schema->deploy();
+    } else {
+        open IN, "t/lib/sqlite.sql";
+        my $sql;
+        { local $/ = undef; $sql = <IN>; }
+        close IN;
+        ($schema->storage->dbh->do($_) || print "Error on SQL: $_\n") for split(/;\n/, $sql);
+    }
+}
+
+=head2 populate_schema
+
+  DBICTest->populate_schema( $schema );
+
+After you deploy your schema you can use this method to populate 
+the tables with test data.
+
+=cut
+
+sub populate_schema {
+    my $self = shift;
+    my $schema = shift;
+
+    $schema->populate('Host', [
+        [ qw/hostname ip/ ],
+        [ 'localhost', 2130706433 ],
+    ]);
+}
+
+1;

Added: trunk/DBIx-Class-InflateColumn-IP/t/lib/sqlite.sql
===================================================================
--- trunk/DBIx-Class-InflateColumn-IP/t/lib/sqlite.sql	                        (rev 0)
+++ trunk/DBIx-Class-InflateColumn-IP/t/lib/sqlite.sql	2007-05-24 02:26:03 UTC (rev 3370)
@@ -0,0 +1,4 @@
+CREATE TABLE host (
+	hostname TEXT NOT NULL PRIMARY KEY,
+	ip INTEGER NOT NULL UNIQUE
+);




More information about the Bast-commits mailing list