DBIx-EAV

 view release on metacpan or  search on metacpan

t/resultset.t  view on Meta::CPAN

    like dies { $rs->find({ name => [qw/ A1 A2 /] }) }, qr/returned more than one entity/;
}


sub test_retrieval {

    empty_database($eav);

    my $rs = $eav->resultset('Artist');
    $rs->populate([{ name => 'A1' }, { name => 'A2' }]);

    # next()
    is $rs->next->get('name'), 'A1', 'next (1)';
    is $rs->next->get('name'), 'A2', 'next (2)';
    is $rs->next, undef, 'next (undef)';

    # reset()
    is $rs->reset->next->get('name'), 'A1', 'reset';

    # all()
    is [map { $_->get('name') } $rs->all ], [qw/ A1 A2 /], 'all in list context';
    my $all = $rs->all;
    is [map { $_->get('name') } @$all ], [qw/ A1 A2 /], 'all in scalar context';

    # first()
    is $rs->first->get('name'), 'A1', 'first';
    is $rs->first->get('name'), $rs->first->get('name'), 'first resets';
}


sub test_delete {

    empty_database($eav);

    my $rs = $eav->resultset('CD');
    $rs->populate([
        { title => 'CD1' },
        { title => 'CD2', tracks => [{ title => 'T1' }, { title => 'T2' }, { title => 'T3' }] }
    ]);

    $rs->delete;

    is $rs->count, 0, 'delete';
    is $eav->resultset("Track")->count, 3, 'delete leaves related entities';
}


sub test_delete_all {

    empty_database($eav);

    my $rs = $eav->resultset('CD');
    $rs->populate([
        { title => 'CD1' },
        { title => 'CD2', tracks => [{ title => 'T1' }, { title => 'T2' }, { title => 'T3' }] }
    ]);

    $rs->delete_all;

    is $rs->count, 0, 'delete_all';
    is $eav->resultset("Track")->count, 0, 'delete_all cascade delete';
}


sub test_count {
    my $rs = $eav->resultset('Artist');
    empty_database($eav);

    $rs->populate([ map { +{ name => 'A'.$_ }} 1..6 ]);
    $rs->populate([ map { +{ name => 'A'.$_ }} 1..6 ]);

    is $rs->count, 12, 'count()';
    ok $rs == 12, 'count() called in number context';
    is $rs->count({ name => [qw/ A2 A3 /]}), 4, 'count(\%where)';
    is $rs->count(undef, { select => ['name'], group_by => ['name'] }), 6, 'count() group_by';
    is $rs->count(undef, { select => ['name'], distinct => 1 }), 6, 'count() distinct';

    is $rs->search(undef, { limit => 5 })->count, 5, 'count() with limit';
    is $rs->search(undef, { limit => 5, offset => 10 })->count, 2, 'count() with limit + offset';
    is $rs->search(undef, { limit => 5, offset => 20 })->count, 0, 'count() with outbound offset';
}


sub test_related {

    my $a1 = $eav->resultset('Artist')->insert({
        name => 'Artist1',
        cds  => [
            {title => 'CD1', rating => 3, tracks => [{ title => 'Track1' }]},
            {title => 'CD2', rating => 6},
            {title => 'CD3', rating => 9}
        ]});

    my $a2 = $eav->resultset('Artist')->insert({
        name => 'Artist2',
        cds  => [{title => 'CD4'}, {title => 'CD5'}] });

    # CDs by artist
    my $cds = $eav->resultset('CD')->search({ artists => $a1 }, { order_by => { -desc => 'title' }});
    is [map { $_->get('title') } $cds->all], [qw/ CD3 CD2 CD1 /], 'fetch cds by artist';

    # CDs by multiple (or'ed) artists
    $cds = $eav->resultset('CD')->search({ artists => [$a1, $a2] }, { order_by => 'title, rating' });
    is [map { $_->get('title') } $cds->all], [qw/ CD1 CD2 CD3 CD4 CD5 /], "fetch cds by multiple (or'ed) artist";

    # cds via artists 'cds' rel
    is [map { $_->get('title') } $a1->get('cds')->all], [qw/ CD1 CD2 CD3 /], 'cds via artists rel';

    # find by related attr
    my $cd4 = $eav->resultset('CD')->find({ title => 'CD4' });
    is $eav->resultset('Artist')->find({ 'cds.id' => $cd4->id })->get('name'), 'Artist2', 'find by related static attr';

    # find by related attr
    is $eav->resultset('Artist')->find({ 'cds.title' => 'CD1' })->get('name'), 'Artist1', 'find by related attr';

    # artist by track name
    is $eav->resultset('Artist')->find({ 'cds.tracks.title' => 'Track1' })->get('name'), 'Artist1', 'find by deep related attr';
}


sub test_distinct {



( run in 0.589 second using v1.01-cache-2.11-cpan-5a3173703d6 )