1 # Before 'make install' is performed this script should be runnable with
2 # 'make test'. After 'make install' it should work as 'perl Blerg-Database.t'
4 #########################
6 # change 'tests => 1' to 'tests => last_test_to_print';
8 use File::Path qw/remove_tree/;
12 use Test::More tests => 34;
13 BEGIN { use_ok('Blerg::Database') };
15 #########################
17 # Insert your test code below, the Test::More module is use()ed here so read
18 # its man page ( perldoc Test::More ) for help writing this test script.
21 my $test_user = 'barfy';
22 my $test_dir = "/tmp/blerg_test_$$";
33 $ptr = Blerg::Database::_open($test_user);
34 ok( ref $ptr eq 'struct blergPtr' );
39 ok( Blerg::Database::_store($ptr, $data) == $n );
41 # Check internal database
42 open DATA, "data/$test_user/data0";
43 my $check_data = <DATA>;
45 ok( $check_data eq $data );
48 ok( Blerg::Database::_fetch($ptr, $n) eq $data );
49 ok( not defined Blerg::Database::_fetch($ptr, $n + 1) );
52 my $t = Blerg::Database::_get_timestamp($ptr, $n);
53 ok( $t0 - $t <= 1.0 );
57 $ptra = Blerg::Database::_open("a");
58 $ptrb = Blerg::Database::_open("b");
59 ok( Blerg::Database::subscription_add("a", "b") == 1 );
60 Blerg::Database::_store($ptrb, "Hello, A!");
61 @list = Blerg::Database::_subscription_list("a", 0, 1);
63 ok( $list[0]->{author} eq 'b' );
64 ok( $list[0]->{record} == 0 );
65 ok( Blerg::Database::_fetch($ptrb, $list[0]->{record}) eq 'Hello, A!' );
68 Blerg::Database::_store($ptr, "This is data A #data");
69 Blerg::Database::_store($ptr, "This is data B #data");
70 Blerg::Database::_store($ptr, "This is data C #data");
73 @list = Blerg::Database::tag_list('#data', 0, 1);
75 ok( $list[0]->{author} eq $test_user );
76 ok( $list[0]->{record} == $n );
79 ok( Blerg::Database::_set_mute($ptr, 1) );
80 ok( Blerg::Database::_get_mute($ptr) == 1 );
81 Blerg::Database::_store($ptr, "Testing more #data");
82 @list = Blerg::Database::tag_list('#data', 0, 1);
84 ok( Blerg::Database::_set_mute($ptr, 0) );
85 ok( Blerg::Database::_get_mute($ptr) == 0 );
86 Blerg::Database::_store($ptr, "Testing more #data");
87 @list = Blerg::Database::tag_list('#data', 0, 1);
92 my $count = Blerg::Database::_get_record_count($ptr);
93 ok( $count == $n + 1 );
96 ok( Blerg::Database::_close($ptr) );
99 ok( Blerg::Database::exists($test_user) );
103 my $blerg = Blerg::Database->open_existing('nonexistent');
104 ok( not defined $blerg );
105 $blerg = Blerg::Database->open($test_user);
106 ok( defined $blerg );
107 ok( ref $blerg->{ptr} eq 'struct blergPtr');
108 ok( $blerg->fetch(0) eq $data );
109 ok( $blerg->store($data) == $n + 1 );
111 ok( $blerg->fetch($n) eq $data );
114 # Subscription OO-style
115 $ptra = Blerg::Database->open_existing('a');
116 $ptrb = Blerg::Database->open_existing('b');
117 @list = $ptra->subscription_list;
119 ok( $list[0]->{author} eq 'b' );
120 ok( $list[0]->{record} == 0 );
121 ok( $ptrb->fetch($list[0]->{record}) eq 'Hello, A!' );
127 remove_tree "/tmp/blerg_test_$$";