]> Dogcows Code - chaz/p5-DBIx-Class-ResultSet-RecursiveUpdate/blob - t/lib/RunTests.pm
d0fce655ea60f4d259c79d919d42aca15796cb2d
[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 name => 'Test name 2',
24 viewings => [ { user_id => $owner->id } ],
25 owner => { id => $owner->id },
26 };
27
28 my $new_dvd = $dvd_rs->recursive_update( $updates );
29 # my $new_dvd = $dvd_rs->create( $updates );
30
31 ok ( $new_dvd->isa( 'DBSchema::Result::Dvd' ), 'Dvd created' );
32 is ( $dvd_rs->count, $initial_dvd_count + 1, 'Dvd created' );
33 is ( $schema->resultset( 'User' )->count, $initial_user_count, "No new user created" );
34 is ( $new_dvd->name, 'Test name 2', 'Dvd name set' );
35 is ( $new_dvd->owner->id, $owner->id, 'Owner set' );
36 is ( $new_dvd->viewings->count, 1, 'Viewing created' );
37 ;
38 # creating new records
39 my $updates = {
40 aaaa => undef,
41 tags => [ '2', { id => '3' } ],
42 name => 'Test name',
43 owner => $owner,
44 current_borrower => {
45 name => 'temp name',
46 username => 'temp name',
47 password => 'temp name',
48 },
49 liner_notes => {
50 notes => 'test note',
51 },
52 like_has_many => [
53 { key2 => 1 }
54 ],
55 };
56
57 my $dvd = $dvd_rs->recursive_update( $updates );
58 ;
59 is ( $dvd_rs->count, $initial_dvd_count + 2, 'Dvd created' );
60 is ( $schema->resultset( 'User' )->count, $initial_user_count + 1, "One new user created" );
61 is ( $dvd->name, 'Test name', 'Dvd name set' );
62 is_deeply ( [ map {$_->id} $dvd->tags ], [ '2', '3' ], 'Tags set' );
63 is ( $dvd->owner->id, $owner->id, 'Owner set' );
64
65 is ( $dvd->current_borrower->name, 'temp name', 'Related record created' );
66 is ( $dvd->liner_notes->notes, 'test note', 'might_have record created' );
67 ok ( $schema->resultset( 'Twokeys' )->find( { dvd_name => 'Test name', key2 => 1 } ), 'Twokeys created' );
68 # changing existing records
69
70 my $num_of_users = $user_rs->count;
71 $updates = {
72 id => $dvd->id,
73 aaaa => undef,
74 name => 'Test name',
75 tags => [ ],
76 'owner' => $another_owner->id,
77 current_borrower => {
78 username => 'new name a',
79 name => 'new name a',
80 password => 'new password a',
81 }
82 };
83 $dvd = $dvd_rs->recursive_update( $updates );
84
85 is ( $schema->resultset( 'User' )->count, $initial_user_count + 1, "No new user created" );
86 is ( $dvd->name, 'Test name', 'Dvd name set' );
87 is ( $dvd->owner->id, $another_owner->id, 'Owner updated' );
88 is ( $dvd->current_borrower->name, 'new name a', 'Related record modified' );
89 is ( $dvd->tags->count, 0, 'Tags deleted' );
90
91 # repeatable
92
93 $updates = {
94 id => undef,
95 name => 'temp name',
96 username => 'temp username',
97 password => 'temp username',
98 owned_dvds =>[
99 {
100 'id' => undef,
101 'name' => 'temp name 1',
102 'tags' => [ 1, 2 ],
103 },
104 {
105 'id' => undef,
106 'name' => 'temp name 2',
107 'tags' => [ 2, 3 ],
108 }
109 ]
110 };
111
112 my $user = $user_rs->recursive_update( $updates );
113 my %owned_dvds = map { $_->name => $_ } $user->owned_dvds;
114 is( scalar keys %owned_dvds, 2, 'Has many relations created' );
115 ok( $owned_dvds{'temp name 1'}, 'Name in a has_many related record saved' );
116 my @tags = $owned_dvds{'temp name 1'}->tags;
117 is( scalar @tags, 2, 'Tags in has_many related record saved' );
118 ok( $owned_dvds{'temp name 2'}, 'Second name in a has_many related record saved' );
119
120 }
This page took 0.038754 seconds and 3 git commands to generate.