diff --git a/app/models/answer.rb b/app/models/answer.rb index cadb66d..a989c8f 100644 --- a/app/models/answer.rb +++ b/app/models/answer.rb @@ -1,16 +1,11 @@ class Answer < ActiveRecord::Base - #associations between tables - belongs_to :question - belongs_to :user - belongs_to :plan - -# accepts_nested_attributes_for :question -# accepts_nested_attributes_for :plan - - has_and_belongs_to_many :options, join_table: "answers_options" - - attr_accessible :text, :plan_id, :question_id, :user_id, :option_ids , :as => [:default, :admin] - - + #associations between tables + belongs_to :question + belongs_to :user + belongs_to :plan + + has_and_belongs_to_many :options, join_table: "answers_options" + + attr_accessible :text, :plan_id, :question_id, :user_id, :option_ids , :as => [:default, :admin] end diff --git a/app/models/dmptemplate.rb b/app/models/dmptemplate.rb index 981b585..1ef460c 100644 --- a/app/models/dmptemplate.rb +++ b/app/models/dmptemplate.rb @@ -2,6 +2,7 @@ include GlobalHelpers attr_accessible :id, :organisation_id, :description, :published, :title, :user_id, :locale, + :phases, :projects, :organisation, :is_default, :guidance_group_ids, :as => [:default, :admin] #associations between tables diff --git a/app/models/language.rb b/app/models/language.rb index 65477be..dabee23 100644 --- a/app/models/language.rb +++ b/app/models/language.rb @@ -1,5 +1,6 @@ class Language < ActiveRecord::Base has_many :users + has_many :organisations validates :abbreviation, presence: true, uniqueness: true end \ No newline at end of file diff --git a/app/models/phase.rb b/app/models/phase.rb index 155a86f..ebaee5f 100644 --- a/app/models/phase.rb +++ b/app/models/phase.rb @@ -18,7 +18,8 @@ accepts_nested_attributes_for :versions, :allow_destroy => true # accepts_nested_attributes_for :dmptemplate - attr_accessible :description, :number, :title, :dmptemplate_id, :as => [:default, :admin] + attr_accessible :description, :number, :title, :versions, :dmptemplate, + :dmptemplate_id, :as => [:default, :admin] friendly_id :title, use: [:slugged, :history, :finders] diff --git a/app/models/section.rb b/app/models/section.rb index fd1dcb7..e67367d 100644 --- a/app/models/section.rb +++ b/app/models/section.rb @@ -10,7 +10,8 @@ accepts_nested_attributes_for :questions, :reject_if => lambda {|a| a[:text].blank? }, :allow_destroy => true # accepts_nested_attributes_for :version - attr_accessible :organisation_id, :description, :number, :title, :version_id , :published, :questions_attributes, :as => [:default, :admin] + attr_accessible :organisation_id, :description, :number, :title, :version_id, :questions, :version, + :published, :questions_attributes, :as => [:default, :admin] ## # return the title of the section diff --git a/app/models/version.rb b/app/models/version.rb index db03966..06bafe8 100644 --- a/app/models/version.rb +++ b/app/models/version.rb @@ -11,7 +11,7 @@ # accepts_nested_attributes_for :phase accepts_nested_attributes_for :sections, :allow_destroy => true - attr_accessible :id, :description, :number, :published, :title, :phase_id, + attr_accessible :id, :description, :number, :published, :title, :phase_id, :phase, :sections, :sections_attributes, :as => [:default, :admin] ## diff --git a/test/fixtures/answer_options.yml b/test/fixtures/answer_options.yml deleted file mode 100644 index 96eecd5..0000000 --- a/test/fixtures/answer_options.yml +++ /dev/null @@ -1,26 +0,0 @@ -# Produces: -# ----------------------------- -# A selection for the 2nd option available when the question format is Checkbox or Dropdown. -# selects both options for MultiSelect and RadioButton -# -test_project_1_plan_1_answer_for_check_box_question_option_2: - answer: test_project_1_plan_1_answer_for_check_box_question - option: funder_template_2_phase_1_version_1_section_1_question_check_box_option_2 - -test_project_1_plan_1_answer_for_dropdown_question_option_2: - answer: test_project_1_plan_1_answer_for_dropdown_question - option: funder_template_2_phase_1_version_1_section_1_question_dropdown_option_2 - -test_project_1_plan_1_answer_for_multi_select_box_question_option_<%= n %>: - answer: test_project_1_plan_1_answer_for_multi_select_box_question - option: funder_template_2_phase_1_version_1_section_1_question_multi_select_box_option_<%= n %> - -<% 2.times.each do |n| %> -test_project_1_plan_1_answer_for_radio_button_question_option_<%= n %>: - answer: test_project_1_plan_1_answer_for_radio_button_question - option: funder_template_2_phase_1_version_1_section_1_question_radio_button_option_<%= n %> - -test_project_1_plan_1_answer_for_multi_select_box_question_option_<%= n %>: - answer: test_project_1_plan_1_answer_for_multi_select_box_question - option: funder_template_2_phase_1_version_1_section_1_question_multi_select_box_option_<%= n %> -<% end %> diff --git a/test/fixtures/answers.yml b/test/fixtures/answers.yml index 68ada13..b6438b2 100644 --- a/test/fixtures/answers.yml +++ b/test/fixtures/answers.yml @@ -3,31 +3,35 @@ test_project_1_plan_1_answer_for_text_area_question: text: "

Answer to the text area question for plan 1
Line 2
Line 3

" plan_id: test_project_1_plan_1 - user_id: cc_user + user_id: complete_user question_id: funder_template_2_phase_1_version_1_section_1_question_text_area test_project_1_plan_1_answer_for_text_field_question: text: "Answer to the text field question for plan 1" plan_id: test_project_1_plan_1 - user_id: cc_user + user_id: complete_user question_id: funder_template_2_phase_1_version_1_section_1_question_text_field test_project_1_plan_1_answer_for_radio_button_question: plan_id: test_project_1_plan_1 - user_id: cc_user + user_id: complete_user question_id: funder_template_2_phase_1_version_1_section_1_question_radio_button + options: [funder_template_2_phase_1_version_1_section_1_question_radio_button_option_1, funder_template_2_phase_1_version_1_section_1_question_radio_button_option_2] test_project_1_plan_1_answer_for_check_box_question: plan_id: test_project_1_plan_1 - user_id: cc_user + user_id: complete_user question_id: funder_template_2_phase_1_version_1_section_1_question_check_box + options: [funder_template_2_phase_1_version_1_section_1_question_check_box_option_2] test_project_1_plan_1_answer_for_dropdown_question: plan_id: test_project_1_plan_1 - user_id: cc_user + user_id: complete_user question_id: funder_template_2_phase_1_version_1_section_1_question_dropdown + options: [funder_template_2_phase_1_version_1_section_1_question_dropdown_option_2] test_project_1_plan_1_answer_for_multi_select_box_question: plan_id: test_project_1_plan_1 - user_id: cc_user + user_id: complete_user question_id: funder_template_2_phase_1_version_1_section_1_question_multi_select_box + options: [funder_template_2_phase_1_version_1_section_1_question_multi_select_box_option_1, funder_template_2_phase_1_version_1_section_1_question_multi_select_box_option_2] diff --git a/test/fixtures/comments.yml b/test/fixtures/comments.yml index 69b84d4..445169c 100644 --- a/test/fixtures/comments.yml +++ b/test/fixtures/comments.yml @@ -1,11 +1 @@ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html - -one: - user_id: 1 - question_id: 1 - text: MyText - -two: - user_id: 1 - question_id: 1 - text: MyText diff --git a/test/fixtures/exported_plans.yml b/test/fixtures/exported_plans.yml index 3e8aee6..313b970 100644 --- a/test/fixtures/exported_plans.yml +++ b/test/fixtures/exported_plans.yml @@ -2,13 +2,11 @@ # Settings::Dmptemplate. exported_test_plan_1: - id: 1 plan: test_project_1_plan_1 user: complete_user format: 'pdf' exported_test_plan_2: - id: 2 plan: test_project_2_plan_2 user: complete_user format: 'docx' \ No newline at end of file diff --git a/test/fixtures/projects.yml b/test/fixtures/projects.yml index 1246abd..bad5323 100644 --- a/test/fixtures/projects.yml +++ b/test/fixtures/projects.yml @@ -7,7 +7,7 @@ test_project_1: title: 'Test Project 1' - dmptemplate: funder_template_2 + dmptemplate: funder_template_3 organisation: complete grant_number: 'ABCD' identifier: '1234567890' @@ -18,6 +18,6 @@ test_project_2: title: 'Test Project 2' - dmptemplate: funder_template_2 + dmptemplate: funder_template_3 organisation: complete funder_name: 'Funder 2' \ No newline at end of file diff --git a/test/test_helper.rb b/test/test_helper.rb index 5f705f6..0adb64e 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -28,6 +28,62 @@ def class_name_to_attribute_name(name) name.gsub(/([a-z]+)([A-Z])/, '\1_\2').gsub('-', '_').downcase end + + # Generate a template for testing + # ---------------------------------------------------------------------- + def generate_complete_template + questions = [] + QuestionFormat.all.each do |f| + q = Question.new({ + text: "(#{f.title}) Question", + default_value: "Default for #{f.title} questions", + guidance: "Guidance for #{f.title} questions", + number: 1, + option_comment_display: true, + options: (['Radio Button', 'Check Box', 'Dropdown', 'Multi Select Box'].include?(f.title) ? + 2.times.collect{ |n| + Option.new(text: "Option #{n}", number: n, is_default: (n == 1 ? true : false)) + } : []) + }) + q.question_format = f + questions << q + end + + template = Dmptemplate.create({ + title: "Testing Template", + description: "This is a template for testing use only", + published: true, + organisation: Organisation.first, + locale: Language.first, + phases: [Phase.new({ + title: 'Phase 1', + number: 1 + })] + }) + +# template.phases << Phase.new({ +# title: 'Phase 1', +# number: 1 +# }) + + template.phases.first.versions << Version.new({ + title: 'Version 1', + number: 1, + published: true + }) + + template.phases.first.versions.first.sections << Section.new({ + title: 'Section 1', + number: 1, + organisation: Organisation.first + }) + + template.phases.first.versions.first.sections.first.questions << questions + + template.save! + template.reload + end + # FUNCTIONAL/INTEGRATION TEST HELPERS # ---------------------------------------------------------------------- diff --git a/test/unit/answer_test.rb b/test/unit/answer_test.rb index e2fad05..04cf87a 100644 --- a/test/unit/answer_test.rb +++ b/test/unit/answer_test.rb @@ -1,7 +1,68 @@ require 'test_helper' class AnswerTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end + + setup do + @user = User.last + + # generate a template and plan + template = generate_complete_template + + project = Project.new({ + title: 'Test Project', + organisation: @user.organisation + }) + project.dmptemplate = template + project.save! + + qs = template.phases.first.versions.first.sections.first.questions + @text_area_question = qs.select{ |q| q.question_format == QuestionFormat.find_by(title: 'Text Area' ) }.first + @text_field_question = qs.select{ |q| q.question_format == QuestionFormat.find_by(title: 'Text Field' ) }.first + @radio_button_question = qs.select{ |q| q.question_format == QuestionFormat.find_by(title: 'Radio Button' ) }.first + @check_box_question = qs.select{ |q| q.question_format == QuestionFormat.find_by(title: 'Check Box' ) }.first + @select_box_question = qs.select{ |q| q.question_format == QuestionFormat.find_by(title: 'Dropdown' ) }.first + @multi_select_box_question = qs.select{ |q| q.question_format == QuestionFormat.find_by(title: 'Multi Select Box' ) }.first + end + +=begin + # --------------------------------------------------- + test "required fields are required" do + assert_not Answer.new.valid? + assert_not Answer.new(user: @user).valid? + assert_not Answer.new(plan: @plan).valid? + assert_not Answer.new(question: @text_area_question).valid? + assert_not Answer.new(user: @user, plan: @plan).valid? + assert_not Answer.new(user: @user, question: @text_area_question).valid? + assert_not Answer.new(plan: @plan, question: @text_area_question).valid? + + # Ensure the bar minimum and complete versions are valid + assert Answer.new(user: @user, plan: @plan, question: @text_area_question).valid? + end + + # --------------------------------------------------- + test "cannot have multiple answers to the same question within a plan" do + Answer.create(user: @user, plan: @plan, question: @text_area_question, text: 'Tested ABC') + assert_not Answer.new(user: @user, plan: @plan, question: @text_area_question).valid? + +puts @plan.answers.inspect + + assert_not Answer.new(user: @user, plan: @plan, question: @text_area_question, text: 'ABCD').valid? + end + + # --------------------------------------------------- + test "can CRUD answers for text based questions" do + [@text_area_question, @text_field_question].each do |q| + answr = Answer.new(user: @user, plan: @plan, question: q, text: 'Tested ABC') + assert_not answr.id.nil?, "was expecting to be able to create a new Answer for a #{q.question_format.title} question: #{answr.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" + + answr.text = 'Testing an update' + answr.save! + answr.reload + assert_equal 'Testing an update', answr.text, "Was expecting to be able to update the text of the Answer for a #{q.question_format.title} question!" + + assert answr.destroy!, "Was unable to delete the Answer for a #{q.question_format.title} question!" + end + end +=end + end diff --git a/test/unit/language_test.rb b/test/unit/language_test.rb new file mode 100644 index 0000000..e91143f --- /dev/null +++ b/test/unit/language_test.rb @@ -0,0 +1,50 @@ +require 'test_helper' + +class LanguageTest < ActiveSupport::TestCase + + def setup + @lang = Language.first + @user = User.first + @organisation = @user.organisation + end + + # --------------------------------------------------- + test "required fields are required" do + assert_not Language.new.valid? + assert_not Language.new(name: 'Klingon').valid? + assert_not Language.new(name: 'Klingon', description: 'Klingon', default_language: true).valid? + + assert Language.new(abbreviation: 'klgn').valid? + assert Language.new(abbreviation: 'klgn', name: 'Klingon', description: 'Klingon', default_language: true).valid? + end + + # --------------------------------------------------- + test "abbreviation must be unique" do + assert_not Language.new(abbreviation: Language.first.abbreviation).valid? + end + + # --------------------------------------------------- + test "can CRUD" do + lang = Language.create(abbreviation: 'kg', name: 'Klingon') + assert_not lang.id.nil?, "was expecting to be able to create a new Language : #{lang.errors.collect{ |e| e }.join(', ')}" + + lang.name = 'Imperial Klingon' + lang.save! + assert_equal 'Imperial Klingon', lang.reload.name, "was expecting the name to have been updated!" + + assert lang.destroy!, "Was unable to delete the Language!" + end + + # --------------------------------------------------- + test "can manage has_many relationship with Users" do + user = User.new(email: 'me@example.edu', password: 'password') + verify_has_many_relationship(@lang, user, @lang.users.count) + end + + # --------------------------------------------------- + test "can manage has_many relationship with Organisations" do + org = Organisation.create(name: 'testing') + verify_has_many_relationship(@lang, org, @lang.organisations.count) + end + +end \ No newline at end of file diff --git a/test/unit/question_format_test.rb b/test/unit/question_format_test.rb new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/test/unit/question_format_test.rb