AmberDB

 view release on metacpan or  search on metacpan

t/amberdb-field_matching_with_index.t  view on Meta::CPAN

#!/usr/bin/perl

# t/amberdb-field_matching_with_index.t - Tests for AmberDB field_fetch & field matching with index (.fld)

use 5.016000;
use strict;
use warnings;
use utf8;
use open ':std', ':utf8';
use Test::More;
binmode Test::More->builder->output,         ':utf8';
binmode Test::More->builder->failure_output, ':utf8';
binmode Test::More->builder->todo_output,    ':utf8';
use File::Temp qw(tempdir);
use File::Spec;
use Encode qw(encode);

use_ok('AmberDB') or BAIL_OUT('Cannot load AmberDB');

my $tmpdir = tempdir( CLEANUP => 1 );

my $adb = AmberDB->new(
    path => { dbase_dir => $tmpdir },
    cfg  => { language  => 'tr' },
);

# ==============================================================================
# SUBTEST 1: match_block Schema & Index Key File (.fld) Generation
# ==============================================================================
subtest '1. match_block Schema & Index Key File (.fld) Generation' => sub {
    plan tests => 8;

    my $tbl = 'catalog_fld_idx';
    my $table_info = {
        record_index => 1,
        match_block  => [ 4, 6 ],         # blk 4: Category, blk 6: Brand
        sort_block   => [
            { blk => 5, type => 'num', len => 8 }, # blk 5: Price
        ],
    };
    $adb->table_attr( $tbl, $table_info );

    # Sample records:
    # [ ID, SKU (blk 1), Title (blk 2), Desc (blk 3), Category (blk 4), Price (blk 5), Brand (blk 6) ]
    my @records = (
        [ 1, 'SKU1', 'Sony Kulaklık',     'Kablosuz kulaklık',    '10, 20', '150', '12' ],
        [ 2, 'SKU2', 'Philips Kulaklık',  'Mikrofonlu kulaklık',  '10',     '120', '12' ],
        [ 3, 'SKU3', 'JBL Hoparlör',      'Taşınabilir hoparlör', '20',     '200', '14' ],
        [ 4, 'SKU4', 'Sony Hoparlör',     'Stereo hoparlör',      '20',     '180', '12' ],
        [ 5, 'SKU5', 'Apple Kulaklık',    'Gürültü önleyici',     '10',     '300', '16' ],
        [ 6, 'SKU6', 'Sennheiser Pro',    'Profesyonel kulaklık', '10',     '400', '14' ],
    );

    for my $r (@records) {
        $adb->insert_id( $tbl, $r->[0], @$r[ 1 .. $#$r ] );
    }

    my $table_path = $adb->table_path($tbl);
    my $fld_path   = "${table_path}.fld";
    my $unq_path   = "${table_path}.unq";

    # Verify unified .fld and .unq index files exist on disk for defined match blocks
    ok( -e $fld_path, "Unified index file ${tbl}.fld created" );
    ok( -e $unq_path, "Unified dictionary file ${tbl}.unq created for match blocks" );
    ok( !-e "${table_path}_4.unq", "No legacy per-block _4.unq created" );

    # Verify .fld posting lists directly using composite keys
    my ($k_cat10) = $adb->get_fieldlist( '10', $table_path, $table_info, 4 );
    my ( undef, @ids_cat10 ) = $adb->index_get( $fld_path, "4:$k_cat10" );
    is_deeply( [ sort { $a <=> $b } @ids_cat10 ], [ 1, 2, 5, 6 ],
        "Category '10' (key 4:$k_cat10) in unified .fld maps to IDs 1, 2, 5, 6" );

    my ($k_cat20) = $adb->get_fieldlist( '20', $table_path, $table_info, 4 );



( run in 0.832 second using v1.01-cache-2.11-cpan-364913b4093 )