summaryrefslogtreecommitdiffstats
path: root/t/cache.t
blob: ded5ad7641211cd8d59d6b0e679df3c3bfe4c3c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use FindBin;
use Test::More;
use DBI;
use lib "$FindBin::Bin/lib";
use lib "$FindBin::Bin/../lib";

BEGIN 
{
  eval { require DBD::CSV; }
    or plan skip_all => 
    "DBD::CSV is needed for this test";

  eval { require Cache::Memcached; }
    or plan skip_all => 
    "Cache::Memcached is needed for this test";

  plan tests => 10;

  unlink('baz_table') if ( -e 'baz_table' );
  unlink('bar_table') if ( -e 'bar_table' );
  my $dbh = DBI->connect('DBI:CSV:f_dir=./') or die $DBI::errstr;
  $dbh->do("create table baz_table (id INTEGER, name CHAR(255))") or $dbh->errstr;
  $dbh->do("insert into baz_table values (1,'ford')") or $dbh->errstr;
  $dbh->do("insert into baz_table values (2,'sam')") or $dbh->errstr;
  $dbh->do("insert into baz_table values (3,'frodo')") or $dbh->errstr;
  $dbh->do("insert into baz_table values (4,'zaphod')") or $dbh->errstr;
  $dbh->do("create table bar_table (id INTEGER, name CHAR(255), baz INTEGER)") or $dbh->errstr;
  $dbh->do("insert into bar_table values (1,'prefect',1)") or $dbh->errstr;
  $dbh->do("insert into bar_table values (2,'gamgee',2)") or $dbh->errstr;
  $dbh->do("insert into bar_table values (3,'baggins',3)") or $dbh->errstr;
  $dbh->do("insert into bar_table values (4,'beeblebrox',4)") or $dbh->errstr;
  $dbh->disconnect();

  use_ok('Bar');
  use_ok('Baz');
}

ok(Bar->cache(1),'setting cache');
ok(Bar->cache_expires(60),'setting cache expiration');
ok(Bar->cache_servers(qw/127.0.0.1:11211/),'setting cache servers');
ok(Bar->compress_threshold(10_000),'setting cache servers');

my @objs = ();
for my $id (1 .. 4)
{
  push @objs, Bar->retrieve($id);
}

ok(4 == @objs,'retrieve all objects');
ok(4 == scalar keys %{$Jaos::DBI::cache->get_multi( qw/Bar:1 Bar:2 Bar:3 Bar:4/ )},'check cache is initially populated');

# extract our baz objects into the cache
$_->baz foreach Bar->retrieve_all();

ok(8 == scalar keys %{$Jaos::DBI::cache->get_multi( qw/Bar:1 Bar:2 Bar:3 Bar:4 Baz:1 Baz:2 Baz:3 Baz:4/ )},'check cache is populated');

ok(!Bar->cache(0),'unsetting cache');

END
{
  unlink('bar_table');
  unlink('baz_table');
}