DBIx-Simple-Class

 view release on metacpan or  search on metacpan

t/01-dbix-simple-class-sqlite.t  view on Meta::CPAN

#!perl

use 5.10.1;
use strict;
use warnings;
use utf8;
use Test::More;


BEGIN {
  eval { require DBD::SQLite; 1 }
    or plan skip_all => 'DBD::SQLite required';
  eval { DBD::SQLite->VERSION gt '1.37' }
    or plan skip_all => 'DBD::SQLite >= 1.37 required';
  use File::Basename 'dirname';
  use Cwd;
  use lib (Cwd::abs_path(dirname(__FILE__) . '/..') . '/examples/lib');
}
use My;
use My::Group;
use My::User;

local $Params::Check::VERBOSE = 0;

#Suppress some warnings from DBIx::Simple::Class during tests.
local $SIG{__WARN__} = sub {
  if ($_[0] =~ /(generated accessors|is not such field|to build)/) {
    my ($package, $filename, $line, $subroutine) = caller(2);
    ok(1, $subroutine . ' warns OK');
  }
  else {
    warn $_[0];
  }
};


my $DSC = 'DBIx::Simple::Class';

# In memory database! No file permission troubles, no I/O slowness.
# http://use.perl.org/~tomhukins/journal/31457 ++

my $dbix = DBIx::Simple->connect('dbi:SQLite:dbname=:memory:', {sqlite_unicode => 1});

#test DBIx::Simple instantiation
like((eval { $DSC->dbix }, $@), qr/not instantiated/);
like((eval { $DSC->dbix('') }, $@), qr/not instantiated/);
isa_ok(ref($DSC->dbix($dbix)), 'DBIx::Simple');
isa_ok(ref($DSC->dbix),        'DBIx::Simple');

my $groups_table = <<"T";
CREATE TEMPORARY TABLE groups(
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  group_name VARCHAR(12)
  )
T

$dbix->query($groups_table);


my $users_table = <<"T";
CREATE TEMPORARY TABLE users(
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  group_id INT DEFAULT 1,
  login_name VARCHAR(12),
  login_password VARCHAR(100), 
  disabled INT DEFAULT 1
  )
T

$dbix->query($users_table);

#$DSC->DEBUG(1);
isa_ok(ref(My->dbix($dbix)), 'DBIx::Simple');
is(My->dbix, $DSC->dbix, 'same instance');
ok(!My->BUILD(), 'nothing to build');
ok(!(My->DEBUG(1) && My->BUILD()), 'nothing to build debugged');
My->DEBUG(0);
my $user;
my $password = time;
like(
  (eval { $user = My::User->new() }, $@),
  qr/Required option/,
  '"Required option" ok'
);


ok($user = My::User->new(login_password => $password));

like((eval { $user->BUILD() }, $@), qr/Call this method as/, 'BUILD() ok');
is(
  My::User->BUILD(),
  $DSC->_attributes_made->{'My::User'},
  'if (eval $code) in BUILD() ok'
);
isa_ok($user, $DSC);

#defaults
is($user->id, undef, 'id is undefined ok');
is($user->group_id, $user->CHECKS->{group_id}{default}, 'group_id default ok');
delete $user->CHECKS->{group_id}{default};
delete $user->{data}->{group_id};
is($user->group_id, $user->CHECKS->{group_id}{default}, 'group_id default ok');



( run in 0.600 second using v1.01-cache-2.11-cpan-39bf76dae61 )