test suite added, partly lifted from DBIx::Class
Luke Saunders [Wed, 30 Jan 2008 23:05:04 +0000 (23:05 +0000)]
12 files changed:
t/02-dump-basic.t [new file with mode: 0644]
t/lib/DBICTest.pm [new file with mode: 0755]
t/lib/DBICTest/Schema.pm [new file with mode: 0644]
t/lib/DBICTest/Schema/Artist.pm [new file with mode: 0644]
t/lib/DBICTest/Schema/CD.pm [new file with mode: 0644]
t/lib/DBICTest/Schema/CD_to_Producer.pm [new file with mode: 0644]
t/lib/DBICTest/Schema/Producer.pm [new file with mode: 0644]
t/lib/DBICTest/Schema/Tag.pm [new file with mode: 0644]
t/lib/DBICTest/Schema/Track.pm [new file with mode: 0644]
t/lib/sqlite.sql [new file with mode: 0644]
t/var/configs/sample.json [new file with mode: 0644]
t/var/configs/simple.json [new file with mode: 0644]

diff --git a/t/02-dump-basic.t b/t/02-dump-basic.t
new file mode 100644 (file)
index 0000000..11c40f2
--- /dev/null
@@ -0,0 +1,35 @@
+#!perl
+
+use DBIx::Class::Fixtures;
+use Test::More tests => 3;
+use DBICTest;
+use Path::Class;
+use Data::Dumper; 
+
+# set up and populate schema
+ok(my $schema = DBICTest->init_schema(), 'got schema');
+
+my $config_dir = 't/var/configs';
+
+# do dump
+ok(my $fixtures = DBIx::Class::Fixtures->new({ config_dir => $config_dir, debug => 0 }), 'object created with correct config dir');
+ok($fixtures->dump({ config => 'simple.json', schema => $schema, directory => 't/var/fixtures' }), 'simple dump executed okay');
+
+# check dump is okay
+my $dir = dir('t/var/fixtures/artist');
+ok(-e 't/var/fixtures/artist', 'artist directory created');
+
+my @children = $dir->children;
+is(scalar(@children), 1, 'right number of fixtures created');
+
+my $fix_file = $children[0];
+my $HASH1; eval($fix_file->slurp());
+is(ref $HASH1, 'HASH', 'fixture evals into hash');
+
+is_deeply([sort $schema->source('Artist')->columns], [sort keys %{$HASH1}], 'fixture has correct keys');
+
+my $artist = $schema->resultset('Artist')->find($HASH1->{artistid});
+is_deeply({$artist->get_columns}, $HASH1, 'dumped fixture is equivalent to artist row');
+
+$schema->resultset('Artist')->delete; # so we can create the row again on the next line
+ok($schema->resultset('Artist')->create($HASH1), 'new dbic row created from fixture');
diff --git a/t/lib/DBICTest.pm b/t/lib/DBICTest.pm
new file mode 100755 (executable)
index 0000000..4133223
--- /dev/null
@@ -0,0 +1,171 @@
+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{"FIXTURETEST_DSN"} || "dbi:SQLite:${db_file}";
+    my $dbuser = $ENV{"FIXTURETEST_DBUSER"} || '';
+    my $dbpass = $ENV{"FIXTURETEST_DBPASS"} || '';
+
+    my $schema;
+
+    my @connect_info = ($dsn, $dbuser, $dbpass, { AutoCommit => 1 });
+
+    if ($args{compose_connection}) {
+      $schema = DBICTest::Schema->compose_connection(
+                  'DBICTest', @connect_info
+                );
+    } else {
+      $schema = DBICTest::Schema->compose_namespace('DBICTest')
+                                ->connect(@connect_info);
+    }
+    $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 );
+
+=cut
+
+sub deploy_schema {
+    my $self = shift;
+    my $schema = shift;
+
+    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('Artist', [
+        [ qw/artistid name/ ],
+        [ 1, 'Caterwauler McCrae' ],
+        [ 2, 'Random Boy Band' ],
+        [ 3, 'We Are Goth' ],
+    ]);
+
+    $schema->populate('CD', [
+        [ qw/cdid artist title year/ ],
+        [ 1, 1, "Spoonful of bees", 1999 ],
+        [ 2, 1, "Forkful of bees", 2001 ],
+        [ 3, 1, "Caterwaulin' Blues", 1997 ],
+        [ 4, 2, "Generic Manufactured Singles", 2001 ],
+        [ 5, 3, "Come Be Depressed With Us", 1998 ],
+    ]);
+
+    $schema->populate('Tag', [
+        [ qw/tagid cd tag/ ],
+        [ 1, 1, "Blue" ],
+        [ 2, 2, "Blue" ],
+        [ 3, 3, "Blue" ],
+        [ 4, 5, "Blue" ],
+        [ 5, 2, "Cheesy" ],
+        [ 6, 4, "Cheesy" ],
+        [ 7, 5, "Cheesy" ],
+        [ 8, 2, "Shiny" ],
+        [ 9, 4, "Shiny" ],
+    ]);
+
+    $schema->populate('Producer', [
+        [ qw/producerid name/ ],
+        [ 1, 'Matt S Trout' ],
+        [ 2, 'Bob The Builder' ],
+        [ 3, 'Fred The Phenotype' ],
+    ]);
+
+    $schema->populate('CD_to_Producer', [
+        [ qw/cd producer/ ],
+        [ 1, 1 ],
+        [ 1, 2 ],
+        [ 1, 3 ],
+    ]);
+
+    $schema->populate('Track', [
+        [ qw/trackid cd  position title/ ],
+        [ 4, 2, 1, "Stung with Success"],
+        [ 5, 2, 2, "Stripy"],
+        [ 6, 2, 3, "Sticky Honey"],
+        [ 7, 3, 1, "Yowlin"],
+        [ 8, 3, 2, "Howlin"],
+        [ 9, 3, 3, "Fowlin"],
+        [ 10, 4, 1, "Boring Name"],
+        [ 11, 4, 2, "Boring Song"],
+        [ 12, 4, 3, "No More Ideas"],
+        [ 13, 5, 1, "Sad"],
+        [ 14, 5, 2, "Under The Weather"],
+        [ 15, 5, 3, "Suicidal"],
+        [ 16, 1, 1, "The Bees Knees"],
+        [ 17, 1, 2, "Apiary"],
+        [ 18, 1, 3, "Beehind You"],
+    ]);
+}
+
+1;
diff --git a/t/lib/DBICTest/Schema.pm b/t/lib/DBICTest/Schema.pm
new file mode 100644 (file)
index 0000000..350eecc
--- /dev/null
@@ -0,0 +1,10 @@
+package # hide from PAUSE
+    DBICTest::Schema;
+
+use base qw/DBIx::Class::Schema/;
+
+no warnings qw/qw/;
+
+__PACKAGE__->load_classes(qw/Artist CD Track Tag Producer CD_to_Producer/);
+
+1;
diff --git a/t/lib/DBICTest/Schema/Artist.pm b/t/lib/DBICTest/Schema/Artist.pm
new file mode 100644 (file)
index 0000000..594a76d
--- /dev/null
@@ -0,0 +1,25 @@
+package # hide from PAUSE 
+    DBICTest::Schema::Artist;
+
+use base 'DBIx::Class::Core';
+
+__PACKAGE__->table('artist');
+__PACKAGE__->add_columns(
+  'artistid' => {
+    data_type => 'integer',
+    is_auto_increment => 1,
+  },
+  'name' => {
+    data_type => 'varchar',
+    size      => 100,
+    is_nullable => 1,
+  },
+);
+__PACKAGE__->set_primary_key('artistid');
+
+__PACKAGE__->has_many(
+    cds => 'DBICTest::Schema::CD', undef,
+    { order_by => 'year' },
+);
+
+1;
diff --git a/t/lib/DBICTest/Schema/CD.pm b/t/lib/DBICTest/Schema/CD.pm
new file mode 100644 (file)
index 0000000..a6f084f
--- /dev/null
@@ -0,0 +1,44 @@
+package # hide from PAUSE 
+    DBICTest::Schema::CD;
+
+use base 'DBIx::Class::Core';
+
+__PACKAGE__->table('cd');
+__PACKAGE__->add_columns(
+  'cdid' => {
+    data_type => 'integer',
+    is_auto_increment => 1,
+  },
+  'artist' => {
+    data_type => 'integer',
+  },
+  'title' => {
+    data_type => 'varchar',
+    size      => 100,
+  },
+  'year' => {
+    data_type => 'varchar',
+    size      => 100,
+  },
+);
+__PACKAGE__->set_primary_key('cdid');
+__PACKAGE__->add_unique_constraint([ qw/artist title/ ]);
+
+__PACKAGE__->belongs_to( artist => 'DBICTest::Schema::Artist' );
+
+__PACKAGE__->has_many( tracks => 'DBICTest::Schema::Track' );
+__PACKAGE__->has_many(
+    tags => 'DBICTest::Schema::Tag', undef,
+    { order_by => 'tag' },
+);
+__PACKAGE__->has_many(
+    cd_to_producer => 'DBICTest::Schema::CD_to_Producer' => 'cd'
+);
+
+__PACKAGE__->many_to_many( producers => cd_to_producer => 'producer' );
+__PACKAGE__->many_to_many(
+    producers_sorted => cd_to_producer => 'producer',
+    { order_by => 'producer.name' },
+);
+
+1;
diff --git a/t/lib/DBICTest/Schema/CD_to_Producer.pm b/t/lib/DBICTest/Schema/CD_to_Producer.pm
new file mode 100644 (file)
index 0000000..117a590
--- /dev/null
@@ -0,0 +1,23 @@
+package # hide from PAUSE 
+    DBICTest::Schema::CD_to_Producer;
+
+use base 'DBIx::Class::Core';
+
+__PACKAGE__->table('cd_to_producer');
+__PACKAGE__->add_columns(
+  cd => { data_type => 'integer' },
+  producer => { data_type => 'integer' },
+);
+__PACKAGE__->set_primary_key(qw/cd producer/);
+
+__PACKAGE__->belongs_to(
+  'cd', 'DBICTest::Schema::CD',
+  { 'foreign.cdid' => 'self.cd' }
+);
+
+__PACKAGE__->belongs_to(
+  'producer', 'DBICTest::Schema::Producer',
+  { 'foreign.producerid' => 'self.producer' }
+);
+
+1;
diff --git a/t/lib/DBICTest/Schema/Producer.pm b/t/lib/DBICTest/Schema/Producer.pm
new file mode 100644 (file)
index 0000000..036f9f2
--- /dev/null
@@ -0,0 +1,20 @@
+package # hide from PAUSE 
+    DBICTest::Schema::Producer;
+
+use base 'DBIx::Class::Core';
+
+__PACKAGE__->table('producer');
+__PACKAGE__->add_columns(
+  'producerid' => {
+    data_type => 'integer',
+    is_auto_increment => 1
+  },
+  'name' => {
+    data_type => 'varchar',
+    size      => 100,
+  },
+);
+__PACKAGE__->set_primary_key('producerid');
+__PACKAGE__->add_unique_constraint(prod_name => [ qw/name/ ]);
+
+1;
diff --git a/t/lib/DBICTest/Schema/Tag.pm b/t/lib/DBICTest/Schema/Tag.pm
new file mode 100644 (file)
index 0000000..b75c2ef
--- /dev/null
@@ -0,0 +1,24 @@
+package # hide from PAUSE 
+    DBICTest::Schema::Tag;
+
+use base qw/DBIx::Class::Core/;
+
+__PACKAGE__->table('tags');
+__PACKAGE__->add_columns(
+  'tagid' => {
+    data_type => 'integer',
+    is_auto_increment => 1,
+  },
+  'cd' => {
+    data_type => 'integer',
+  },
+  'tag' => {
+    data_type => 'varchar',
+    size      => 100,
+  },
+);
+__PACKAGE__->set_primary_key('tagid');
+
+__PACKAGE__->belongs_to( cd => 'DBICTest::Schema::CD' );
+
+1;
diff --git a/t/lib/DBICTest/Schema/Track.pm b/t/lib/DBICTest/Schema/Track.pm
new file mode 100644 (file)
index 0000000..3f224d5
--- /dev/null
@@ -0,0 +1,37 @@
+package # hide from PAUSE 
+    DBICTest::Schema::Track;
+
+use base 'DBIx::Class::Core';
+__PACKAGE__->load_components(qw/InflateColumn::DateTime/);
+
+__PACKAGE__->table('track');
+__PACKAGE__->add_columns(
+  'trackid' => {
+    data_type => 'integer',
+    is_auto_increment => 1,
+  },
+  'cd' => {
+    data_type => 'integer',
+  },
+  'position' => {
+    data_type => 'integer',
+    accessor => 'pos',
+  },
+  'title' => {
+    data_type => 'varchar',
+    size      => 100,
+  },
+  last_updated_on => {
+    data_type => 'datetime',
+    accessor => 'updated_date',
+    is_nullable => 1
+  },
+);
+__PACKAGE__->set_primary_key('trackid');
+
+__PACKAGE__->add_unique_constraint([ qw/cd position/ ]);
+__PACKAGE__->add_unique_constraint([ qw/cd title/ ]);
+
+__PACKAGE__->belongs_to( cd => 'DBICTest::Schema::CD' );
+
+1;
diff --git a/t/lib/sqlite.sql b/t/lib/sqlite.sql
new file mode 100644 (file)
index 0000000..1e21627
--- /dev/null
@@ -0,0 +1,63 @@
+-- 
+-- Created by SQL::Translator::Producer::SQLite
+-- Created on Tue Aug  8 01:53:20 2006
+-- 
+BEGIN TRANSACTION;
+
+--
+-- Table: cd_to_producer
+--
+CREATE TABLE cd_to_producer (
+  cd integer NOT NULL,
+  producer integer NOT NULL,
+  PRIMARY KEY (cd, producer)
+);
+
+--
+-- Table: artist
+--
+CREATE TABLE artist (
+  artistid INTEGER PRIMARY KEY NOT NULL,
+  name varchar(100)
+);
+
+
+--
+-- Table: cd
+--
+CREATE TABLE cd (
+  cdid INTEGER PRIMARY KEY NOT NULL,
+  artist integer NOT NULL,
+  title varchar(100) NOT NULL,
+  year varchar(100) NOT NULL
+);
+
+--
+-- Table: track
+--
+CREATE TABLE track (
+  trackid INTEGER PRIMARY KEY NOT NULL,
+  cd integer NOT NULL,
+  position integer NOT NULL,
+  title varchar(100) NOT NULL,
+  last_updated_on datetime NULL
+);
+
+--
+-- Table: tags
+--
+CREATE TABLE tags (
+  tagid INTEGER PRIMARY KEY NOT NULL,
+  cd integer NOT NULL,
+  tag varchar(100) NOT NULL
+);
+
+--
+-- Table: producer
+--
+CREATE TABLE producer (
+  producerid INTEGER PRIMARY KEY NOT NULL,
+  name varchar(100) NOT NULL
+);
+
+COMMIT;
diff --git a/t/var/configs/sample.json b/t/var/configs/sample.json
new file mode 100644 (file)
index 0000000..e218d4c
--- /dev/null
@@ -0,0 +1,41 @@
+{
+        might_have: {
+            fetch: 0
+        },
+        has_many: {
+            fetch: 0
+        },
+        sets: [{
+            class: 'User',
+            cond: { photo_count: { '<': 5 } },
+            quantity: 3,
+            datetime_relative: today,
+            fetch: [{ 
+                rel: comments,
+                cond: { comments: { '!=': 'faks' }},
+                quantity: 2
+            }]
+        }, {
+            class: 'User',
+            cond: { photo_count: { '>=': 5 } },
+            quantity: 3
+        }, {
+            class: 'Video',
+            quantity: 3
+        }, {
+            class: 'FeaturedVideo',
+            quantity: 3
+        }],
+        rules: {
+            'User': {
+                cond: { video_count: { '>=': 5 } }
+            },
+            'Video': {
+                fetch: [{
+                    rel: user_videos,
+                    cond: { me.fk_user_id: '\= video.fk_owner_id' },
+                    join: 'video'
+                }]
+            }
+        }
+}
\ No newline at end of file
diff --git a/t/var/configs/simple.json b/t/var/configs/simple.json
new file mode 100644 (file)
index 0000000..7abd148
--- /dev/null
@@ -0,0 +1,12 @@
+{
+        might_have: {
+            fetch: 0
+        },
+        has_many: {
+            fetch: 0
+        },
+        sets: [{
+            class: 'Artist',
+            quantity: 1
+        }]
+}
\ No newline at end of file