diff --git a/app/models/plan.rb b/app/models/plan.rb index 41b27d2..00d52fb 100644 --- a/app/models/plan.rb +++ b/app/models/plan.rb @@ -10,7 +10,8 @@ has_many :exported_plans has_many :roles - has_many :users, through: :roles +# COMMENTED OUT THE DIRECT CONNECTION HERE TO Users to prevent assignment of users without an access_level specified (currently defaults to creator) +# has_many :users, through: :roles ## @@ -26,7 +27,7 @@ # public is a Ruby keyword so using publicly enum visibility: [:organisationally_visible, :publicly_visible, :is_test, :privately_visible] - validates :template, :title, :users, presence: true + validates :template, :title, presence: true ## # Constants diff --git a/app/models/suggested_answer.rb b/app/models/suggested_answer.rb index 5b94c8d..f3aaaf8 100644 --- a/app/models/suggested_answer.rb +++ b/app/models/suggested_answer.rb @@ -12,6 +12,8 @@ :org, :question, :as => [:default, :admin] + validates :question, :org, :text, presence: true + # EVALUATE CLASS AND INSTANCE METHODS BELOW # # What do they do? do they do it efficiently, and do we need them? diff --git a/app/models/template.rb b/app/models/template.rb index 63b48de..ad51a65 100644 --- a/app/models/template.rb +++ b/app/models/template.rb @@ -21,7 +21,7 @@ s.key :export, defaults: Settings::Template::DEFAULT_SETTINGS end - + validates :org, :title, :version, presence: true # EVALUATE CLASS AND INSTANCE METHODS BELOW # diff --git a/test/test_helper.rb b/test/test_helper.rb index 9b8050d..c5698d7 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -79,7 +79,7 @@ principal_investigator: 'me', principal_investigator_identifier: 'me-1234', description: "this is my plan's informative description", identifier: '1234567890', data_contact: 'me@example.com', visibility: 0, - users: [User.last]) + roles: [Role.new(user: User.last, creator: true)]) assert @plan.valid?, "unable to create new Plan: #{@plan.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" @plan.save! diff --git a/test/unit/plan_test.rb b/test/unit/plan_test.rb index 1275ab6..fe3d2ce 100644 --- a/test/unit/plan_test.rb +++ b/test/unit/plan_test.rb @@ -8,20 +8,20 @@ @plan = Plan.create(title: 'Test Plan', template: @template, grant_number: 'Plan12345', identifier: '000912', description: 'This is a test plan', principal_investigator: 'John Doe', principal_investigator_identifier: 'ABC', - data_contact: 'john.doe@example.com', visibility: 1, users: [User.last]) + data_contact: 'john.doe@example.com', visibility: 1, + roles: [Role.new(user: User.last, creator: true)]) end # --------------------------------------------------- test "required fields are required" do assert_not Plan.new.valid? - assert_not Plan.new(users: [User.last], title: 'Testing').valid?, "expected the template field to be required" - assert_not Plan.new(template: @template, title: 'Testing').valid?, "expected at least one user to be required" + assert_not Plan.new(title: 'Testing').valid?, "expected the template field to be required" # Make sure that the Settings gem is defaulting the title for us - assert Plan.new(users: [User.last], template: @template).valid?, "expected the title field to have been set by default by the Settings gem" + assert Plan.new(template: @template).valid?, "expected the title field to have been set by default by the Settings gem" # Ensure the bare minimum and complete versions are valid - a = Plan.new(title: 'Testing', template: @template, users: [User.last]) + a = Plan.new(title: 'Testing', template: @template) assert a.valid?, "expected the 'title', 'template' and at least one 'user' fields to be enough to create an Plan! - #{a.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" end @@ -39,7 +39,7 @@ # --------------------------------------------------- test "can CRUD Plan" do obj = Plan.create(title: 'Testing CRUD', template: Template.where.not(id: @template.id).first, - users: [User.last], description: "should change") + roles: [Role.new(user: User.last, creator: true)], description: "should change") assert_not obj.id.nil?, "was expecting to be able to create a new Plan! - #{obj.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" obj.description = 'changed' @@ -57,8 +57,9 @@ end # --------------------------------------------------- - test "can manage has_many relationship with Users" do - verify_has_many_relationship(@plan, User.first, @plan.users.count) + test "can manage has_many relationship with Role" do + role = Role.new(user: User.first, editor: true) + verify_has_many_relationship(@plan, role, @plan.roles.count) end # --------------------------------------------------- diff --git a/test/unit/suggested_answer_test.rb b/test/unit/suggested_answer_test.rb new file mode 100644 index 0000000..7cae6ce --- /dev/null +++ b/test/unit/suggested_answer_test.rb @@ -0,0 +1,56 @@ +require 'test_helper' + +class SuggestedAnswerTest < ActiveSupport::TestCase + + setup do + scaffold_template + + @org = Org.last + @question = @template.phases.first.sections.first.questions.first + + @suggested_answer = SuggestedAnswer.create(org: @org, question: @question, text: 'Test', + is_example: true) + end + + # --------------------------------------------------- + test "required fields are required" do + assert_not SuggestedAnswer.new.valid? + assert_not SuggestedAnswer.new(org: @org, text: 'Tester').valid?, "expected the 'question' field to be required" + assert_not SuggestedAnswer.new(question: @question, text: 'Tester').valid?, "expected the 'org' field to be required" + assert_not SuggestedAnswer.new(org: @org, question: @question).valid?, "expected the 'text' field to be required" + + # Ensure the bare minimum and complete versions are valid + a = SuggestedAnswer.new(org: @org, question: @question, text: 'Tester') + assert a.valid?, "expected the 'org', 'question' and 'text' fields to be enough to create an SuggestedAnswer! - #{a.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" + end + + # --------------------------------------------------- + test "to_s returns the text" do + assert_equal @suggested_answer.text, @suggested_answer.to_s + end + + # --------------------------------------------------- + test "can CRUD SuggestedAnswer" do + obj = SuggestedAnswer.create(org: @org, question: @question, text: 'Tester') + assert_not obj.id.nil?, "was expecting to be able to create a new SuggestedAnswer: #{obj.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" + + obj.text = 'my tester' + obj.save! + obj.reload + assert_equal 'my tester', obj.text, "Was expecting to be able to update the text of the SuggestedAnswer!" + + assert obj.destroy!, "Was unable to delete the SuggestedAnswer!" + end + + # --------------------------------------------------- + test "can manage belongs_to relationship with Org" do + suggested_answer = SuggestedAnswer.new(question: @question, text: 'Testing') + verify_belongs_to_relationship(suggested_answer, @org) + end + + # --------------------------------------------------- + test "can manage belongs_to relationship with Question" do + suggested_answer = SuggestedAnswer.new(org: @org, text: 'Testing') + verify_belongs_to_relationship(suggested_answer, @question) + end +end \ No newline at end of file diff --git a/test/unit/template_test.rb b/test/unit/template_test.rb index 0db44c4..fff10dc 100644 --- a/test/unit/template_test.rb +++ b/test/unit/template_test.rb @@ -3,33 +3,35 @@ class TemplateTest < ActiveSupport::TestCase setup do - @template = Dmptemplate.first - - @organisation = Organisation.first + @org = Org.last + + scaffold_template end - def settings(extras = {}) - {margin: (@margin || { top: 10, bottom: 10, left: 10, right: 10 }), - font_face: (@font_face || Settings::Dmptemplate::VALID_FONT_FACES.first), - font_size: (@font_size || 11) - }.merge(extras) - end +# def settings(extras = {}) +# {margin: (@margin || { top: 10, bottom: 10, left: 10, right: 10 }), +# font_face: (@font_face || Settings::Template::VALID_FONT_FACES.first), +# font_size: (@font_size || 11) +# }.merge(extras) +# end - def default_formatting - Settings::Dmptemplate::DEFAULT_SETTINGS[:formatting] - end +# def default_formatting +# Settings::Template::DEFAULT_SETTINGS[:formatting] +# end # --------------------------------------------------- test "required fields are required" do - assert_not Dmptemplate.new.valid? - assert_not Dmptemplate.new(title: 'Testing tmeplate').valid?, "expected the 'organisation' field to be required" - assert_not Dmptemplate.new(organisation: @organisation).valid?, "expected the 'title' field to be required" + assert_not Template.new.valid? + assert_not Template.new(org: @org, title: 'Tester').valid?, "expected the 'version' field to be required" + assert_not Template.new(version: 1, title: 'Tester').valid?, "expected the 'org' field to be required" + assert_not Template.new(org: @org, version: 1).valid?, "expected the 'title' field to be required" - # Ensure the bar minimum and complete versions are valid - a = Dmptemplate.new(organisation: @organisation, title: 'Testing tmeplate') - assert a.valid?, "expected the 'title' and 'organisation' fields to be enough to create an Dmptemplate! - #{a.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" + # Ensure the bare minimum and complete versions are valid + a = Template.new(org: @org, version: 1, title: 'Tester') + assert a.valid?, "expected the 'org', 'version' and 'title' fields to be enough to create an Template! - #{a.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" end - + +=begin # --------------------------------------------------- test "to_s method returns the title" do assert_equal @template.title, @template.to_s @@ -230,67 +232,6 @@ assert_equal(default_formatting, @template.settings(:export).formatting) end -<<<<<<< HEAD -======= - # ---------- templates_org_type ---------- - test "templates_org_type returns all published" do - OrganisationType.find_each do |org_type| - result_templates = Dmptemplate.templates_org_type(org_type.name) - my_list = Array.new - org_type.organisations.each do |org| - my_list += org.dmptemplates - end - my_list.each do |template| - if template.published - assert_includes(result_templates, template, "Template: #{template.title}} of type #{org_type.name}, not returned by templates_org_type") - end - end - end - end - - # ---------- funders_templates ---------- - test "funders_templates returns all funder organisation templates" do - result_templates = Dmptemplate.funders_templates - funder_templates = OrganisationType.first.organisations do |org| - org.dmptemplates.each do |template| - assert_includes( result_templates, template, "Funder Template: #{template.title} not included in result of funders_templates") - end - end - end - - # ---------- own_institutional_templates ---------- - test "own_institutional_templates returns all templates belonging to given org_id" do - Org.find_each do |org| - result_templates = Dmptemplate.own_institutional_templates(org.id) - org.dmptemplates.each do |template| - assert_includes(result_templates, template, "Template: #{template.title} not returned by own_institutional_templates") - end - end - end - - # ---------- funders_and_own_templates ---------- - test "funders_and_own_templates returns all funder and own given org_id templates" do - Org.find_each do |org| - result_templates = Dmptemplate.funders_and_own_templates(org.id) - org.dmptemplates.each do |template| - assert_includes(result_templates, template, "Template #{template.title} not returned by funders and own templates") - end - end - funder_templates = OrganisationType.first.organisations do |org| - org.dmptemplates.each do |template| - assert_includes( result_templates, template, "Funder Template: #{template.title} not included in result of funders_and_own_templates") - end - end - end - - # ---------- org_type ---------- - test "org_type properly returns the name of the template's organisation's type" do - Dmptemplate.find_each do |template| - assert_equal( template.org_type, template.organisation.organisation_type.name, "Template: #{template.title} returned #{template.org_type}, instead of #{template.organisation.organisation_type.name}") - end - end - ->>>>>>> final_schema # ---------- has_customisations? ---------- test "has_customisations? correctly identifies if a given org has customised the template" do # TODO: Impliment after understanding has_customisations @@ -299,7 +240,7 @@ # ---------- has_published_versions? ---------- test "has_published_versions? correctly identifies published versions" do - Dmptemplate.find_each do |template| + Template.find_each do |template| template.phases.each do |phase| unless phase.latest_published_version.nil? assert(template.has_published_versions? , "there was a published version of phase: #{phase.title}") @@ -307,18 +248,19 @@ end end end - +=end + # --------------------------------------------------- test "can CRUD Template" do - tmplt = Dmptemplate.create(organisation: @organisation, title: 'Testing tmeplate') - assert_not tmplt.id.nil?, "was expecting to be able to create a new Dmptemplate!" + tmplt = Template.create(org: @org, version: 1, title: 'Tester') + assert_not tmplt.id.nil?, "was expecting to be able to create a new Template!" tmplt.description = 'Testing an update' tmplt.save! tmplt.reload - assert_equal 'Testing an update', tmplt.description, "Was expecting to be able to update the description of the Dmptemplate!" + assert_equal 'Testing an update', tmplt.description, "Was expecting to be able to update the description of the Template!" - assert tmplt.destroy!, "Was unable to delete the Dmptemplate!" + assert tmplt.destroy!, "Was unable to delete the Template!" end # --------------------------------------------------- @@ -328,20 +270,15 @@ end # --------------------------------------------------- - test "can manage has_many relationship with Project" do - project = Project.new(title: 'Test Project', organisation: @organisation) - verify_has_many_relationship(@template, project, @template.projects.count) + test "can manage has_many relationship with Plan" do + plan = Plan.new(title: 'Test Plan') + verify_has_many_relationship(@template, plan, @template.plans.count) end # --------------------------------------------------- - test "can manage has_many relationship with GuidanceGroup" do - grp = GuidanceGroup.new(name: 'Test Group', organisation: @organisation) - verify_has_many_relationship(@template, grp, @template.guidance_groups.count) - end - - # --------------------------------------------------- - test "can manage belongs_to relationship with Organisation" do - verify_belongs_to_relationship(@template, @organisation) + test "can manage belongs_to relationship with Org" do + tmplt = Template.new(title: 'My test', version: 1) + verify_belongs_to_relationship(tmplt, @org) end end