diff --git a/app/models/phase.rb b/app/models/phase.rb index 6a3b3ef..2f3c247 100644 --- a/app/models/phase.rb +++ b/app/models/phase.rb @@ -59,6 +59,7 @@ end end +# TODO: reevaluate this method. It seems like the 1st query is unecessary ## # verify if a phase has a published version or a version with one or more sections # diff --git a/test/unit/option_test.rb b/test/unit/option_test.rb new file mode 100644 index 0000000..640b42e --- /dev/null +++ b/test/unit/option_test.rb @@ -0,0 +1,64 @@ +require 'test_helper' + +class OptionTest < ActiveSupport::TestCase + include GlobalHelpers + + setup do + @user = User.first + + tmplt = generate_complete_template + @plan = Plan.create(project: Project.first, version: tmplt.phases.first.versions.first) + + @question = @plan.version.sections.first.questions.first + + @option = Option.create(question: @question, text: 'Test Option', number: 1) + end + + # --------------------------------------------------- + test "required fields are required" do + assert_not Option.new.valid? + assert_not Option.new(question: @question, text: 'Test').valid?, "expected the 'number' field to be required" + assert_not Option.new(question: @question, number: 1).valid?, "expected the 'text' and 'number' field to be required" + assert_not Option.new(text: 'Test', number: 1).valid?, "expected the 'question' and 'number' field to be required" + + # Ensure the bare minimum and complete versions are valid + a = Option.new(question: @question, text: 'Test', number: 1) + assert a.valid?, "expected the 'text', 'question' and 'number fields to be enough to create an Option! - #{a.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" + end + + # --------------------------------------------------- + test "to_s returns the text of the option warning" do + assert_equal @option.text, @option.to_s, "expected the to_s method to return the text field" + end + + # --------------------------------------------------- + test "can CRUD Guidance" do + obj = Option.create(question: @question, text: 'Test', number: 1) + assert_not obj.id.nil?, "was expecting to be able to create a new Option! #{obj.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" + + obj.text = 'Testing an update' + obj.save! + obj.reload + assert_equal 'Testing an update', obj.text, "Was expecting to be able to update the text of the Option!" + + assert obj.destroy!, "Was unable to delete the Option!" + end + + # --------------------------------------------------- + test "can manage belongs_to relationship with Question" do + question = Question.new(text: 'Testing 123', section: Section.first, question_format: QuestionFormat.first) + verify_belongs_to_relationship(@option, question) + end + + # --------------------------------------------------- + test "can manage has_many relationship with OptionWarnings" do + ow = OptionWarning.new(text: 'Test', organisation: @user.organisation, option: @option) + verify_has_many_relationship(@option, ow, @option.option_warnings.count) + end + + # --------------------------------------------------- + test "can manage has_many relationship with Answers" do + answer = Answer.new(user: @user, plan: @plan, question: @question, text: 'Testing new answer') + verify_has_many_relationship(@option, answer, @option.answers.count) + end +end \ No newline at end of file diff --git a/test/unit/option_warning_test.rb b/test/unit/option_warning_test.rb new file mode 100644 index 0000000..e682774 --- /dev/null +++ b/test/unit/option_warning_test.rb @@ -0,0 +1,49 @@ +require 'test_helper' + +class OptionWarningTest < ActiveSupport::TestCase + include GlobalHelpers + + setup do + @option = Option.first + @organisation = Organisation.first + + @option_warning = OptionWarning.create(organisation: @organisation, option: @option, text: 'Testing') + end + + # --------------------------------------------------- + test "required fields are required" do + assert_not OptionWarning.new.valid? + assert_not OptionWarning.new(option: @option, organisation: @organisation).valid?, "expected the 'text' field to be required" + + # Ensure the bare minimum and complete versions are valid + assert OptionWarning.new(text: 'Test', organisation: @organisation, option: @option).valid?, "expected the 'text', 'option' and 'organisation' fields to be enough to create an OptionWarning!" + end + + # --------------------------------------------------- + test "to_s returns the text of the option warning" do + assert_equal @option_warning.text, @option_warning.to_s, "expected the to_s method to return the text field" + end + + # --------------------------------------------------- + test "can CRUD Guidance" do + obj = OptionWarning.create(organisation: @organisation, option: @option, text: 'Testing option warnings') + assert_not obj.id.nil?, "was expecting to be able to create a new OptionWarning!" + + obj.text = 'Testing an update' + obj.save! + obj.reload + assert_equal 'Testing an update', obj.text, "Was expecting to be able to update the text of the OptionWarning!" + + assert obj.destroy!, "Was unable to delete the OptionWarning!" + end + + # --------------------------------------------------- + test "can manage belongs_to relationship with Organisation" do + verify_belongs_to_relationship(@option_warning, @organisation) + end + + # --------------------------------------------------- + test "can manage belongs_to relationship with Option" do + verify_belongs_to_relationship(@option_warning, @option) + end +end \ No newline at end of file diff --git a/test/unit/organisation_type_test.rb b/test/unit/organisation_type_test.rb new file mode 100644 index 0000000..de82306 --- /dev/null +++ b/test/unit/organisation_type_test.rb @@ -0,0 +1,37 @@ +require 'test_helper' + +class OrganisationTypeTest < ActiveSupport::TestCase + + setup do + @organisation_type = OrganisationType.first + end + + # --------------------------------------------------- + test "required fields are required" do + assert_not OrganisationType.new.valid? + + # Ensure the bar minimum and complete versions are valid + a = OrganisationType.new(name: 'Tester') + assert a.valid?, "expected the 'name' field to be enough to create an OrganisationType! - #{a.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" + end + + # --------------------------------------------------- + test "can CRUD OrganisationType" do + obj = OrganisationType.create(name: 'Testing CRUD') + assert_not obj.id.nil?, "was expecting to be able to create a new OrganisationType! - #{obj.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" + + obj.name = 'Testing an update' + obj.save! + obj.reload + assert_equal 'Testing an update', obj.name, "Was expecting to be able to update the name of the OrganisationType!" + + assert obj.destroy!, "Was unable to delete the OrganisationType!" + end + + # --------------------------------------------------- + test "can manage has_many relationship with Organisation" do + org = Organisation.new(name: 'Test Organisation') + verify_has_many_relationship(@organisation_type, org, @organisation_type.organisations.count) + end + +end diff --git a/test/unit/phase_test.rb b/test/unit/phase_test.rb new file mode 100644 index 0000000..c55189c --- /dev/null +++ b/test/unit/phase_test.rb @@ -0,0 +1,92 @@ +require 'test_helper' + +class PhaseTest < ActiveSupport::TestCase + + setup do + @organisation = Organisation.first + @template = Dmptemplate.first + @phase = Phase.create(title: 'Test Phase 1', number: 1, dmptemplate: @template) + end + + # --------------------------------------------------- + test "required fields are required" do + assert_not Phase.new.valid? + assert_not Phase.new(title: 'Testing', number: 1).valid?, "expected the dmptemplate field to be required" + assert_not Phase.new(number: 2, dmptemplate: @template).valid?, "expected the title field to be required" + assert_not Phase.new(title: 'Testing', dmptemplate: @template).valid?, "expected the number field to be required" + + # Ensure the bar minimum and complete versions are valid + a = Phase.new(title: 'Testing', dmptemplate: @template, number: 2) + assert a.valid?, "expected the 'title', 'number' and 'dmptemplate' fields to be enough to create an Phase! - #{a.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" + end + + # --------------------------------------------------- + test "a slug is properly generated when creating a record" do + a = Phase.create(title: 'Testing 123', dmptemplate: @template, number: 2) + assert_equal "testing-123", a.slug + end + + # --------------------------------------------------- + test "to_s returns the title" do + assert_equal @phase.title, @phase.to_s + end + + # --------------------------------------------------- + test "latest_version returns the correct version" do + assert_equal @phase.versions.first, @phase.latest_version, "expected the version if there is only one version" + + [2..4].each do |i| + @phase.versions << Version.new(title: "Version #{i}", number: i) + end + + @phase.save! + assert_equal 4, @phase.latest_version.number, "expected the last version if there there were multiple versions" + end + + # --------------------------------------------------- + test "latest_published_version returns the correct version" do + assert_equal nil, @phase.latest_published_version, "expected nil if there is only one version and it was not specifically designated as published" + + @phase.published = true + @phase.save! + assert_equal @phase.versions.first, @phase.latest_published_version, "expected the version if there is only one published version" + + [2..4].each do |i| + @phase.versions << Version.new(title: "Version #{i}", number: i, + published: (i == 3 ? true : false)) + end + + @phase.save! + assert_equal 3, @phase.latest_published_version.number, "expected the last published version if there there were multiple published versions" + end + + # --------------------------------------------------- + test "has_sections returns false if there are NO published versions with sections" do + # TODO: build out this test if the has_sections method is actually necessary + end + + # --------------------------------------------------- + test "can CRUD Phase" do + obj = Phase.create(title: 'Testing CRUD', dmptemplate: @template, number: 4) + assert_not obj.id.nil?, "was expecting to be able to create a new Phase! - #{obj.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" + + obj.title = 'Testing an update' + obj.save! + obj.reload + assert_equal 'Testing an update', obj.title, "Was expecting to be able to update the title of the Phase!" + + assert obj.destroy!, "Was unable to delete the Phase!" + end + + # --------------------------------------------------- + test "can manage has_many relationship with Versions" do + v = Version.new(title: 'Test Version', number: 2) + verify_has_many_relationship(@phase, v, @phase.versions.count) + end + + # --------------------------------------------------- + test "can manage belongs_to relationship with Dmptemplate" do + tmplt = Dmptemplate.create(organisation: @organisation, title: 'Testing relationship') + verify_belongs_to_relationship(@phase, tmplt) + end +end