DBIx-QuickORM

 view release on metacpan or  search on metacpan

worktrees/audit-fixes-master/t/omit.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;

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 sub {
            autotype 'JSON';
        };

        schema my_schema => sub {
            table example => sub {
                column data => sub {
                    type 'JSON';
                    omit;
                };
            };
        }
    };

    ok(my $orm = orm('my_orm')->connect, "Got a connection");
    ok(my $row = $orm->handle('example')->insert({name => 'a', data => {foo => 'bar'}}), "Inserted a row");
    ok($orm->schema->{tables}->{example}->{columns}->{data}->{omit}, "omit was merged into the autofill schema");
    $row = undef; $row = $orm->handle('example')->one(name => 'a');
    ok(!exists($row->row_data->{stored}->{data}), "did not fetch data");

    is($row->field('data'), {foo => 'bar'}, "Can fetch data");
    my $weak = $row;
    weaken($weak);
    $row = undef;
    ok(!defined($weak), "Previous row expired once all references were cleared");
    $row = $orm->handle('example')->one({name => 'a'});

    ok($row, "got row");
    ok(!exists($row->row_data->{stored}->{data}), "did not fetch data");

    $row = undef;

    $row = $orm->handle('example')->omit(['name'])->one({name => 'a'});
    ok(!exists($row->row_data->{stored}->{name}), "Did not fetch name (query-level omit via omit())");

    like(
        dies { $orm->handle('example')->omit(['id'])->one({name => 'a'}) },
        qr/Cannot omit primary key field 'id'/,
        "Cannot omit a primary key field"
    );
};

done_testing;



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