]> Dogcows Code - chaz/p5-DBIx-Class-ResultSet-RecursiveUpdate/blob - t/lib/RunTests.pm
compatible with 0.08012
[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
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 # repeatable
110
111 $updates = {
112 name => 'temp name',
113 username => 'temp username',
114 password => 'temp username',
115 owned_dvds =>[
116 {
117 'name' => 'temp name 1',
118 'tags' => [ 1, 2 ],
119 },
120 {
121 'name' => 'temp name 2',
122 'tags' => [ 2, 3 ],
123 }
124 ]
125 };
126
127 my $user = $user_rs->recursive_update( $updates );
128 is ( $schema->resultset( 'User' )->count, $initial_user_count + 2, "New user created" );
129 is ( $dvd_rs->count, $initial_dvd_count + 4, 'Dvds created' );
130 my %owned_dvds = map { $_->name => $_ } $user->owned_dvds;
131 is( scalar keys %owned_dvds, 2, 'Has many relations created' );
132 ok( $owned_dvds{'temp name 1'}, 'Name in a has_many related record saved' );
133 my @tags = $owned_dvds{'temp name 1'}->tags;
134 is( scalar @tags, 2, 'Tags in has_many related record saved' );
135 ok( $owned_dvds{'temp name 2'}, 'Second name in a has_many related record saved' );
136
137 # $updates = {
138 # name => 'Test name 1',
139 # };
140 # $dvd = $dvd_rs->search( { id => $dvd->id } )->recursive_update( $updates, [ 'id' ] );
141 # is ( $dvd->name, 'Test name 1', 'Dvd name set in a resultset with restricted id' );
142 }
This page took 0.046335 seconds and 4 git commands to generate.