fix a couple stray regexen
[dbsrgits/DBIx-Class.git] / t / run / 01core.tl
CommitLineData
0567538f 1sub run_tests {
1edaf6fe 2my $schema = shift;
0567538f 3
4c4ccf29 4plan tests => 41;
0567538f 5
f9db5527 6my @art = $schema->resultset("Artist")->search({ }, { order_by => 'name DESC'});
0567538f 7
8cmp_ok(@art, '==', 3, "Three artists returned");
9
10my $art = $art[0];
11
12is($art->name, 'We Are Goth', "Correct order too");
13
14$art->name('We Are In Rehab');
15
16is($art->name, 'We Are In Rehab', "Accessor update ok");
17
18is($art->get_column("name"), 'We Are In Rehab', 'And via get_column');
19
20ok($art->update, 'Update run');
21
f9db5527 22@art = $schema->resultset("Artist")->search({ name => 'We Are In Rehab' });
0567538f 23
24cmp_ok(@art, '==', 1, "Changed artist returned by search");
25
26cmp_ok($art[0]->artistid, '==', 3,'Correct artist too');
27
28$art->delete;
29
f9db5527 30@art = $schema->resultset("Artist")->search({ });
0567538f 31
32cmp_ok(@art, '==', 2, 'And then there were two');
33
34ok(!$art->in_storage, "It knows it's dead");
35
36eval { $art->delete; };
37
38ok($@, "Can't delete twice: $@");
39
40is($art->name, 'We Are In Rehab', 'But the object is still live');
41
42$art->insert;
43
44ok($art->in_storage, "Re-created");
45
f9db5527 46@art = $schema->resultset("Artist")->search({ });
0567538f 47
48cmp_ok(@art, '==', 3, 'And now there are three again');
49
f9db5527 50my $new = $schema->resultset("Artist")->create({ artistid => 4 });
0567538f 51
52cmp_ok($new->artistid, '==', 4, 'Create produced record ok');
53
f9db5527 54@art = $schema->resultset("Artist")->search({ });
0567538f 55
56cmp_ok(@art, '==', 4, "Oh my god! There's four of them!");
57
58$new->set_column('name' => 'Man With A Fork');
59
60is($new->name, 'Man With A Fork', 'set_column ok');
61
62$new->discard_changes;
63
64ok(!defined $new->name, 'Discard ok');
65
66$new->name('Man With A Spoon');
67
68$new->update;
69
f9db5527 70$new_again = $schema->resultset("Artist")->find(4);
0567538f 71
72is($new_again->name, 'Man With A Spoon', 'Retrieved correctly');
73
9bbd8963 74is($new_again->ID, 'DBICTest::Artist|artist|artistid=4', 'unique object id generated correctly');
1f6715ab 75
f9db5527 76is($schema->resultset("Artist")->count, 4, 'count ok');
0567538f 77
f9db5527 78my $cd = $schema->resultset("CD")->find(1);
076a6864 79my %cols = $cd->get_columns;
80
81cmp_ok(keys %cols, '==', 4, 'get_columns number of columns ok');
82
83is($cols{title}, 'Spoonful of bees', 'get_columns values ok');
84
85%cols = ( title => 'Forkful of bees', year => 2005);
86$cd->set_columns(\%cols);
87
88is($cd->title, 'Forkful of bees', 'set_columns ok');
89
90is($cd->year, 2005, 'set_columns ok');
91
92$cd->discard_changes;
93
20518cb4 94# check whether ResultSource->columns returns columns in order originally supplied
95my @cd = $schema->source("CD")->columns;
571dced3 96
20518cb4 97is_deeply( \@cd, [qw/cdid artist title year/], 'column order');
571dced3 98
f9db5527 99$cd = $schema->resultset("CD")->search({ title => 'Spoonful of bees' }, { cols => ['title'] })->next;
90f3f5ff 100is($cd->title, 'Spoonful of bees', 'subset of columns returned correctly');
101
5ac6a044 102$cd = $schema->resultset("CD")->search(undef, { include_columns => [ 'artist.name' ], join => [ 'artist' ] })->find(1);
103
104is($cd->title, 'Spoonful of bees', 'Correct CD returned with include');
105is($cd->get_column('name'), 'Caterwauler McCrae', 'Additional column returned');
106
0567538f 107# insert_or_update
f9db5527 108$new = $schema->resultset("Track")->new( {
0567538f 109 trackid => 100,
110 cd => 1,
111 position => 1,
112 title => 'Insert or Update',
113} );
114$new->insert_or_update;
115ok($new->in_storage, 'insert_or_update insert ok');
116
117# test in update mode
91b0fbd7 118$new->pos(5);
0567538f 119$new->insert_or_update;
91b0fbd7 120is( $schema->resultset("Track")->find(100)->pos, 5, 'insert_or_update update ok');
0567538f 121
8c49f629 122eval { $schema->class("Track")->load_components('DoesNotExist'); };
0567538f 123
124ok $@, $@;
125
1edaf6fe 126is($schema->class("Artist")->field_name_for->{name}, 'artist name', 'mk_classdata usage ok');
90e6de6c 127
54540863 128my $search = [ { 'tags.tag' => 'Cheesy' }, { 'tags.tag' => 'Blue' } ];
129
f9db5527 130my $or_rs = $schema->resultset("CD")->search($search, { join => 'tags',
6aeb9185 131 order_by => 'cdid' });
54540863 132
6aeb9185 133cmp_ok($or_rs->count, '==', 5, 'Search with OR ok');
54540863 134
f9db5527 135my $distinct_rs = $schema->resultset("CD")->search($search, { join => 'tags', distinct => 1 });
54540863 136
6aeb9185 137cmp_ok($distinct_rs->all, '==', 4, 'DISTINCT search with OR ok');
138
f9db5527 139my $tag_rs = $schema->resultset('Tag')->search(
6aeb9185 140 [ { 'me.tag' => 'Cheesy' }, { 'me.tag' => 'Blue' } ]);
141
142my $rel_rs = $tag_rs->search_related('cd');
143
144cmp_ok($rel_rs->count, '==', 5, 'Related search ok');
145
146cmp_ok($or_rs->next->cdid, '==', $rel_rs->next->cdid, 'Related object ok');
54540863 147
a953d8d9 148
4c4ccf29 149my $tag = $schema->resultset('Tag')->search(
150 [ { 'me.tag' => 'Blue' } ], { cols=>[qw/tagid/] } )->next;
151
152cmp_ok($tag->has_column_loaded('tagid'), '==', 1, 'Has tagid loaded');
153cmp_ok($tag->has_column_loaded('tag'), '==', 0, 'Has not tag loaded');
154
a953d8d9 155ok($schema->storage(), 'Storage available');
156
0009fa49 157$schema->source("Artist")->{_columns}{'artistid'} = {};
158
a953d8d9 159my $typeinfo = $schema->source("Artist")->column_info('artistid');
160is($typeinfo->{data_type}, 'INTEGER', 'column_info ok');
5afa2a15 161$schema->source("Artist")->column_info('artistid');
162ok($schema->source("Artist")->{_columns_info_loaded} == 1, 'Columns info flag set');
163
0567538f 164}
165
1661;