failing test for prefetch bug where prefetched values of 0 might break inflation
[dbsrgits/DBIx-Class.git] / t / prefetch / bugs.t
CommitLineData
571df676 1use warnings;
2use strict;
3
4use Test::More;
5
6@INC{qw(Test::Schema::Foo Test::Schema::Baz)} = (1) x 2;
7
8package Test::Schema::Foo;
9use parent 'DBIx::Class';
10__PACKAGE__->load_components(qw(Core));
11__PACKAGE__->table('foo');
12__PACKAGE__->add_columns(qw(id bar_id));
13__PACKAGE__->set_primary_key('id');
14__PACKAGE__->belongs_to(
15 baz => 'Test::Schema::Baz',
16 { 'foreign.id' => 'self.bar_id' }
17);
18
19package Test::Schema::Baz;
20use parent 'DBIx::Class';
21__PACKAGE__->load_components(qw(Core));
22__PACKAGE__->table('baz');
23__PACKAGE__->add_columns(qw(id quux));
24__PACKAGE__->set_primary_key('id');
25__PACKAGE__->has_many(
26 foos => 'Test::Schema::Foo' => { 'foreign.bar_id' => 'self.id' } );
27
28package Test::Schema;
29use parent 'DBIx::Class::Schema';
30__PACKAGE__->register_source(
31 $_ => "Test::Schema::$_"->result_source_instance )
32 for qw(Foo Baz);
33
34package main;
35
36my $schema = Test::Schema->connect( 'dbi:SQLite:dbname=:memory:', '', '' );
37$schema->deploy;
38
39my $foo_rs = $schema->resultset('Foo');
40# create a condition that guarantees all values have 0 in them,
41# which makes the inflation process skip the row because of:
42# next unless first { defined $_ } values %{$me_pref->[0]};
43# all values need to be zero to ensure that the arbitrary order in
44# which values() returns the results doesn't break the test
45$foo_rs->create( { id => 0, baz => { id => 0, quux => 0 } } );
46
47my $baz_rs = $schema->resultset('Baz');
48ok( $baz_rs->search( {}, { prefetch => 'foos' } )->first->foos->first );
49
50$foo_rs->delete;
51$baz_rs->delete;
52
53$foo_rs->create( { id => 1, baz => { id => 1, quux => 1 } } );
54ok( $baz_rs->search( {}, { prefetch => 'foos' } )->first->foos->first );
55
56done_testing();