surveyor 0.8.0 → 0.9.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.
Files changed (45) hide show
  1. data/README.md +22 -5
  2. data/VERSION +1 -1
  3. data/app/models/dependency.rb +16 -32
  4. data/app/models/question.rb +1 -1
  5. data/app/models/question_group.rb +1 -1
  6. data/app/models/response.rb +1 -4
  7. data/app/models/response_set.rb +4 -3
  8. data/app/models/survey.rb +0 -1
  9. data/app/models/validation.rb +20 -0
  10. data/app/models/validation_condition.rb +19 -17
  11. data/generators/surveyor/surveyor_generator.rb +9 -5
  12. data/generators/surveyor/templates/migrate/add_display_order_to_surveys.rb +9 -0
  13. data/generators/surveyor/templates/surveys/kitchen_sink_survey.rb +15 -4
  14. data/lib/surveyor.rb +16 -0
  15. data/script/surveyor/answer.rb +32 -55
  16. data/script/surveyor/base.rb +61 -0
  17. data/script/surveyor/dependency.rb +4 -41
  18. data/script/surveyor/dependency_condition.rb +13 -38
  19. data/script/surveyor/question.rb +16 -50
  20. data/script/surveyor/question_group.rb +7 -19
  21. data/script/surveyor/specs/answer_spec.rb +15 -52
  22. data/script/surveyor/specs/question_spec.rb +37 -85
  23. data/script/surveyor/specs/spec_helper.rb +6 -0
  24. data/script/surveyor/specs/survey_section_spec.rb +23 -0
  25. data/script/surveyor/specs/validation_condition_spec.rb +20 -0
  26. data/script/surveyor/specs/validation_spec.rb +20 -0
  27. data/script/surveyor/survey.rb +10 -85
  28. data/script/surveyor/survey_parser.rb +160 -38
  29. data/script/surveyor/survey_section.rb +6 -130
  30. data/script/surveyor/validation.rb +19 -0
  31. data/script/surveyor/validation_condition.rb +19 -0
  32. data/spec/lib/surveyor_spec.rb +44 -0
  33. data/spec/models/dependency_spec.rb +9 -16
  34. data/spec/models/question_group_spec.rb +3 -3
  35. data/spec/models/question_spec.rb +1 -1
  36. data/spec/models/validation_condition_spec.rb +29 -0
  37. data/spec/models/validation_spec.rb +27 -0
  38. data/spec/spec_helper.rb +0 -2
  39. data/surveyor.gemspec +12 -7
  40. metadata +12 -7
  41. data/lib/tiny_code.rb +0 -58
  42. data/script/surveyor/columnizer.rb +0 -36
  43. data/script/surveyor/specs/question_dependency_spec.rb +0 -46
  44. data/script/surveyor/specs/question_group_spec.rb +0 -9
  45. data/script/surveyor/specs/section_spec.rb +0 -58
@@ -0,0 +1,61 @@
1
+ class SurveyParser
2
+ class Base
3
+
4
+ # Class level instance variable, because class variable are shared with subclasses
5
+ class << self
6
+ attr_accessor :children
7
+ end
8
+
9
+ @children = []
10
+
11
+ # Class methods
12
+ def self.inherited(subclass)
13
+ # set the class level instance variable default on subclasses
14
+ subclass.instance_variable_set(:@children, self.instance_variable_get(:@children))
15
+ end
16
+
17
+ def self.has_children(*args)
18
+ args.each{|model| attr_accessor model}
19
+ self.instance_variable_set(:@children, args)
20
+ end
21
+
22
+ # Instance methods
23
+ def initialize(obj, args, opts)
24
+ # inherit the parser from parent (obj)
25
+ self.parser = (obj.nil? ? nil : obj.class == SurveyParser ? obj : obj.parser)
26
+ # get a new id from the parser
27
+ self.id = parser.nil? ? nil : parser.send("new_#{self.class.name.underscore}_id")
28
+ # set [parent]_id to obj.id, if we have that attribute
29
+ self.send("#{obj.class.name.underscore}_id=", obj.nil? ? nil : obj.id) if self.respond_to?("#{obj.class.name.underscore}_id=")
30
+ # initialize descendant models
31
+ self.class.children.each{|model| self.send("#{model}=", [])}
32
+ # combine default options, parsed opts, parsed args, and initialize instance variables
33
+ self.default_options.merge(parse_opts(opts)).merge(parse_args(args)).each{|k,v| self.send("#{k.to_s}=", v)}
34
+ # print to the log
35
+ print "#{self.class.name.gsub(/[a-z]/, "")[-1,1]}#{self.id} "
36
+ end
37
+ def default_options
38
+ {}
39
+ end
40
+ def parse_opts(opts)
41
+ opts.reject{|k,v| k == :method_name} # toss the method name by default
42
+ end
43
+ def parse_args(args)
44
+ args[0] || {}
45
+ end
46
+
47
+ # Filter out attributes that shouldn't be in fixtures, including children, parser, placeholders
48
+ def yml_attrs
49
+ instance_variables.sort - self.class.children.map{|model| "@#{model.to_s}"} - %w(@parser @dependency @validation @question_reference @answer_reference)
50
+ end
51
+ def to_yml
52
+ out = [ %(#{@data_export_identifier}_#{@id}:) ]
53
+ yml_attrs.each{|a| out << " #{a[1..-1]}: #{instance_variable_get(a).is_a?(String) ? "\"#{instance_variable_get(a)}\"" : instance_variable_get(a) }"}
54
+ (out << nil ).join("\r\n")
55
+ end
56
+ def to_file
57
+ File.open(self.parser.send("#{self.class.name.underscore.pluralize}_yml"), File::CREAT|File::APPEND|File::WRONLY) {|f| f << to_yml}
58
+ self.class.children.each{|model| self.send(model).compact.map(&:to_file)}
59
+ end
60
+ end
61
+ end
@@ -1,48 +1,11 @@
1
- class Dependency
2
-
1
+ class Dependency < SurveyParser::Base
3
2
  # Context, Conditional, Children
4
3
  attr_accessor :id, :question_id, :question_group_id, :parser
5
4
  attr_accessor :rule
6
- attr_accessor :dependency_conditions
5
+ has_children :dependency_conditions
7
6
 
8
- # id, question, and rule required
9
- def initialize(question, args, options)
10
- self.parser = question.parser
11
- self.id = parser.new_dependency_id
12
- if question.class == QuestionGroup
13
- self.question_group_id = question.id
14
- else
15
- self.question_id = question.id
16
- end
17
- self.rule = (args[0] || {})[:rule]
18
- self.dependency_conditions = []
19
- self.default_options().merge(options).merge(args[1] || {}).each{|key,value| self.instance_variable_set("@#{key}", value)}
7
+ def parse_opts(opts)
8
+ {} # toss the method name and reference identifier by default
20
9
  end
21
-
22
- def default_options()
23
- {}
24
- end
25
-
26
- # Injecting the id of the current dependency object into the dependency_condition on assignment
27
- def add_dependency_condition(dc_obj)
28
- dc_obj.dependency_id = self.id
29
- self.dependency_conditions << dc_obj
30
- end
31
-
32
- def yml_attrs
33
- instance_variables.sort - ["@parser", "@dependency_conditions", "@reference_identifier"]
34
- end
35
- def to_yml
36
- out = [ %(#{@data_export_identifier}_#{@id}:) ]
37
- yml_attrs.each{|a| out << " #{a[1..-1]}: #{instance_variable_get(a).is_a?(String) ? "\"#{instance_variable_get(a)}\"" : instance_variable_get(a) }"}
38
- (out << nil ).join("\r\n")
39
- end
40
-
41
- def to_file
42
- File.open(self.parser.dependencies_yml, File::CREAT|File::APPEND|File::WRONLY) {|f| f << to_yml}
43
- self.dependency_conditions.compact.map(&:to_file)
44
- end
45
-
46
-
47
10
 
48
11
  end
@@ -1,4 +1,4 @@
1
- class DependencyCondition
1
+ class DependencyCondition < SurveyParser::Base
2
2
 
3
3
  # Context, Conditional, Value
4
4
  attr_accessor :id, :dependency_id, :rule_key, :parser
@@ -6,58 +6,33 @@ class DependencyCondition
6
6
  attr_accessor :answer_id, :datetime_value, :integer_value, :float_value, :unit, :text_value, :string_value, :response_other
7
7
  attr_accessor :question_reference, :answer_reference
8
8
 
9
- # id, dependency, and question_id required
10
- def initialize(dependency, args, options)
11
- self.parser = dependency.parser
12
- self.id = parser.new_dependency_condition_id
13
- self.dependency_id = dependency.id
14
- args_options = parse_args_options(args)
15
- self.default_options().merge(options).merge(args_options).each{|key,value| self.instance_variable_set("@#{key}", value)}
16
- end
17
-
18
- def default_options()
9
+ def default_options
19
10
  { :operator => "==" }
20
11
  end
21
-
22
- def parse_args_options(args)
12
+ def parse_args(args)
23
13
  a0, a1, a2 = args
24
- options = {:question_reference => a0.to_s.gsub("q_", ""), :operator => a1}
25
- a2.is_a?(Hash) ? options.merge(a2) : options.merge({:answer_reference => a2.to_s.gsub("a_", "")})
26
- end
27
-
28
- def yml_attrs
29
- instance_variables.sort - ["@parser", "@question_reference", "@answer_reference", "@reference_identifier"]
14
+ {:question_reference => a0.to_s.gsub("q_", ""), :operator => a1}.merge(a2.is_a?(Hash) ? a2 : {:answer_reference => a2.to_s.gsub("a_", "")})
30
15
  end
31
- def to_yml
32
- out = [ %(#{@data_export_identifier}_#{@id}:) ]
33
- yml_attrs.each{|a| out << " #{a[1..-1]}: #{instance_variable_get(a).is_a?(String) ? "\"#{instance_variable_get(a)}\"" : instance_variable_get(a) }"}
34
- (out << nil ).join("\r\n")
16
+ def parse_opts(opts)
17
+ {:rule_key => opts[:reference_identifier]}
35
18
  end
36
-
19
+
37
20
  def reconcile_dependencies
38
21
  # Looking up references to questions and answers for linking the dependency objects
39
- puts "Looking up references for question: #{@question_reference}"
40
- ref_question = Survey.current_survey.find_question_by_reference(@question_reference) # TODO change this. Argh. I can't think of a better way to get a hold of this reference here...
41
- if ref_question
42
- puts " found question: #{ref_question.data_export_identifier} (id:#{ref_question.id})"
22
+ print "Lookup Q ref #{@question_reference}:"
23
+ if (ref_question = parser.current_survey.find_question_by_reference(@question_reference))
24
+ print " found Q#{ref_question.id} "
43
25
  @question_id = ref_question.id
44
- ref_answer = ref_question.find_answer_by_reference(@answer_reference)
45
- if ref_answer
26
+ print "Lookup A ref #{@answer_reference}"
27
+ if (ref_answer = ref_question.find_answer_by_reference(@answer_reference))
28
+ print " found A#{ref_answer.id} "
46
29
  @answer_id = ref_answer.id
47
30
  else
48
31
  raise "Could not find referenced answer #{@answer_reference}"
49
32
  end
50
-
51
33
  else
52
34
  raise "Could not find referenced question #{@question_reference}"
53
35
  end
54
36
  end
55
37
 
56
- def to_file
57
- # Reconciling the references used in the dsl to actual object ids
58
- #puts "Reconcile of dependency:"
59
- # reconcile_dependencies
60
- File.open(self.parser.dependency_conditions_yml, File::CREAT|File::APPEND|File::WRONLY) {|f| f << to_yml}
61
- end
62
-
63
38
  end
@@ -1,67 +1,33 @@
1
- require File.dirname(__FILE__) + '/columnizer'
2
-
3
- class Question
4
- include Columnizer
5
-
1
+ class Question < SurveyParser::Base
6
2
  # Context, Content, Reference, Display, Children
7
3
  attr_accessor :id, :parser, :survey_section_id, :question_group_id
8
4
  attr_accessor :text, :short_text, :help_text, :pick
9
- attr_accessor :reference_identifier, :data_export_identifier, :common_namespace, :common_identitier
5
+ attr_accessor :reference_identifier, :data_export_identifier, :common_namespace, :common_identifier
10
6
  attr_accessor :display_order, :display_type, :is_mandatory, :display_width, :custom_class, :custom_renderer
11
- attr_accessor :answers, :dependency
7
+ attr_accessor :dependency
8
+ has_children :answers
12
9
 
13
- def initialize(section, args, options)
14
- self.parser = section.parser
15
- self.id = parser.new_question_id
16
- self.display_order = self.id
17
- self.survey_section_id = section.id
18
- self.text = args[0]
19
- self.answers = []
20
- self.dependency = nil
21
- self.default_options(text).merge(options).merge(args[1] || {}).each{|key,value| self.instance_variable_set("@#{key}", value)}
22
- end
23
-
24
- def default_options(text)
25
- { :short_text => text,
26
- :pick => :none,
10
+ def default_options
11
+ { :pick => :none,
27
12
  :display_type => :default,
28
13
  :is_mandatory => true,
29
- :data_export_identifier => Columnizer.to_normalized_column(text)
14
+ :display_order => self.id
30
15
  }
31
16
  end
32
-
33
- # Injecting the id of this question object into the dependency object on assignment
34
- def dependency=(dep)
35
- unless dep.nil?
36
- dep.question_id = self.id
37
- end
38
- @dependency = dep
17
+ def parse_opts(opts)
18
+ (name = opts.delete(:method_name)) =~ /label|image/ ? opts.merge(:display_type => name) : opts
39
19
  end
40
-
41
- def find_answer_by_reference(ref_id)
42
- answer = nil
43
- count = 0
44
- puts "Looking up answer with ref: #{ref_id}"
45
- while answer.nil? and count < self.answers.size
46
- answer = self.answers[count] if self.answers[count].reference_identifier == ref_id
47
- count += 1
48
- end
49
- puts " found answer: '#{answer.text}' (id:#{answer.id})" unless answer.nil?
50
- answer
20
+ def parse_args(args)
21
+ text = args[0] || "Question"
22
+ {:text => text, :short_text => text, :data_export_identifier => Surveyor.to_normalized_string(text)}.merge(args[1] || {})
51
23
  end
52
24
 
53
- def yml_attrs
54
- instance_variables.sort - ["@parser", "@answers", "@dependency"]
55
- end
56
- def to_yml
57
- out = [ %(#{@data_export_identifier}_#{@id}:) ]
58
- yml_attrs.each{|a| out << " #{a[1..-1]}: #{instance_variable_get(a).is_a?(String) ? "\"#{instance_variable_get(a)}\"" : instance_variable_get(a) }"}
59
- (out << nil ).join("\r\n")
25
+ def find_answer_by_reference(ref_id)
26
+ self.answers.detect{|a| a.reference_identifier == ref_id}
60
27
  end
61
-
28
+
62
29
  def to_file
63
- File.open(self.parser.questions_yml, File::CREAT|File::APPEND|File::WRONLY){ |f| f << to_yml }
64
- self.answers.compact.map(&:to_file)
30
+ super
65
31
  if self.dependency then self.dependency.to_file end
66
32
  end
67
33
 
@@ -1,5 +1,4 @@
1
- class QuestionGroup
2
-
1
+ class QuestionGroup < SurveyParser::Base
3
2
  # Context, Content, Display, Children
4
3
  attr_accessor :id, :parser
5
4
  attr_accessor :text, :help_text
@@ -7,29 +6,18 @@ class QuestionGroup
7
6
  attr_accessor :display_type, :custom_class, :custom_renderer
8
7
  attr_accessor :dependency
9
8
 
10
- # id, section and text required
11
- def initialize(section, args, options)
12
- self.parser = section.parser
13
- self.id = parser.new_question_group_id
14
- self.text = args[0]
15
- self.default_options().merge(options).merge(args[1] || {}).each{|key,value| self.instance_variable_set("@#{key}", value)}
16
- end
17
-
18
- def default_options()
9
+ def default_options
19
10
  {:display_type => "default"}
20
11
  end
21
-
22
- def yml_attrs
23
- instance_variables.sort - ["@parser", "@dependency"]
12
+ def parse_args(args)
13
+ {:text => args[0] || "Question Group"}.merge(args[1] || {})
24
14
  end
25
- def to_yml
26
- out = [ %(#{@id}:) ]
27
- yml_attrs.each{|a| out << " #{a[1..-1]}: #{instance_variable_get(a).is_a?(String) ? "\"#{instance_variable_get(a)}\"" : instance_variable_get(a) }"}
28
- (out << nil ).join("\r\n")
15
+ def parse_opts(opts)
16
+ (name = opts.delete(:method_name)) =~ /grid|repeater/ ? opts.merge(:display_type => name) : opts
29
17
  end
30
18
 
31
19
  def to_file
32
- File.open(self.parser.question_groups_yml, File::CREAT|File::APPEND|File::WRONLY){ |f| f << to_yml }
20
+ super
33
21
  if self.dependency then self.dependency.to_file end
34
22
  end
35
23
 
@@ -1,66 +1,29 @@
1
- require File.dirname(__FILE__) + '/../answer'
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require 'answer'
2
3
 
3
4
  describe Answer, " when first created" do
4
5
 
5
- TEST_ID = 1
6
- TEST_CONTEXT_ID = "b3a_1"
7
- TEST_QUESTION_ID = "2"
8
- TEST_TEXT = "No / Rarely"
9
- TEST_OPTIONS = {:help_text => "Never or rarely ever"}
10
-
11
- before do
12
- @ans = Answer.new(TEST_ID, TEST_QUESTION_ID, TEST_CONTEXT_ID, TEST_TEXT, TEST_OPTIONS)
6
+ before(:each) do
7
+ question = mock("Question", :id => 2, :parser => mock("Parser", :new_answer_id => 1))
8
+ question.stub!(:class => Question)
9
+ options = {:help_text => "Never or rarely ever", :reference_identifier => "b3a_1"}
10
+ @ans = Answer.new(question, ["No / Rarely"], options)
13
11
  end
14
12
 
15
13
  it "should set inititalized variables to those passed in" do
16
- @ans.id.should == TEST_ID
17
- @ans.question_id.should == TEST_QUESTION_ID
18
- @ans.context_id.should == TEST_CONTEXT_ID
19
- @ans.text.should == TEST_TEXT
20
- @ans.help_text.should == TEST_OPTIONS[:help_text]
21
- end
14
+ @ans.id.should == 1
15
+ @ans.question_id.should == 2
16
+ @ans.reference_identifier.should == "b3a_1"
17
+ @ans.text.should == "No / Rarely"
18
+ @ans.help_text.should == "Never or rarely ever"
19
+ end
22
20
 
23
21
  it "should output current state to yml" do
24
22
  @ans.should.respond_to?(:to_yml)
25
23
  end
26
-
27
- it "should create a normalized code from the answer text" do
28
- # The answer object should take the title of the text and convert
29
- # it to a code that is more appropirate for a database entry
30
-
31
- # Taking a few answers from the survey for testing
32
- str = []
33
- str[0] = "This? is a in - t3rrible-@nswer of! (question) on"
34
- str[1] = "Private insurance/ HMO/ PPO"
35
- str[2] = "VA"
36
- str[3] = "PMS (Premenstrual syndrome)/ PMDD (Premenstrual Dysphoric Disorder)"
37
- str[4] = "Have never been employed outside the home"
38
- str[5] = "Professional"
39
- str[6] = "Not working because of temporary disability, but expect to return to a job"
40
-
41
- # What the results should look like
42
- r_str = []
43
- r_str[0] = "this_t3rrible_nswer"
44
- r_str[1] = "private_insurance_hmo_ppo"
45
- r_str[2] = "va"
46
- r_str[3] = "pms_pmdd"
47
- r_str[4] = "never_been_employed_outside_home"
48
- r_str[5] = "professional"
49
- r_str[6] = "temporary_disability_expect_return_job"
50
-
51
- count = 0
52
- str.each do |s|
53
-
54
- code = Answer.to_normalized_code(s)
55
- code.should eql(r_str[count])
56
- count += 1
57
-
58
- end
59
-
60
- end
61
-
24
+
62
25
  it "should create a normalized code automatically when initalized" do
63
- @ans.code.should eql("no_rarely")
26
+ @ans.data_export_identifier.should eql("no_rarely")
64
27
  end
65
28
 
66
29
  end
@@ -1,111 +1,63 @@
1
- require File.dirname(__FILE__) + '/../question'
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require 'question'
2
3
 
3
4
  describe Question, " when first created" do
4
- TEST_ID = 1
5
- TEST_CONTEXT_ID = "B3"
6
- TEST_SECTION_ID = 2
7
- TEST_TEXT = "In the past 12 months how many times have you been to a doctor?"
8
- TEST_HELP_TEXT = "Please give a rough estimate"
9
- TEST_OPTIONS = {:help_text => TEST_HELP_TEXT }
10
- TEST_CODE = "many_times_you_been_doctor"
11
-
12
5
  before do
13
- @ques = Question.new(TEST_ID, TEST_SECTION_ID, TEST_CONTEXT_ID, TEST_TEXT, TEST_OPTIONS)
6
+ section = mock("SurveySection", :id => 2, :parser => mock("Parser", :new_question_id => 1))
7
+ section.stub!(:class => SurveySection)
8
+ args = {:help_text => "Please give a rough estimate", :reference_identifier => "B3"}
9
+ options = {}
10
+ @ques = Question.new(section, ["In the past 12 months how many times have you been to a doctor?", args], options)
14
11
  end
15
12
 
16
13
  it "should set initialization parameters properly" do
17
- @ques.id.should == TEST_ID
18
- @ques.context_id.should == TEST_CONTEXT_ID
19
- @ques.section_id.should == TEST_SECTION_ID
20
- @ques.code.should == TEST_CODE
21
- @ques.options[:help_text].should == TEST_HELP_TEXT
14
+ @ques.id.should == 1
15
+ @ques.reference_identifier.should == "B3"
16
+ @ques.survey_section_id.should == 2
17
+ @ques.help_text.should == "Please give a rough estimate"
22
18
 
23
19
  #Checking the defaults
24
- @ques.options[:has_omit?].should == true
25
- @ques.options[:has_other?].should == false
26
- @ques.options[:answer_format].should == :medium_text
20
+ @ques.pick.should == :none
21
+ @ques.display_type.should == :default
22
+ @ques.is_mandatory.should == true
27
23
  end
28
24
 
29
25
  it "should output current state to yml" do
30
26
  @ques.should.respond_to?(:to_yml)
31
27
  end
32
-
33
- it "should create a normalized code from the answer text" do
34
- # The question object should take the title of the text and convert
35
- # it to a code that is more appropirate for a database entry
36
-
37
- # Taking a few questions from the survey for testing
38
- str = []
39
- str[0] = "How long has it been since you last visited a doctor for a routine checkup (routine being not for a particular reason)?"
40
- str[1] = "Do you take medications as directed?"
41
- str[2] = "Do you every leak urine (or) water when you didn't want to?" #checking for () and ' removal
42
- str[3] = "Do your biological family members (not adopted) have a \"history\" of any of the following?"
43
- str[4] = "Your health:"
44
- str[5] = "In general, you would say your health is:"
45
-
46
- # What the results should look like
47
- r_str = []
48
- r_str[0] = "visited_doctor_for_routine_checkup"
49
- r_str[1] = "you_take_medications_as_directed"
50
- r_str[2] = "urine_water_you_didnt_want"
51
- r_str[3] = "family_members_history_any_following"
52
- r_str[4] = "your_health"
53
- r_str[5] = "you_would_say_your_health"
54
-
55
- count = 0
56
- str.each do |s|
57
-
58
- code = Question.to_normalized_code(s)
59
- code.should eql(r_str[count])
60
- count += 1
61
-
62
- end
63
-
64
- end
65
-
28
+
66
29
  it "should create a normalized code automatically when initalized" do
67
- @ques.code.should eql(TEST_CODE)
30
+ @ques.data_export_identifier.should eql("many_times_you_been_doctor")
68
31
  end
69
32
 
70
- it "should update the normalized code if the title is changed" do
71
- @ques.code.should eql(TEST_CODE)
72
- @ques.text = "Sometimes or All the time?"
73
- @ques.code.should eql("sometimes_all_time")
74
- end
33
+ # We don't change titles via the DSL
34
+ # it "should update the normalized code if the title is changed" do
35
+ # @ques.data_export_identifier.should == "many_times_you_been_doctor"
36
+ # @ques.text = "Sometimes or All the time?"
37
+ # @ques.data_export_identifier.should == "sometimes_all_time"
38
+ # end
75
39
 
76
40
  end
77
41
 
78
- describe Question, " when it contains data" do
79
-
80
- before do # Mocking up some answers
81
- @question = Question.new(1,TEST_SECTION_ID,TEST_CONTEXT_ID,TEST_TEXT,TEST_OPTIONS)
82
- ma1 = mock("answer")
83
- ma1.stub!(:context_id).and_return("1")
84
- @question.add_answer(ma1)
85
-
86
- ma2 = mock("answer")
87
- ma2.stub!(:context_id).and_return("2")
88
- @question.add_answer(ma2)
89
-
90
- ma3 = mock("answer")
91
- ma3.stub!(:context_id).and_return("3")
92
- @question.add_answer(ma3)
93
-
42
+ describe Question, "when it contains data" do
43
+ before(:each) do
44
+ section = mock("SurveySection", :id => 2, :parser => mock("Parser", :new_question_id => 1))
45
+ args = {:help_text => "Please give a rough estimate", :reference_identifier => "B3"}
46
+ options = {}
47
+ @ques = Question.new(section, ["In the past 12 months how many times have you been to a doctor?", args], options)
48
+ @ques.answers << mock("Answer", :reference_identifier => "1", :text => "foo")
49
+ @ques.answers << mock("Answer", :reference_identifier => "2", :text => "foo")
50
+ @ques.answers << mock("Answer", :reference_identifier => "3", :text => "foo")
94
51
  end
95
-
52
+
96
53
  it "should have added the test answers correctly" do
97
- @question.answers.length.should eql(3)
98
- end
99
-
100
- it "should have a question text" do
101
- @question.text.should eql(TEST_TEXT)
54
+ @ques.answers.length.should eql(3)
102
55
  end
103
56
 
104
- it "should find an answer by context_id" do
105
- pending # yoon: commented out during dsl refactoring
106
- a_to_find = @question.find_answer_by_context_id("2")
107
- a_to_find.should_not eql(nil)
108
- a_to_find.context_id.should eql("2")
57
+ it "should find an answer by reference" do
58
+ a_to_find = @ques.find_answer_by_reference("2")
59
+ a_to_find.should_not be_nil
60
+ a_to_find.reference_identifier.should eql("2")
109
61
  end
110
62
 
111
63
  end