]> Dogcows Code - chaz/p5-DBIx-Class-ResultSet-RecursiveUpdate/blob - t/lib/RunTests.pm
code for resolving - fix
[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 sub run_tests{
9 my $schema = shift;
10
11 my $dvd_rs = $schema->resultset( 'Dvd' );
12 my $user_rs = $schema->resultset( 'User' );
13
14 my $owner = $user_rs->next;
15 my $another_owner = $user_rs->next;
16 my $initial_user_count = $user_rs->count;
17 my $initial_dvd_count = $dvd_rs->count;
18
19 # creating new record linked to some old record
20
21 my $updates;
22 $updates = {
23 id => undef,
24 name => 'Test name 2',
25 viewings => [ { user_id => $owner->id } ],
26 owner => { id => $owner->id },
27 };
28
29 my $new_dvd = $dvd_rs->recursive_update( $updates );
30 # my $new_dvd = $dvd_rs->create( $updates );
31
32 ok ( $new_dvd->isa( 'DBSchema::Result::Dvd' ), 'Dvd created' );
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, $owner->id, 'Owner set' );
37 is ( $new_dvd->viewings->count, 1, 'Viewing created' );
38 ;
39 # creating new records
40 my $updates = {
41 id => undef,
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 };
58
59 my $dvd = $dvd_rs->recursive_update( $updates );
60 ;
61 is ( $dvd_rs->count, $initial_dvd_count + 2, 'Dvd created' );
62 is ( $schema->resultset( 'User' )->count, $initial_user_count + 1, "One new user created" );
63 is ( $dvd->name, 'Test name', 'Dvd name set' );
64 is_deeply ( [ map {$_->id} $dvd->tags ], [ '2', '3' ], 'Tags set' );
65 is ( $dvd->owner->id, $owner->id, 'Owner set' );
66
67 is ( $dvd->current_borrower->name, 'temp name', 'Related record created' );
68 is ( $dvd->liner_notes->notes, 'test note', 'might_have record created' );
69 ok ( $schema->resultset( 'Twokeys' )->find( { dvd_name => 'Test name', key2 => 1 } ), 'Twokeys created' );
70 # changing existing records
71
72 my $num_of_users = $user_rs->count;
73 $updates = {
74 id => $dvd->id,
75 aaaa => undef,
76 name => 'Test name',
77 tags => [ ],
78 'owner' => $another_owner->id,
79 current_borrower => {
80 username => 'new name a',
81 name => 'new name a',
82 password => 'new password a',
83 }
84 };
85 $dvd = $dvd_rs->recursive_update( $updates );
86
87 is ( $schema->resultset( 'User' )->count, $initial_user_count + 1, "No new user created" );
88 is ( $dvd->name, 'Test name', 'Dvd name set' );
89 is ( $dvd->owner->id, $another_owner->id, 'Owner updated' );
90 is ( $dvd->current_borrower->name, 'new name a', 'Related record modified' );
91 is ( $dvd->tags->count, 0, 'Tags deleted' );
92
93 # repeatable
94
95 $updates = {
96 id => undef,
97 name => 'temp name',
98 username => 'temp username',
99 password => 'temp username',
100 owned_dvds =>[
101 {
102 'id' => undef,
103 'name' => 'temp name 1',
104 'tags' => [ 1, 2 ],
105 },
106 {
107 'id' => undef,
108 'name' => 'temp name 2',
109 'tags' => [ 2, 3 ],
110 }
111 ]
112 };
113
114 my $user = $user_rs->recursive_update( $updates );
115 my %owned_dvds = map { $_->name => $_ } $user->owned_dvds;
116 is( scalar keys %owned_dvds, 2, 'Has many relations created' );
117 ok( $owned_dvds{'temp name 1'}, 'Name in a has_many related record saved' );
118 my @tags = $owned_dvds{'temp name 1'}->tags;
119 is( scalar @tags, 2, 'Tags in has_many related record saved' );
120 ok( $owned_dvds{'temp name 2'}, 'Second name in a has_many related record saved' );
121
122 }
This page took 0.046382 seconds and 4 git commands to generate.