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