hashrefs in m2m
[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 my $dvd_rs = $schema->resultset( 'Dvd' );
13 my $owner = $schema->resultset( 'User' )->first;
14 my $initial_user_count = $schema->resultset( 'User' )->count;
15
16 # creating new records
17
18 my $updates = {
19 id => undef,
20 aaaa => undef,
21 tags => [ '2', { id => '3' } ],
22 name => 'Test name',
23 # 'creation_date.year' => 2002,
24 # 'creation_date.month' => 1,
25 # 'creation_date.day' => 3,
26 # 'creation_date.hour' => 4,
27 # 'creation_date.minute' => 33,
28 # 'creation_date.pm' => 1,
29 owner => $owner->id,
30 current_borrower => {
31 name => 'temp name',
32 username => 'temp name',
33 password => 'temp name',
34 },
35 liner_notes => {
36
37 notes => 'test note',
38 }
39 };
40
41 my $dvd = $dvd_rs->recursive_update( $updates );
42
43 is ( $schema->resultset( 'User' )->count, $initial_user_count + 1, "One new user created" );
44 is ( $dvd->name, 'Test name', 'Dvd name set' );
45 is_deeply ( [ map {$_->id} $dvd->tags ], [ '2', '3' ], 'Tags set' );
46 #my $value = $dvd->creation_date;
47 #is( "$value", '2002-01-03T16:33:00', 'Date set');
48 is ( $dvd->owner->id, $owner->id, 'Owner set' );
49
50 is ( $dvd->current_borrower->name, 'temp name', 'Related record created' );
51 is ( $dvd->liner_notes->notes, 'test note', 'might_have record created' );
52
53 # changing existing records
54
55 $updates = {
56 id => $dvd->id,
57 aaaa => undef,
58 name => 'Test name',
59 tags => [ ],
60 'owner' => $owner->id,
61 current_borrower => {
62 username => 'new name a',
63 name => 'new name a',
64 password => 'new password a',
65 }
66 };
67 $dvd = $dvd_rs->recursive_update( $updates );
68
69 is ( $schema->resultset( 'User' )->count, $initial_user_count + 1, "No new user created" );
70 is ( $dvd->name, 'Test name', 'Dvd name set' );
71 is ( $dvd->owner->id, $owner->id, 'Owner set' );
72 is ( $dvd->current_borrower->name, 'new name a', 'Related record modified' );
73 is ( $dvd->tags->count, 0, 'Tags deleted' );
74
75 # repeatable
76
77 $updates = {
78 id => undef,
79 name => 'temp name',
80 username => 'temp username',
81 password => 'temp username',
82 owned_dvds =>[
83 {
84 'id' => undef,
85 'name' => 'temp name 1',
86 'tags' => [ 1, 2 ],
87 },
88 {
89 'id' => undef,
90 'name' => 'temp name 2',
91 'tags' => [ 2, 3 ],
92 }
93 ]
94 };
95
96 my $user_rs = $schema->resultset( 'User' );
97 my $user = $user_rs->recursive_update( $updates );
98 my %owned_dvds = map { $_->name => $_ } $user->owned_dvds;
99 is( scalar keys %owned_dvds, 2, 'Has many relations created' );
100 ok( $owned_dvds{'temp name 1'}, 'Name in a has_many related record saved' );
101 my @tags = $owned_dvds{'temp name 1'}->tags;
102 is( scalar @tags, 2, 'Tags in has_many related record saved' );
103 ok( $owned_dvds{'temp name 2'}, 'Second name in a has_many related record saved' );
104 }
This page took 0.037671 seconds and 4 git commands to generate.