Convert-Pheno

 view release on metacpan or  search on metacpan

t/15-convertpheno-orchestration.t  view on Meta::CPAN

        'f1',
        'fhir2omop merges fhir2bff output'
    );

    my $csv = Convert::Pheno->new( { method => 'csv2omop', in_textfile => 1 } )->csv2omop;
    is( $csv->{merged}{data}[0]{id}, 'csv1', 'csv2omop merges csv2bff output' );

    my $pxf = Convert::Pheno->new( { method => 'pxf2omop', in_textfile => 1 } )->pxf2omop;
    is( $pxf->{merged}{data}[0]{id}, 'p1', 'pxf2omop merges pxf2bff output' );
}

{
    my $input = [ { id => 'source-1' } ];
    my $convert = Convert::Pheno->new(
        {
            method   => 'csv2omop',
            data     => $input,
            entities => ['individuals'],
        }
    );
    my $request = Convert::Pheno::ConversionRequest->from_converter($convert);

    is( $request->method, 'csv2omop', 'conversion request keeps the public method' );
    is_deeply(
        $request->pipeline,
        [ 'csv2bff', 'bff2omop' ],
        'conversion request exposes immutable registry stages'
    );
    throws_ok(
        sub { $request->{method} = 'bff2omop' },
        qr/read-only/,
        'conversion request top-level state is immutable'
    );

    my $execution = Convert::Pheno::ExecutionContext->new(
        { request => $request }
    );
    my ( $first_stage, $first_args ) = $execution->begin_next_stage;
    is( $first_stage, 'csv2bff', 'execution context starts the first stage' );
    is( $first_args->{data}, $input, 'first stage receives caller-owned input by reference' );

    my $intermediate = [ { id => 'bff-1' } ];
    $execution->complete_stage($intermediate);
    my ( $second_stage, $second_args ) = $execution->begin_next_stage;
    is( $second_stage, 'bff2omop', 'execution context advances to the second stage' );
    is( $second_args->{data}, $intermediate, 'second stage receives the prior stage result' );
    is( $second_args->{in_textfile}, 0, 'intermediate stages use in-memory input' );
}

{
    my ( undef, $tmp_file ) = tempfile();
    my $convert = Convert::Pheno->new(
        {
            method    => 'omop2bff',
            out_file  => $tmp_file,
            in_textfile => 0,
        }
    );
    $convert->{omop_cli} = 1;
    my $stream = Convert::Pheno::_dispatcher_open_stream_out($convert);
    ok( $stream->{fh}, '_dispatcher_open_stream_out opens output file in streaming mode' );
    close $stream->{fh};
}

{
    my $convert = Convert::Pheno->new(
        {
            method      => 'bff2pxf',
            in_textfile => 0,
            data        => { inline => 1 },
        }
    );
    is_deeply( Convert::Pheno::_dispatcher_input_data($convert), { inline => 1 }, '_dispatcher_input_data returns in-memory data when not reading a file' );
}

{
    my $convert = Convert::Pheno->new(
        {
            method      => 'bff2pxf',
            in_textfile => 1,
            in_file     => 't/bff2pxf/in/individuals.json',
        }
    );
    my $data = Convert::Pheno::_dispatcher_input_data($convert);
    is( ref($data), 'ARRAY', '_dispatcher_input_data reads structured file input for bff-like methods' );
}

{
    my $data = Convert::Pheno::io_yaml_or_json(
        {
            filepath => 't/bff2pxf/in/individuals.json',
            mode     => 'read',
        }
    );
    my $before = JSON::XS->new->canonical->encode($data);
    my $convert = Convert::Pheno->new(
        {
            method => 'bff2pxf',
            data   => $data,
            test   => 1,
        }
    );

    my $result = $convert->bff2pxf;
    is( scalar @{$result}, scalar @{$data}, 'in-memory BFF conversion returns every input record' );
    is(
        JSON::XS->new->canonical->encode($data),
        $before,
        'in-memory BFF conversion does not modify caller-owned data'
    );
    is( $convert->{data}, $data, 'converter retains caller-owned data for reuse' );
}

{
    my $data = Convert::Pheno::io_yaml_or_json(
        {
            filepath => 't/pxf2bff/in/pxf.json',
            mode     => 'read',
        }
    );
    my $before = JSON::XS->new->canonical->encode($data);



( run in 0.910 second using v1.01-cache-2.11-cpan-5c0b1e786e0 )