Rearrange makefile to put auth stuff in blerg_auth.a
[blerg.git] / lib / perl / Blerg-Database / t / Blerg-Database.t
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'
3
4 #########################
5
6 # change 'tests => 1' to 'tests => last_test_to_print';
7
8 use File::Path qw/remove_tree/;
9 use strict;
10 use warnings;
11
12 use Test::More tests => 34;
13 BEGIN { use_ok('Blerg::Database') };
14
15 #########################
16
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.
19
20 # Setup
21 my $test_user = 'barfy';
22 my $test_dir = "/tmp/blerg_test_$$";
23 mkdir $test_dir;
24 chdir $test_dir;
25 mkdir 'data';
26 mkdir 'hash_tags';
27 mkdir 'ref_tags';
28 my ($ptr, @list, $n);
29 $n = 0;
30
31 ### C interfaces
32 # Open a database
33 $ptr = Blerg::Database::_open($test_user);
34 ok( ref $ptr eq 'struct blergPtr' );
35
36 # Store
37 my $data = "Hello!";
38 my $t0 = time;
39 ok( Blerg::Database::_store($ptr, $data) == $n );
40
41 # Check internal database
42 open DATA, "data/$test_user/data0";
43 my $check_data = <DATA>;
44 close DATA;
45 ok( $check_data eq $data );
46
47 # Fetch
48 ok( Blerg::Database::_fetch($ptr, $n) eq $data );
49 ok( not defined Blerg::Database::_fetch($ptr, $n + 1) );
50
51 # Get time
52 my $t = Blerg::Database::_get_timestamp($ptr, $n);
53 ok( $t0 - $t <= 1.0 );
54
55 # Subscription
56 my ($ptra, $ptrb);
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);
62 ok( @list == 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!' );
66
67 # Test tags
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");
71 $n += 3;
72
73 @list = Blerg::Database::tag_list('#data', 0, 1);
74 ok( @list == 3 );
75 ok( $list[0]->{author} eq $test_user );
76 ok( $list[0]->{record} == $n );
77
78 # Test mute
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);
83 ok( @list == 3 );
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);
88 ok( @list == 4 );
89 $n += 2;
90
91 # Count
92 my $count = Blerg::Database::_get_record_count($ptr);
93 ok( $count == $n + 1 );
94
95 # Close database
96 ok( Blerg::Database::_close($ptr) );
97
98 # Test existence
99 ok( Blerg::Database::exists($test_user) );
100
101
102 # Perl OO interface
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 );
110 $n++;
111 ok( $blerg->fetch($n) eq $data );
112 $blerg->close;
113
114 # Subscription OO-style
115 $ptra = Blerg::Database->open_existing('a');
116 $ptrb = Blerg::Database->open_existing('b');
117 @list = $ptra->subscription_list;
118 ok( @list == 1 );
119 ok( $list[0]->{author} eq 'b' );
120 ok( $list[0]->{record} == 0 );
121 ok( $ptrb->fetch($list[0]->{record}) eq 'Hello, A!' );
122 $ptrb->close;
123 $ptra->close;
124
125 END {
126         chdir;
127         remove_tree "/tmp/blerg_test_$$";
128 }