/lib/perl/Blerg-Database/t/subscription.t
use File::Path qw/remove_tree/;
use strict;
use warnings;

use Test::More tests => 10;
BEGIN { use_ok('Blerg::Database') };

### Setup
my $test_dir = "/tmp/blerg_test_$$";
mkdir $test_dir;
chdir $test_dir;
mkdir 'data';
mkdir 'hash_tags';
mkdir 'ref_tags';

$ENV{BLERG_PATH} = $test_dir;
Blerg::Database::init();

my ($ptra, $ptrb, @list);

### Subscription
$ptra = Blerg::Database::_open("a");
$ptrb = Blerg::Database::_open("b");
ok( Blerg::Database::subscription_add("a", "b") == 1 );
Blerg::Database::_store($ptrb, "Hello, A!");
@list = Blerg::Database::_subscription_list("a", 0, 1);
ok( @list == 1 );
ok( $list[0]->{author} eq 'b' );
ok( $list[0]->{record} == 0 );
ok( Blerg::Database::_fetch($ptrb, $list[0]->{record}) eq 'Hello, A!' );

### Subscription OO-style
$ptra = Blerg::Database->open_existing('a');
$ptrb = Blerg::Database->open_existing('b');
@list = $ptra->subscription_list;
ok( @list == 1 );
ok( $list[0]->{author} eq 'b' );
ok( $list[0]->{record} == 0 );
ok( $ptrb->fetch($list[0]->{record}) eq 'Hello, A!' );
$ptrb->close;
$ptra->close;

END {
	chdir;
	remove_tree "/tmp/blerg_test_$$";
}