ClickHouse-Encoder

 view release on metacpan or  search on metacpan

t/select-blocks.t  view on Meta::CPAN

#!/usr/bin/env perl
# select_blocks is mainly a streaming HTTP wrapper; the meaningful
# unit-test we can do without a real server is to verify the
# argument validation and SQL-format guard. The end-to-end behavior
# is exercised by t/live.t against a real ClickHouse server.
use strict;
use warnings;
use Test::More;
use lib 'blib/lib', 'blib/arch';
use ClickHouse::Encoder;

# Required on_block coderef
{
    my $err = eval {
        ClickHouse::Encoder->select_blocks('select 1'); 1
    } ? '' : $@;
    like($err, qr/on_block.*required/i,
         'select_blocks croaks without on_block');
}

# Reject SQL with a trailing format clause (we always append
# default_format=Native via the URL, and a user format would shadow
# it). The mixed-case inputs below also exercise the case-insensitive
# guard.
{
    for my $sql ('select 1 format JSON', 'select 1 FORMAT TabSeparated',
                 "select 1  format  csv") {
        my $err = eval {
            ClickHouse::Encoder->select_blocks($sql,
                on_block => sub { }); 1
        } ? '' : $@;
        like($err, qr/format clause/, "rejects '$sql'");
    }
}

# Option-stripping: select_blocks drops keys it consumes (on_block,
# keep, timeout, decompress) and dedup_token (INSERT-only, undocumented
# on SELECT) before forwarding to _http_url_headers. Capture by
# monkey-patching HTTP::Tiny->post so we can inspect the URL it sees.
{
    require HTTP::Tiny;
    my @seen;
    # 'once' too: the typeglob is mentioned a single time in this file.
    no warnings qw(redefine once);
    local *HTTP::Tiny::post = sub {
        my (undef, $url, undef) = @_;
        push @seen, $url;
        # Returning a successful empty response keeps select_blocks happy.
        return { success => 1, status => 200, content => '', headers => {} };
    };
    ClickHouse::Encoder->select_blocks(
        'select 1',
        on_block    => sub { },
        host        => 'h.example',
        settings    => { max_execution_time => 7 },
        dedup_token => 'should-be-stripped',
    );
    is(scalar @seen, 1, 'select_blocks POSTed exactly once');
    like($seen[0], qr/max_execution_time=7/,
         'select_blocks: settings forwarded to URL');
    unlike($seen[0], qr/insert_deduplication_token/,
           'select_blocks: dedup_token stripped (INSERT-only)');



( run in 0.549 second using v1.01-cache-2.11-cpan-140bd7fdf52 )