#!/usr/bin/env perl

# Copyright (C) 2005  Joshua Hoblitt
#
# $Id: 03_mdb-find-new.t,v 1.2 2005-10-14 21:33:44 jhoblitt Exp $

use strict;
use warnings;

use Test::Cmd;
use Test::More tests => 21;

use Env qw( MDB_DSN MDB_USER MDB_PASS );

local $MDB_DSN;
local $MDB_USER;
local $MDB_PASS;

my $test = Test::Cmd->new(prog => 'scripts/mdb-find-new', workdir => '');
isa_ok($test, 'Test::Cmd');

{
    $test->run(args => '');
    missing_args(3, "Required options: --dsn --user");
}

{
    local $MDB_DSN  = "foo";

    $test->run(args => '');
    missing_args(3, "Required options: --dsn --user");
}

{
    local $MDB_USER = "foo";

    $test->run(args => '');
    missing_args(3, "Required options: --dsn --user");
}

{
    local $MDB_PASS = "foo";

    $test->run(args => '');
    missing_args(3, "Required options: --dsn --user");
}

$test->run(args => '--dsn foo');
missing_args(3, "Required options: --dsn --user");

$test->run(args => '--user foo');
missing_args(3, "Required options: --dsn --user");

$test->run(args => '--pass foo');
missing_args(3, "Required options: --dsn --user");

$MDB_DSN  = "DBI:mysql:test";
$MDB_USER = "test";

$test->run(args => '');
is($? >> 8, 0, "no options");
is($test->stderr, "", "no output to stderr");

$test->run(args => '--dsn $MDB_DSN --user $MDB_USER');
is($? >> 8, 0, "no options");
is($test->stderr, "", "no output to stderr");

$test->run(args => '--exp_id 1 --class 2 --class_id 3');
is($? >> 8, 0, "all manditory options");
is($test->stderr, "", "no output to stderr");

END { reset_db() }

sub missing_args
{
    my ($exit, $errstr) = @_;

    is($? >> 8, $exit, "error code is: $exit");
    like($test->stderr, qr/$errstr/, "error string is: $errstr");
}

sub reset_db
{
        use PS::IPP::MetaDB dsn => "DBI:mysql:test", user => "test";
            PS::IPP::MetaDB::Image::New->drop_table();
}

