first checkin tests fail everywhere but demo works. yay?
[catagits/Reaction.git] / t / lib / RTest / InterfaceModel / Reflector / DBIC.pm
CommitLineData
7adfd53f 1package RTest::InterfaceModel::Reflector::DBIC;
2
3use base qw/Reaction::Test::WithDB Reaction::Object/;
4use Reaction::Class;
5use Class::MOP ();
6use ComponentUI::TestModel;
7use Test::More ();
8use Reaction::InterfaceModel::Reflector::DBIC;
9
10has '+schema_class' => (default => sub { 'RTest::TestDB' });
11
12has im_schema => (is =>'ro', isa => 'RTest::TestIM', lazy_build => 1);
13
14#at the moment I am only testing with the "reflect all" functionality
15#when I have time I will write test cases that cover all the other bases
16#it's just kind of a pain in the ass right now and I am behind on a lot of other shit.
17
18sub build_im_schema{
19 my $self = shift;
20
f670cfd0 21 my $reflector = Reaction::InterfaceModel::Reflector::DBIC->new;
7adfd53f 22
f670cfd0 23 $reflector->reflect_schema(
24 model_class => 'RTest::TestIM',
25 schema_class => 'RTest::TestDB',
26 sources => [qw/Foo Bar Baz/]
7adfd53f 27 );
28 my (@dm) = RTest::TestIM->domain_models;
29 Test::More::ok(@dm == 1, 'Correct number of Domain Models');
30 my $dm = shift @dm;
7adfd53f 31 RTest::TestIM->new($dm->name => $self->schema);
32}
33
34sub test_classnames : Tests{
35 my $self = shift;
36
f670cfd0 37 my $reflector = Reaction::InterfaceModel::Reflector::DBIC->new;
7adfd53f 38
7adfd53f 39
40 Test::More::is(
f670cfd0 41 $reflector->class_name_from_source_name('RTest::__TestIM','Foo'),
7adfd53f 42 'RTest::__TestIM::Foo',
43 'Correct naming scheme for submodels'
44 );
7adfd53f 45 Test::More::is(
f670cfd0 46 $reflector->class_name_for_collection_of('RTest::__TestIM::Foo'),
7adfd53f 47 'RTest::__TestIM::Foo::Collection',
48 'Correct naming scheme for submodel collections'
49 );
50}
51
f670cfd0 52sub test_reflect_schema :Tests {
7adfd53f 53 my $self = shift;
54 my $s = $self->im_schema;
55
f670cfd0 56 Test::More::isa_ok( $s, 'Reaction::InterfaceModel::Object', 'Correct base' );
7adfd53f 57
58 my %pa = map{$_->name => $_ } $s->parameter_attributes;
59 Test::More::ok(keys %pa == 3, 'Correct number of Parameter Attributes');
60
61 Test::More::ok($pa{Foo} && $pa{'Bar'} && $pa{'Baz'},
62 'Parameter Attributes named correctly');
63
64 for my $submodel (values %pa){
65 Test::More::ok(
66 $submodel->_isa_metadata->isa('Reaction::InterfaceModel::Collection::Virtual::ResultSet'),
67 'Parameter Attribute typed correctly'
68 );
69 }
70
71 Test::More::can_ok($s, qw/foo_collection bar_collection baz_collection/);
72
73 for ( qw/Foo Bar Baz/ ){
74 Test::More::ok(
75 Class::MOP::is_class_loaded("RTest::TestIM::${_}"),
76 "Successfully created ${_} IM class"
77 );
78 Test::More::ok(
79 Class::MOP::is_class_loaded("RTest::TestIM::${_}::Collection"),
80 "Successfully created ${_} IM class Collection"
81 );
82 }
83}
84
85
f670cfd0 86sub test_add_source_to_model :Tests {
7adfd53f 87 my $self = shift;
88 my $s = $self->im_schema;
89
90 for (qw/Foo Bar Baz /) {
91 my $attr = $s->meta->find_attribute_by_name($_);
92 my $reader = $_;
93 $reader =~ s/([a-z0-9])([A-Z])/${1}_${2}/g ;
94 $reader = lc($reader) . "_collection";
95
96 Test::More::ok( $attr->is_required, "${_} is required");
97 Test::More::ok( $attr->has_reader, "${_} has a reader");
98 Test::More::ok( $attr->has_predicate, "${_} has a predicate");
99 Test::More::ok( $attr->has_domain_model, "${_} has a domain_model");
100 Test::More::ok( $attr->has_default, "${_} has a default");
101 Test::More::ok( $attr->is_default_a_coderef, "${_}'s defaultis a coderef");
102 Test::More::is( $attr->reader, $reader, "Correct ${_} reader");
f670cfd0 103 Test::More::is( $attr->domain_model, "_rtest_testdb_store", "Correct ${_} domain_model");
7adfd53f 104
105 Test::More::isa_ok(
106 $s->$reader,
107 "RTest::TestIM::${_}::Collection",
108 "${_} default method works"
109 );
110
111 }
112}
113
114sub test_reflect_collection_for :Tests{
115 my $self = shift;
116 my $s = $self->im_schema;
117
118 for ( qw/Foo Bar Baz/ ){
119 my $reader = $s->meta->find_attribute_by_name($_)->reader;
120 my $collection = $s->$reader;
121
122 Test::More::is(
123 $collection->meta->name,
124 "RTest::TestIM::${_}::Collection",
125 "Correct Classname"
126 );
127 Test::More::isa_ok(
128 $collection,
129 'Reaction::InterfaceModel::Collection',
130 "Collection ISA Collection"
131 );
132 Test::More::isa_ok(
133 $collection,
134 'Reaction::InterfaceModel::Collection::Virtual',
135 "Collection ISA virtual collection"
136 );
137 Test::More::isa_ok(
138 $collection,
139 'Reaction::InterfaceModel::Collection::Virtual::ResultSet',
140 "Collection ISA virtual resultset"
141 );
142 Test::More::can_ok($collection, '_build_im_class');
143 Test::More::is(
144 $collection->_build_im_class,
145 "RTest::TestIM::${_}",
146 "Collection has correct _im_class"
147 );
148 }
149}
150
151sub test_reflect_submodel :Tests{
152 my $self = shift;
153 my $s = $self->im_schema;
154
155 for my $sm ( qw/Foo Bar Baz/ ){
156 my $reader = $s->meta->find_attribute_by_name($sm)->reader;
157 my $collection = $s->$reader;
158 my ($member) = $collection->members;
159 Test::More::ok($member, "Successfully retrieved member");
160 Test::More::isa_ok(
161 $member,
162 "Reaction::InterfaceModel::Object",
163 "Member isa IM::Object"
164 );
165 Test::More::isa_ok($member, $collection->_im_class);
166
167 my (@dm) = $member->domain_models;
168 Test::More::ok(@dm == 1, 'Correct number of Domain Models');
169 my $dm = shift @dm;
170
f670cfd0 171 my $dm_name = Reaction::InterfaceModel::Reflector::DBIC
172 ->dm_name_from_source_name($sm);
7adfd53f 173
174 Test::More::is($dm->_is_metadata, "rw", "Correct is metadata");
175 Test::More::ok($dm->is_required, "DM is_required");
176 Test::More::is($dm->name, $dm_name, "Correct DM name");
177 Test::More::can_ok($member, "inflate_result");
178 Test::More::is(
179 $dm->_isa_metadata,
180 "RTest::TestDB::${sm}",
181 "Correct isa metadata"
182 );
183
184 my %attrs = map { $_->name => $_ } $member->parameter_attributes;
185 my $target;
186 if( $sm eq "Bar"){$target = 4; }
187 elsif($sm eq "Baz"){$target = 3; }
188 elsif($sm eq "Foo"){$target = 4; }
189 Test::More::is( scalar keys %attrs, $target, "Correct # of attributes");
190
191 for my $attr_name (keys %attrs){
192 my $attr = $attrs{$attr_name};
193 Test::More::ok($attr->is_lazy, "is lazy");
194 Test::More::ok($attr->is_required, "is required");
195 Test::More::ok($attr->has_clearer, "has clearer");
196 Test::More::ok($attr->has_default, "has defau;t");
197 Test::More::ok($attr->has_predicate, "has predicate");
198 Test::More::ok($attr->has_domain_model, "has domain model");
199 Test::More::ok($attr->has_orig_attr_name, "has orig attr name");
200 Test::More::ok($attr->is_default_a_coderef, "default is coderef");
201 Test::More::is($attr->_is_metadata, "ro", "Correct is metadata");
202 Test::More::is($attr->domain_model, $dm_name, "Correct domain model");
203 Test::More::is($attr->orig_attr_name, $attr_name, "Correct orig attr name");
204 }
205
206 if($sm eq "Foo"){
207 Test::More::is($attrs{id}->_isa_metadata, "Int", "Correct id isa metadata");
208 Test::More::is($attrs{first_name}->_isa_metadata, "NonEmptySimpleStr", "Correct first_name isa metadata");
209 Test::More::is($attrs{last_name}->_isa_metadata, "NonEmptySimpleStr", "Correct last_name isa metadata");
210 Test::More::is(
211 $attrs{baz_list}->_isa_metadata,
212 "RTest::TestIM::Baz::Collection",
213 "Correct baz_list isa metadata"
214 );
215 } elsif($sm eq 'Bar'){
216 Test::More::is($attrs{name}->_isa_metadata, "NonEmptySimpleStr", "Correct name isa metadata");
217 Test::More::is($attrs{foo}->_isa_metadata, "RTest::TestIM::Foo", "Correct foo isa metadata");
218 Test::More::is($attrs{published_at}->_isa_metadata, "DateTime", "Correct published_at isa metadata");
219 Test::More::is($attrs{avatar}->_isa_metadata, "File", "Correct avatar isa metadata");
220 } elsif($sm eq "Baz"){
221 Test::More::is($attrs{id}->_isa_metadata, "Int", "Correct id isa metadata");
222 Test::More::is($attrs{name}->_isa_metadata, "NonEmptySimpleStr", "Correct name isa metadata");
223 Test::More::is(
224 $attrs{foo_list}->_isa_metadata,
225 "RTest::TestIM::Foo::Collection",
226 "Correct foo_list isa metadata"
227 );
228 }
229
230 }
231}
232
233sub test_reflect_submodel_action :Tests{
234 my $self = shift;
235 my $s = $self->im_schema;
236
237 for my $sm ( qw/Foo Bar Baz/ ){
238 my $reader = $s->meta->find_attribute_by_name($sm)->reader;
239 my $collection = $s->$reader;
240 my ($member) = $collection->members;
241 Test::More::ok($member, "Successfully retrieved member");
242 Test::More::isa_ok(
243 $member,
244 "Reaction::InterfaceModel::Object",
245 "Member isa IM::Object"
246 );
247 Test::More::isa_ok($member, $collection->_im_class);
248
249 my $ctx = $self->simple_mock_context;
250 foreach my $action_name (qw/Update Delete Create/){
251
252 my $target_im = $action_name eq 'Create' ? $collection : $member;
253 my $action = $target_im->action_for($action_name, ctx => $ctx);
254
255 Test::More::isa_ok( $action, "Reaction::InterfaceModel::Action",
256 "Create action isa Action" );
257 Test::More::is(
258 $action->meta->name,
259 "RTest::TestIM::${sm}::Action::${action_name}",
260 "${action_name} action has correct name"
261 );
262
263 my $base = 'Reaction::InterfaceModel::Action::DBIC' .
264 ($action_name eq 'Create' ? '::ResultSet::Create' : "::Result::${action_name}");
265 Test::More::isa_ok($action, $base, 'Create action has correct base');
266
267
268 my %attrs = map { $_->name => $_ } $action->parameter_attributes;
269 my $attr_num;
270 if($action_name eq 'Delete'){next; }
271 elsif($sm eq "Bar"){$attr_num = 4; }
272 elsif($sm eq "Baz"){$attr_num = 1; }
273 elsif($sm eq "Foo"){$attr_num = 3; }
274 Test::More::is( scalar keys %attrs, $attr_num, "Correct # of attributes");
275 if($attr_num != keys %attrs ){
276 print STDERR "\t..." . join ", ", keys %attrs, "\n";
277 }
278
279 for my $attr_name (keys %attrs){
280 my $attr = $attrs{$attr_name};
281 Test::More::ok($attr->has_predicate, "has predicate");
282 Test::More::is($attr->_is_metadata, "rw", "Correct is metadata");
283 if ($attr->is_required){
284 Test::More::ok($attr->is_lazy, "is lazy");
285 Test::More::ok($attr->has_default, "has default");
286 Test::More::ok($attr->is_default_a_coderef, "default is coderef");
287 }
288 }
289
290 if($sm eq "Foo"){
291 Test::More::is($attrs{first_name}->_isa_metadata, "NonEmptySimpleStr", "Correct first_name isa metadata");
292 Test::More::is($attrs{last_name}->_isa_metadata, "NonEmptySimpleStr", "Correct last_name isa metadata");
293 Test::More::is($attrs{baz_list}->_isa_metadata, "ArrayRef", "Correct baz_list isa metadata");
294 } elsif($sm eq 'Bar'){
295 Test::More::is($attrs{name}->_isa_metadata, "NonEmptySimpleStr", "Correct name isa metadata");
296 Test::More::is($attrs{foo}->_isa_metadata, "RTest::TestDB::Foo", "Correct foo isa metadata");
297 Test::More::is($attrs{published_at}->_isa_metadata, "DateTime", "Correct published_at isa metadata");
298 Test::More::is($attrs{avatar}->_isa_metadata, "File", "Correct avatar isa metadata");
299 } elsif($sm eq "Baz"){
300 Test::More::is($attrs{name}->_isa_metadata, "NonEmptySimpleStr", "Correct name isa metadata");
301 }
302 }
303 }
304}
305
3061;