# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl Modwheel.t'

BEGIN {
    use lib '/opt/devel/Modwheel/lib';
}

#########################

use Test::More tests => 30;
BEGIN {
    use_ok('Modwheel');
    use_ok('Modwheel::Session');
};

#########################

use strict;
my $modwheel_config = {
        prefix             => './',
        configfile         => 't/modwheelconfig.yml',
        site               => 'modwheeltest',
        configcachetype => 'memshare',
        locale            => 'en_EN',
        logmode            => 'off',
};
ok(
    my($modwheel, $user, $db, $object, $template) =
    modwheel_session($modwheel_config, qw(db user template object)
        ), 'Create Modwheel CGI/Terminal session'
);
isa_ok($db, 'Modwheel::DB::Generic', 'Has the db object inherited from Modwheel::Generic?');
my $dsn = $db->create_dsn;
ok($dsn, 'Create DBI dsn');
ok($db->connect, 'Connect to test database');
ok($db->connected, 'Are we connected?');

ok($db->current_timestamp, 'Get current database timestamp');
ok($db->fetch_next_id('users'), 'Get a new user id');
ok($db->fetch_next_id('groups'), 'Get a new group id');
ok($db->fetch_next_id('objtagmap'), 'Get a new object to tag map id');
ok($db->fetch_next_id('tags', 'tagid'), 'Get a new tag id');
ok($db->fetch_next_id('repository'), 'Get a new repository id.');
my $newobjid;
ok($newobjid = $db->fetch_next_id('object'), 'Get a new object id');
my $newobjquery = $db->build_insert_q('object', {id => '%d', name => "'%s'", parent => '%d'});
ok($newobjquery, 'Generate object insert query.');
ok(Modwheel::Object::MW_TREE_TRASH(), 'MW_TREE_TRASH defined');
ok($db->exec_query($newobjquery, $newobjid, 'Test Object!', Modwheel::Object::MW_TREE_TRASH), 'Insert in object table');
is($db->fetch_singlevar($db->build_select_q('object', 'name', {id => '?'}), $newobjid), 'Test Object!', 'Fetch from object table');
ok($db->exec_query($db->build_delete_q('object', {id => '?'}), $newobjid), 'Delete from object table.');
ok(!$db->build_delete_q('object'), 'Bail on build_delete_q without where clause');
ok(!$db->build_update_q('object'), 'Bail on build_udpate_q without where clause');
is($db->trim('    trim'), 'trim', 'Trim leading spaces with trim()');
is($db->trim('mirt    '), 'mirt', 'Trim spaces at end with trim()');
is($db->sqlescape("sql'escape"), "sql''escape", "sqlescape() (instance) handles \"'\"?");
is($db->sqlescape("sql?escape"), 'sql\?escape', "sqlescape() (instance) handles \"?\"?");
is(Modwheel::DB::Generic::sqlescape("sql'escape"), "sql''escape", "sqlescape() (export) handles \"'\"?");
is(Modwheel::DB::Generic::sqlescape("sql?escape"), 'sql\?escape', "sqlescape() (export) handles \"?\"?");
is($db->quote("'quote'"), "''quote''", "quote() handles \"'\"?");


ok($db->disconnect, 'Disconnected from db.');
ok(!$db->connected, 'Are we disconnected?');

