App-Sqitch

 view release on metacpan or  search on metacpan

t/clickhouse.t  view on Meta::CPAN

is $dt->minute,  7, 'DateTime minute should be set';
is $dt->second,  1, 'DateTime second should be set';
is $dt->time_zone->name, 'UTC', 'DateTime TZ should be set';

##############################################################################
# Test SQL helpers.
is $ch->_listagg_format, q{groupArraySorted(10000)(%1$s)},
    'Should have _listagg_format';
is $ch->_regex_op, 'REGEXP', 'Should have _regex_op';
is $ch->_simple_from, '', 'Should have _simple_from';
is $ch->_limit_default, undef, 'Should have no _limit_default';

is $ch->_ts_default, q{now64(6, 'UTC')},
    'Should have _ts_default with now64()';

DBI: {
    local *DBI::state;
    ok !$ch->_no_table_error, 'Should have no table error';
    ok !$ch->_no_column_error, 'Should have no column error';
    dies_ok { $ch->_initialized } '_initialized should die';
    dies_ok { $ch->_cid('ASC') } '_cid should die';
    $DBI::state = 'HY000';
    ok $ch->_no_table_error, 'Should now have table error';
    ok !$ch->_no_column_error, 'Still should have no column error';
    ok !$ch->_initialized, 'Should get false from _initialized';
    is undef, $ch->_cid('ASC'), 'Should get undef from _cid';
    $DBI::state = '42703';
    ok !$ch->_no_table_error, 'Should again have no table error';
    ok $ch->_no_column_error, 'Should now have no column error';
    ok !$ch->_unique_error, 'Unique constraints not supported by ClickHouse';
}

is_deeply [$ch->_limit_offset(8, 4)],
    [['LIMIT 8', 'OFFSET 4'], []],
    'Should get limit and offset';
is_deeply [$ch->_limit_offset(0, 2)],
    [['OFFSET 2'], []],
    'Should get limit and offset when offset only';
is_deeply [$ch->_limit_offset(12, 0)], [['LIMIT 12'], []],
    'Should get only limit with 0 offset';
is_deeply [$ch->_limit_offset(12)], [['LIMIT 12'], []],
    'Should get only limit with noa offset';
is_deeply [$ch->_limit_offset(0, 0)], [[], []],
    'Should get no limit or offset for 0s';
is_deeply [$ch->_limit_offset()], [[], []],
    'Should get no limit or offset for no args';

is_deeply [$ch->_regex_expr('corn', 'Obama$')],
    ['corn REGEXP ?', 'Obama$'],
    'Should use REGEXP for regex expr';

##############################################################################
# Test parse_array.
is_deeply $ch->_parse_array(''), [], 'Should get empty array from empty string';
is_deeply $ch->_parse_array(undef), [], 'Should get empty array from undef';
is_deeply $ch->_parse_array('no'), [], 'Should get empty array invalid array';
is_deeply $ch->_parse_array('[1]'), [1], 'Should parse single int array';
is_deeply $ch->_parse_array('[1, 2, 3]'), [1,2,3], 'Should parse int array';
is_deeply $ch->_parse_array(q{['hi']}), ['hi'], 'Should parse single string array';
is_deeply $ch->_parse_array(q{['O\'Toole']}), ["O'Toole"],
    'Should parse string array with escape';
is_deeply $ch->_parse_array(q{['bread\\water']}), ['bread\\water'],
    'Should parse string array with escape slash';
is_deeply $ch->_parse_array(q{['🏠']}), ['🏠'],
    'Should parse string array with Unicode';
is_deeply $ch->_parse_array(q{['', 'hi', 'there']}), ['hi', 'there'],
    'Should pop empty string when first value in array';
is_deeply $ch->_parse_array(q{['']}), [],
    'Should pop empty string when only value in array';

# Test _version_query
is $ch->_version_query,
    'SELECT CAST(ROUND(MAX(version), 1) AS CHAR) FROM releases',
    'Should have version query';

##############################################################################
# Test unexpected database error in initialized() and _cid().
MOCKDBH: {
    my $mock = Test::MockModule->new($CLASS);
    $mock->mock(dbh => sub { die 'OW' });
    throws_ok { $ch->initialized } qr/OW/,
        'initialized() should rethrow unexpected DB error';
    throws_ok { $ch->_cid } qr/OW/,
        '_cid should rethrow unexpected DB error';
}

##############################################################################
# Test run_upgrade().
UPGRADE: {
    # Mock run.
    my @run;
    $mock_sqitch->mock(run => sub { shift; @run = @_ });

    # Mock File::Temp so we hang on to the file.
    my $mock_ft = Test::MockModule->new('File::Temp');
    my $tmp_fh;
    my $ft_new;
    $mock_ft->mock(new => sub { $tmp_fh = 'File::Temp'->$ft_new() });
    $ft_new = $mock_ft->original('new');

    # Assemble the expected command.
    my @cmd = $ch->cli;
    my $db_opt_idx = firstidx { $_ eq '--database' } @cmd;
    $cmd[$db_opt_idx + 1] = $ch->registry;
    my $fn = file($INC{'App/Sqitch/Engine/clickhouse.pm'})->dir->file('clickhouse.sql');

    # Test the upgrade.
    ok $ch->run_upgrade($fn), 'Run the upgrade';
    is $tmp_fh, undef, 'Should not have created a temp file';
    is_deeply \@run, [@cmd, '--queries-file', $fn],
        'It should have run the unchanged file';

    # Test without the --database option.
    splice @cmd, $db_opt_idx, 2;
    my $mock = Test::MockModule->new($CLASS);
    $mock->mock(cli => sub { @cmd });
    ok $ch->run_upgrade($fn), 'Run the upgrade';
    is $tmp_fh, undef, 'Should not have created a temp file';
    is_deeply \@run, [@cmd, '--database', $ch->registry, '--queries-file', $fn],
        'It should appended the --database option';

    $mock_sqitch->unmock_all;
}



( run in 1.104 second using v1.01-cache-2.11-cpan-0fb53d1c279 )