1 package RTest::InterfaceModel::Reflector::DBIC;
3 use base qw/Reaction::Test::WithDB Reaction::Object/;
6 use ComponentUI::TestModel;
8 use Reaction::InterfaceModel::Reflector::DBIC;
10 has '+schema_class' => (default => sub { 'RTest::TestDB' });
12 has im_schema => (is =>'ro', isa => 'RTest::TestIM', lazy_build => 1);
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.
21 my $reflector = Reaction::InterfaceModel::Reflector::DBIC->new;
23 $reflector->reflect_schema(
24 model_class => 'RTest::TestIM',
25 schema_class => 'RTest::TestDB',
26 sources => [qw/Foo Bar Baz/]
28 my (@dm) = RTest::TestIM->domain_models;
29 Test::More::ok(@dm == 1, 'Correct number of Domain Models');
31 RTest::TestIM->new($dm->name => $self->schema);
34 sub test_classnames : Tests{
37 my $reflector = Reaction::InterfaceModel::Reflector::DBIC->new;
41 $reflector->class_name_from_source_name('RTest::__TestIM','Foo'),
42 'RTest::__TestIM::Foo',
43 'Correct naming scheme for submodels'
46 $reflector->class_name_for_collection_of('RTest::__TestIM::Foo'),
47 'RTest::__TestIM::Foo::Collection',
48 'Correct naming scheme for submodel collections'
52 sub test_reflect_schema :Tests {
54 my $s = $self->im_schema;
56 Test::More::isa_ok( $s, 'Reaction::InterfaceModel::Object', 'Correct base' );
58 my %pa = map{$_->name => $_ } $s->parameter_attributes;
59 Test::More::ok(keys %pa == 3, 'Correct number of Parameter Attributes');
61 Test::More::ok($pa{Foo} && $pa{'Bar'} && $pa{'Baz'},
62 'Parameter Attributes named correctly');
64 for my $submodel (values %pa){
66 $submodel->_isa_metadata->isa('Reaction::InterfaceModel::Collection::Virtual::ResultSet'),
67 'Parameter Attribute typed correctly'
71 Test::More::can_ok($s, qw/foo_collection bar_collection baz_collection/);
73 for ( qw/Foo Bar Baz/ ){
75 Class::MOP::is_class_loaded("RTest::TestIM::${_}"),
76 "Successfully created ${_} IM class"
79 Class::MOP::is_class_loaded("RTest::TestIM::${_}::Collection"),
80 "Successfully created ${_} IM class Collection"
86 sub test_add_source_to_model :Tests {
88 my $s = $self->im_schema;
90 for (qw/Foo Bar Baz /) {
91 my $attr = $s->meta->find_attribute_by_name($_);
93 $reader =~ s/([a-z0-9])([A-Z])/${1}_${2}/g ;
94 $reader = lc($reader) . "_collection";
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");
104 Test::More::skip "Not working", 1;
105 Test::More::is( $attr->domain_model, "_rtest_testdb_store", "Correct ${_} domain_model");
109 "RTest::TestIM::${_}::Collection",
110 "${_} default method works"
116 sub test_reflect_collection_for :Tests{
118 my $s = $self->im_schema;
120 for ( qw/Foo Bar Baz/ ){
121 my $reader = $s->meta->find_attribute_by_name($_)->reader;
122 my $collection = $s->$reader;
125 $collection->meta->name,
126 "RTest::TestIM::${_}::Collection",
131 'Reaction::InterfaceModel::Collection',
132 "Collection ISA Collection"
136 'Reaction::InterfaceModel::Collection::Virtual',
137 "Collection ISA virtual collection"
141 'Reaction::InterfaceModel::Collection::Virtual::ResultSet',
142 "Collection ISA virtual resultset"
145 Test::More::skip 'Does not work', 2;
146 Test::More::can_ok($collection, '_build__im_class');
148 $collection->_build__im_class,
149 "RTest::TestIM::${_}",
150 "Collection has correct _im_class"
156 sub test_reflect_submodel :Tests{
158 my $s = $self->im_schema;
160 for my $sm ( qw/Foo Bar Baz/ ){
161 my $reader = $s->meta->find_attribute_by_name($sm)->reader;
162 my $collection = $s->$reader;
163 my ($member) = $collection->members;
164 Test::More::ok($member, "Successfully retrieved member");
167 "Reaction::InterfaceModel::Object",
168 "Member isa IM::Object"
171 Test::More::skip 'Attribute does not exist', 1;
172 Test::More::isa_ok($member, $collection->_im_class);
175 my (@dm) = $member->domain_models;
176 Test::More::ok(@dm == 1, 'Correct number of Domain Models');
179 my $dm_name = Reaction::InterfaceModel::Reflector::DBIC
180 ->dm_name_from_source_name($sm);
182 Test::More::is($dm->_is_metadata, "rw", "Correct is metadata");
183 Test::More::ok($dm->is_required, "DM is_required");
184 Test::More::is($dm->name, $dm_name, "Correct DM name");
185 Test::More::can_ok($member, "inflate_result");
187 Test::More::skip 'Does not exist', 1;
190 "RTest::TestDB::${sm}",
191 "Correct isa metadata"
195 my %attrs = map { $_->name => $_ } $member->parameter_attributes;
197 if( $sm eq "Bar"){$target = 4; }
198 elsif($sm eq "Baz"){$target = 5; }
199 elsif($sm eq "Foo"){$target = 5; }
200 Test::More::is( scalar keys %attrs, $target, "Correct # of attributes for $sm");
202 for my $attr_name (keys %attrs){
203 my $attr = $attrs{$attr_name};
204 Test::More::ok($attr->is_lazy, "is lazy");
205 Test::More::ok($attr->is_required, "is required");
206 Test::More::ok($attr->has_clearer, "has clearer");
207 Test::More::ok($attr->has_default, "has defau;t");
208 Test::More::ok($attr->has_predicate, "has predicate");
209 Test::More::ok($attr->has_domain_model, "has domain model");
210 Test::More::ok($attr->has_orig_attr_name, "has orig attr name");
211 Test::More::ok($attr->is_default_a_coderef, "default is coderef");
212 Test::More::is($attr->_is_metadata, "ro", "Correct is metadata");
213 Test::More::is($attr->domain_model, $dm_name, "Correct domain model");
214 Test::More::is($attr->orig_attr_name, $attr_name, "Correct orig attr name");
219 Test::More::skip '_isa_metadata does not exist', 4;
221 Test::More::is($attrs{id}->_isa_metadata, "Int", "Correct id isa metadata");
222 Test::More::is($attrs{first_name}->_isa_metadata, "Reaction::Types::Core::NonEmptySimpleStr", "Correct first_name isa metadata");
223 Test::More::is($attrs{last_name}->_isa_metadata, "Reaction::Types::Core::NonEmptySimpleStr", "Correct last_name isa metadata");
225 $attrs{baz_list}->_isa_metadata,
226 "RTest::TestIM::Baz::Collection",
227 "Correct baz_list isa metadata"
229 } elsif($sm eq 'Bar'){
230 Test::More::skip '_isa_metadata does not exist', 4;
232 Test::More::is($attrs{name}->_isa_metadata, "Reaction::Types::Core::NonEmptySimpleStr", "Correct name isa metadata");
233 Test::More::is($attrs{foo}->_isa_metadata, "RTest::TestIM::Foo", "Correct foo isa metadata");
234 Test::More::is($attrs{published_at}->_isa_metadata, "DateTime", "Correct published_at isa metadata");
235 Test::More::is($attrs{avatar}->_isa_metadata, "File", "Correct avatar isa metadata");
236 } elsif($sm eq "Baz"){
237 Test::More::skip '_isa_metadata does not exist', 3;
239 Test::More::is($attrs{id}->_isa_metadata, "Int", "Correct id isa metadata");
240 Test::More::is($attrs{name}->_isa_metadata, "Reaction::Types::Core::NonEmptySimpleStr", "Correct name isa metadata");
242 $attrs{foo_list}->_isa_metadata,
243 "RTest::TestIM::Foo::Collection",
244 "Correct foo_list isa metadata"
252 sub test_reflect_submodel_action :Tests{
254 my $s = $self->im_schema;
256 for my $sm ( qw/Foo Bar Baz/ ){
257 my $reader = $s->meta->find_attribute_by_name($sm)->reader;
258 my $collection = $s->$reader;
259 my ($member) = $collection->members;
260 Test::More::ok($member, "Successfully retrieved member");
263 "Reaction::InterfaceModel::Object",
264 "Member isa IM::Object"
267 Test::More::skip 'Does not exist any more', 1;
268 Test::More::isa_ok($member, $collection->_im_class);
271 my $ctx = $self->simple_mock_context;
272 foreach my $action_name (qw/Update Delete DeleteAll Create/){
274 my $target_im = $action_name =~ /(?:Create|DeleteAll)/ ? $collection : $member;
275 my $action = $target_im->action_for($action_name, ctx => $ctx);
277 Test::More::isa_ok( $action, "Reaction::InterfaceModel::Action",
278 "Create action isa Action" );
281 "RTest::TestIM::${sm}::Action::${action_name}",
282 "${action_name} action has correct name"
285 my $base = 'Reaction::InterfaceModel::Action::DBIC'.
286 ($action_name =~ /(?:Create|DeleteAll)/
287 ? "::ResultSet::${action_name}" : "::Result::${action_name}");
288 Test::More::isa_ok($action, $base, "${action_name} has correct base");
291 my %attrs = map { $_->name => $_ } $action->parameter_attributes;
293 if($action_name =~ /Delete/){next; }
294 elsif($sm eq "Bar"){$attr_num = 4; }
295 elsif($sm eq "Baz"){$attr_num = 4; }
296 elsif($sm eq "Foo"){$attr_num = 3; }
297 Test::More::is( scalar keys %attrs, $attr_num, "Correct # of attributes for $sm");
298 if($attr_num != keys %attrs ){
299 print STDERR "\t..." . join ", ", keys %attrs, "\n";
302 for my $attr_name (keys %attrs){
303 my $attr = $attrs{$attr_name};
304 Test::More::ok($attr->has_predicate, "has predicate");
305 Test::More::is($attr->_is_metadata, "rw", "Correct is metadata");
306 if ($attr->is_required){
307 Test::More::ok($attr->is_lazy, "is lazy");
308 Test::More::ok($attr->has_default, "has default");
309 Test::More::ok($attr->is_default_a_coderef, "default is coderef");
315 Test::More::skip '_isa_metadata no longer exists', 3;
317 Test::More::is($attrs{first_name}->_isa_metadata, "Reaction::Types::Core::NonEmptySimpleStr", "Correct first_name isa metadata");
318 Test::More::is($attrs{last_name}->_isa_metadata, "Reaction::Types::Core::NonEmptySimpleStr", "Correct last_name isa metadata");
319 Test::More::is($attrs{baz_list}->_isa_metadata, "ArrayRef", "Correct baz_list isa metadata");
320 } elsif($sm eq 'Bar'){
321 Test::More::skip '_isa_metadata no longer exists', 4;
323 Test::More::is($attrs{name}->_isa_metadata, "Reaction::Types::Core::NonEmptySimpleStr", "Correct name isa metadata");
324 Test::More::is($attrs{foo}->_isa_metadata, "RTest::TestDB::Foo", "Correct foo isa metadata");
325 Test::More::is($attrs{published_at}->_isa_metadata, "DateTime", "Correct published_at isa metadata");
326 Test::More::is($attrs{avatar}->_isa_metadata, "File", "Correct avatar isa metadata");
327 } elsif($sm eq "Baz"){
328 Test::More::skip '_isa_metadata no longer exists', 1;
330 Test::More::is($attrs{name}->_isa_metadata, "Reaction::Types::Core::NonEmptySimpleStr", "Correct name isa metadata");