1f31adacc9a5357728e399b8b4476ebe31230c40
[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 => 28;
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
22 # creating new record linked to some old record
23
24 my $updates;
25 $updates = {
26 name => 'Test name 2',
27 viewings => [ { user_id => $owner->id } ],
28 owner => { id => $another_owner->id },
29 };
30
31 my $new_dvd = $dvd_rs->recursive_update( $updates );
32 # my $new_dvd = $dvd_rs->create( $updates );
33
34 is ( $dvd_rs->count, $initial_dvd_count + 1, 'Dvd created' );
35 is ( $schema->resultset( 'User' )->count, $initial_user_count, "No new user created" );
36 is ( $new_dvd->name, 'Test name 2', 'Dvd name set' );
37 is ( $new_dvd->owner->id, $another_owner->id, 'Owner set' );
38 is ( $new_dvd->viewings->count, 1, 'Viewing created' );
39
40 # creating new records
41 my $updates = {
42 aaaa => undef,
43 tags => [ '2', { id => '3' } ],
44 name => 'Test name',
45 owner => $owner,
46 current_borrower => {
47 name => 'temp name',
48 username => 'temp name',
49 password => 'temp name',
50 },
51 liner_notes => {
52 notes => 'test note',
53 },
54 like_has_many => [
55 { key2 => 1 }
56 ],
57 like_has_many2 => [
58 {
59 onekey => { name => 'aaaaa' },
60 key2 => 1
61 }
62 ],
63 };
64
65 my $dvd = $dvd_rs->recursive_update( $updates );
66 ;
67 is ( $dvd_rs->count, $initial_dvd_count + 2, 'Dvd created' );
68 is ( $schema->resultset( 'User' )->count, $initial_user_count + 1, "One new user created" );
69 is ( $dvd->name, 'Test name', 'Dvd name set' );
70 is_deeply ( [ map {$_->id} $dvd->tags ], [ '2', '3' ], 'Tags set' );
71 is ( $dvd->owner->id, $owner->id, 'Owner set' );
72
73 is ( $dvd->current_borrower->name, 'temp name', 'Related record created' );
74 is ( $dvd->liner_notes->notes, 'test note', 'might_have record created' );
75 ok ( $schema->resultset( 'Twokeys' )->find( { dvd_name => 'Test name', key2 => 1 } ), 'Twokeys created' );
76 my $onekey = $schema->resultset( 'Onekey' )->search( name => 'aaaaa' )->first;
77 ok ( $onekey, 'Onekey created' );
78 ok ( $schema->resultset( 'Twokeys_belongsto' )->find( { key1 => $onekey->id, key2 => 1 } ), 'Twokeys created' );
79
80
81 # changing existing records
82
83 my $num_of_users = $user_rs->count;
84 $updates = {
85 id => $dvd->id,
86 aaaa => undef,
87 name => 'Test name',
88 tags => [ ],
89 'owner' => $another_owner->id,
90 current_borrower => {
91 username => 'new name a',
92 name => 'new name a',
93 password => 'new password a',
94 },
95 liner_notes => {
96 notes => 'test note changed',
97 },
98
99 };
100 $dvd = $dvd_rs->recursive_update( $updates );
101
102 is ( $schema->resultset( 'User' )->count, $initial_user_count + 1, "No new user created" );
103 is ( $dvd->name, 'Test name', 'Dvd name set' );
104 is ( $dvd->owner->id, $another_owner->id, 'Owner updated' );
105 is ( $dvd->current_borrower->name, 'new name a', 'Related record modified' );
106 is ( $dvd->tags->count, 0, 'Tags deleted' );
107 is ( $dvd->liner_notes->notes, 'test note changed', 'might_have record changed' );
108
109 $updates = {
110 name => 'Test name 1',
111 };
112 $dvd = $dvd_rs->search( { id => $dvd->id } )->recursive_update( $updates, [ 'id' ] );
113 is ( $dvd->name, 'Test name 1', 'Dvd name set in a resultset with restricted id' );
114
115 # repeatable
116
117 $updates = {
118 name => 'temp name',
119 username => 'temp username',
120 password => 'temp username',
121 owned_dvds =>[
122 {
123 'name' => 'temp name 1',
124 'tags' => [ 1, 2 ],
125 },
126 {
127 'name' => 'temp name 2',
128 'tags' => [ 2, 3 ],
129 }
130 ]
131 };
132
133 my $user = $user_rs->recursive_update( $updates );
134 is ( $schema->resultset( 'User' )->count, $initial_user_count + 2, "New user created" );
135 is ( $dvd_rs->count, $initial_dvd_count + 4, 'Dvds created' );
136 my %owned_dvds = map { $_->name => $_ } $user->owned_dvds;
137 is( scalar keys %owned_dvds, 2, 'Has many relations created' );
138 ok( $owned_dvds{'temp name 1'}, 'Name in a has_many related record saved' );
139 my @tags = $owned_dvds{'temp name 1'}->tags;
140 is( scalar @tags, 2, 'Tags in has_many related record saved' );
141 ok( $owned_dvds{'temp name 2'}, 'Second name in a has_many related record saved' );
142
143 }
This page took 0.050833 seconds and 3 git commands to generate.