Fix check for blessedness and minor cleanup in many_to_many helper
[dbsrgits/DBIx-Class.git] / t / relationship / set_column_on_fk.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7
8 my $schema = DBICTest->init_schema();
9
10
11 # test with relname == colname
12 my $bookmark = $schema->resultset("Bookmark")->find(1);
13 ok( $bookmark->has_column ('link'), 'Right column name' );
14 ok( $bookmark->has_relationship ('link'), 'Right rel name' );
15
16 my $link = $bookmark->link;
17
18 my $new_link = $schema->resultset("Link")->create({
19   url     => "http://bugsarereal.com",
20   title   => "bugsarereal.com",
21   id      => 9,
22 });
23
24 is( $bookmark->link->id, 1, 'Initial relation id' );
25
26 $bookmark->set_column( 'link', 9 );
27 is( $bookmark->link->id, 9, 'Correct object re-selected after belongs_to set' );
28
29 $bookmark->discard_changes;
30 is( $bookmark->link->id, 1, 'Pulled the correct old object after belongs_to reset' );
31
32
33 $bookmark->link($new_link);
34 is( $bookmark->get_column('link'), 9, 'Correct column set from related' );
35
36 $bookmark->discard_changes;
37 is( $bookmark->link->id, 1, 'Pulled the correct old object after belongs_to reset' );
38
39
40 $bookmark->link(9);
41 is( $bookmark->link->id, 9, 'Correct object selected on deflated accessor set');
42
43 $bookmark->discard_changes;
44 is( $bookmark->link->id, 1, 'Pulled the correct old object after belongs_to reset' );
45
46
47 $bookmark->update({ link => 9 });
48 is( $bookmark->link->id, 9, 'Correct relationship after update' );
49 is( $bookmark->get_from_storage->link->id, 9, 'Correct relationship after re-select' );
50
51
52 # test with relname != colname
53 my $lyric = $schema->resultset('Lyrics')->create({ track_id => 5 });
54 is( $lyric->track->id, 5, 'Initial relation id');
55
56 $lyric->track_id(6);
57 my $track6 = $lyric->track;
58 is( $track6->trackid, 6, 'Correct object re-selected after belongs_to set');
59
60 $lyric->discard_changes;
61 is( $lyric->track->trackid, 5, 'Pulled the correct old rel object after belongs_to reset');
62
63 $lyric->track($track6);
64 is( $lyric->track_id, 6, 'Correct column set from related');
65
66 $lyric->discard_changes;
67 is( $lyric->track->trackid, 5, 'Pulled the correct old rel object after belongs_to reset');
68
69 $lyric->update({ track => $track6 });
70 is( $lyric->track->trackid, 6, 'Correct relationship obj after update' );
71 is( $lyric->get_from_storage->track->trackid, 6, 'Correct relationship after re-select' );
72
73 done_testing;