Entire test suite passes under DBIC_TRACE=1
[dbsrgits/DBIx-Class.git] / t / row / inflate_result.t
CommitLineData
db4e9221 1package My::Schema::Result::User;
2
3use strict;
4use warnings;
5use base qw/DBIx::Class::Core/;
6
7### Define what our admin class is, for ensure_class_loaded()
8my $admin_class = __PACKAGE__ . '::Admin';
9
10__PACKAGE__->table('users');
11
12__PACKAGE__->add_columns(
13 qw/user_id email password
14 firstname lastname active
15 admin/
16);
17
18__PACKAGE__->set_primary_key('user_id');
19
20sub inflate_result {
21 my $self = shift;
22 my $ret = $self->next::method(@_);
23 if ( $ret->admin ) { ### If this is an admin, rebless for extra functions
24 $self->ensure_class_loaded($admin_class);
25 bless $ret, $admin_class;
26 }
27 return $ret;
28}
29
30sub hello {
31 return "I am a regular user.";
32}
33
34package My::Schema::Result::User::Admin;
35
36use strict;
37use warnings;
38use base qw/My::Schema::Result::User/;
39
40# This line is important
41__PACKAGE__->table('users');
42
43sub hello {
44 return "I am an admin.";
45}
46
47sub do_admin_stuff {
48 return "I am doing admin stuff";
49}
50
51package My::Schema;
52
53use base qw/DBIx::Class::Schema/;
54
55My::Schema->register_class( Admin => 'My::Schema::Result::User::Admin' );
56My::Schema->register_class( User => 'My::Schema::Result::User' );
57
581;
59
60package main;
61
62use lib qw(t/lib);
63use DBICTest;
64
65use Test::More;
66
67my $user_data = {
68 email => 'someguy@place.com',
69 password => 'pass1',
70 admin => 0
71};
72
73my $admin_data = {
74 email => 'someadmin@adminplace.com',
75 password => 'pass2',
76 admin => 1
77};
78
d9c17594 79ok( my $schema = My::Schema->connection(DBICTest->_database) );
db4e9221 80
81ok(
82 $schema->storage->dbh->do(
83"create table users (user_id, email, password, firstname, lastname, active, admin)"
84 )
85);
86
87TODO: {
88 local $TODO = 'New objects should also be inflated';
d4a20f38 89 my $user = $schema->resultset('User')->create($user_data);
90 my $admin = $schema->resultset('User')->create($admin_data);
db4e9221 91
92 is( ref $user, 'My::Schema::Result::User' );
93 is( ref $admin, 'My::Schema::Result::User::Admin' );
db4e9221 94}
95
96my $user = $schema->resultset('User')->single($user_data);
97my $admin = $schema->resultset('User')->single($admin_data);
98
99is( ref $user, 'My::Schema::Result::User' );
100is( ref $admin, 'My::Schema::Result::User::Admin' );
101
102is( $user->password, 'pass1' );
103is( $admin->password, 'pass2' );
104is( $user->hello, 'I am a regular user.' );
105is( $admin->hello, 'I am an admin.' );
106
107ok( !$user->can('do_admin_stuff') );
108ok( $admin->can('do_admin_stuff') );
109
110done_testing;