Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 208 lines (158 sloc) 5.565 kb
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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
use strict;
use warnings;

use Test::More;
use Test::Exception;
use lib qw(t/lib);
use DBICTest;
use Storable qw(dclone freeze nfreeze thaw);
use Scalar::Util qw/refaddr/;
use Carp;

sub ref_ne {
  my ($refa, $refb) = map { refaddr $_ or croak "$_ is not a reference!" } @_[0,1];
  cmp_ok (
    $refa,
      '!=',
    $refb,
    sprintf ('%s (0x%07x != 0x%07x)',
      $_[2],
      $refa,
      $refb,
    ),
  );
}

my $schema = DBICTest->init_schema;

my %stores = (
    dclone_method => sub { return $schema->dclone($_[0]) },
    dclone_func => sub {
      local $DBIx::Class::ResultSourceHandle::thaw_schema = $schema;
      return dclone($_[0])
    },
    "freeze/thaw_method" => sub {
      my $ice = $schema->freeze($_[0]);
      return $schema->thaw($ice);
    },
    "nfreeze/thaw_func" => sub {
      my $ice = freeze($_[0]);
      local $DBIx::Class::ResultSourceHandle::thaw_schema = $schema;
      return thaw($ice);
    },

    "freeze/thaw_func (cdbi legacy)" => sub {
      # this one is special-cased to leak the $schema all over
      # the same way as cdbi-compat does
      DBICTest::Artist->result_source_instance->schema($schema);
      DBICTest::CD->result_source_instance->schema($schema);

      my $fire = thaw(freeze($_[0]));

      # clean up the mess
      $_->result_source_instance->schema(undef)
        for map { $schema->class ($_) } $schema->sources;

      return $fire;
    },

);

if ($ENV{DBICTEST_MEMCACHED}) {
  if (DBIx::Class::Optional::Dependencies->req_ok_for ('test_memcached')) {
    my $memcached = Cache::Memcached->new(
      { servers => [ $ENV{DBICTEST_MEMCACHED} ] }
    );

    my $key = 'tmp_dbic_84serialize_memcached_test';

    $stores{memcached} = sub {
      $memcached->set( $key, $_[0], 60 )
        or die "Unable to insert into $ENV{DBICTEST_MEMCACHED} - is server running?";
      local $DBIx::Class::ResultSourceHandle::thaw_schema = $schema;
      return $memcached->get($key);
    };
  }
  else {
    SKIP: {
      skip 'Memcached tests need ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_memcached'), 1;
    }
  }
}
else {
  SKIP: {
    skip 'Set $ENV{DBICTEST_MEMCACHED} to run the memcached serialization tests', 1;
  }
}



for my $name (keys %stores) {

    my $store = $stores{$name};
    my $copy;

    my $artist = $schema->resultset('Artist')->find(1);

    lives_ok { $copy = $store->($artist) } "serialize row object lives: $name";
    ref_ne($copy, $artist, 'Simple row cloned');
    is_deeply($copy, $artist, "serialize row object works: $name");

    my $cd_rs = $artist->search_related("cds");

    # test that a live result source can be serialized as well
    is( $cd_rs->count, 3, '3 CDs in database');
    ok( $cd_rs->next, 'Advance cursor' );

    lives_ok {
      $copy = $store->($cd_rs);

      ref_ne($copy, $artist, 'Simple row cloned');

      is_deeply (
        [ $copy->all ],
        [ $cd_rs->all ],
        "serialize resultset works: $name",
      );
    } "serialize resultset lives: $name";

    # Test that an object with a related_resultset can be serialized.
    ok $artist->{related_resultsets}, 'has key: related_resultsets';

    lives_ok { $copy = $store->($artist) } "serialize row object with related_resultset lives: $name";
    for my $key (keys %$artist) {
        next if $key eq 'related_resultsets';
        next if $key eq '_inflated_column';

        ref_ne($copy->{$key}, $artist->{$key}, "Simple row internals cloned '$key'")
          if ref $artist->{$key};

        is_deeply($copy->{$key}, $artist->{$key},
                  qq[serialize with related_resultset '$key']);
    }

    lives_ok(
      sub { $copy->discard_changes }, "Discard changes works: $name"
    ) or diag $@;
    is($copy->id, $artist->id, "IDs still match ");


    # Test resultsource with cached rows
    my $query_count;
    $cd_rs = $cd_rs->search ({}, { cache => 1 });

    my $orig_debug = $schema->storage->debug;
    $schema->storage->debug(1);
    $schema->storage->debugcb(sub { $query_count++ } );

    # this will hit the database once and prime the cache
    my @cds = $cd_rs->all;

    lives_ok {
      $copy = $store->($cd_rs);
      ref_ne($copy, $cd_rs, 'Cached resultset cloned');
      is_deeply (
        [ $copy->all ],
        [ $cd_rs->all ],
        "serialize cached resultset works: $name",
      );

      is ($copy->count, $cd_rs->count, 'Cached count identical');
    } "serialize cached resultset lives: $name";

    is ($query_count, 1, 'Only one db query fired');

    $schema->storage->debug($orig_debug);
    $schema->storage->debugcb(undef);
}

# test schema-less detached thaw
{
  my $artist = $schema->resultset('Artist')->find(1);

  $artist = dclone $artist;

  is( $artist->name, 'Caterwauler McCrae', 'getting column works' );

  ok( $artist->update, 'Non-dirty update noop' );

  ok( $artist->name( 'Beeeeeeees' ), 'setting works' );

  ok( $artist->is_column_changed( 'name' ), 'Column dirtyness works' );
  ok( $artist->is_changed, 'object dirtyness works' );

  my $rs = $artist->result_source->resultset;
  $rs->set_cache([ $artist ]);

  is( $rs->count, 1, 'Synthetic resultset count works' );

  my $exc = qr/Unable to perform storage-dependent operations with a detached result source.+use \$schema->thaw/;

  throws_ok { $artist->update }
    $exc,
    'Correct exception on row op'
  ;

  throws_ok { $artist->discard_changes }
    $exc,
    'Correct exception on row op'
  ;

  throws_ok { $rs->find(1) }
    $exc,
    'Correct exception on rs op'
  ;
}

done_testing;
Something went wrong with that request. Please try again.