AmberDB

 view release on metacpan or  search on metacpan

t/amberdb_encapsulation.t  view on Meta::CPAN

    };
    like( $@, qr/disallowed key/i, "Direct assignment to \$adb->{path} is strictly disallowed" );
};

subtest "8. Hash::Util protection: Locked core container references" => sub {
    plan tests => 3;

    eval {
        $adb->{_table} = {};
    };
    like( $@, qr/read-only/i, "Attempting to overwrite \$adb->{_table} throws read-only error" );

    eval {
        $adb->{_cfg} = {};
    };
    like( $@, qr/read-only/i, "Attempting to overwrite \$adb->{_cfg} throws read-only error" );

    eval {
        $adb->{_path} = {};
    };
    like( $@, qr/read-only/i, "Attempting to overwrite \$adb->{_path} throws read-only error" );
};

subtest "9. Full CRUD operation under encapsulated object" => sub {
    plan tests => 5;

    $adb->config( no_write => 0 );
    my $table = "crud_table";
    
    # Insert
    my $rid = $adb->insert_id( $table, 1, "Record 1", "Active", "2026-08-27" );
    is( $rid, 1, "insert_id succeeded: rid = $rid" );

    # Read
    my ($id, @rec) = $adb->read_id( $table, 1 );
    is( $rec[0], "Record 1", "read_id retrieved correct data" );

    # Modify
    my $mod_ok = $adb->modify_id( $table, 1, "Record 1 Modified", "Active", "2026-08-27" );
    ok( $mod_ok, "modify_id succeeded" );

    my ($id_mod, @rec_mod) = $adb->read_id( $table, 1 );
    is( $rec_mod[0], "Record 1 Modified", "read_id verified modified data" );

    # Delete
    my $del_ok = $adb->delete_id( $table, 1 );
    ok( $del_ok, "delete_id succeeded" );
};

subtest "10. path() getter, setter and shallow copy immutability" => sub {
    plan tests => 6;

    # Getter
    is( $adb->path('dbase_dir'), $tmpdir, "path('dbase_dir') returns expected directory" );

    # Setter via key-value
    $adb->path( custom_path => "/opt/data" );
    is( $adb->path('custom_path'), "/opt/data", "path(custom_path => ...) sets path" );

    # Setter via hashref
    $adb->path({ backup_dir => "$tmpdir/backup" });
    is( $adb->path('backup_dir'), "$tmpdir/backup", "path({ backup_dir => ... }) sets path" );

    # Bulk copy
    my $paths_copy = $adb->path();
    ok( ref($paths_copy) eq 'HASH', "path() returns HASH ref" );
    is( $paths_copy->{dbase_dir}, $tmpdir, "path copy contains dbase_dir" );

    # Mutation protection
    $paths_copy->{dbase_dir} = "/compromised/path";
    is( $adb->path('dbase_dir'), $tmpdir, "Internal path is NOT affected by mutating copy" );
};



( run in 0.363 second using v1.01-cache-2.11-cpan-4ef0a570458 )