surveyor 0.7.1 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,17 +2,18 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  describe SurveyorController do
4
4
 
5
- # map.available_surveys 'surveys', :conditions => {:method => :get}, :action => "new" # GET survey list
6
- # map.take_survey 'surveys/:survey_code', :conditions => {:method => :post}, :action => "create" # Only POST of survey to create
7
- # map.view_my_survey 'surveys/:survey_code/:response_set_code', :conditions => {:method => :get}, :action => "show" # GET viewable/printable? survey
8
- # map.edit_my_survey 'surveys/:survey_code/:response_set_code/take', :conditions => {:method => :get}, :action => "edit" # GET editable survey
9
- # map.update_my_survey 'surveys/:survey_code/:response_set_code', :conditions => {:method => :put}, :action => "update" # PUT edited survey
10
- # map.finish_my_survey 'surveys/:survey_code/:response_set_code/finish', :conditions => {:method => :put}, :action => "finish" # PUT to close out the response_set
5
+ # map.with_options :controller => 'surveyor' do |s|
6
+ # s.available_surveys "#{root}", :conditions => {:method => :get}, :action => "new" # GET survey list
7
+ # s.take_survey "#{root}:survey_code", :conditions => {:method => :post}, :action => "create" # Only POST of survey to create
8
+ # s.view_my_survey "#{root}:survey_code/:response_set_code", :conditions => {:method => :get}, :action => "show" # GET viewable/printable? survey
9
+ # s.edit_my_survey "#{root}:survey_code/:response_set_code/take", :conditions => {:method => :get}, :action => "edit" # GET editable survey
10
+ # s.update_my_survey "#{root}:survey_code/:response_set_code", :conditions => {:method => :put}, :action => "update" # PUT edited survey
11
+ # end
11
12
 
12
- describe "handling GET /surveys (available_surveys)" do
13
+ describe "available surveys: GET /surveys" do
13
14
 
14
15
  before(:each) do
15
- @survey = mock_model(Survey)
16
+ @survey = Factory(:survey)
16
17
  Survey.stub!(:find).and_return([@survey])
17
18
  end
18
19
 
@@ -20,123 +21,67 @@ describe SurveyorController do
20
21
  get :new
21
22
  end
22
23
 
23
- it "should be successful" do
24
- do_get
25
- response.should be_success
26
- end
27
-
28
24
  it "should render index template" do
29
25
  do_get
26
+ response.should be_success
30
27
  response.should render_template('new')
31
28
  end
32
-
33
29
  it "should find all surveys" do
34
30
  Survey.should_receive(:find).with(:all).and_return([@survey])
35
31
  do_get
36
32
  end
37
-
38
33
  it "should assign the found surveys for the view" do
39
34
  do_get
40
35
  assigns[:surveys].should == [@survey]
41
36
  end
42
37
  end
43
38
 
44
- describe "handling GET /surveys.xml (available_surveys)" do
45
- before(:each) do
46
- @surveys = mock("Array of Surveys", :to_xml => "XML")
47
- Survey.stub!(:find).and_return(@surveys)
48
- end
49
-
50
- def do_get
51
- @request.env["HTTP_ACCEPT"] = "application/xml"
52
- get :new
53
- end
54
-
55
- it "should be successful" do
56
- do_get
57
- response.should be_success
58
- end
59
-
60
- it "should find all surveys" do
61
- Survey.should_receive(:find).with(:all).and_return(@surveys)
62
- do_get
63
- end
64
-
65
- it "should render the found surveys as xml" do
66
- @surveys.should_receive(:to_xml).and_return("XML")
67
- do_get
68
- response.body.should == "XML"
69
- end
70
- end
71
-
72
- describe "handling POST /surveys/XYZ (take_survey)" do
39
+ describe "take survey: POST /surveys/XYZ" do
73
40
 
74
41
  before(:each) do
75
- @survey = mock_model(Survey, :access_code => "XYZ")
76
- @response_set = mock_model(ResponseSet, :access_code => "PDQ")
77
- ResponseSet.stub!(:new).and_return(@response_set)
42
+ @survey = Factory(:survey, :title => "XYZ", :access_code => "XYZ")
43
+ @response_set = Factory(:response_set, :access_code => "PDQ")
44
+ ResponseSet.stub!(:create).and_return(@response_set)
78
45
  Survey.stub!(:find_by_access_code).and_return(@survey)
79
46
  end
80
47
 
81
- describe "with successful save" do
82
-
48
+ describe "with success" do
83
49
  def do_post
84
- @response_set.should_receive(:save!).and_return(true)
85
50
  post :create, :survey_code => "XYZ"
86
51
  end
87
-
88
52
  it "should look for the survey" do
89
53
  Survey.should_receive(:find_by_access_code).with("XYZ").and_return(@survey)
90
54
  do_post
91
55
  end
92
56
  it "should create a new response_set" do
93
- ResponseSet.should_receive(:new).and_return(@response_set)
57
+ ResponseSet.should_receive(:create).and_return(@response_set)
94
58
  do_post
95
59
  end
96
-
97
60
  it "should redirect to the new response_set" do
98
61
  do_post
99
62
  response.should redirect_to(edit_my_survey_url(:survey_code => "XYZ", :response_set_code => "PDQ"))
100
63
  end
101
-
102
64
  end
103
65
 
104
- describe "with failed save" do
105
-
106
- def do_post
107
- @response_set.should_receive(:save!).and_return(false)
66
+ describe "with failures" do
67
+ it "should re-redirect to 'new' if ResponseSet failed create" do
68
+ ResponseSet.should_receive(:create).and_return(false)
108
69
  post :create, :survey_code => "XYZ"
109
- end
110
-
111
- it "should re-redirect to 'new'" do
112
- do_post
113
70
  response.should redirect_to(available_surveys_url)
114
71
  end
115
-
116
- end
117
-
118
- describe "with survey not found" do
119
-
120
- def do_post
72
+ it "should re-redirect to 'new' if Survey failed find" do
121
73
  Survey.should_receive(:find_by_access_code).and_return(nil)
122
74
  post :create, :survey_code => "XYZ"
123
- end
124
-
125
- it "should re-redirect to 'new'" do
126
- do_post
127
75
  response.should redirect_to(available_surveys_url)
128
76
  end
129
-
130
77
  end
131
78
  end
132
79
 
133
- describe "handling GET /surveys/XYZ/PDQ (view_my_survey)" do
80
+ describe "view my survey: GET /surveys/XYZ/PDQ" do
134
81
 
135
82
  before(:each) do
136
- @survey = mock_model(Survey, :access_code => "XYZ", :sections => [mock_model(SurveySection)])
137
- @response_set = mock_model(ResponseSet, :access_code => "PDQ")
138
- ResponseSet.stub!(:find_by_access_code).with("PDQ").and_return(@response_set)
139
- @response_set.stub!(:survey).and_return(@survey)
83
+ @survey = Factory(:survey, :title => "XYZ", :access_code => "XYZ", :sections => [Factory(:survey_section)])
84
+ @response_set = Factory(:response_set, :access_code => "PDQ", :survey => @survey)
140
85
  end
141
86
 
142
87
  def do_get
@@ -147,28 +92,26 @@ describe SurveyorController do
147
92
  do_get
148
93
  response.should be_success
149
94
  end
150
-
151
95
  it "should render show template" do
152
96
  do_get
153
97
  response.should render_template('show')
154
98
  end
155
-
156
99
  it "should find the response_set requested" do
100
+ pending
157
101
  ResponseSet.should_receive(:find_by_access_code).with("PDQ").and_return(@response_set)
158
102
  do_get
159
103
  end
160
-
161
104
  it "should assign the found response_set and survey for the view" do
105
+ pending
162
106
  do_get
163
107
  assigns[:response_set].should equal(@response_set)
164
108
  assigns[:survey].should equal(@survey)
165
109
  end
166
-
167
110
  it "should redirect if :response_code not found" do
111
+ pending
168
112
  get :show, :survey_code => "XYZ", :response_set_code => "DIFFERENT"
169
113
  response.should redirect_to(available_surveys_url)
170
114
  end
171
-
172
115
  # I'm not sure this is enterly neccessary since we look up the survey from the response_code in the url -BC
173
116
  it "should redirect if :survey_code in url doesn't match response_set.survey.access_code" do
174
117
  pending
@@ -177,27 +120,22 @@ describe SurveyorController do
177
120
  end
178
121
  end
179
122
 
180
- describe "handling GET /surveys/XYZ/PDQ/take (edit_my_survey)" do
123
+ describe "edit my survey: GET /surveys/XYZ/PDQ/take" do
181
124
 
182
125
  before(:each) do
183
- @survey = mock_model(Survey, :access_code => "XYZ")
184
- @survey_section = mock_model(SurveySection)
185
- @survey.stub!(:sections).and_return([@survey_section])
186
- @response_set = mock_model(ResponseSet, :access_code => "PDQ")
187
- ResponseSet.stub!(:find_by_access_code).with("PDQ").and_return(@response_set)
188
- @response_set.stub!(:survey).and_return(@survey)
126
+ @survey = Factory(:survey, :title => "XYZ", :access_code => "XYZ")
127
+ @section = Factory(:survey_section, :survey => @survey)
128
+ @response_set = Factory(:response_set, :access_code => "PDQ", :survey => @survey)
189
129
  end
190
130
 
191
131
  it "should be successful, render edit with the requested survey" do
192
- ResponseSet.should_receive(:find_by_access_code).with("PDQ").and_return(@response_set)
193
-
132
+ ResponseSet.should_receive(:find_by_access_code).and_return(@response_set)
194
133
  get :edit, :survey_code => "XYZ", :response_set_code => "PDQ"
195
134
  response.should be_success
196
135
  response.should render_template('edit')
197
- assigns[:response_set].should equal(@response_set)
198
- assigns[:survey].should equal(@survey)
136
+ assigns[:response_set].should == @response_set
137
+ assigns[:survey].should == @survey
199
138
  end
200
-
201
139
  it "should redirect if :response_code not found" do
202
140
  get :edit, :survey_code => "XYZ", :response_set_code => "DIFFERENT"
203
141
  response.should redirect_to(available_surveys_url)
@@ -205,124 +143,51 @@ describe SurveyorController do
205
143
 
206
144
  end
207
145
 
208
- describe "handling PUT /surveys/XYZ/PDQ (update_my_survey)" do
146
+ describe "update my survey: PUT /surveys/XYZ/PDQ" do
209
147
 
210
148
  before(:each) do
211
- @survey = mock_model(Survey, :access_code => "XYZ", :sections => [mock_model(SurveySection)])
212
- @response_set = mock_model(ResponseSet, :access_code => "PDQ")
213
- ResponseSet.stub!(:find_by_access_code).with("PDQ").and_return(@response_set)
214
- @response_set.stub!(:survey).and_return(@survey)
215
- @response_set.stub!(:add_responses).and_return(true)
149
+ @survey = Factory(:survey, :title => "XYZ", :access_code => "XYZ")
150
+ @section = Factory(:survey_section, :survey => @survey)
151
+ @response_set = Factory(:response_set, :access_code => "PDQ", :survey => @survey)
152
+ # @response_set.stub!(:update_attributes).and_return(true)
153
+ # @response_set.stub!(:complete!).and_return(Time.now)
154
+ # @response_set.stub!(:save).and_return(true)
216
155
  end
217
156
 
218
- describe "with no response_set in update" do
219
-
220
- it "should find the response set requested" do
221
- ResponseSet.should_receive(:find_by_access_code).with("PDQ").and_return(@response_set)
222
- put :update, :survey_code => "XYZ", :response_set_code => "PDQ"
223
-
224
- end
225
-
157
+ def do_put
158
+ put :update, :survey_code => "XYZ", :response_set_code => "PDQ"
159
+ end
160
+ def do_put_with_finish
161
+ responses = {
162
+ "6"=>{"question_id"=>"6", "20"=>{"string_value"=>"saf"}}, #string
163
+ "7"=>{"question_id"=>"7", "21"=>{"text_value"=>""}}, #text
164
+ "1"=>{"question_id"=>"1", "answer_id"=>"1", "4"=>{"string_value"=>""}}, #radio+txt
165
+ "2"=>{"answer_id"=>"6"}, #radio
166
+ "3"=>{"answer_id"=>"10"}, #radio
167
+ "4"=>{"question_id"=>"4", "answer_id"=>"15"}, #check
168
+ "5"=>{"question_id"=>"5", "16"=>{"selected"=>"1"}, "19"=>{"string_value"=>""}} #check+txt
169
+ }
170
+ put :update, :survey_code => "XYZ", :response_set_code => "PDQ", :finish => "finish", :responses => responses
226
171
  end
227
172
 
228
- describe "with a new response set" do
229
-
230
- it "should accept properly formatted params and save the data" do
231
- response_set_params = {"survey_code"=>"XYZ", "response_set"=>{"new_response_attributes"=>{"1"=>[{"answer_id"=>"2"}]}}}
232
-
233
-
234
- end
235
-
236
- # describe "issue with posting data to an existing survey and the data not saving properly" do
237
- # @survey = mock_model(Survey, :access_code => "XYZ")
238
- # @response_set = mock_model(ResponseSet, :access_code => "PDQ")
239
- # ResponseSet.stub!(:find_by_access_code).with("PDQ").and_return(@response_set)
240
- # @response_set.stub!(:survey).and_return(@survey)
241
- # @response_set.stub!(:complete!).and_return(Time.now)
242
- #
243
- #
244
- # end
245
-
246
- # first post {"survey_code"=>"test_survey", "commit"=>"Next Section (Utensiles and you!) >>", "response_set"=>{"new_response_attributes"=>{"1"=>[{"answer_id"=>"2"}, {"answer_id"=>"0", "string_value"=>""}], "2"=>[{"answer_id"=>"6"}], "3"=>[{"answer_id"=>"10"}], "4"=>[{"answer_id"=>"14"}], "5"=>[{"answer_id"=>"0"}, {"answer_id"=>"0"}]}, "existing_response_attributes"=>{"6"=>{"1"=>{"answer_id"=>"20", "string_value"=>"B"}}, "7"=>{"2"=>{"text_value"=>"foo", "answer_id"=>"21"}}, "5"=>{"7"=>{"answer_id"=>"17"}, "16"=>{"answer_id"=>"19", "string_value"=>"blah"}}}}, "authenticity_token"=>"d9ba68fe20a46703f3737b5cb0b7e17b7390de32", "_method"=>"put", "action"=>"update", "controller"=>"app", "response_set_code"=>"9VEsec1dK6", "section"=>"2"}
247
- # second post {"survey_code"=>"test_survey", "commit"=>"Next Section (Utensiles and you!) >>", "response_set"=>{"new_response_attributes"=>{"1"=>[{"answer_id"=>"2"}, {"answer_id"=>"0", "string_value"=>""}], "2"=>[{"answer_id"=>"6"}], "3"=>[{"answer_id"=>"10"}], "4"=>[{"answer_id"=>"14"}], "5"=>[{"answer_id"=>"0"}, {"answer_id"=>"0"}]}, "existing_response_attributes"=>{"6"=>{"1"=>{"answer_id"=>"20", "string_value"=>"B"}}, "7"=>{"2"=>{"text_value"=>"boooo", "answer_id"=>"21"}}, "5"=>{"7"=>{"answer_id"=>"17"}, "16"=>{"answer_id"=>"19", "string_value"=>"blahblahstink"}}}}, "authenticity_token"=>"d9ba68fe20a46703f3737b5cb0b7e17b7390de32", "_method"=>"put", "action"=>"update", "controller"=>"app", "response_set_code"=>"9VEsec1dK6", "section"=>"2"}
248
-
249
-
173
+ it "should find the response set requested" do
174
+ ResponseSet.should_receive(:find_by_access_code).and_return(@response_set)
175
+ do_put
250
176
  end
251
-
252
-
253
- describe "with failed update" do
254
-
255
- it "should re-render 'edit'" do
256
- put :update, :survey_code => "XYZ", :response_set_code => "PDQ"
257
- response.should be_success
258
- response.should render_template('edit')
259
- flash[:notice].should == "Unable to update survey"
260
- end
261
-
177
+ it "should redirect to 'edit' without params" do
178
+ do_put
179
+ response.should redirect_to(:action => :edit)
180
+ flash[:notice].should == "Unable to update survey"
262
181
  end
263
- end
264
-
265
- describe "handling PUT /surveys/XYZ/PDQ/finish (finish_my_survey)" do
266
-
267
- before(:each) do
268
- @survey = mock_model(Survey, :access_code => "XYZ", :sections => [mock_model(SurveySection)])
269
- @response_set = mock_model(ResponseSet, :access_code => "PDQ")
270
- ResponseSet.stub!(:find_by_access_code).with("PDQ").and_return(@response_set)
271
- @response_set.stub!(:survey).and_return(@survey)
272
- @response_set.stub!(:complete!).and_return(Time.now)
182
+ it "should complete the found response set on finish" do
183
+ do_put_with_finish
184
+ flash[:notice].should == "Completed survey"
273
185
  end
274
-
275
- describe "with successful update" do
276
-
277
- def do_put
278
- put :finish, :survey_code => "XYZ", :response_set_code => "PDQ"
279
- end
280
-
281
- it "should find the response_set requested" do
282
- ResponseSet.should_receive(:find_by_access_code).with("PDQ").and_return(@response_set)
283
- do_put
284
- end
285
-
286
- it "should update the found response set" do
287
- @response_set.should_receive(:complete!).and_return(Time.now)
288
- do_put
289
- end
290
-
291
- it "should assign the found response set and survey for the view" do
292
- do_put
293
- assigns(:response_set).should equal(@response_set)
294
- assigns(:survey).should equal(@response_set.survey)
295
- end
296
-
297
- it "should render the 'edit' template" do
298
- do_put
299
- response.should render_template('edit')
300
- flash[:notice].should == "Completed survey"
301
- end
302
-
303
- it "should redirect to available surveys if :response_code not found" do
304
- put :update, :survey_code => "XYZ", :response_set_code => "DIFFERENT"
305
- response.should redirect_to(available_surveys_url)
306
- flash[:notice].should == "Unable to find your responses to the survey"
307
- end
308
-
186
+ it "should redirect to available surveys if :response_code not found" do
187
+ put :update, :survey_code => "XYZ", :response_set_code => "DIFFERENT"
188
+ response.should redirect_to(available_surveys_url)
189
+ flash[:notice].should == "Unable to find your responses to the survey"
309
190
  end
310
-
311
- describe "with failed update" do
312
-
313
- def do_put
314
- put :finish, :survey_code => "XYZ", :response_set_code => "PDQ"
315
- end
316
191
 
317
- it "should re-render 'edit'" do
318
- @response_set.should_receive(:complete!).and_return(false)
319
- do_put
320
- response.should render_template('edit')
321
- flash[:notice].should == "Unable to complete survey"
322
- end
323
-
324
- end
325
192
  end
326
-
327
-
328
193
  end
data/spec/factories.rb CHANGED
@@ -22,19 +22,33 @@ end
22
22
 
23
23
  Factory.sequence(:question_display_order){|n| n }
24
24
 
25
- Factory.define :question do |s|
26
- s.association :survey_section # s.survey_section_id {}
27
- s.text {"What is your favorite color?"}
28
- s.short_text {"favorite_color"}
29
- s.help_text {"just write it in the box"}
30
- s.pick {:none}
31
- s.display_type {} # nil is default
32
- s.display_order {Factory.next :question_display_order}
33
- s.question_group_id {}
34
- s.is_mandatory {false}
35
- s.reference_identifier {|me| "q_#{me.object_id}"}
36
- s.data_export_identifier {}
37
- s.display_width {}
25
+ Factory.define :question do |q|
26
+ q.association :survey_section # s.survey_section_id {}
27
+ q.question_group_id {}
28
+ q.text {"What is your favorite color?"}
29
+ q.short_text {"favorite_color"}
30
+ q.help_text {"just write it in the box"}
31
+ q.pick {:none}
32
+ q.reference_identifier {|me| "q_#{me.object_id}"}
33
+ q.data_export_identifier {}
34
+ q.common_namespace {}
35
+ q.common_identifier {}
36
+ q.display_order {Factory.next :question_display_order}
37
+ q.display_type {} # nil is default
38
+ q.is_mandatory {false}
39
+ q.display_width {}
40
+ end
41
+
42
+ Factory.define :question_group do |g|
43
+ g.text {"Describe your family"}
44
+ g.help_text {}
45
+ g.reference_identifier {|me| "g_#{me.object_id}"}
46
+ g.data_export_identifier {}
47
+ g.common_namespace {}
48
+ g.common_identifier {}
49
+ g.display_type {}
50
+ g.custom_class {}
51
+ g.custom_renderer {}
38
52
  end
39
53
 
40
54
  Factory.sequence(:answer_display_order){|n| n }
@@ -46,26 +60,23 @@ Factory.define :answer do |a|
46
60
  a.help_text {"Clear is the absense of color"}
47
61
  a.weight {}
48
62
  a.response_class {"String"}
49
- a.display_order {}
50
- a.is_exclusive {}
51
- a.hide_label {}
52
63
  a.reference_identifier {}
53
64
  a.data_export_identifier {}
54
- a.common_data_identitier {}
55
- a.max_value {}
56
- a.min_value {}
57
- a.length {}
58
- a.decimal_precision {}
59
- a.allow_negative {}
60
- a.allow_blank {}
61
- a.unit {}
65
+ a.common_namespace {}
66
+ a.common_identifier {}
67
+ a.display_order {Factory.next :answer_display_order}
68
+ a.is_exclusive {}
69
+ a.hide_label {}
62
70
  a.display_length {}
71
+ a.custom_class {}
72
+ a.custom_renderer {}
63
73
  end
64
74
 
65
75
  Factory.define :dependency do |d|
66
76
  # the dependent question
67
77
  d.association :question # d.question_id {}
68
- d.rule {"1"}
78
+ d.question_group_id {}
79
+ d.rule {"A and B"}
69
80
  end
70
81
 
71
82
  Factory.define :dependency_condition do |d|
@@ -105,3 +116,25 @@ Factory.define :response do |r|
105
116
  r.response_other {}
106
117
  r.response_group {}
107
118
  end
119
+
120
+ Factory.define :validation do |v|
121
+ v.association :answer # v.answer_id {}
122
+ v.rule {"A"}
123
+ v.message {}
124
+ end
125
+
126
+ Factory.define :validation_condition do |v|
127
+ v.association :validation # v.validation_id {}
128
+ v.rule_key {"A"}
129
+ v.question_id {}
130
+ v.operator {"=="}
131
+ v.answer_id {}
132
+ v.datetime_value {}
133
+ v.integer_value {}
134
+ v.float_value {}
135
+ v.unit {}
136
+ v.text_value {}
137
+ v.string_value {}
138
+ v.response_other {}
139
+ v.regexp {}
140
+ end
@@ -9,7 +9,7 @@ describe Answer, "when creating a new answer" do
9
9
  @answer.should be_valid
10
10
  end
11
11
 
12
- it "should be invalid without a unique reference identifier (within the scope of its parent)" do
12
+ it "should be invalid without a question_id" do
13
13
  @answer.question_id = nil
14
14
  @answer.should_not be_valid
15
15
  end