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