AmberDB

 view release on metacpan or  search on metacpan

t/amberdb_cli.t  view on Meta::CPAN

subtest '3. Dash tolerance & global config update' => sub {
    plan tests => 4;

    # Connect to get token
    my $out_conn = `"$perl_bin" -Ilib "$cli_path" connect path-dbase_dir="$test_dbdir" format=json`;
    my $conn = eval { decode_json($out_conn) };
    my $token = $conn ? $conn->{token} : '';

    # Update with --cfg-no_write=1 (double dash)
    my $upd1 = `"$perl_bin" -Ilib "$cli_path" --token=$token --cfg-no_write=1 format=json 2>&1`;
    my $data1 = eval { decode_json($upd1) };
    is(eval { $data1->{cfg}->{no_write} }, 1, "Double dash --cfg-no_write=1 updated") or diag("upd1 output: $upd1");

    # Update with -cfg-no_write=0 (single dash)
    my $upd2 = `"$perl_bin" -Ilib "$cli_path" -token=$token -cfg-no_write=0 format=json 2>&1`;
    my $data2 = eval { decode_json($upd2) };
    is(eval { $data2->{cfg}->{no_write} }, 0, "Single dash -cfg-no_write=0 updated") or diag("upd2 output: $upd2");

    # Update with cfg-no_write=1 (no dash)
    my $upd3 = `"$perl_bin" -Ilib "$cli_path" token=$token cfg-no_write=1 format=json 2>&1`;
    my $data3 = eval { decode_json($upd3) };
    is(eval { $data3->{cfg}->{no_write} }, 1, "No dash cfg-no_write=1 updated") or diag("upd3 output: $upd3");

    # Clean disconnect
    my $disc = `"$perl_bin" -Ilib "$cli_path" token=$token disconnect format=json 2>&1`;
    my $ddata = eval { decode_json($disc) };
    is(eval { $ddata->{status} }, 'disconnected', "Disconnected successfully") or diag("disc output: $disc");
};

# ---------------------------------------------------------------------------
subtest '4. Table attributes with deep nested parsing and session persistence' => sub {
    plan tests => 6;

    my $out_conn = `"$perl_bin" -Ilib "$cli_path" connect path-dbase_dir="$test_dbdir" format=json`;
    my $token = decode_json($out_conn)->{token};

    # table_attr with deep keys: blocks-1-rdbm and search_block=[1]
    my $cmd = qq{"$perl_bin" -Ilib "$cli_path" token=$token action=table_attr table=cli_test_prod search_block=[1] keep_deleted=1 blocks-1-rdbm=cat,1 format=json};
    my $out_attr = `$cmd`;
    my $attr_data = decode_json($out_attr);
    is($attr_data->{status}, 'ok', "table_attr executed successfully");
    is($attr_data->{attributes}->{keep_deleted}, 1, "keep_deleted attribute set to 1");
    is_deeply($attr_data->{attributes}->{search_block}, [1], "search_block parsed as JSON array [1]");
    is(ref $attr_data->{attributes}->{blocks}->{1}->{rdbm}, 'HASH', "rdbm normalized to HASH");
    is($attr_data->{attributes}->{blocks}->{1}->{rdbm}->{table}, 'cat', "Deep nested blocks-1-rdbm table parsed");

    # Disconnect
    `"$perl_bin" -Ilib "$cli_path" token=$token disconnect`;
    my $sess_file = File::Spec->catfile($test_dbdir, "session", "cli_$token");
    ok(!-f $sess_file, "Session file deleted on disconnect");
};

# ---------------------------------------------------------------------------
subtest '5. CRUD operations & no_write protection' => sub {
    plan tests => 8;

    my $out_conn = `"$perl_bin" -Ilib "$cli_path" connect path-dbase_dir="$test_dbdir" cfg-no_write=1 format=json`;
    my $token = decode_json($out_conn)->{token};

    # Attempt insert when no_write is 1
    my $ins_blocked = `"$perl_bin" -Ilib "$cli_path" token=$token action=insert_id table=cli_items id=1 data='["Book",15]' 2>&1`;
    like($ins_blocked, qr/no_write aktif/, "insert_id blocked when no_write=1");

    # Enable writes in session
    `"$perl_bin" -Ilib "$cli_path" token=$token cfg-no_write=0`;

    # Insert record
    my $ins_ok = `"$perl_bin" -Ilib "$cli_path" token=$token action=insert_id table=cli_items id=1 data='["Book",15]' format=json`;
    my $ins_data = eval { decode_json($ins_ok) };
    is($ins_data->{status}, 'ok', "insert_id succeeded after enabling writes");
    is($ins_data->{id}, 1, "Inserted record ID is 1");

    # Read record
    my $read_ok = `"$perl_bin" -Ilib "$cli_path" token=$token action=read_id table=cli_items id=1 format=json`;
    my $read_data = eval { decode_json($read_ok) };
    is(ref $read_data, 'ARRAY', "read_id returned array of fields");
    is($read_data->[1], "Book", "read_id field 1 is Book");

    # Count table
    my $cnt_ok = `"$perl_bin" -Ilib "$cli_path" token=$token action=table_count table=cli_items format=json`;
    my $cnt_data = eval { decode_json($cnt_ok) };
    is($cnt_data->{count}, 1, "table_count is 1");

    # Read all
    my $all_ok = `"$perl_bin" -Ilib "$cli_path" token=$token action=read_all table=cli_items format=json`;
    my $all_data = eval { decode_json($all_ok) };
    is($all_data->{count}, 1, "read_all count is 1");

    # Delete record
    my $del_ok = `"$perl_bin" -Ilib "$cli_path" token=$token action=delete_id table=cli_items id=1 format=json`;
    my $del_data = eval { decode_json($del_ok) };
    is($del_data->{status}, 'ok', "delete_id succeeded");

    `"$perl_bin" -Ilib "$cli_path" token=$token disconnect`;
};

# ---------------------------------------------------------------------------
subtest '6. Tools operations: reindex, check, vacuum' => sub {
    plan tests => 3;

    my $out_conn = `"$perl_bin" -Ilib "$cli_path" connect path-dbase_dir="$test_dbdir" format=json`;
    my $token = decode_json($out_conn)->{token};

    # Insert a dummy record
    `"$perl_bin" -Ilib "$cli_path" token=$token action=insert_id table=cli_tools_test id=1 data='["Item A",100]'`;

    # Reindex
    my $reindex_out = `"$perl_bin" -Ilib "$cli_path" token=$token action=reindex table=cli_tools_test format=json`;
    my $reindex_data = eval { decode_json($reindex_out) };
    is($reindex_data->{status}, 'ok', "Tools reindex succeeded");

    # Check
    my $check_out = `"$perl_bin" -Ilib "$cli_path" token=$token action=check table=cli_tools_test format=json`;
    my $check_data = eval { decode_json($check_out) };
    is($check_data->{status}, 'ok', "Tools check succeeded");

    # Vacuum
    my $vac_out = `"$perl_bin" -Ilib "$cli_path" token=$token action=vacuum table=cli_tools_test format=json`;
    my $vac_data = eval { decode_json($vac_out) };
    is($vac_data->{status}, 'ok', "Tools vacuum succeeded");

    `"$perl_bin" -Ilib "$cli_path" token=$token disconnect`;



( run in 1.757 second using v1.01-cache-2.11-cpan-800906f7e73 )