File: simple.t

package info (click to toggle)
libdbix-class-cursor-cached-perl 1.001004-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 184 kB
  • sloc: perl: 2,141; makefile: 2
file content (152 lines) | stat: -rw-r--r-- 4,006 bytes parent folder | download | duplicates (5)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
BEGIN {
  package SchemaClass::CD;

  use base qw(DBIx::Class::Core);

  __PACKAGE__->table('cd');
  __PACKAGE__->add_columns(
    'id' => {
      data_type => 'integer',
      is_auto_increment => 1,
    },
    'title' => {
      data_type => 'varchar',
      size      => 100,
    },
  );
  __PACKAGE__->set_primary_key('id');

  package SchemaClass;

  use base qw(DBIx::Class::Schema);

  __PACKAGE__->register_class(CD => 'SchemaClass::CD');

  sub deploy {
    my $self = shift;
    $self->storage->dbh->do(q{
      CREATE TABLE cd (
        id INTEGER PRIMARY KEY NOT NULL,
        title varchar(100) NOT NULL
      );
    });
  }

  sub init {
    my $self = shift;
    $self->deploy;
    $self->resultset('CD')->populate([
      map { { title => $_ } } 'CD one', 'CD two'
    ]);
  }

}

use Cache::FileCache;
use DBIx::Class::Cursor::Cached;
use Test::More 'no_plan';

mkdir('t/var');
unlink('t/var/test.db');

my ($dsn, $user, $pass) = ('dbi:SQLite:t/var/test.db');

SchemaClass->connect($dsn,$user,$pass)->init;

my $expect_data = [ [ 1, 'CD one' ], [ 2, 'CD two' ] ];

{ ## start test block

  my $schema = SchemaClass->connect(
    $dsn, $user, $pass, { cursor_class => 'DBIx::Class::Cursor::Cached' }
  );

  $schema->default_resultset_attributes({
    cache_object => Cache::FileCache->new({ namespace => 'SchemaClass' }),
  });

my $cache = $schema->default_resultset_attributes->{cache_object};

  my $rs = $schema->resultset('CD')->search(undef, { cache_for => 300 });

  my @cds = $rs->all; # fills cache

is_deeply([ map { [ $_->id, $_->title ] } @cds ], $expect_data,
  'correct data in objects');
is_deeply($cache->get($rs->cursor->cache_key), $expect_data,
  'correct data in cache');

  $rs = $schema->resultset('CD')->search(undef, { cache_for => 300 });
    # refresh resultset

$schema->storage->disconnect;

  @cds = $rs->all; # uses cache, no SQL run

ok(!$schema->storage->connected, 'no reconnect made since no SQL required');
is_deeply([ map { [ $_->id, $_->title ] } @cds ], $expect_data,
  'correct data in objects');
is_deeply($cache->get($rs->cursor->cache_key), $expect_data,
  'correct data in cache');

  $rs->cursor->clear_cache; # deletes data from cache

ok(!defined($cache->get($rs->cursor->cache_key)), 'cache cleared');

  @cds = (); while (my $rec = $rs->next) { push(@cds, $rec); }

is_deeply([ map { [ $_->id, $_->title ] } @cds ], $expect_data,
  'correct data in objects');
is_deeply($cache->get($rs->cursor->cache_key), $expect_data,
  'correct data in cache');

}

{
  my $schema = SchemaClass->connect(
    sub {
      DBI->connect('dbi:SQLite:t/var/test.db', '', '', { RaiseError => 1 }) },
        { cursor_class => 'DBIx::Class::Cursor::Cached' }
  );

  $schema->default_resultset_attributes({
    cache_object => Cache::FileCache->new({ namespace => 'SchemaClass' }),
  });

my $cache = $schema->default_resultset_attributes->{cache_object};

  my $rs = $schema->resultset('CD')->search(undef, { cache_for => 300 });

  my @cds = $rs->all; # fills cache

is_deeply([ map { [ $_->id, $_->title ] } @cds ], $expect_data,
  'correct data in objects');
is_deeply($cache->get($rs->cursor->cache_key), $expect_data,
  'correct data in cache');

  $rs = $schema->resultset('CD')->search(undef, { cache_for => 300 });
    # refresh resultset

$schema->storage->disconnect;

  @cds = $rs->all; # uses cache, no SQL run

ok(!$schema->storage->connected, 'no reconnect made since no SQL required');
is_deeply([ map { [ $_->id, $_->title ] } @cds ], $expect_data,
  'correct data in objects');
is_deeply($cache->get($rs->cursor->cache_key), $expect_data,
  'correct data in cache');

  $rs->cursor->clear_cache; # deletes data from cache

ok(!defined($cache->get($rs->cursor->cache_key)), 'cache cleared');

  @cds = (); while (my $rec = $rs->next) { push(@cds, $rec); }

is_deeply([ map { [ $_->id, $_->title ] } @cds ], $expect_data,
  'correct data in objects');
is_deeply($cache->get($rs->cursor->cache_key), $expect_data,
  'correct data in cache');
}