DBIx-QuickORM

 view release on metacpan or  search on metacpan

worktrees/audit-fixes-master/t/handle/one.t  view on Meta::CPAN

use Test2::V0 -target => 'DBIx::QuickORM', '!meta', '!pass';
use DBIx::QuickORM;

use Scalar::Util qw/weaken/;

use lib 't/lib';
use DBIx::QuickORM::Test;

sub SCHEMA_DIR { 't/handle/schema' }

do_for_all_dbs {
    my $db = shift;

    db mydb => sub {
        dialect curdialect();
        db_name 'quickdb';
        connect sub { $db->connect };
    };

    orm my_orm => sub {
        db 'mydb';
        autofill;
    };

    ok(my $orm = orm('my_orm')->connect, "Got a connection");
    my $h = $orm->handle('example');
    ok(my $row = $h->insert({name => 'a'}), "Inserted a row");
    is($row->field('id'), 1, "Got generated primary key");

    my $row_clone = $h->one({id => 1});
    ref_is($row_clone, $row, "Got the same ref using one({id => 1})");

    my $row_clone2 = $h->one(id => 1);
    ref_is($row_clone2, $row, "Got the same ref using one(id => 1)");

    my $weak = $row;
    weaken($weak);
    $row        = undef;
    $row_clone  = undef;
    $row_clone2 = undef;
    ok(!defined($weak), "Previous row expired once all references were cleared");

    $row = $h->one({name => 'a'});
    is($row->field('id'), 1, "Got the right ID");

    my $data = $h->data_only->one({id => 1});
    ref_ok($data, 'HASH', "Got a hashref");
    like(
        $data,
        {id => 1, name => 'a'},
        "Got just the data",
    );

    $h->insert({name => 'dup1', xxx => 'dupz'});
    $h->insert({name => 'dup2', xxx => 'dupz'});
    like(
        dies { $h->one({xxx => 'dupz'}) },
        qr/Expected only 1 row, but got more than one/,
        "sync one() croaks when more than one row matches",
    );

    # sqlite does not support async
    unless (curdialect() =~ m/sqlite|duckdb/i) {
        subtest async => sub {
            my $h = $orm->handle('example');
            $h->insert({name => 'b'});

            $h = $h->async;
            my $row = $h->one({name => 'b'});
            isa_ok($row, ['DBIx::QuickORM::Row::Async'], "Got an async row");
            wait_ready($row);
            is($row->field('name'), 'b', "Got field 'b' value");

            isa_ok($row, ['DBIx::QuickORM::Row'], "Row is 'normal'");
        };

        subtest aside => sub {
            my $h = $orm->handle('example');
            $h->insert({name => 'c'});

            $h = $h->aside;
            my $row = $h->one({name => 'c'});
            isa_ok($row, ['DBIx::QuickORM::Row::Async'], "Got an async row");
            wait_ready($row);
            is($row->field('name'), 'c', "Got field 'c' value");

            isa_ok($row, ['DBIx::QuickORM::Row'], "Row is 'normal'");
        };

        subtest forked => sub {
            my $h = $orm->handle('example');
            $h->insert({name => 'd'});

            $h = $h->forked;
            my $row = $h->one({name => 'd'});
            isa_ok($row, ['DBIx::QuickORM::Row::Async'], "Got an async row");
            wait_ready($row);



( run in 0.466 second using v1.01-cache-2.11-cpan-995e09ba956 )