Populate caches for related result sets even if they're empty
[dbsrgits/DBIx-Class.git] / t / 85utf8.t
CommitLineData
70350518 1use strict;
55087b99 2use warnings;
e59c17fe 3
70350518 4use Test::More;
d38cd95c 5use Test::Warn;
70350518 6use lib qw(t/lib);
7use DBICTest;
e59c17fe 8
72ae8e40 9{
10 package A::Comp;
11 use base 'DBIx::Class';
12 sub store_column { shift->next::method (@_) };
13 1;
14}
15
16{
17 package A::SubComp;
18 use base 'A::Comp';
1415f198 19
72ae8e40 20 1;
21}
7146f619 22
72ae8e40 23warnings_are (
24 sub {
3c2a505c 25 local $ENV{DBIC_UTF8COLUMNS_OK} = 1;
1415f198 26 package A::Test1;
72ae8e40 27 use base 'DBIx::Class::Core';
28 __PACKAGE__->load_components(qw(Core +A::Comp Ordered UTF8Columns));
1415f198 29 __PACKAGE__->load_components(qw(Ordered +A::SubComp Row UTF8Columns Core));
30 sub store_column { shift->next::method (@_) };
72ae8e40 31 1;
32 },
33 [],
34 'no spurious warnings issued',
35);
36
70c28808 37warnings_like (
38 sub {
eed5492f 39 local $ENV{DBIC_UTF8COLUMNS_OK};
70c28808 40 package A::Test1Loud;
41 use base 'DBIx::Class::Core';
42 __PACKAGE__->load_components(qw(Core +A::Comp Ordered UTF8Columns));
43 __PACKAGE__->load_components(qw(Ordered +A::SubComp Row UTF8Columns Core));
44 sub store_column { shift->next::method (@_) };
45 1;
46 },
47 [qr/Use of DBIx::Class::UTF8Columns is strongly discouraged/],
48 'issued deprecation warning',
49);
50
51
1415f198 52my $test1_mro;
72ae8e40 53my $idx = 0;
1415f198 54for (@{mro::get_linear_isa ('A::Test1')} ) {
55 $test1_mro->{$_} = $idx++;
72ae8e40 56}
57
1415f198 58cmp_ok ($test1_mro->{'A::SubComp'}, '<', $test1_mro->{'A::Comp'}, 'mro of Test1 correct (A::SubComp before A::Comp)' );
59cmp_ok ($test1_mro->{'A::Comp'}, '<', $test1_mro->{'DBIx::Class::UTF8Columns'}, 'mro of Test1 correct (A::Comp before UTF8Col)' );
60cmp_ok ($test1_mro->{'DBIx::Class::UTF8Columns'}, '<', $test1_mro->{'DBIx::Class::Core'}, 'mro of Test1 correct (UTF8Col before Core)' );
61cmp_ok ($test1_mro->{'DBIx::Class::Core'}, '<', $test1_mro->{'DBIx::Class::Row'}, 'mro of Test1 correct (Core before Row)' );
62
1415f198 63warnings_like (
64 sub {
65 package A::Test2;
66 use base 'DBIx::Class::Core';
67 __PACKAGE__->load_components(qw(UTF8Columns +A::Comp));
68 sub store_column { shift->next::method (@_) };
69 1;
70 },
71 [qr/Incorrect loading order of DBIx::Class::UTF8Columns.+affect other components overriding 'store_column' \(A::Comp\)/],
72 'incorrect order warning issued (violator defines)',
73);
74
75warnings_like (
76 sub {
77 package A::Test3;
78 use base 'DBIx::Class::Core';
79 __PACKAGE__->load_components(qw(UTF8Columns +A::SubComp));
80 sub store_column { shift->next::method (@_) };
81 1;
82 },
83 [qr/Incorrect loading order of DBIx::Class::UTF8Columns.+affect other components overriding 'store_column' \(A::SubComp \(via A::Comp\)\)/],
84 'incorrect order warning issued (violator inherits)',
85);
72ae8e40 86
a47e1233 87my $schema = DBICTest->init_schema();
404939a4 88DBICTest::Schema::CD->load_components('UTF8Columns');
89DBICTest::Schema::CD->utf8_columns('title');
cc8ffd7b 90Class::C3->reinitialize() if DBIx::Class::_ENV_::OLD_MRO;
70350518 91
fed2c7d7 92# as per http://search.cpan.org/dist/Test-Simple/lib/Test/More.pm#utf8
93binmode (Test::More->builder->$_, ':utf8') for qw/output failure_output todo_output/;
cdd254a5 94
95my $bytestream_title = my $utf8_title = "weird \x{466} stuff";
96utf8::encode($bytestream_title);
97cmp_ok ($bytestream_title, 'ne', $utf8_title, 'unicode/raw differ (sanity check)');
98
2cfc22dd 99my $cd;
4ca1fd6f 100{
cdd254a5 101 local $TODO = "This has been broken since rev 1191, Mar 2006";
2cfc22dd 102
103 $schema->is_executed_sql_bind( sub {
104 $cd = $schema->resultset('CD')->create( { artist => 1, title => $utf8_title, year => '2048' } )
105 }, [[
106 'INSERT INTO cd ( artist, title, year) VALUES ( ?, ?, ? )',
107 [ { dbic_colname => "artist", sqlt_datatype => "integer" }
108 => 1 ],
109 [ { dbic_colname => "title", sqlt_datatype => "varchar", sqlt_size => 100 }
110 => $bytestream_title ],
111 [ { dbic_colname => "year", sqlt_datatype => "varchar", sqlt_size => 100 }
112 => 2048 ],
113 ]], 'INSERT: raw bytes sent to the database' );
114};
cdd254a5 115
116# this should be using the cursor directly, no inflation/processing of any sort
117my ($raw_db_title) = $schema->resultset('CD')
118 ->search ($cd->ident_condition)
119 ->get_column('title')
120 ->_resultset
121 ->cursor
122 ->next;
123
124is ($raw_db_title, $bytestream_title, 'INSERT: raw bytes retrieved from database');
125
126for my $reloaded (0, 1) {
127 my $test = $reloaded ? 'reloaded' : 'stored';
128 $cd->discard_changes if $reloaded;
1d0057bd 129
cdd254a5 130 ok( utf8::is_utf8( $cd->title ), "got $test title with utf8 flag" );
131 ok(! utf8::is_utf8( $cd->{_column_data}{title} ), "in-object $test title without utf8" );
132
133 ok(! utf8::is_utf8( $cd->year ), "got $test year without utf8 flag" );
134 ok(! utf8::is_utf8( $cd->{_column_data}{year} ), "in-object $test year without utf8" );
135}
136
137$cd->title('nonunicode');
138ok(! utf8::is_utf8( $cd->title ), 'update title without utf8 flag' );
139ok(! utf8::is_utf8( $cd->{_column_data}{title} ), 'store utf8-less title' );
140
141$cd->update;
142$cd->discard_changes;
143ok(! utf8::is_utf8( $cd->title ), 'reloaded title without utf8 flag' );
144ok(! utf8::is_utf8( $cd->{_column_data}{title} ), 'reloaded utf8-less title' );
145
146$bytestream_title = $utf8_title = "something \x{219} else";
147utf8::encode($bytestream_title);
148
2cfc22dd 149$schema->is_executed_sql_bind( sub {
150 $cd->update ({ title => $utf8_title });
151}, [
152 [ 'BEGIN' ],
153 [
154 'UPDATE cd SET title = ? WHERE cdid = ?',
155 [ { dbic_colname => "title", sqlt_datatype => "varchar", sqlt_size => 100 }
156 => $bytestream_title ],
157 [ { dbic_colname => "cdid", sqlt_datatype => "integer" }
158 => 6 ],
159 ],
160 [ 'COMMIT' ],
161], 'UPDATE: raw bytes sent to the database');
5cfb131d 162
cdd254a5 163($raw_db_title) = $schema->resultset('CD')
164 ->search ($cd->ident_condition)
165 ->get_column('title')
166 ->_resultset
167 ->cursor
168 ->next;
169is ($raw_db_title, $bytestream_title, 'UPDATE: raw bytes retrieved from database');
170
171$cd->discard_changes;
172$cd->title($utf8_title);
1d0057bd 173ok( !$cd->is_column_changed('title'), 'column is not dirty after setting the same unicode value' );
174
cdd254a5 175$cd->update ({ title => $utf8_title });
1d0057bd 176$cd->title('something_else');
177ok( $cd->is_column_changed('title'), 'column is dirty after setting to something completely different');
db087eb1 178
4ca1fd6f 179{
db087eb1 180 local $TODO = 'There is currently no way to propagate aliases to inflate_result()';
cdd254a5 181 $cd = $schema->resultset('CD')->find ({ title => $utf8_title }, { select => 'title', as => 'name' });
55087b99 182 ok (utf8::is_utf8( $cd->get_column ('name') ), 'utf8 flag propagates via as');
db087eb1 183}
184
55087b99 185done_testing;