ActiveRecord-Simple
view release on metacpan or search on metacpan
t/09-find.t view on Meta::CPAN
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use FindBin '$Bin';
use lib "$Bin/../lib";
use Data::Dumper;
use Test::More;
use DBI;
eval { require DBD::SQLite } or plan skip_all => 'Need DBD::SQLite for testing';
package Customer;
use parent 'ActiveRecord::Simple';
__PACKAGE__->table_name('customers');
__PACKAGE__->primary_key('id');
__PACKAGE__->columns(qw/id first_name second_name age email/);
#__PACKAGE__->has_one(info => 'CustomersInfo');
__PACKAGE__->mixins(
mixin => sub {
'SUM("id")'
},
);
package main;
my $dbh = DBI->connect("dbi:SQLite:dbname=:memory:","","")
or die DBI->errstr;
my $_INIT_SQL = q{
CREATE TABLE `customers` (
`id` int AUTO_INCREMENT,
`first_name` varchar(200) NULL,
`second_name` varchar(200) NOT NULL,
`age` tinyint(2) NULL,
`email` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
);
};
my $_DATA_SQL = q{
INSERT INTO `customers` (`id`, `first_name`, `second_name`, `age`, `email`)
VALUES
(1,'Bob','Dylan',NULL,'bob.dylan@aol.com'),
(2,'John','Doe',77,'john@doe.com'),
(3,'Bill','Clinton',50,'mynameisbill@gmail.com'),
(4,'Bob','Marley',NULL,'bob.marley@forever.com'),
(5,'','',NULL,'foo.bar@bazz.com'),
(6, 'Lady', 'Gaga', 666, 'gaga-o-la-la@bad.romance');
};
$dbh->do($_INIT_SQL);
$dbh->do($_DATA_SQL);
Customer->dbh($dbh);
my $finder = Customer->objects->find({ first_name => 'Bob' })->order_by('id');
isa_ok $finder, 'ActiveRecord::Simple::Find';
my @bobs = (1, 4); my $i = 0;
while (my $bob = $finder->next) {
ok $bob->id == $bobs[$i], 'next, bob.id == ' . $bobs[$i];
$i++;
}
$finder = Customer->objects->find;
while (my @customers_pair = $finder->next(2)) {
is scalar @customers_pair, 2, 'next(n) works good';
}
( run in 1.583 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )