#!/usr/bin/perl

# Copryight (C) 2004-2005  Joshua Hoblitt
#
# $Id: 08_server_delete_instance.t,v 1.8 2007-05-03 01:44:24 jhoblitt Exp $

use strict;
use warnings FATAL => qw( all );

use Test::More tests => 8;

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

use Nebulous::Server;
use Nebulous::Util qw( :standard );
use Test::Nebulous;

my $neb = Nebulous::Server->new(
    dsn         => "DBI:mysql:database=test:host=localhost",
    dbuser      => "test",
    dbpasswd    => "",
);

Test::Nebulous->setup;

{
    my $uri = $neb->create_object("foo");

    ok($neb->delete_instance($uri), "delete instance");
}

Test::Nebulous->setup;

{
    my $uri1 = $neb->create_object("foo");
    my $uri2 = $neb->replicate_object("foo");

    ok($neb->delete_instance($uri1), "delete instance");

    my $locations = $neb->find_instances("foo");

    is($locations->[0], $uri2, "instance remains");

    ok($neb->delete_instance( $uri2 ), "delete instance");

    eval {
        $neb->find_instances("foo");
    };
    like($@, qr/is valid object key/, "storage object was deleted");
}

Test::Nebulous->setup;

eval {
    $neb->delete_instance("file:/foo");
};
like($@, qr/no instance is associated with uri/, "uri does not exist");

Test::Nebulous->setup;

eval {
    $neb->delete_instance();
};
like($@, qr/1 was expected/, "no params");

Test::Nebulous->setup;

eval {
    $neb->delete_instance("foo", 2);
};
like($@, qr/1 was expected/, "too many params");

Test::Nebulous->cleanup;
