#!/usr/bin/perl

use strict;
use warnings;

use Test::More tests => 13;

use lib qw( ./t ./lib );

use PS::IPP::IData::Server;
use PS::IPP::IData::Util qw( :standard );
use Test::IData;

my $idata = "PS::IPP::IData::Server";

$idata->setup(
    "DBI:mysql:database=test:host=localhost",
    "test",
    "",
);

Test::IData->setup;

{
    $idata->create_object( "foo" );

    ok( $idata->lock_object( "foo", "read" ), "read lock" );
}

Test::IData->setup;

{
    $idata->create_object( "foo" );

    ok( $idata->lock_object( "foo", "read" ), "read lock" );
    ok( $idata->lock_object( "foo", "read" ), "read lock" );
}

Test::IData->setup;

{
    $idata->create_object( "foo" );

    ok( $idata->lock_object( "foo", "write" ), "write lock" );
}

Test::IData->setup;

eval {
    $idata->lock_object( "foo", "read" );
};
like( $@, qr/storage object does not exist/, "storage object does not exist" );

Test::IData->setup;

eval {
    $idata->lock_object( "foo", "write" );
};
like( $@, qr/storage object does not exist/, "storage object does not exist" );

Test::IData->setup;

eval {
    $idata->create_object( "foo" );

    $idata->lock_object( "foo", "write" );
    $idata->lock_object( "foo", "write" );
};
like( $@, qr/can not write lock twice/, "can not write lock twice" );

Test::IData->setup;

eval {
    $idata->create_object( "foo" );

    $idata->lock_object( "foo", "read" );
    $idata->lock_object( "foo", "write" );
};
like( $@, qr/can not write lock after read lock/, "can not write lock after read lock" );

Test::IData->setup;

eval {
    $idata->create_object( "foo" );

    $idata->lock_object( "foo", "write" );
    $idata->lock_object( "foo", "read" );
};
like( $@, qr/can not read lock after write lock/, "can not read lock after write lock" );

Test::IData->setup;

eval {
    $idata->lock_object();
};
like( $@, qr/2 were expected/, "no params" );

Test::IData->setup;

eval {
    $idata->lock_object( "foo" );
};
like( $@, qr/2 were expected/, "not enough params" );

Test::IData->setup;

eval {
    $idata->lock_object( "foo", "both" );
};
like( $@, qr/is read or write/, "not read or write" );

Test::IData->setup;

eval {
    $idata->lock_object( "foo", 'read', 3 );
};
like( $@, qr/2 were expected/, "too many params" );

Test::IData->cleanup;
