aboutsummaryrefslogtreecommitdiffstats
path: root/t/lib/RTest/InterfaceModel/Reflector/DBIC.pm
blob: 7a263587d826ef20d15b7b0d8175637e6b4394e8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package RTest::InterfaceModel::Reflector::DBIC;

use base qw/Reaction::Test::WithDB Reaction::Object/;
use Reaction::Class;
use Class::MOP ();
use ComponentUI::TestModel;
use Test::More ();
use Reaction::InterfaceModel::Reflector::DBIC;

has '+schema_class' => (default => sub { 'RTest::TestDB' });

has im_schema => (is =>'ro', isa => 'RTest::TestIM', lazy_build => 1);

#at the moment I am only testing with the "reflect all" functionality
#when I have time I will write test cases that cover all the other bases
#it's just kind of a pain in the ass right now and I am behind on a lot of other shit.

sub _build_im_schema{
  my $self = shift;

  my $reflector = Reaction::InterfaceModel::Reflector::DBIC->new;

  $reflector->reflect_schema(
                             model_class  => 'RTest::TestIM',
                             schema_class => 'RTest::TestDB',
                             sources => [qw/Foo Bar Baz/]
                           );
  my (@dm) = RTest::TestIM->domain_models;
  Test::More::ok(@dm == 1, 'Correct number of Domain Models');
  my $dm = shift @dm;
  RTest::TestIM->new($dm->name => $self->schema);
}

sub test_classnames : Tests{
  my $self = shift;

  my $reflector = Reaction::InterfaceModel::Reflector::DBIC->new;


  Test::More::is(
                 $reflector->class_name_from_source_name('RTest::__TestIM','Foo'),
                 'RTest::__TestIM::Foo',
                 'Correct naming scheme for submodels'
                );
  Test::More::is(
                 $reflector->class_name_for_collection_of('RTest::__TestIM::Foo'),
                 'RTest::__TestIM::Foo::Collection',
                 'Correct naming scheme for submodel collections'
                );
}

sub test_reflect_schema :Tests {
  my $self = shift;
  my $s = $self->im_schema;

  Test::More::isa_ok( $s, 'Reaction::InterfaceModel::Object', 'Correct base' );

  my %pa = map{$_->name => $_ } $s->parameter_attributes;
  Test::More::ok(keys %pa == 3,  'Correct number of Parameter Attributes');

  Test::More::ok($pa{Foo} && $pa{'Bar'} && $pa{'Baz'},
                 'Parameter Attributes named correctly');

  for my $submodel (values %pa){
    Test::More::ok(
                   $submodel->_isa_metadata->isa('Reaction::InterfaceModel::Collection::Virtual::ResultSet'),
                   'Parameter Attribute typed correctly'
                  );
  }

  Test::More::can_ok($s, qw/foo_collection bar_collection baz_collection/);

  for ( qw/Foo Bar Baz/ ){
    Test::More::ok(
                   Class::MOP::is_class_loaded("RTest::TestIM::${_}"),
                   "Successfully created ${_} IM class"
                  );
    Test::More::ok(
                   Class::MOP::is_class_loaded("RTest::TestIM::${_}::Collection"),
                   "Successfully created ${_} IM class Collection"
                  );
  }
}


sub test_add_source_to_model :Tests {
  my $self = shift;
  my $s = $self->im_schema;

  for (qw/Foo Bar Baz /) {
    my $attr = $s->meta->find_attribute_by_name($_);
    my $reader = $_;
    $reader =~ s/([a-z0-9])([A-Z])/${1}_${2}/g ;
    $reader = lc($reader) . "_collection";

    Test::More::ok( $attr->is_required,           "${_} is required");
    Test::More::ok( $attr->has_reader,            "${_} has a reader");
    Test::More::ok( $attr->has_predicate,         "${_} has a predicate");
    Test::More::ok( $attr->has_domain_model,      "${_} has a domain_model");
    Test::More::ok( $attr->has_default,           "${_} has a default");
    Test::More::ok( $attr->is_default_a_coderef,  "${_}'s defaultis a coderef");
    Test::More::is( $attr->reader,   $reader,     "Correct ${_} reader");
    Test::More::is( $attr->domain_model, "_rtest_testdb_store", "Correct ${_} domain_model");

    Test::More::isa_ok(
                       $s->$reader,
                       "RTest::TestIM::${_}::Collection",
                       "${_} default method works"
                      );

  }
}

sub test_reflect_collection_for :Tests{
  my $self = shift;
  my $s = $self->im_schema;

  for ( qw/Foo Bar Baz/ ){
    my $reader = $s->meta->find_attribute_by_name($_)->reader;
    my $collection = $s->$reader;

    Test::More::is(
                   $collection->meta->name,
                   "RTest::TestIM::${_}::Collection",
                   "Correct Classname"
                  );
    Test::More::isa_ok(
                       $collection,
                       'Reaction::InterfaceModel::Collection',
                       "Collection ISA Collection"
                      );
    Test::More::isa_ok(
                       $collection,
                       'Reaction::InterfaceModel::Collection::Virtual',
                       "Collection ISA virtual collection"
                      );
    Test::More::isa_ok(
                       $collection,
                       'Reaction::InterfaceModel::Collection::Virtual::ResultSet',
                       "Collection ISA virtual resultset"
                      );
    Test::More::can_ok($collection, '_build__im_class');
    Test::More::is(
                   $collection->_build__im_class,
                   "RTest::TestIM::${_}",
                   "Collection has correct _im_class"
                  );
  }
}

sub test_reflect_submodel :Tests{
  my $self = shift;
  my $s = $self->im_schema;

  for my $sm ( qw/Foo Bar Baz/ ){
    my $reader = $s->meta->find_attribute_by_name($sm)->reader;
    my $collection = $s->$reader;
    my ($member) = $collection->members;
    Test::More::ok($member, "Successfully retrieved member");
    Test::More::isa_ok(
                       $member,
                       "Reaction::InterfaceModel::Object",
                       "Member isa IM::Object"
                      );
    Test::More::isa_ok($member, $collection->_im_class);

    my (@dm) = $member->domain_models;
    Test::More::ok(@dm == 1, 'Correct number of Domain Models');
    my $dm = shift @dm;

    my $dm_name = Reaction::InterfaceModel::Reflector::DBIC
      ->dm_name_from_source_name($sm);

    Test::More::is($dm->_is_metadata, "rw", "Correct is metadata");
    Test::More::ok($dm->is_required,  "DM is_required");
    Test::More::is($dm->name, $dm_name, "Correct DM name");
    Test::More::can_ok($member, "inflate_result");
    Test::More::is(
                   $dm->_isa_metadata,
                   "RTest::TestDB::${sm}",
                   "Correct isa metadata"
                  );

    my %attrs = map { $_->name => $_ } $member->parameter_attributes;
    my $target;
    if(   $sm eq "Bar"){$target = 4; }
    elsif($sm eq "Baz"){$target = 3; }
    elsif($sm eq "Foo"){$target = 4; }
    Test::More::is( scalar keys %attrs, $target, "Correct # of attributes");

    for my $attr_name (keys %attrs){
      my $attr = $attrs{$attr_name};
      Test::More::ok($attr->is_lazy,                "is lazy");
      Test::More::ok($attr->is_required,            "is required");
      Test::More::ok($attr->has_clearer,            "has clearer");
      Test::More::ok($attr->has_default,            "has defau;t");
      Test::More::ok($attr->has_predicate,          "has predicate");
      Test::More::ok($attr->has_domain_model,       "has domain model");
      Test::More::ok($attr->has_orig_attr_name,     "has orig attr name");
      Test::More::ok($attr->is_default_a_coderef,   "default is coderef");
      Test::More::is($attr->_is_metadata,  "ro",    "Correct is metadata");
      Test::More::is($attr->domain_model, $dm_name, "Correct domain model");
      Test::More::is($attr->orig_attr_name, $attr_name, "Correct orig attr name");
    }

    if($sm eq "Foo"){
      Test::More::is($attrs{id}->_isa_metadata, "Int", "Correct id isa metadata");
      Test::More::is($attrs{first_name}->_isa_metadata, "NonEmptySimpleStr", "Correct first_name isa metadata");
      Test::More::is($attrs{last_name}->_isa_metadata,  "NonEmptySimpleStr", "Correct last_name isa metadata");
      Test::More::is(
                     $attrs{baz_list}->_isa_metadata,
                     "RTest::TestIM::Baz::Collection",
                     "Correct baz_list isa metadata"
                    );
    } elsif($sm eq 'Bar'){
      Test::More::is($attrs{name}->_isa_metadata, "NonEmptySimpleStr", "Correct name isa metadata");
      Test::More::is($attrs{foo}->_isa_metadata, "RTest::TestIM::Foo", "Correct foo isa metadata");
      Test::More::is($attrs{published_at}->_isa_metadata, "DateTime",  "Correct published_at isa metadata");
      Test::More::is($attrs{avatar}->_isa_metadata, "File",            "Correct avatar isa metadata");
    } elsif($sm eq "Baz"){
      Test::More::is($attrs{id}->_isa_metadata, "Int", "Correct id isa metadata");
      Test::More::is($attrs{name}->_isa_metadata, "NonEmptySimpleStr", "Correct name isa metadata");
      Test::More::is(
                     $attrs{foo_list}->_isa_metadata,
                     "RTest::TestIM::Foo::Collection",
                     "Correct foo_list isa metadata"
                    );
    }

  }
}

sub test_reflect_submodel_action :Tests{
  my $self = shift;
  my $s = $self->im_schema;

  for my $sm ( qw/Foo Bar Baz/ ){
    my $reader = $s->meta->find_attribute_by_name($sm)->reader;
    my $collection = $s->$reader;
    my ($member) = $collection->members;
    Test::More::ok($member, "Successfully retrieved member");
    Test::More::isa_ok(
                       $member,
                       "Reaction::InterfaceModel::Object",
                       "Member isa IM::Object"
                      );
    Test::More::isa_ok($member, $collection->_im_class);

    my $ctx = $self->simple_mock_context;
    foreach my $action_name (qw/Update Delete DeleteAll Create/){

      my $target_im = $action_name =~ /(?:Create|DeleteAll)/ ? $collection : $member;
      my $action = $target_im->action_for($action_name, ctx => $ctx);

      Test::More::isa_ok( $action, "Reaction::InterfaceModel::Action",
                          "Create action isa Action" );
      Test::More::is(
                     $action->meta->name,
                     "RTest::TestIM::${sm}::Action::${action_name}",
                     "${action_name} action has correct name"
                    );

      my $base = 'Reaction::InterfaceModel::Action::DBIC'.
        ($action_name =~ /(?:Create|DeleteAll)/
         ? "::ResultSet::${action_name}" : "::Result::${action_name}");
      Test::More::isa_ok($action, $base, "${action_name} has correct base");


      my %attrs = map { $_->name => $_ } $action->parameter_attributes;
      my $attr_num;
      if($action_name =~ /Delete/){next; }
      elsif($sm eq "Bar"){$attr_num = 4; }
      elsif($sm eq "Baz"){$attr_num = 1; }
      elsif($sm eq "Foo"){$attr_num = 3; }
      Test::More::is( scalar keys %attrs, $attr_num, "Correct # of attributes");
      if($attr_num != keys %attrs ){
        print STDERR "\t..." . join ", ", keys %attrs, "\n";
      }

      for my $attr_name (keys %attrs){
        my $attr = $attrs{$attr_name};
        Test::More::ok($attr->has_predicate,        "has predicate");
        Test::More::is($attr->_is_metadata,  "rw",  "Correct is metadata");
        if ($attr->is_required){
          Test::More::ok($attr->is_lazy,     "is lazy");
          Test::More::ok($attr->has_default, "has default");
          Test::More::ok($attr->is_default_a_coderef, "default is coderef");
        }
      }

      if($sm eq "Foo"){
        Test::More::is($attrs{first_name}->_isa_metadata, "NonEmptySimpleStr", "Correct first_name isa metadata");
        Test::More::is($attrs{last_name}->_isa_metadata,  "NonEmptySimpleStr", "Correct last_name isa metadata");
        Test::More::is($attrs{baz_list}->_isa_metadata,  "ArrayRef", "Correct baz_list isa metadata");
      } elsif($sm eq 'Bar'){
        Test::More::is($attrs{name}->_isa_metadata, "NonEmptySimpleStr",  "Correct name isa metadata");
        Test::More::is($attrs{foo}->_isa_metadata,  "RTest::TestDB::Foo", "Correct foo isa metadata");
        Test::More::is($attrs{published_at}->_isa_metadata, "DateTime",   "Correct published_at isa metadata");
        Test::More::is($attrs{avatar}->_isa_metadata, "File",             "Correct avatar isa metadata");
      } elsif($sm eq "Baz"){
        Test::More::is($attrs{name}->_isa_metadata, "NonEmptySimpleStr",  "Correct name isa metadata");
      }
    }
  }
}

1;