AmberDB

 view release on metacpan or  search on metacpan

t/amberdb_standard_api_args.t  view on Meta::CPAN


    my @first_desc = $adb->read_id('products', 0, { type => 'first', sort => { block => 'price', dir => 'desc' } });
    is( $first_desc[0], 3, 'read_id with type => "first", sort desc returns highest price (ID 3)' );

    my @last_desc = $adb->read_id('products', 0, { type => 'last', sort => { block => 'price', dir => 'desc' } });
    is( $last_desc[0], 6, 'read_id with type => "last", sort desc returns lowest price (ID 6)' );

    my @rf_opts = $adb->read_firstid('products', { sort => 'price' });
    is( $rf_opts[0], 6, 'read_firstid with { sort => "price" } returns ID 6' );

    my @rl_opts = $adb->read_lastid('products', { sort => 'price' });
    is( $rl_opts[0], 3, 'read_lastid with { sort => "price" } returns ID 3' );

    my @rf_str = $adb->read_firstid('products', 'price');
    is( $rf_str[0], 6, 'read_firstid with string "price" returns ID 6' );

    my @rl_str = $adb->read_lastid('products', 'price');
    is( $rl_str[0], 3, 'read_lastid with string "price" returns ID 3' );

    my @first_range = $adb->read_id('products', 0, { type => 'first', sort => 'price', range => { block => 'price', min => 1000 } });
    is( $first_range[0], 2, 'read_id with type => "first", sort => "price", range min 1000 returns cheapest >= 1000 (ID 2)' );

    # Test on a table without sort_block (unindexed in-memory fallback)
    $adb->table_create( 'simple_items', { blocks => [ { name => 'id' }, { name => 'val' } ] } );
    $adb->insert_id( 'simple_items', 10, 50 );
    $adb->insert_id( 'simple_items', 20, 10 );
    $adb->insert_id( 'simple_items', 30, 80 );
    my @unindexed_first = $adb->read_id('simple_items', 0, { type => 'first', sort => 'val' });
    is( $unindexed_first[0], 20, 'Unindexed table read_id type => "first", sort => "val" returns ID 20' );
    my @unindexed_last = $adb->read_id('simple_items', 0, { type => 'last', sort => 'val' });
    is( $unindexed_last[0], 30, 'Unindexed table read_id type => "last", sort => "val" returns ID 30' );
};

# ---------------------------------------------------------------------------
subtest '8. field_allfltkeys with standardized \%options and legacy' => sub {
    plan tests => 5;

    # Standardized: single hashref with target_blocks
    my $all1 = $adb->field_allfltkeys('products', {
        target_blocks => [ 2, 3 ]
    });
    is( ref($all1), 'HASH', 'field_allfltkeys returns HASH' );
    is( $all1->{2}{'Smartphones'}, 2, '2 smartphones counted' );
    is( $all1->{3}{'Apple'}, 3, '3 Apple products counted' );

    # Standardized: blocks + base_ids hashref
    my $all2 = $adb->field_allfltkeys('products', [ 2, 3 ], { base_ids => [ 1, 2 ] });
    is( $all2->{2}{'Smartphones'}, 2, 'Scoped field_allfltkeys with hashref base_ids' );

    # Legacy: blocks + base_ids arrayref
    my $all3 = $adb->field_allfltkeys('products', [ 2, 3 ], [ 1, 2 ]);
    is( $all3->{2}{'Smartphones'}, 2, 'Legacy field_allfltkeys with arrayref base_scope' );
};

# ---------------------------------------------------------------------------
subtest '9. facet_menu with standardized single \%options and legacy' => sub {
    plan tests => 6;

    # Standardized: single unified hashref
    my $menu1 = $adb->facet_menu('products', {
        selected => { 2 => 'Smartphones' },
        sort     => 'count'
    });
    ok( $menu1, 'facet_menu with unified hashref generated successfully' );
    is( $menu1->{count}, 2, 'Total matching count is 2' );
    is( scalar(@{ $menu1->{groups} }), 2, '2 facet groups in menu' );

    # Standardized: with offset & limit
    my $menu_paged = $adb->facet_menu('products', {
        offset => 0,
        limit  => 3
    });
    is( scalar(@{ $menu_paged->{ids} }), 3, 'facet_menu with offset/limit paged to 3' );

    # Legacy: 4 arguments
    my $menu_leg = $adb->facet_menu('products', { 2 => 'Smartphones' }, [ 2, 3 ], { sort => 'count' });
    is( $menu_leg->{count}, 2, 'Legacy 4-arg facet_menu matches count' );

    # Legacy: 2 arguments
    my $menu_2arg = $adb->facet_menu('products', { 2 => 'Smartphones' });
    is( $menu_2arg->{count}, 2, 'Legacy 2-arg facet_menu matches count' );
};



( run in 0.675 second using v1.01-cache-2.11-cpan-e623d60df62 )