]> Dogcows Code - chaz/p5-DBIx-Class-ResultSet-RecursiveUpdate/blob - t/lib/RunTests.pm
m2m accepts one element instead of an arrayref; id as generic pk
[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 => 30;
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 is ( $dvd->name, 'Test name', 'Dvd name set' );
80 # changing existing records
81
82 my $num_of_users = $user_rs->count;
83 $updates = {
84 id => $dvd->dvd_id, # id instead of dvd_id
85 aaaa => undef,
86 name => undef,
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 my $dvd_updated = $dvd_rs->recursive_update( $updates );
100
101 is ( $dvd_updated->dvd_id, $dvd->dvd_id, 'Pk from "id"' );
102 is ( $schema->resultset( 'User' )->count, $initial_user_count + 1, "No new user created" );
103 is ( $dvd_updated->name, undef, 'Dvd name deleted' );
104 is ( $dvd_updated->owner->id, $another_owner->id, 'Owner updated' );
105 is ( $dvd_updated->current_borrower->name, 'new name a', 'Related record modified' );
106 is ( $dvd_updated->tags->count, 0, 'Tags deleted' );
107 is ( $dvd_updated->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 id => $user->id,
139 address => {
140 street => "101 Main Street",
141 city => "Podunk",
142 state => "New York"
143 }
144 };
145 $user = $user_rs->recursive_update( $updates );
146 $user = $user_rs->recursive_update( $updates );
147 is( $schema->resultset( 'Address' )->search({ user_id => $user->id })->count, 1,
148 'the right number of addresses' );
149
150
151 # $updates = {
152 # name => 'Test name 1',
153 # };
154 # $dvd = $dvd_rs->search( { id => $dvd->id } )->recursive_update( $updates, [ 'id' ] );
155 # is ( $dvd->name, 'Test name 1', 'Dvd name set in a resultset with restricted id' );
156 }
This page took 0.043579 seconds and 4 git commands to generate.