a53692cc58a630919665b9c88237b5321407062d
[dbsrgits/DBIx-Class.git] / t / prefetch / bugs.t
1 use warnings;
2 use strict;
3
4 use Test::More;
5
6 @INC{qw(Test::Schema::Foo Test::Schema::Baz)} = (1) x 2;
7
8 package Test::Schema::Foo;
9 use 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
19 package Test::Schema::Baz;
20 use 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
28 package Test::Schema;
29 use parent 'DBIx::Class::Schema';
30 __PACKAGE__->register_source(
31   $_ => "Test::Schema::$_"->result_source_instance )
32   for qw(Foo Baz);
33
34 package main;
35
36 my $schema = Test::Schema->connect( 'dbi:SQLite:dbname=:memory:', '', '' );
37 $schema->deploy;
38
39 my $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
47 my $baz_rs = $schema->resultset('Baz');
48 ok( $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 } } );
54 ok( $baz_rs->search( {}, { prefetch => 'foos' } )->first->foos->first );
55
56 done_testing();