Commit | Line | Data |
0567538f |
1 | sub run_tests { |
1edaf6fe |
2 | my $schema = shift; |
0567538f |
3 | |
e6a8b3fc |
4 | plan tests => 58; |
0567538f |
5 | |
f2de4889 |
6 | # figure out if we've got a version of sqlite that is older than 3.2.6, in |
7 | # which case COUNT(DISTINCT()) doesn't work |
8 | my $is_broken_sqlite = 0; |
9 | my ($sqlite_major_ver,$sqlite_minor_ver,$sqlite_patch_ver) = |
10 | split /\./, $schema->storage->dbh->get_info(18); |
11 | if( $schema->storage->dbh->get_info(17) eq 'SQLite' && |
12 | ( ($sqlite_major_ver < 3) || |
13 | ($sqlite_major_ver == 3 && $sqlite_minor_ver < 2) || |
14 | ($sqlite_major_ver == 3 && $sqlite_minor_ver == 2 && $sqlite_patch_ver < 6) ) ) { |
15 | $is_broken_sqlite = 1; |
16 | } |
17 | |
0567538f |
18 | |
f9db5527 |
19 | my @art = $schema->resultset("Artist")->search({ }, { order_by => 'name DESC'}); |
0567538f |
20 | |
21 | cmp_ok(@art, '==', 3, "Three artists returned"); |
22 | |
23 | my $art = $art[0]; |
24 | |
25 | is($art->name, 'We Are Goth', "Correct order too"); |
26 | |
27 | $art->name('We Are In Rehab'); |
28 | |
29 | is($art->name, 'We Are In Rehab', "Accessor update ok"); |
30 | |
31 | is($art->get_column("name"), 'We Are In Rehab', 'And via get_column'); |
32 | |
33 | ok($art->update, 'Update run'); |
34 | |
f9db5527 |
35 | @art = $schema->resultset("Artist")->search({ name => 'We Are In Rehab' }); |
0567538f |
36 | |
37 | cmp_ok(@art, '==', 1, "Changed artist returned by search"); |
38 | |
39 | cmp_ok($art[0]->artistid, '==', 3,'Correct artist too'); |
40 | |
41 | $art->delete; |
42 | |
f9db5527 |
43 | @art = $schema->resultset("Artist")->search({ }); |
0567538f |
44 | |
45 | cmp_ok(@art, '==', 2, 'And then there were two'); |
46 | |
47 | ok(!$art->in_storage, "It knows it's dead"); |
48 | |
49 | eval { $art->delete; }; |
50 | |
51 | ok($@, "Can't delete twice: $@"); |
52 | |
53 | is($art->name, 'We Are In Rehab', 'But the object is still live'); |
54 | |
55 | $art->insert; |
56 | |
57 | ok($art->in_storage, "Re-created"); |
58 | |
f9db5527 |
59 | @art = $schema->resultset("Artist")->search({ }); |
0567538f |
60 | |
61 | cmp_ok(@art, '==', 3, 'And now there are three again'); |
62 | |
f9db5527 |
63 | my $new = $schema->resultset("Artist")->create({ artistid => 4 }); |
0567538f |
64 | |
65 | cmp_ok($new->artistid, '==', 4, 'Create produced record ok'); |
66 | |
f9db5527 |
67 | @art = $schema->resultset("Artist")->search({ }); |
0567538f |
68 | |
69 | cmp_ok(@art, '==', 4, "Oh my god! There's four of them!"); |
70 | |
71 | $new->set_column('name' => 'Man With A Fork'); |
72 | |
73 | is($new->name, 'Man With A Fork', 'set_column ok'); |
74 | |
75 | $new->discard_changes; |
76 | |
77 | ok(!defined $new->name, 'Discard ok'); |
78 | |
79 | $new->name('Man With A Spoon'); |
80 | |
81 | $new->update; |
82 | |
f9db5527 |
83 | $new_again = $schema->resultset("Artist")->find(4); |
0567538f |
84 | |
85 | is($new_again->name, 'Man With A Spoon', 'Retrieved correctly'); |
86 | |
9bbd8963 |
87 | is($new_again->ID, 'DBICTest::Artist|artist|artistid=4', 'unique object id generated correctly'); |
1f6715ab |
88 | |
a87eb971 |
89 | # Test backwards compatibility |
90 | { |
91 | my $artist_by_hash = $schema->resultset('Artist')->find(artistid => 4); |
92 | is($artist_by_hash->name, 'Man With A Spoon', 'Retrieved correctly'); |
93 | is($artist_by_hash->ID, 'DBICTest::Artist|artist|artistid=4', 'unique object id generated correctly'); |
94 | } |
95 | |
f9db5527 |
96 | is($schema->resultset("Artist")->count, 4, 'count ok'); |
0567538f |
97 | |
b3e1f1f5 |
98 | # test find_or_new |
99 | { |
100 | my $existing_obj = $schema->resultset('Artist')->find_or_new({ |
101 | artistid => 4, |
102 | }); |
103 | |
104 | is($existing_obj->name, 'Man With A Spoon', 'find_or_new: found existing artist'); |
105 | ok($existing_obj->in_storage, 'existing artist is in storage'); |
106 | |
107 | my $new_obj = $schema->resultset('Artist')->find_or_new({ |
108 | artistid => 5, |
109 | name => 'find_or_new', |
110 | }); |
111 | |
112 | is($new_obj->name, 'find_or_new', 'find_or_new: instantiated a new artist'); |
113 | ok(! $new_obj->in_storage, 'new artist is not in storage'); |
114 | } |
115 | |
f9db5527 |
116 | my $cd = $schema->resultset("CD")->find(1); |
076a6864 |
117 | my %cols = $cd->get_columns; |
118 | |
119 | cmp_ok(keys %cols, '==', 4, 'get_columns number of columns ok'); |
120 | |
121 | is($cols{title}, 'Spoonful of bees', 'get_columns values ok'); |
122 | |
123 | %cols = ( title => 'Forkful of bees', year => 2005); |
124 | $cd->set_columns(\%cols); |
125 | |
126 | is($cd->title, 'Forkful of bees', 'set_columns ok'); |
127 | |
128 | is($cd->year, 2005, 'set_columns ok'); |
129 | |
130 | $cd->discard_changes; |
131 | |
20518cb4 |
132 | # check whether ResultSource->columns returns columns in order originally supplied |
133 | my @cd = $schema->source("CD")->columns; |
571dced3 |
134 | |
20518cb4 |
135 | is_deeply( \@cd, [qw/cdid artist title year/], 'column order'); |
571dced3 |
136 | |
82a96700 |
137 | $cd = $schema->resultset("CD")->search({ title => 'Spoonful of bees' }, { columns => ['title'] })->next; |
90f3f5ff |
138 | is($cd->title, 'Spoonful of bees', 'subset of columns returned correctly'); |
139 | |
5ac6a044 |
140 | $cd = $schema->resultset("CD")->search(undef, { include_columns => [ 'artist.name' ], join => [ 'artist' ] })->find(1); |
141 | |
142 | is($cd->title, 'Spoonful of bees', 'Correct CD returned with include'); |
143 | is($cd->get_column('name'), 'Caterwauler McCrae', 'Additional column returned'); |
144 | |
82a96700 |
145 | # update_or_insert |
f9db5527 |
146 | $new = $schema->resultset("Track")->new( { |
0567538f |
147 | trackid => 100, |
148 | cd => 1, |
149 | position => 1, |
150 | title => 'Insert or Update', |
151 | } ); |
82a96700 |
152 | $new->update_or_insert; |
153 | ok($new->in_storage, 'update_or_insert insert ok'); |
0567538f |
154 | |
155 | # test in update mode |
91b0fbd7 |
156 | $new->pos(5); |
82a96700 |
157 | $new->update_or_insert; |
158 | is( $schema->resultset("Track")->find(100)->pos, 5, 'update_or_insert update ok'); |
0567538f |
159 | |
8c49f629 |
160 | eval { $schema->class("Track")->load_components('DoesNotExist'); }; |
0567538f |
161 | |
162 | ok $@, $@; |
163 | |
1edaf6fe |
164 | is($schema->class("Artist")->field_name_for->{name}, 'artist name', 'mk_classdata usage ok'); |
90e6de6c |
165 | |
54540863 |
166 | my $search = [ { 'tags.tag' => 'Cheesy' }, { 'tags.tag' => 'Blue' } ]; |
167 | |
5b89a768 |
168 | my( $or_rs ) = $schema->resultset("CD")->search_rs($search, { join => 'tags', |
6aeb9185 |
169 | order_by => 'cdid' }); |
54540863 |
170 | |
6aeb9185 |
171 | cmp_ok($or_rs->count, '==', 5, 'Search with OR ok'); |
54540863 |
172 | |
f9db5527 |
173 | my $distinct_rs = $schema->resultset("CD")->search($search, { join => 'tags', distinct => 1 }); |
6aeb9185 |
174 | cmp_ok($distinct_rs->all, '==', 4, 'DISTINCT search with OR ok'); |
175 | |
f2de4889 |
176 | SKIP: { |
177 | skip "SQLite < 3.2.6 doesn't understand COUNT(DISTINCT())", 1 |
178 | if $is_broken_sqlite; |
179 | |
180 | my $tcount = $schema->resultset("Track")->search( |
286f32b3 |
181 | {}, |
f2de4889 |
182 | { |
183 | select => {count => {distinct => ['position', 'title']}}, |
184 | as => ['count'] |
286f32b3 |
185 | } |
186 | ); |
f2de4889 |
187 | cmp_ok($tcount->next->get_column('count'), '==', 13, 'multiple column COUNT DISTINCT ok'); |
286f32b3 |
188 | |
f2de4889 |
189 | } |
f9db5527 |
190 | my $tag_rs = $schema->resultset('Tag')->search( |
6aeb9185 |
191 | [ { 'me.tag' => 'Cheesy' }, { 'me.tag' => 'Blue' } ]); |
192 | |
193 | my $rel_rs = $tag_rs->search_related('cd'); |
194 | |
195 | cmp_ok($rel_rs->count, '==', 5, 'Related search ok'); |
196 | |
197 | cmp_ok($or_rs->next->cdid, '==', $rel_rs->next->cdid, 'Related object ok'); |
a4731ae0 |
198 | $or_rs->reset; |
199 | $rel_rs->reset; |
a953d8d9 |
200 | |
4c4ccf29 |
201 | my $tag = $schema->resultset('Tag')->search( |
202 | [ { 'me.tag' => 'Blue' } ], { cols=>[qw/tagid/] } )->next; |
203 | |
204 | cmp_ok($tag->has_column_loaded('tagid'), '==', 1, 'Has tagid loaded'); |
205 | cmp_ok($tag->has_column_loaded('tag'), '==', 0, 'Has not tag loaded'); |
206 | |
a953d8d9 |
207 | ok($schema->storage(), 'Storage available'); |
208 | |
16b4fd26 |
209 | { |
210 | my $rs = $schema->resultset("Artist")->search({ |
211 | -and => [ |
212 | artistid => { '>=', 1 }, |
213 | artistid => { '<', 3 } |
214 | ] |
215 | }); |
216 | |
217 | $rs->update({ name => 'Test _cond_for_update_delete' }); |
218 | |
219 | my $art; |
220 | |
221 | $art = $schema->resultset("Artist")->find(1); |
222 | is($art->name, 'Test _cond_for_update_delete', 'updated first artist name'); |
223 | |
224 | $art = $schema->resultset("Artist")->find(2); |
225 | is($art->name, 'Test _cond_for_update_delete', 'updated second artist name'); |
226 | } |
227 | |
825135d8 |
228 | # test source_name |
229 | { |
230 | # source_name should be set for normal modules |
231 | is($schema->source('CD')->source_name, 'CD', 'source_name is set to moniker'); |
a4731ae0 |
232 | |
825135d8 |
233 | # test the result source that sets source_name explictly |
234 | ok($schema->source('SourceNameArtists'), 'SourceNameArtists result source exists'); |
0009fa49 |
235 | |
825135d8 |
236 | my @artsn = $schema->resultset('SourceNameArtists')->search({}, { order_by => 'name DESC' }); |
237 | cmp_ok(@artsn, '==', 4, "Four artists returned"); |
238 | } |
bab77431 |
239 | |
9c2c91ea |
240 | my $newbook = $schema->resultset( 'Bookmark' )->find(1); |
241 | |
242 | $@ = ''; |
243 | eval { |
244 | my $newlink = $newbook->link; |
245 | }; |
246 | ok(!$@, "stringify to false value doesn't cause error"); |
247 | |
825135d8 |
248 | # test cascade_delete through many_to_many relations |
249 | { |
250 | my $art_del = $schema->resultset("Artist")->find({ artistid => 1 }); |
251 | $art_del->delete; |
252 | cmp_ok( $schema->resultset("CD")->search({artist => 1}), '==', 0, 'Cascading through has_many top level.'); |
253 | cmp_ok( $schema->resultset("CD_to_Producer")->search({cd => 1}), '==', 0, 'Cascading through has_many children.'); |
254 | } |
bab77431 |
255 | |
825135d8 |
256 | # test column_info |
257 | { |
258 | $schema->source("Artist")->{_columns}{'artistid'} = {}; |
bab77431 |
259 | |
825135d8 |
260 | my $typeinfo = $schema->source("Artist")->column_info('artistid'); |
261 | is($typeinfo->{data_type}, 'INTEGER', 'column_info ok'); |
262 | $schema->source("Artist")->column_info('artistid'); |
263 | ok($schema->source("Artist")->{_columns_info_loaded} == 1, 'Columns info flag set'); |
264 | } |
bab77431 |
265 | |
825135d8 |
266 | # test remove_columns |
267 | { |
268 | is_deeply([$schema->source('CD')->columns], [qw/cdid artist title year/]); |
269 | $schema->source('CD')->remove_columns('year'); |
270 | is_deeply([$schema->source('CD')->columns], [qw/cdid artist title/]); |
271 | } |
bab77431 |
272 | |
0567538f |
273 | } |
274 | |
275 | 1; |