one less warning in tests
[chaz/p5-DBIx-Class-ResultSet-RecursiveUpdate] / t / lib / RunTests.pm
1 # -*- perl -*-
2 package RunTests;
3 use Exporter 'import'; # gives you Exporter's import() method directly
4 @EXPORT = qw(run_tests);
5 use strict;
6 use Test::More;
7
8
9 sub run_tests{
10 my $schema = shift;
11
12 plan tests => 27;
13
14 my $dvd_rs = $schema->resultset( 'Dvd' );
15 my $user_rs = $schema->resultset( 'User' );
16
17 my $owner = $user_rs->next;
18 my $another_owner = $user_rs->next;
19 my $initial_user_count = $user_rs->count;
20 my $initial_dvd_count = $dvd_rs->count;
21 my $updates;
22
23 # creating new record linked to some old record
24 $updates = {
25 name => 'Test name 2',
26 viewings => [ { user_id => $owner->id } ],
27 owner => { id => $another_owner->id },
28 };
29
30 my $new_dvd = $dvd_rs->recursive_update( $updates );
31 # my $new_dvd = $dvd_rs->create( $updates );
32
33 is ( $dvd_rs->count, $initial_dvd_count + 1, 'Dvd created' );
34 is ( $schema->resultset( 'User' )->count, $initial_user_count, "No new user created" );
35 is ( $new_dvd->name, 'Test name 2', 'Dvd name set' );
36 is ( $new_dvd->owner->id, $another_owner->id, 'Owner set' );
37 is ( $new_dvd->viewings->count, 1, 'Viewing created' );
38
39 # creating new records
40 $updates = {
41 aaaa => undef,
42 tags => [ '2', { id => '3' } ],
43 name => 'Test name',
44 owner => $owner,
45 current_borrower => {
46 name => 'temp name',
47 username => 'temp name',
48 password => 'temp name',
49 },
50 liner_notes => {
51 notes => 'test note',
52 },
53 like_has_many => [
54 { key2 => 1 }
55 ],
56 like_has_many2 => [
57 {
58 onekey => { name => 'aaaaa' },
59 key2 => 1
60 }
61 ],
62 };
63
64 my $dvd = $dvd_rs->recursive_update( $updates );
65 ;
66 is ( $dvd_rs->count, $initial_dvd_count + 2, 'Dvd created' );
67 is ( $schema->resultset( 'User' )->count, $initial_user_count + 1, "One new user created" );
68 is ( $dvd->name, 'Test name', 'Dvd name set' );
69 is_deeply ( [ map {$_->id} $dvd->tags ], [ '2', '3' ], 'Tags set' );
70 is ( $dvd->owner->id, $owner->id, 'Owner set' );
71
72 is ( $dvd->current_borrower->name, 'temp name', 'Related record created' );
73 is ( $dvd->liner_notes->notes, 'test note', 'might_have record created' );
74 ok ( $schema->resultset( 'Twokeys' )->find( { dvd_name => 'Test name', key2 => 1 } ), 'Twokeys created' );
75 my $onekey = $schema->resultset( 'Onekey' )->search( name => 'aaaaa' )->first;
76 ok ( $onekey, 'Onekey created' );
77 ok ( $schema->resultset( 'Twokeys_belongsto' )->find( { key1 => $onekey->id, key2 => 1 } ), 'Twokeys created' );
78
79
80 # changing existing records
81
82 my $num_of_users = $user_rs->count;
83 $updates = {
84 id => $dvd->id,
85 aaaa => undef,
86 name => 'Test name',
87 tags => [ ],
88 'owner' => $another_owner->id,
89 current_borrower => {
90 username => 'new name a',
91 name => 'new name a',
92 password => 'new password a',
93 },
94 liner_notes => {
95 notes => 'test note changed',
96 },
97
98 };
99 $dvd = $dvd_rs->recursive_update( $updates );
100
101 is ( $schema->resultset( 'User' )->count, $initial_user_count + 1, "No new user created" );
102 is ( $dvd->name, 'Test name', 'Dvd name set' );
103 is ( $dvd->owner->id, $another_owner->id, 'Owner updated' );
104 is ( $dvd->current_borrower->name, 'new name a', 'Related record modified' );
105 is ( $dvd->tags->count, 0, 'Tags deleted' );
106 is ( $dvd->liner_notes->notes, 'test note changed', 'might_have record changed' );
107
108 # repeatable
109
110 $updates = {
111 name => 'temp name',
112 username => 'temp username',
113 password => 'temp username',
114 owned_dvds =>[
115 {
116 'name' => 'temp name 1',
117 'tags' => [ 1, 2 ],
118 },
119 {
120 'name' => 'temp name 2',
121 'tags' => [ 2, 3 ],
122 }
123 ]
124 };
125
126 my $user = $user_rs->recursive_update( $updates );
127 is ( $schema->resultset( 'User' )->count, $initial_user_count + 2, "New user created" );
128 is ( $dvd_rs->count, $initial_dvd_count + 4, 'Dvds created' );
129 my %owned_dvds = map { $_->name => $_ } $user->owned_dvds;
130 is( scalar keys %owned_dvds, 2, 'Has many relations created' );
131 ok( $owned_dvds{'temp name 1'}, 'Name in a has_many related record saved' );
132 my @tags = $owned_dvds{'temp name 1'}->tags;
133 is( scalar @tags, 2, 'Tags in has_many related record saved' );
134 ok( $owned_dvds{'temp name 2'}, 'Second name in a has_many related record saved' );
135
136 # $updates = {
137 # name => 'Test name 1',
138 # };
139 # $dvd = $dvd_rs->search( { id => $dvd->id } )->recursive_update( $updates, [ 'id' ] );
140 # is ( $dvd->name, 'Test name 1', 'Dvd name set in a resultset with restricted id' );
141 }
This page took 0.048263 seconds and 5 git commands to generate.