upgrade_surveyor 1.4.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (203) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +30 -0
  3. data/.rspec +1 -0
  4. data/.travis.yml +18 -0
  5. data/CHANGELOG.md +253 -0
  6. data/Gemfile +5 -0
  7. data/Gemfile.rails_version +25 -0
  8. data/MIT-LICENSE +20 -0
  9. data/README.md +164 -0
  10. data/Rakefile +65 -0
  11. data/app/controllers/surveyor_controller.rb +5 -0
  12. data/app/helpers/results_helper.rb +20 -0
  13. data/app/helpers/survey_form_builder.rb +37 -0
  14. data/app/helpers/surveyor_helper.rb +3 -0
  15. data/app/inputs/quiet_input.rb +5 -0
  16. data/app/inputs/surveyor_check_boxes_input.rb +35 -0
  17. data/app/inputs/surveyor_radio_input.rb +18 -0
  18. data/app/models/answer.rb +3 -0
  19. data/app/models/dependency.rb +3 -0
  20. data/app/models/dependency_condition.rb +3 -0
  21. data/app/models/permitted_params.rb +102 -0
  22. data/app/models/question.rb +3 -0
  23. data/app/models/question_group.rb +3 -0
  24. data/app/models/response.rb +4 -0
  25. data/app/models/response_set.rb +3 -0
  26. data/app/models/survey.rb +3 -0
  27. data/app/models/survey_section.rb +4 -0
  28. data/app/models/survey_translation.rb +4 -0
  29. data/app/models/validation.rb +3 -0
  30. data/app/models/validation_condition.rb +3 -0
  31. data/app/views/layouts/results.html.erb +13 -0
  32. data/app/views/layouts/surveyor_default.html.erb +12 -0
  33. data/app/views/partials/_answer.html.haml +25 -0
  34. data/app/views/partials/_dependents.html.haml +5 -0
  35. data/app/views/partials/_question.html.haml +28 -0
  36. data/app/views/partials/_question_group.html.haml +44 -0
  37. data/app/views/partials/_section.html.haml +12 -0
  38. data/app/views/partials/_section_menu.html.haml +12 -0
  39. data/app/views/surveyor/edit.html.haml +21 -0
  40. data/app/views/surveyor/export.json.rabl +85 -0
  41. data/app/views/surveyor/new.html.haml +24 -0
  42. data/app/views/surveyor/show.html.haml +74 -0
  43. data/app/views/surveyor/show.json.rabl +15 -0
  44. data/config/routes.rb +8 -0
  45. data/doc/REPRESENTATIONS.md +34 -0
  46. data/doc/api_id_schema.json +7 -0
  47. data/doc/question types.png +0 -0
  48. data/doc/response_set_schema.json +54 -0
  49. data/doc/surveyor question combinations.png +0 -0
  50. data/doc/surveyor reject or delete decision matrix.png +0 -0
  51. data/doc/surveyor_models.png +0 -0
  52. data/doc/surveyor_models2.png +0 -0
  53. data/doc/surveyor_timestamp_schema.json +9 -0
  54. data/lib/assets/images/surveyor/next.gif +0 -0
  55. data/lib/assets/images/surveyor/prev.gif +0 -0
  56. data/lib/assets/images/surveyor/ui-bg_flat_0_aaaaaa_40x100.png +0 -0
  57. data/lib/assets/images/surveyor/ui-bg_flat_75_ffffff_40x100.png +0 -0
  58. data/lib/assets/images/surveyor/ui-bg_glass_55_fbf9ee_1x400.png +0 -0
  59. data/lib/assets/images/surveyor/ui-bg_glass_65_ffffff_1x400.png +0 -0
  60. data/lib/assets/images/surveyor/ui-bg_glass_75_dadada_1x400.png +0 -0
  61. data/lib/assets/images/surveyor/ui-bg_glass_75_e6e6e6_1x400.png +0 -0
  62. data/lib/assets/images/surveyor/ui-bg_glass_95_fef1ec_1x400.png +0 -0
  63. data/lib/assets/images/surveyor/ui-bg_highlight-soft_75_cccccc_1x100.png +0 -0
  64. data/lib/assets/images/surveyor/ui-icons_222222_256x240.png +0 -0
  65. data/lib/assets/images/surveyor/ui-icons_2e83ff_256x240.png +0 -0
  66. data/lib/assets/images/surveyor/ui-icons_454545_256x240.png +0 -0
  67. data/lib/assets/images/surveyor/ui-icons_888888_256x240.png +0 -0
  68. data/lib/assets/images/surveyor/ui-icons_cd0a0a_256x240.png +0 -0
  69. data/lib/assets/javascripts/surveyor/jquery-1.9.0.js +9555 -0
  70. data/lib/assets/javascripts/surveyor/jquery-ui-1.10.0.custom.js +14850 -0
  71. data/lib/assets/javascripts/surveyor/jquery-ui-timepicker-addon.js +1919 -0
  72. data/lib/assets/javascripts/surveyor/jquery.maskedinput.js +338 -0
  73. data/lib/assets/javascripts/surveyor/jquery.selectToUISlider.js +240 -0
  74. data/lib/assets/javascripts/surveyor/jquery.surveyor.js +154 -0
  75. data/lib/assets/stylesheets/surveyor/jquery-ui-1.10.0.custom.sass +1380 -0
  76. data/lib/assets/stylesheets/surveyor/jquery-ui-timepicker-addon.css +11 -0
  77. data/lib/assets/stylesheets/surveyor/reset.css +48 -0
  78. data/lib/assets/stylesheets/surveyor/results.css +125 -0
  79. data/lib/assets/stylesheets/surveyor/ui.slider.extras.css +110 -0
  80. data/lib/assets/stylesheets/surveyor.sass +132 -0
  81. data/lib/generators/surveyor/custom_generator.rb +18 -0
  82. data/lib/generators/surveyor/install_generator.rb +98 -0
  83. data/lib/generators/surveyor/templates/app/assets/javascripts/surveyor_all.js +6 -0
  84. data/lib/generators/surveyor/templates/app/assets/stylesheets/surveyor_all.css +9 -0
  85. data/lib/generators/surveyor/templates/app/controllers/surveyor_controller.rb +40 -0
  86. data/lib/generators/surveyor/templates/app/views/layouts/surveyor_custom.html.erb +13 -0
  87. data/lib/generators/surveyor/templates/config/locales/surveyor_en.yml +19 -0
  88. data/lib/generators/surveyor/templates/config/locales/surveyor_es.yml +19 -0
  89. data/lib/generators/surveyor/templates/config/locales/surveyor_he.yml +19 -0
  90. data/lib/generators/surveyor/templates/config/locales/surveyor_ko.yml +19 -0
  91. data/lib/generators/surveyor/templates/db/migrate/add_api_id_to_question_groups.rb +10 -0
  92. data/lib/generators/surveyor/templates/db/migrate/add_api_ids.rb +14 -0
  93. data/lib/generators/surveyor/templates/db/migrate/add_api_ids_to_response_sets_and_responses.rb +12 -0
  94. data/lib/generators/surveyor/templates/db/migrate/add_correct_answer_id_to_questions.rb +10 -0
  95. data/lib/generators/surveyor/templates/db/migrate/add_default_value_to_answers.rb +10 -0
  96. data/lib/generators/surveyor/templates/db/migrate/add_display_order_to_surveys.rb +10 -0
  97. data/lib/generators/surveyor/templates/db/migrate/add_display_type_to_answers.rb +14 -0
  98. data/lib/generators/surveyor/templates/db/migrate/add_index_to_response_sets.rb +10 -0
  99. data/lib/generators/surveyor/templates/db/migrate/add_index_to_surveys.rb +10 -0
  100. data/lib/generators/surveyor/templates/db/migrate/add_input_mask_attributes_to_answer.rb +12 -0
  101. data/lib/generators/surveyor/templates/db/migrate/add_section_id_to_responses.rb +12 -0
  102. data/lib/generators/surveyor/templates/db/migrate/add_unique_index_on_access_code_and_version_in_surveys.rb +10 -0
  103. data/lib/generators/surveyor/templates/db/migrate/add_unique_indicies.rb +18 -0
  104. data/lib/generators/surveyor/templates/db/migrate/add_version_to_surveys.rb +10 -0
  105. data/lib/generators/surveyor/templates/db/migrate/api_ids_must_be_unique.rb +23 -0
  106. data/lib/generators/surveyor/templates/db/migrate/create_answers.rb +38 -0
  107. data/lib/generators/surveyor/templates/db/migrate/create_dependencies.rb +23 -0
  108. data/lib/generators/surveyor/templates/db/migrate/create_dependency_conditions.rb +30 -0
  109. data/lib/generators/surveyor/templates/db/migrate/create_question_groups.rb +28 -0
  110. data/lib/generators/surveyor/templates/db/migrate/create_questions.rb +37 -0
  111. data/lib/generators/surveyor/templates/db/migrate/create_response_sets.rb +23 -0
  112. data/lib/generators/surveyor/templates/db/migrate/create_responses.rb +34 -0
  113. data/lib/generators/surveyor/templates/db/migrate/create_survey_sections.rb +30 -0
  114. data/lib/generators/surveyor/templates/db/migrate/create_survey_translations.rb +19 -0
  115. data/lib/generators/surveyor/templates/db/migrate/create_surveys.rb +32 -0
  116. data/lib/generators/surveyor/templates/db/migrate/create_validation_conditions.rb +33 -0
  117. data/lib/generators/surveyor/templates/db/migrate/create_validations.rb +21 -0
  118. data/lib/generators/surveyor/templates/db/migrate/drop_unique_index_on_access_code_in_surveys.rb +10 -0
  119. data/lib/generators/surveyor/templates/db/migrate/update_blank_api_ids_on_question_group.rb +22 -0
  120. data/lib/generators/surveyor/templates/db/migrate/update_blank_versions_on_surveys.rb +13 -0
  121. data/lib/generators/surveyor/templates/surveys/EXTENDING_SURVEYOR.md +52 -0
  122. data/lib/generators/surveyor/templates/surveys/MODIFYING_SURVEYOR.md +91 -0
  123. data/lib/generators/surveyor/templates/surveys/date_survey.rb +16 -0
  124. data/lib/generators/surveyor/templates/surveys/kitchen_sink_survey.rb +280 -0
  125. data/lib/generators/surveyor/templates/surveys/languages.rb +14 -0
  126. data/lib/generators/surveyor/templates/surveys/quiz.rb +11 -0
  127. data/lib/generators/surveyor/templates/surveys/translations/languages.es.yml +18 -0
  128. data/lib/generators/surveyor/templates/surveys/translations/languages.he.yml +18 -0
  129. data/lib/generators/surveyor/templates/surveys/translations/languages.ko.yml +18 -0
  130. data/lib/generators/surveyor/templates/vendor/assets/stylesheets/custom.sass +5 -0
  131. data/lib/surveyor/acts_as_response.rb +19 -0
  132. data/lib/surveyor/common.rb +59 -0
  133. data/lib/surveyor/engine.rb +15 -0
  134. data/lib/surveyor/helpers/formtastic_custom_input.rb +13 -0
  135. data/lib/surveyor/helpers/surveyor_helper_methods.rb +86 -0
  136. data/lib/surveyor/models/answer_methods.rb +76 -0
  137. data/lib/surveyor/models/dependency_condition_methods.rb +65 -0
  138. data/lib/surveyor/models/dependency_methods.rb +57 -0
  139. data/lib/surveyor/models/question_group_methods.rb +62 -0
  140. data/lib/surveyor/models/question_methods.rb +103 -0
  141. data/lib/surveyor/models/response_methods.rb +125 -0
  142. data/lib/surveyor/models/response_set_methods.rb +187 -0
  143. data/lib/surveyor/models/survey_methods.rb +95 -0
  144. data/lib/surveyor/models/survey_section_methods.rb +48 -0
  145. data/lib/surveyor/models/survey_translation_methods.rb +28 -0
  146. data/lib/surveyor/models/validation_condition_methods.rb +42 -0
  147. data/lib/surveyor/models/validation_methods.rb +41 -0
  148. data/lib/surveyor/mustache_context.rb +13 -0
  149. data/lib/surveyor/parser.rb +427 -0
  150. data/lib/surveyor/redcap_parser.rb +288 -0
  151. data/lib/surveyor/surveyor_controller_methods.rb +237 -0
  152. data/lib/surveyor/unparser.rb +161 -0
  153. data/lib/surveyor/version.rb +3 -0
  154. data/lib/surveyor.rb +13 -0
  155. data/lib/tasks/surveyor_tasks.rake +88 -0
  156. data/rails/init.rb +1 -0
  157. data/spec/controllers/surveyor_controller_spec.rb +304 -0
  158. data/spec/factories.rb +166 -0
  159. data/spec/features/ajax_spec.rb +205 -0
  160. data/spec/features/api_spec.rb +157 -0
  161. data/spec/features/duplicate_prevention_spec.rb +50 -0
  162. data/spec/features/translations_spec.rb +116 -0
  163. data/spec/features/ui_spec.rb +466 -0
  164. data/spec/fixtures/REDCapDemoDatabase_DataDictionary.csv +127 -0
  165. data/spec/fixtures/chinese_survey.rb +14 -0
  166. data/spec/fixtures/everything.rb +215 -0
  167. data/spec/fixtures/favorites-ish.rb +22 -0
  168. data/spec/fixtures/favorites.rb +22 -0
  169. data/spec/fixtures/feelings.rb +38 -0
  170. data/spec/fixtures/lifestyle.rb +55 -0
  171. data/spec/fixtures/numbers.rb +21 -0
  172. data/spec/fixtures/redcap_new_headers.csv +1 -0
  173. data/spec/fixtures/redcap_siblings.csv +1 -0
  174. data/spec/fixtures/redcap_whitespace.csv +1 -0
  175. data/spec/helpers/formtastic_custom_input_spec.rb +15 -0
  176. data/spec/helpers/surveyor_helper_spec.rb +114 -0
  177. data/spec/lib/common_spec.rb +37 -0
  178. data/spec/lib/parser_spec.rb +393 -0
  179. data/spec/lib/rake_kitchen_sink.rb +40 -0
  180. data/spec/lib/redcap_parser_spec.rb +129 -0
  181. data/spec/lib/tasks_spec.rake +26 -0
  182. data/spec/lib/unparser_spec.rb +126 -0
  183. data/spec/models/answer_spec.rb +144 -0
  184. data/spec/models/dependency_condition_spec.rb +428 -0
  185. data/spec/models/dependency_spec.rb +90 -0
  186. data/spec/models/question_group_spec.rb +66 -0
  187. data/spec/models/question_spec.rb +176 -0
  188. data/spec/models/response_set_spec.rb +447 -0
  189. data/spec/models/response_spec.rb +205 -0
  190. data/spec/models/survey_section_spec.rb +58 -0
  191. data/spec/models/survey_spec.rb +155 -0
  192. data/spec/models/validation_condition_spec.rb +98 -0
  193. data/spec/models/validation_spec.rb +64 -0
  194. data/spec/rcov.opts +2 -0
  195. data/spec/spec.opts +4 -0
  196. data/spec/spec_helper.rb +99 -0
  197. data/spec/support/shared.rb +5 -0
  198. data/spec/support/surveyor_api_helpers.rb +15 -0
  199. data/spec/support/surveyor_ui_helpers.rb +108 -0
  200. data/spec/support/wait_for_ajax.rb +11 -0
  201. data/surveyor.gemspec +40 -0
  202. data/switch.sh +53 -0
  203. metadata +510 -0
@@ -0,0 +1,428 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+
4
+ describe DependencyCondition do
5
+ it "should have a list of operators" do
6
+ %w(== != < > <= >=).each do |operator|
7
+ DependencyCondition.operators.include?(operator).should be_true
8
+ end
9
+ end
10
+
11
+ describe "instance" do
12
+ before(:each) do
13
+ @dependency_condition = DependencyCondition.new(
14
+ :dependency_id => 1, :question_id => 45, :operator => "==",
15
+ :answer_id => 23, :rule_key => "A")
16
+ end
17
+
18
+ it "should be valid" do
19
+ @dependency_condition.should be_valid
20
+ end
21
+
22
+ it "should be invalid without a parent dependency_id, question_id" do
23
+ # this causes issues with building and saving
24
+ # @dependency_condition.dependency_id = nil
25
+ # @dependency_condition.should have(1).errors_on(:dependency_id)
26
+ # @dependency_condition.question_id = nil
27
+ # @dependency_condition.should have(1).errors_on(:question_id)
28
+ end
29
+
30
+ it "should be invalid without an operator" do
31
+ @dependency_condition.operator = nil
32
+ @dependency_condition.should have(2).errors_on(:operator)
33
+ end
34
+
35
+ it "should be invalid without a rule_key" do
36
+ @dependency_condition.should be_valid
37
+ @dependency_condition.rule_key = nil
38
+ @dependency_condition.should_not be_valid
39
+ @dependency_condition.should have(1).errors_on(:rule_key)
40
+ end
41
+
42
+ it "should have unique rule_key within the context of a dependency" do
43
+ @dependency_condition.should be_valid
44
+ DependencyCondition.create(
45
+ :dependency_id => 2, :question_id => 46, :operator => "==",
46
+ :answer_id => 14, :rule_key => "B")
47
+ @dependency_condition.rule_key = "B" # rule key uniquness is scoped by dependency_id
48
+ @dependency_condition.dependency_id = 2
49
+ @dependency_condition.should_not be_valid
50
+ @dependency_condition.should have(1).errors_on(:rule_key)
51
+ end
52
+
53
+ it "should have an operator in DependencyCondition.operators" do
54
+ DependencyCondition.operators.each do |o|
55
+ @dependency_condition.operator = o
56
+ @dependency_condition.should have(0).errors_on(:operator)
57
+ end
58
+ @dependency_condition.operator = "#"
59
+ @dependency_condition.should have(1).error_on(:operator)
60
+ end
61
+ it "should have a properly formed count operator" do
62
+ %w(count>1 count<1 count>=1 count<=1 count==1 count!=1).each do |o|
63
+ @dependency_condition.operator = o
64
+ @dependency_condition.should have(0).errors_on(:operator)
65
+ end
66
+ %w(count> count< count>= count<= count== count!=).each do |o|
67
+ @dependency_condition.operator = o
68
+ @dependency_condition.should have(1).errors_on(:operator)
69
+ end
70
+ %w(count=1 count><1 count<>1 count!1 count!!1 count=>1 count=<1).each do |o|
71
+ @dependency_condition.operator = o
72
+ @dependency_condition.should have(1).errors_on(:operator)
73
+ end
74
+ %w(count= count>< count<> count! count!! count=> count=< count> count< count>= count<= count== count!=).each do |o|
75
+ @dependency_condition.operator = o
76
+ @dependency_condition.should have(1).errors_on(:operator)
77
+ end
78
+ end
79
+ end
80
+
81
+ it "returns true for != with no responses" do
82
+ question = FactoryGirl.create(:question)
83
+ dependency_condition = FactoryGirl.create(:dependency_condition, :rule_key => "C", :question => question)
84
+ rs = FactoryGirl.create(:response_set)
85
+ dependency_condition.to_hash(rs).should == {:C => false}
86
+ end
87
+
88
+
89
+ it "should not assume that Response#as is not nil" do
90
+ # q_HEIGHT_FT "Portion of height in whole feet (e.g., 5)",
91
+ # :pick=>:one
92
+ # a :integer
93
+ # a_neg_1 "Refused"
94
+ # a_neg_2 "Don't know"
95
+ # label "Provided value is outside of the suggested range (4 to 7 feet). This value is admissible, but you may wish to verify."
96
+ # dependency :rule=>"A or B"
97
+ # condition_A :q_HEIGHT_FT, "<", {:integer_value => "4"}
98
+ # condition_B :q_HEIGHT_FT, ">", {:integer_value => "7"}
99
+
100
+ answer = FactoryGirl.create(:answer, :response_class => :integer)
101
+ @dependency_condition = DependencyCondition.new(
102
+ :dependency => FactoryGirl.create(:dependency),
103
+ :question => answer.question,
104
+ :answer => answer,
105
+ :operator => ">",
106
+ :integer_value => 4,
107
+ :rule_key => "A")
108
+
109
+ response = FactoryGirl.create(:response, :answer => answer, :question => answer.question)
110
+ response_set = response.response_set
111
+ response.integer_value.should == nil
112
+
113
+ @dependency_condition.to_hash(response_set).should == {:A => false}
114
+ end
115
+
116
+ describe "evaluate '==' operator" do
117
+ before(:each) do
118
+ @a = FactoryGirl.create(:answer, :response_class => "answer")
119
+ @b = FactoryGirl.create(:answer, :question => @a.question)
120
+ @r = FactoryGirl.create(:response, :question => @a.question, :answer => @a)
121
+ @rs = @r.response_set
122
+ @dc = FactoryGirl.create(:dependency_condition, :question => @a.question, :answer => @a, :operator => "==", :rule_key => "D")
123
+ @dc.as(:answer).should == @r.as(:answer)
124
+ end
125
+
126
+ it "with checkbox/radio type response" do
127
+ @dc.to_hash(@rs).should == {:D => true}
128
+ @dc.answer = @b
129
+ @dc.to_hash(@rs).should == {:D => false}
130
+ end
131
+
132
+ it "with string value response" do
133
+ @a.update_attributes(:response_class => "string")
134
+ @r.update_attributes(:string_value => "hello123")
135
+ @dc.string_value = "hello123"
136
+ @dc.to_hash(@rs).should == {:D => true}
137
+ @r.update_attributes(:string_value => "foo_abc")
138
+ @dc.to_hash(@rs).should == {:D => false}
139
+ end
140
+
141
+ it "with a text value response" do
142
+ @a.update_attributes(:response_class => "text")
143
+ @r.update_attributes(:text_value => "hello this is some text for comparison")
144
+ @dc.text_value = "hello this is some text for comparison"
145
+ @dc.to_hash(@rs).should == {:D => true}
146
+ @r.update_attributes(:text_value => "Not the same text")
147
+ @dc.to_hash(@rs).should == {:D => false}
148
+ end
149
+
150
+ it "with an integer value response" do
151
+ @a.update_attributes(:response_class => "integer")
152
+ @r.update_attributes(:integer_value => 10045)
153
+ @dc.integer_value = 10045
154
+ @dc.to_hash(@rs).should == {:D => true}
155
+ @r.update_attributes(:integer_value => 421)
156
+ @dc.to_hash(@rs).should == {:D => false}
157
+ end
158
+
159
+ it "with a float value response" do
160
+ @a.update_attributes(:response_class => "float")
161
+ @r.update_attributes(:float_value => 121.1)
162
+ @dc.float_value = 121.1
163
+ @dc.to_hash(@rs).should == {:D => true}
164
+ @r.update_attributes(:float_value => 130.123)
165
+ @dc.to_hash(@rs).should == {:D => false}
166
+ end
167
+ end
168
+
169
+ describe "evaluate '!=' operator" do
170
+ before(:each) do
171
+ @a = FactoryGirl.create(:answer)
172
+ @b = FactoryGirl.create(:answer, :question => @a.question)
173
+ @r = FactoryGirl.create(:response, :question => @a.question, :answer => @a)
174
+ @rs = @r.response_set
175
+ @dc = FactoryGirl.create(:dependency_condition, :question => @a.question, :answer => @a, :operator => "!=", :rule_key => "E")
176
+ @dc.as(:answer).should == @r.as(:answer)
177
+ end
178
+
179
+ it "with checkbox/radio type response" do
180
+ @dc.to_hash(@rs).should == {:E => false}
181
+ @dc.answer_id = @a.id.to_i+1
182
+ @dc.to_hash(@rs).should == {:E => true}
183
+ end
184
+
185
+ it "with string value response" do
186
+ @a.update_attributes(:response_class => "string")
187
+ @r.update_attributes(:string_value => "hello123")
188
+ @dc.string_value = "hello123"
189
+ @dc.to_hash(@rs).should == {:E => false}
190
+ @r.update_attributes(:string_value => "foo_abc")
191
+ @dc.to_hash(@rs).should == {:E => true}
192
+ end
193
+
194
+ it "with a text value response" do
195
+ @a.update_attributes(:response_class => "text")
196
+ @r.update_attributes(:text_value => "hello this is some text for comparison")
197
+ @dc.text_value = "hello this is some text for comparison"
198
+ @dc.to_hash(@rs).should == {:E => false}
199
+ @r.update_attributes(:text_value => "Not the same text")
200
+ @dc.to_hash(@rs).should == {:E => true}
201
+ end
202
+
203
+ it "with an integer value response" do
204
+ @a.update_attributes(:response_class => "integer")
205
+ @r.update_attributes(:integer_value => 10045)
206
+ @dc.integer_value = 10045
207
+ @dc.to_hash(@rs).should == {:E => false}
208
+ @r.update_attributes(:integer_value => 421)
209
+ @dc.to_hash(@rs).should == {:E => true}
210
+ end
211
+
212
+ it "with a float value response" do
213
+ @a.update_attributes(:response_class => "float")
214
+ @r.update_attributes(:float_value => 121.1)
215
+ @dc.float_value = 121.1
216
+ @dc.to_hash(@rs).should == {:E => false}
217
+ @r.update_attributes(:float_value => 130.123)
218
+ @dc.to_hash(@rs).should == {:E => true}
219
+ end
220
+ end
221
+
222
+ describe "evaluate the '<' operator" do
223
+ before(:each) do
224
+ @a = FactoryGirl.create(:answer)
225
+ @b = FactoryGirl.create(:answer, :question => @a.question)
226
+ @r = FactoryGirl.create(:response, :question => @a.question, :answer => @a)
227
+ @rs = @r.response_set
228
+ @dc = FactoryGirl.create(:dependency_condition, :question => @a.question, :answer => @a, :operator => "<", :rule_key => "F")
229
+ @dc.as(:answer).should == @r.as(:answer)
230
+ end
231
+
232
+ it "with an integer value response" do
233
+ @a.update_attributes(:response_class => "integer")
234
+ @r.update_attributes(:integer_value => 50)
235
+ @dc.integer_value = 100
236
+ @dc.to_hash(@rs).should == {:F => true}
237
+ @r.update_attributes(:integer_value => 421)
238
+ @dc.to_hash(@rs).should == {:F => false}
239
+ end
240
+
241
+ it "with a float value response" do
242
+ @a.update_attributes(:response_class => "float")
243
+ @r.update_attributes(:float_value => 5.1)
244
+ @dc.float_value = 121.1
245
+ @dc.to_hash(@rs).should == {:F => true}
246
+ @r.update_attributes(:float_value => 130.123)
247
+ @dc.to_hash(@rs).should == {:F => false}
248
+ end
249
+ end
250
+
251
+ describe "evaluate the '<=' operator" do
252
+ before(:each) do
253
+ @a = FactoryGirl.create(:answer)
254
+ @b = FactoryGirl.create(:answer, :question => @a.question)
255
+ @r = FactoryGirl.create(:response, :question => @a.question, :answer => @a)
256
+ @rs = @r.response_set
257
+ @dc = FactoryGirl.create(:dependency_condition, :question => @a.question, :answer => @a, :operator => "<=", :rule_key => "G")
258
+ @dc.as(:answer).should == @r.as(:answer)
259
+ end
260
+
261
+ it "with an integer value response" do
262
+ @a.update_attributes(:response_class => "integer")
263
+ @r.update_attributes(:integer_value => 50)
264
+ @dc.integer_value = 100
265
+ @dc.to_hash(@rs).should == {:G => true}
266
+ @r.update_attributes(:integer_value => 100)
267
+ @dc.to_hash(@rs).should == {:G => true}
268
+ @r.update_attributes(:integer_value => 421)
269
+ @dc.to_hash(@rs).should == {:G => false}
270
+ end
271
+
272
+ it "with a float value response" do
273
+ @a.update_attributes(:response_class => "float")
274
+ @r.update_attributes(:float_value => 5.1)
275
+ @dc.float_value = 121.1
276
+ @dc.to_hash(@rs).should == {:G => true}
277
+ @r.update_attributes(:float_value => 121.1)
278
+ @dc.to_hash(@rs).should == {:G => true}
279
+ @r.update_attributes(:float_value => 130.123)
280
+ @dc.to_hash(@rs).should == {:G => false}
281
+ end
282
+
283
+ end
284
+
285
+ describe "evaluate the '>' operator" do
286
+ before(:each) do
287
+ @a = FactoryGirl.create(:answer)
288
+ @b = FactoryGirl.create(:answer, :question => @a.question)
289
+ @r = FactoryGirl.create(:response, :question => @a.question, :answer => @a)
290
+ @rs = @r.response_set
291
+ @dc = FactoryGirl.create(:dependency_condition, :question => @a.question, :answer => @a, :operator => ">", :rule_key => "H")
292
+ @dc.as(:answer).should == @r.as(:answer)
293
+ end
294
+
295
+ it "with an integer value response" do
296
+ @a.update_attributes(:response_class => "integer")
297
+ @r.update_attributes(:integer_value => 50)
298
+ @dc.integer_value = 100
299
+ @dc.to_hash(@rs).should == {:H => false}
300
+ @r.update_attributes(:integer_value => 421)
301
+ @dc.to_hash(@rs).should == {:H => true}
302
+ end
303
+
304
+ it "with a float value response" do
305
+ @a.update_attributes(:response_class => "float")
306
+ @r.update_attributes(:float_value => 5.1)
307
+ @dc.float_value = 121.1
308
+ @dc.to_hash(@rs).should == {:H => false}
309
+ @r.update_attributes(:float_value => 130.123)
310
+ @dc.to_hash(@rs).should == {:H => true}
311
+ end
312
+ end
313
+
314
+ describe "evaluate the '>=' operator" do
315
+ before(:each) do
316
+ @a = FactoryGirl.create(:answer)
317
+ @b = FactoryGirl.create(:answer, :question => @a.question)
318
+ @r = FactoryGirl.create(:response, :question => @a.question, :answer => @a)
319
+ @rs = @r.response_set
320
+ @dc = FactoryGirl.create(:dependency_condition, :question => @a.question, :answer => @a, :operator => ">=", :rule_key => "I")
321
+ @dc.as(:answer).should == @r.as(:answer)
322
+ end
323
+
324
+ it "with an integer value response" do
325
+ @a.update_attributes(:response_class => "integer")
326
+ @r.update_attributes(:integer_value => 50)
327
+ @dc.integer_value = 100
328
+ @dc.to_hash(@rs).should == {:I => false}
329
+ @r.update_attributes(:integer_value => 100)
330
+ @dc.to_hash(@rs).should == {:I => true}
331
+ @r.update_attributes(:integer_value => 421)
332
+ @dc.to_hash(@rs).should == {:I => true}
333
+ end
334
+
335
+ it "with a float value response" do
336
+ @a.update_attributes(:response_class => "float")
337
+ @r.update_attributes(:float_value => 5.1)
338
+ @dc.float_value = 121.1
339
+ @dc.to_hash(@rs).should == {:I => false}
340
+ @r.update_attributes(:float_value => 121.1)
341
+ @dc.to_hash(@rs).should == {:I => true}
342
+ @r.update_attributes(:float_value => 130.123)
343
+ @dc.to_hash(@rs).should == {:I => true}
344
+ end
345
+ end
346
+
347
+ describe "evaluating with response_class string" do
348
+ it "should compare answer ids when the dependency condition string_value is nil" do
349
+ @a = FactoryGirl.create(:answer, :response_class => "string")
350
+ @b = FactoryGirl.create(:answer, :question => @a.question)
351
+ @r = FactoryGirl.create(:response, :question => @a.question, :answer => @a, :string_value => "")
352
+ @rs = @r.response_set
353
+ @dc = FactoryGirl.create(:dependency_condition, :question => @a.question, :answer => @a, :operator => "==", :rule_key => "J")
354
+ @dc.to_hash(@rs).should == {:J => true}
355
+ end
356
+
357
+ it "should compare strings when the dependency condition string_value is not nil, even if it is blank" do
358
+ @a = FactoryGirl.create(:answer, :response_class => "string")
359
+ @b = FactoryGirl.create(:answer, :question => @a.question)
360
+ @r = FactoryGirl.create(:response, :question => @a.question, :answer => @a, :string_value => "foo")
361
+ @rs = @r.response_set
362
+ @dc = FactoryGirl.create(:dependency_condition, :question => @a.question, :answer => @a, :operator => "==", :rule_key => "K", :string_value => "foo")
363
+ @dc.to_hash(@rs).should == {:K => true}
364
+
365
+ @r.update_attributes(:string_value => "")
366
+ @dc.string_value = ""
367
+ @dc.to_hash(@rs).should == {:K => true}
368
+ end
369
+ end
370
+
371
+ describe "evaluate 'count' operator" do
372
+ before(:each) do
373
+ @q = FactoryGirl.create(:question)
374
+ @dc = DependencyCondition.new(:operator => "count>2", :rule_key => "M", :question => @q)
375
+ @as = []
376
+ 3.times do
377
+ @as << FactoryGirl.create(:answer, :question => @q, :response_class => "answer")
378
+ end
379
+ @rs = FactoryGirl.create(:response_set)
380
+ @as.slice(0,2).each do |a|
381
+ FactoryGirl.create(:response, :question => @q, :answer => a, :response_set => @rs)
382
+ end
383
+ @rs.save
384
+ end
385
+
386
+ it "with operator with >" do
387
+ @dc.to_hash(@rs).should == {:M => false}
388
+ FactoryGirl.create(:response, :question => @q, :answer => @as.last, :response_set => @rs)
389
+ @rs.reload.responses.count.should == 3
390
+ @dc.to_hash(@rs.reload).should == {:M => true}
391
+ end
392
+
393
+ it "with operator with <" do
394
+ @dc.operator = "count<2"
395
+ @dc.to_hash(@rs).should == {:M => false}
396
+ @dc.operator = "count<3"
397
+ @dc.to_hash(@rs).should == {:M => true}
398
+ end
399
+
400
+ it "with operator with <=" do
401
+ @dc.operator = "count<=1"
402
+ @dc.to_hash(@rs).should == {:M => false}
403
+ @dc.operator = "count<=2"
404
+ @dc.to_hash(@rs).should == {:M => true}
405
+ @dc.operator = "count<=3"
406
+ @dc.to_hash(@rs).should == {:M => true}
407
+ end
408
+
409
+ it "with operator with >=" do
410
+ @dc.operator = "count>=1"
411
+ @dc.to_hash(@rs).should == {:M => true}
412
+ @dc.operator = "count>=2"
413
+ @dc.to_hash(@rs).should == {:M => true}
414
+ @dc.operator = "count>=3"
415
+ @dc.to_hash(@rs).should == {:M => false}
416
+ end
417
+
418
+ it "with operator with !=" do
419
+ @dc.operator = "count!=1"
420
+ @dc.to_hash(@rs).should == {:M => true}
421
+ @dc.operator = "count!=2"
422
+ @dc.to_hash(@rs).should == {:M => false}
423
+ @dc.operator = "count!=3"
424
+ @dc.to_hash(@rs).should == {:M => true}
425
+ end
426
+ end
427
+
428
+ end
@@ -0,0 +1,90 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Dependency do
4
+ before(:each) do
5
+ @dependency = FactoryGirl.create(:dependency)
6
+ end
7
+
8
+ it "should be valid" do
9
+ @dependency.should be_valid
10
+ end
11
+
12
+ it "should be invalid without a rule" do
13
+ @dependency.rule = nil
14
+ @dependency.should have(2).errors_on(:rule)
15
+ @dependency.rule = " "
16
+ @dependency.should have(1).errors_on(:rule)
17
+ end
18
+
19
+ it "should be invalid without a question_id" do
20
+ @dependency.question_id = nil
21
+ @dependency.should have(1).error_on(:question_id)
22
+
23
+ @dependency.question_group_id = 1
24
+ @dependency.should be_valid
25
+
26
+ @dependency.question_id.should be_nil
27
+ @dependency.question_group_id = nil
28
+ @dependency.should have(1).error_on(:question_group_id)
29
+ end
30
+
31
+ it "should alias question_id as dependent_question_id" do
32
+ @dependency.question_id = 19
33
+ @dependency.dependent_question_id.should == 19
34
+ @dependency.dependent_question_id = 14
35
+ @dependency.question_id.should == 14
36
+ end
37
+
38
+ it "should be invalid unless rule composed of only references and operators" do
39
+ @dependency.rule = "foo"
40
+ @dependency.should have(1).error_on(:rule)
41
+ @dependency.rule = "1 to 2"
42
+ @dependency.should have(1).error_on(:rule)
43
+ @dependency.rule = "a and b"
44
+ @dependency.should have(1).error_on(:rule)
45
+ end
46
+ end
47
+
48
+ describe Dependency, "when evaluating dependency conditions of a question in a response set" do
49
+
50
+ before(:each) do
51
+ @dep = Dependency.new(:rule => "A", :question_id => 1)
52
+ @dep2 = Dependency.new(:rule => "A and B", :question_id => 1)
53
+ @dep3 = Dependency.new(:rule => "A or B", :question_id => 1)
54
+ @dep4 = Dependency.new(:rule => "!(A and B) and C", :question_id => 1)
55
+
56
+ @dep_c = mock_model(DependencyCondition, :id => 1, :rule_key => "A", :to_hash => {:A => true})
57
+ @dep_c2 = mock_model(DependencyCondition, :id => 2, :rule_key => "B", :to_hash => {:B => false})
58
+ @dep_c3 = mock_model(DependencyCondition, :id => 3, :rule_key => "C", :to_hash => {:C => true})
59
+
60
+ @dep.stub(:dependency_conditions).and_return([@dep_c])
61
+ @dep2.stub(:dependency_conditions).and_return([@dep_c, @dep_c2])
62
+ @dep3.stub(:dependency_conditions).and_return([@dep_c, @dep_c2])
63
+ @dep4.stub(:dependency_conditions).and_return([@dep_c, @dep_c2, @dep_c3])
64
+ end
65
+
66
+ it "knows if the dependencies are met" do
67
+ @dep.is_met?(@response_set).should be_true
68
+ @dep2.is_met?(@response_set).should be_false
69
+ @dep3.is_met?(@response_set).should be_true
70
+ @dep4.is_met?(@response_set).should be_true
71
+ end
72
+
73
+ it "returns the proper keyed pairs from the dependency conditions" do
74
+ @dep.conditions_hash(@response_set).should == {:A => true}
75
+ @dep2.conditions_hash(@response_set).should == {:A => true, :B => false}
76
+ @dep3.conditions_hash(@response_set).should == {:A => true, :B => false}
77
+ @dep4.conditions_hash(@response_set).should == {:A => true, :B => false, :C => true}
78
+ end
79
+ end
80
+ describe Dependency, "with conditions" do
81
+ it "should destroy conditions when destroyed" do
82
+ @dependency = Dependency.new(:rule => "A and B and C", :question_id => 1)
83
+ FactoryGirl.create(:dependency_condition, :dependency => @dependency, :rule_key => "A")
84
+ FactoryGirl.create(:dependency_condition, :dependency => @dependency, :rule_key => "B")
85
+ FactoryGirl.create(:dependency_condition, :dependency => @dependency, :rule_key => "C")
86
+ dc_ids = @dependency.dependency_conditions.map(&:id)
87
+ @dependency.destroy
88
+ dc_ids.each{|id| DependencyCondition.find_by_id(id).should == nil}
89
+ end
90
+ end