diff --git a/app/models/question.rb b/app/models/question.rb index ab3ced5..5493b79 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -44,64 +44,25 @@ "#{text}" end - def select_text - cleantext = text.gsub(/<[^<]+>/, '') - if cleantext.length > 120 - cleantext = cleantext.slice(0,120) - end - cleantext - end +# TODO: Commented this amoeba cloning gem definition out to see if its even used. The +# amoeba documentations uses [object].amoeba_dup to clone the object, but that +# command does not exist in the codebase - amoeba do - include_association :options - include_association :suggested_answers - clone [:themes] - end - - #def question_type? - # type_label = {} - # if self.is_text_field? - # type_label = 'Text field' - # elsif self.multiple_choice? - # type_label = 'Multiple choice' - # else - # type_label = 'Text area' - # end - # return type_label - #end +# amoeba do +# include_association :options +# include_association :suggested_answers +# clone [:themes] +# end ## - # for each question theme, appends them separated by comas - # shouldnt have a ? after the method name + # guidance for org # - # @return [Hash{String=> String}] - def question_themes? - themes_label = {} - i = 1 - themes_quest = self.themes - - themes_quest.each do |tt| - themes_label = tt.title - - if themes_quest.count > i then - themes_label += ',' - i +=1 - end - end - - return themes_label - end - - ## - # guidance for question in the org admin - # - # @param question [Question] the question to find guidance for - # @param org_admin [Organisation] the organisation to find guidance for + # @param org [Org] the org to find guidance for # @return [Hash{String => String}] - def guidance_for_question(question, org) + def guidance_for_org(org) # pulls together guidance from various sources for question guidances = {} - theme_ids = question.theme_ids + theme_ids = themes.collect{|t| t.id} if theme_ids.present? GuidanceGroup.where(org_id: org.id).each do |group| group.guidances.each do |g| diff --git a/app/models/region.rb b/app/models/region.rb index 8147fed..2c57e79 100644 --- a/app/models/region.rb +++ b/app/models/region.rb @@ -1,3 +1,8 @@ class Region < ActiveRecord::Base - + has_many :sub_regions, class_name: 'Region', foreign_key: 'super_region_id' + + belongs_to :super_region, class_name: 'Region' + + validates :name, presence: true, uniqueness: true + validates :abbreviation, uniqueness: true, allow_nil: true end \ No newline at end of file diff --git a/app/models/region_group.rb b/app/models/region_group.rb deleted file mode 100644 index 8f83a83..0000000 --- a/app/models/region_group.rb +++ /dev/null @@ -1,3 +0,0 @@ -class RegionGroup < ActiveRecord::Base - -end \ No newline at end of file diff --git a/app/models/section.rb b/app/models/section.rb index ee98e8f..60fd094 100644 --- a/app/models/section.rb +++ b/app/models/section.rb @@ -22,8 +22,11 @@ "#{title}" end - amoeba do - include_association :questions - end +# TODO: Commented this amoeba cloning gem definition out to see if its even used. The +# amoeba documentations uses [object].amoeba_dup to clone the object, but that +# command does not exist in the codebase +# amoeba do +# include_association :questions +# end end diff --git a/app/views/templates/_guidance_display.html.erb b/app/views/templates/_guidance_display.html.erb index d43793a..9a0755a 100644 --- a/app/views/templates/_guidance_display.html.erb +++ b/app/views/templates/_guidance_display.html.erb @@ -17,7 +17,7 @@ <% end %> - <% question.guidance_for_question(question, current_user.org).each_pair do |title,guidance| %> + <% question.guidance_for_org(current_user.org).each_pair do |title,guidance| %>
diff --git a/db/migrate/20170201194502_remove_region_group_and_add_super_region_id_to_regions.rb b/db/migrate/20170201194502_remove_region_group_and_add_super_region_id_to_regions.rb new file mode 100644 index 0000000..07a1e1d --- /dev/null +++ b/db/migrate/20170201194502_remove_region_group_and_add_super_region_id_to_regions.rb @@ -0,0 +1,7 @@ +class RemoveRegionGroupAndAddSuperRegionIdToRegions < ActiveRecord::Migration + def change + drop_table :region_groups if table_exists?(:region_groups) + + add_column :regions, :super_region_id, :integer unless column_exists?(:regions, :super_region_id) + end +end diff --git a/db/schema.rb b/db/schema.rb index 87b1514..90ab057 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,60 +11,61 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170130173612) do - - # These are extensions that must be enabled in order to support this database - enable_extension "plpgsql" +ActiveRecord::Schema.define(version: 20170201194502) do create_table "answers", force: :cascade do |t| - t.text "text" - t.integer "plan_id" - t.integer "user_id" - t.integer "question_id" + t.text "text", limit: 65535 + t.integer "plan_id", limit: 4 + t.integer "user_id", limit: 4 + t.integer "question_id", limit: 4 t.datetime "created_at" t.datetime "updated_at" end + add_index "answers", ["plan_id"], name: "fk_rails_84a6005a3e", using: :btree + add_index "answers", ["question_id"], name: "fk_rails_3d5ed4418f", using: :btree + add_index "answers", ["user_id"], name: "fk_rails_584be190c2", using: :btree + create_table "answers_question_options", id: false, force: :cascade do |t| - t.integer "answer_id", null: false - t.integer "question_option_id", null: false + t.integer "answer_id", limit: 4, null: false + t.integer "question_option_id", limit: 4, null: false end add_index "answers_question_options", ["answer_id", "question_option_id"], name: "answer_question_option_index", using: :btree add_index "answers_question_options", ["question_option_id", "answer_id"], name: "question_option_answer_index", using: :btree create_table "exported_plans", force: :cascade do |t| - t.integer "plan_id" - t.integer "user_id" - t.string "format" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.integer "plan_id", limit: 4 + t.integer "user_id", limit: 4 + t.string "format", limit: 255 + t.datetime "created_at" + t.datetime "updated_at" end create_table "file_types", force: :cascade do |t| - t.string "name" - t.string "icon_name" - t.integer "icon_size" - t.string "icon_location" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.string "name", limit: 255 + t.string "icon_name", limit: 255 + t.integer "icon_size", limit: 4 + t.string "icon_location", limit: 255 + t.datetime "created_at" + t.datetime "updated_at" end create_table "file_uploads", force: :cascade do |t| - t.string "name" - t.string "title" - t.text "description" - t.integer "size" + t.string "name", limit: 255 + t.string "title", limit: 255 + t.text "description", limit: 65535 + t.integer "size", limit: 4 t.boolean "published" - t.string "location" - t.integer "file_type_id" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.string "location", limit: 255 + t.integer "file_type_id", limit: 4 + t.datetime "created_at" + t.datetime "updated_at" end create_table "friendly_id_slugs", force: :cascade do |t| - t.string "slug", null: false - t.integer "sluggable_id", null: false + t.string "slug", limit: 255, null: false + t.integer "sluggable_id", limit: 4, null: false t.string "sluggable_type", limit: 40 t.datetime "created_at" end @@ -74,293 +75,329 @@ add_index "friendly_id_slugs", ["sluggable_type"], name: "index_friendly_id_slugs_on_sluggable_type", using: :btree create_table "guidance_groups", force: :cascade do |t| - t.string "name" - t.integer "org_id" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.string "name", limit: 255 + t.integer "org_id", limit: 4 + t.datetime "created_at" + t.datetime "updated_at" t.boolean "optional_subset" t.boolean "published" end + add_index "guidance_groups", ["org_id"], name: "fk_rails_819c1dbbc7", using: :btree + create_table "guidances", force: :cascade do |t| - t.text "text" - t.integer "guidance_group_id" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.integer "question_id" + t.text "text", limit: 65535 + t.integer "guidance_group_id", limit: 4 + t.datetime "created_at" + t.datetime "updated_at" + t.integer "question_id", limit: 4 t.boolean "published" end + add_index "guidances", ["guidance_group_id"], name: "fk_rails_20d29da787", using: :btree + create_table "identifier_schemes", force: :cascade do |t| - t.string "name" - t.string "description" + t.string "name", limit: 255 + t.string "description", limit: 255 t.boolean "active" t.datetime "created_at" t.datetime "updated_at" end create_table "languages", force: :cascade do |t| - t.string "abbreviation" - t.string "description" - t.string "name" + t.string "abbreviation", limit: 255 + t.string "description", limit: 255 + t.string "name", limit: 255 t.boolean "default_language" end create_table "notes", force: :cascade do |t| - t.integer "user_id" - t.text "text" + t.integer "user_id", limit: 4 + t.text "text", limit: 65535 t.boolean "archived" - t.integer "answer_id" - t.integer "archived_by" + t.integer "answer_id", limit: 4 + t.integer "archived_by", limit: 4 t.datetime "created_at" t.datetime "updated_at" end + add_index "notes", ["answer_id"], name: "fk_rails_907f8d48bf", using: :btree + add_index "notes", ["user_id"], name: "fk_rails_7f2323ad43", using: :btree + create_table "org_token_permissions", force: :cascade do |t| - t.integer "org_id" - t.integer "token_permission_type_id" + t.integer "org_id", limit: 4 + t.integer "token_permission_type_id", limit: 4 t.datetime "created_at" t.datetime "updated_at" end + add_index "org_token_permissions", ["org_id"], name: "fk_rails_e1db1b22c5", using: :btree + add_index "org_token_permissions", ["token_permission_type_id"], name: "fk_rails_2aa265f538", using: :btree + create_table "orgs", force: :cascade do |t| - t.string "name" - t.string "abbreviation" - t.string "target_url" - t.string "wayfless_entity" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.integer "parent_id" + t.string "name", limit: 255 + t.string "abbreviation", limit: 255 + t.string "target_url", limit: 255 + t.string "wayfless_entity", limit: 255 + t.datetime "created_at" + t.datetime "updated_at" + t.integer "parent_id", limit: 4 t.boolean "is_other" - t.string "sort_name" - t.text "banner_text" - t.string "logo_file_name" - t.integer "region_id" - t.integer "language_id" - t.string "logo_uid" - t.string "logo_name" - t.string "contact_email" - t.integer "org_type", default: 0, null: false + t.string "sort_name", limit: 255 + t.text "banner_text", limit: 65535 + t.string "logo_file_name", limit: 255 + t.integer "region_id", limit: 4 + t.integer "language_id", limit: 4 + t.string "logo_uid", limit: 255 + t.string "logo_name", limit: 255 + t.string "contact_email", limit: 255 + t.integer "org_type", limit: 4, default: 0, null: false end + add_index "orgs", ["language_id"], name: "fk_rails_5640112cab", using: :btree + add_index "orgs", ["region_id"], name: "fk_rails_5a6adf6bab", using: :btree + create_table "perms", force: :cascade do |t| - t.string "name" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.string "name", limit: 255 + t.datetime "created_at" + t.datetime "updated_at" end add_index "perms", ["name"], name: "index_perms_on_name", using: :btree add_index "perms", ["name"], name: "index_roles_on_name_and_resource_type_and_resource_id", using: :btree create_table "phases", force: :cascade do |t| - t.string "title" - t.text "description" - t.integer "number" - t.integer "template_id" + t.string "title", limit: 255 + t.text "description", limit: 65535 + t.integer "number", limit: 4 + t.integer "template_id", limit: 4 t.datetime "created_at" t.datetime "updated_at" - t.string "slug" + t.string "slug", limit: 255 t.boolean "modifiable" end + add_index "phases", ["template_id"], name: "fk_rails_0f8036cb2e", using: :btree + create_table "plans", force: :cascade do |t| - t.integer "project_id" - t.string "title" - t.integer "template_id" + t.integer "project_id", limit: 4 + t.string "title", limit: 255 + t.integer "template_id", limit: 4 t.datetime "created_at" t.datetime "updated_at" - t.string "slug" - t.string "grant_number" - t.string "identifier" - t.text "description" - t.string "principal_investigator" - t.string "principal_investigator_identifier" - t.string "data_contact" - t.string "funder_name" - t.integer "visibility", default: 0, null: false + t.string "slug", limit: 255 + t.string "grant_number", limit: 255 + t.string "identifier", limit: 255 + t.text "description", limit: 65535 + t.string "principal_investigator", limit: 255 + t.string "principal_investigator_identifier", limit: 255 + t.string "data_contact", limit: 255 + t.string "funder_name", limit: 255 + t.integer "visibility", limit: 4, default: 0, null: false end + add_index "plans", ["template_id"], name: "fk_rails_3424ca281f", using: :btree + create_table "question_formats", force: :cascade do |t| - t.string "title" - t.text "description" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.boolean "option_based", default: false + t.string "title", limit: 255 + t.text "description", limit: 65535 + t.datetime "created_at" + t.datetime "updated_at" + t.boolean "option_based", default: false end create_table "question_options", force: :cascade do |t| - t.integer "question_id" - t.string "text" - t.integer "number" + t.integer "question_id", limit: 4 + t.string "text", limit: 255 + t.integer "number", limit: 4 t.boolean "is_default" t.datetime "created_at" t.datetime "updated_at" end + add_index "question_options", ["question_id"], name: "fk_rails_b9c5f61cf9", using: :btree + create_table "questions", force: :cascade do |t| - t.text "text" - t.text "default_value" - t.text "guidance" - t.integer "number" - t.integer "section_id" + t.text "text", limit: 65535 + t.text "default_value", limit: 65535 + t.text "guidance", limit: 65535 + t.integer "number", limit: 4 + t.integer "section_id", limit: 4 t.datetime "created_at" t.datetime "updated_at" - t.integer "question_format_id" - t.boolean "option_comment_display", default: true + t.integer "question_format_id", limit: 4 + t.boolean "option_comment_display", default: true t.boolean "modifiable" end + add_index "questions", ["question_format_id"], name: "fk_rails_4fbc38c8c7", using: :btree + add_index "questions", ["section_id"], name: "fk_rails_c50eadc3e3", using: :btree + create_table "questions_themes", id: false, force: :cascade do |t| - t.integer "question_id", null: false - t.integer "theme_id", null: false + t.integer "question_id", limit: 4, null: false + t.integer "theme_id", limit: 4, null: false end add_index "questions_themes", ["question_id", "theme_id"], name: "question_theme_index", using: :btree add_index "questions_themes", ["theme_id", "question_id"], name: "theme_question_index", using: :btree - create_table "region_groups", force: :cascade do |t| - t.integer "super_region_id" - t.integer "region_id" - end - create_table "regions", force: :cascade do |t| - t.string "abbreviation" - t.string "description" - t.string "name" + t.string "abbreviation", limit: 255 + t.string "description", limit: 255 + t.string "name", limit: 255 + t.integer "super_region_id", limit: 4 end create_table "roles", force: :cascade do |t| - t.integer "user_id" - t.integer "plan_id" + t.integer "user_id", limit: 4 + t.integer "plan_id", limit: 4 t.datetime "created_at" t.datetime "updated_at" - t.integer "access", default: 0, null: false + t.integer "access", limit: 4, default: 0, null: false end + add_index "roles", ["plan_id"], name: "fk_rails_a1ce6c2772", using: :btree + add_index "roles", ["user_id"], name: "fk_rails_ab35d699f0", using: :btree + create_table "sections", force: :cascade do |t| - t.string "title" - t.text "description" - t.integer "number" + t.string "title", limit: 255 + t.text "description", limit: 65535 + t.integer "number", limit: 4 t.datetime "created_at" t.datetime "updated_at" t.boolean "published" - t.integer "phase_id" + t.integer "phase_id", limit: 4 t.boolean "modifiable" end + add_index "sections", ["phase_id"], name: "fk_rails_1853581585", using: :btree + create_table "settings", force: :cascade do |t| - t.string "var", null: false - t.text "value" - t.integer "target_id", null: false - t.string "target_type", null: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.string "var", limit: 255, null: false + t.text "value", limit: 65535 + t.integer "target_id", limit: 4, null: false + t.string "target_type", limit: 255, null: false + t.datetime "created_at" + t.datetime "updated_at" end add_index "settings", ["target_type", "target_id", "var"], name: "index_settings_on_target_type_and_target_id_and_var", unique: true, using: :btree create_table "splash_logs", force: :cascade do |t| - t.string "destination" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.string "destination", limit: 255 + t.datetime "created_at" + t.datetime "updated_at" end create_table "suggested_answers", force: :cascade do |t| - t.integer "question_id" - t.integer "org_id" - t.text "text" + t.integer "question_id", limit: 4 + t.integer "org_id", limit: 4 + t.text "text", limit: 65535 t.boolean "is_example" t.datetime "created_at" t.datetime "updated_at" end + add_index "suggested_answers", ["org_id"], name: "fk_rails_473de65779", using: :btree + add_index "suggested_answers", ["question_id"], name: "fk_rails_daa60b5b70", using: :btree + create_table "templates", force: :cascade do |t| - t.string "title" - t.text "description" + t.string "title", limit: 255 + t.text "description", limit: 65535 t.boolean "published" - t.integer "org_id" - t.string "locale" + t.integer "org_id", limit: 4 + t.string "locale", limit: 255 t.boolean "is_default" t.datetime "created_at" t.datetime "updated_at" - t.integer "version" - t.integer "visibility" - t.integer "customization_of" - t.integer "dmptemplate_id" + t.integer "version", limit: 4 + t.integer "visibility", limit: 4 + t.integer "customization_of", limit: 4 + t.integer "dmptemplate_id", limit: 4 end + add_index "templates", ["org_id"], name: "fk_rails_481431e1bd", using: :btree + create_table "themes", force: :cascade do |t| - t.string "title" - t.text "description" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.string "locale" + t.string "title", limit: 255 + t.text "description", limit: 65535 + t.datetime "created_at" + t.datetime "updated_at" + t.string "locale", limit: 255 end create_table "themes_in_guidance", id: false, force: :cascade do |t| - t.integer "theme_id" - t.integer "guidance_id" + t.integer "theme_id", limit: 4 + t.integer "guidance_id", limit: 4 end + add_index "themes_in_guidance", ["guidance_id"], name: "fk_rails_a5ab9402df", using: :btree + add_index "themes_in_guidance", ["theme_id"], name: "fk_rails_7d708f6f1e", using: :btree + create_table "token_permission_types", force: :cascade do |t| - t.string "token_type" - t.text "text_description" + t.string "token_type", limit: 255 + t.text "text_description", limit: 65535 t.datetime "created_at" t.datetime "updated_at" end create_table "user_identifiers", force: :cascade do |t| - t.string "identifier" + t.string "identifier", limit: 255 t.datetime "created_at" t.datetime "updated_at" - t.integer "user_id" - t.integer "identifier_scheme_id" + t.integer "user_id", limit: 4 + t.integer "identifier_scheme_id", limit: 4 end + add_index "user_identifiers", ["identifier_scheme_id"], name: "fk_rails_fe95df7db0", using: :btree + add_index "user_identifiers", ["user_id"], name: "fk_rails_65c9a98cdb", using: :btree + create_table "users", force: :cascade do |t| - t.string "firstname" - t.string "surname" - t.string "email", default: "", null: false - t.string "orcid_id" - t.string "shibboleth_id" + t.string "firstname", limit: 255 + t.string "surname", limit: 255 + t.string "email", limit: 255, default: "", null: false + t.string "shibboleth_id", limit: 255 t.datetime "created_at" t.datetime "updated_at" - t.string "encrypted_password", default: "" - t.string "reset_password_token" + t.string "encrypted_password", limit: 255, default: "" + t.string "reset_password_token", limit: 255 t.datetime "reset_password_sent_at" t.datetime "remember_created_at" - t.integer "sign_in_count", default: 0 + t.integer "sign_in_count", limit: 4, default: 0 t.datetime "current_sign_in_at" t.datetime "last_sign_in_at" - t.string "current_sign_in_ip" - t.string "last_sign_in_ip" - t.string "confirmation_token" + t.string "current_sign_in_ip", limit: 255 + t.string "last_sign_in_ip", limit: 255 + t.string "confirmation_token", limit: 255 t.datetime "confirmed_at" t.datetime "confirmation_sent_at" - t.string "invitation_token" + t.string "invitation_token", limit: 255 t.datetime "invitation_created_at" t.datetime "invitation_sent_at" t.datetime "invitation_accepted_at" - t.string "other_organisation" + t.string "other_organisation", limit: 255 t.boolean "dmponline3" t.boolean "accept_terms" - t.integer "org_id" - t.string "api_token" - t.integer "invited_by_id" - t.string "invited_by_type" - t.integer "language_id" + t.integer "org_id", limit: 4 + t.string "api_token", limit: 255 + t.integer "invited_by_id", limit: 4 + t.string "invited_by_type", limit: 255 + t.integer "language_id", limit: 4 end add_index "users", ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true, using: :btree add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree add_index "users", ["invitation_token"], name: "index_users_on_invitation_token", unique: true, using: :btree + add_index "users", ["language_id"], name: "fk_rails_45f4f12508", using: :btree + add_index "users", ["org_id"], name: "fk_rails_e73753bccb", using: :btree add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree create_table "users_perms", id: false, force: :cascade do |t| - t.integer "user_id" - t.integer "perm_id" + t.integer "user_id", limit: 4 + t.integer "perm_id", limit: 4 end + add_index "users_perms", ["perm_id"], name: "fk_rails_457217c31c", using: :btree add_index "users_perms", ["user_id", "perm_id"], name: "index_users_perms_on_user_id_and_perm_id", using: :btree add_foreign_key "answers", "plans" diff --git a/db/seeds.rb b/db/seeds.rb index 2467322..216bfda 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -73,6 +73,7 @@ {abbreviation: 'horizon', description: 'European super region', name: 'Horizon2020', + sub_regions: [ {abbreviation: 'uk', description: 'United Kingdom', @@ -99,12 +100,12 @@ if Region.find_by(abbreviation: r[:abbreviation]).nil? region = Region.create!(r) - + unless srs.nil? srs.each do |sr| if Region.find_by(abbreviation: sr[:abbreviation]).nil? - subregion = Region.create!(sr) - RegionGroup.create!({region_id: subregion.id, super_region_id: region.id}) + sr[:super_region] = region + Region.create!((sr)) end end end diff --git a/test/unit/question_test.rb b/test/unit/question_test.rb new file mode 100644 index 0000000..f5f3d4d --- /dev/null +++ b/test/unit/question_test.rb @@ -0,0 +1,138 @@ +require 'test_helper' + +class QuestionTest < ActiveSupport::TestCase + + setup do + @user = User.last + + scaffold_template + + @section = @template.phases.first.sections.first + + @question = Question.create(text: 'Test question', default_value: 'ABCD', guidance: 'Hello', + number: 999, section: @section, + question_format: QuestionFormat.where(option_based: false).first, + option_comment_display: true, modifiable: true) + end + + # --------------------------------------------------- + test "required fields are required" do + assert_not Question.new.valid? + assert_not Question.new(section: @section, number: 7).valid?, "expected the 'text' field to be required" + assert_not Question.new(number: 7, text: 'Testing').valid?, "expected the 'section' field to be required" + assert_not Question.new(section: @section, text: 'Testing').valid?, "expected the 'number' field to be required" + + # Ensure the bar minimum and complete versions are valid + a = Question.new(section: @section, text: 'Testing', number: 7) + assert a.valid?, "expected the 'text', 'section' and 'number' fields to be enough to create an Question! - #{a.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" + end + + # --------------------------------------------------- + test "to_s returns the Question text" do + assert_equal @question.text, @question.to_s + end + + # --------------------------------------------------- + test "returns the correct themed guidance for the org" do + all = Theme.first.guidances + Theme.last.guidances + + # Attach 2 themes to the question + @question.themes = [Theme.first, Theme.last] + @question.save! + + # Attach the first theme's first gudiance's group to the org + @user.org.guidance_groups << Theme.first.guidances.first.guidance_group + @user.save! + + assert_not @question.guidance_for_org(@user.org).empty?, "expected guidance to be returned" + + assert @question.guidance_for_org(@user.org).first.first.include?(Theme.first.title), "expected the theme.title" + assert @question.guidance_for_org(@user.org).first.first.include?(Theme.first.guidances.first.guidance_group.name), "expected the guidance_group.name" + assert_equal Theme.first.guidances.first, @question.guidance_for_org(@user.org).first.last, "expected the guidance object to be returned" + end + + # --------------------------------------------------- + test "returns the correct suggested answer for the org" do + @question.suggested_answers = [SuggestedAnswer.new(org: @user.org, text: 'Test 1', is_example: false), + SuggestedAnswer.new(org: Org.first, text: 'Test 2', is_example: false)] + @question.save! + + assert_equal 'Test 1', @question.get_suggested_answer(@user.org.id).text, "expected the correct suggested answer" + assert_equal 'Test 2', @question.get_suggested_answer(Org.first.id).text, "expected the correct suggested answer" + + org = Org.create(name: 'New One') + assert_equal nil, @question.get_suggested_answer(org.id), "expected no suggested answer for a new org" + end + + # --------------------------------------------------- +# TODO: amoeba gem doesn't seem to be in play anymore +=begin + test "should be able to clone a Question (should include its question_options, themes suggested_answers)" do + Question.all.each do |question| +puts question.inspect + q = question.amoeba_dup + + assert_equal question.text, q.text, "expected the 'text' field to match" + assert_equal question.default_value, q.default_value, "expected the 'default_value' field to match" + assert_equal question.guidance, q.guidance, "expected the 'guidance' field to match" + assert_equal question.number, q.number, "expected the 'number' field to match" + assert_equal question.section, q.section, "expected the 'section' field to match" + assert_equal question.question_format, q.question_format, "expected the 'question_format' field to match" + assert_equal question.option_comment_display, q.option_comment_display, "expected the 'option_comment_display' field to match" + assert_equal question.modifiable, q.modifiable, "expected the 'modifiable' field to match" + + assert q.question_options.eql?(question.question_options), "expected the clone to carry over all of the question_options instead got: original - #{question.question_options.count}, clone - #{q.question_options.count}" + assert q.suggested_answers.eql?(question.suggested_answers), "expected the clone to carry over all of the suggested_answers instead got: original - #{question.suggested_answers.count}, clone - #{q.suggested_answers.count}" + assert q.themes.eql?(question.themes), "expected the clone to carry over all of the suggested_answers instead got: original - #{question.themes.count}, clone - #{q.themes.count}" + end + end +=end + + # --------------------------------------------------- + test "can CRUD Question" do + obj = Question.create(section: @section, text: 'Test ABC', number: 7) + assert_not obj.id.nil?, "was expecting to be able to create a new Question: #{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 Question!" + + assert obj.destroy!, "Was unable to delete the Question!" + end + + # --------------------------------------------------- + test "can manage belongs_to relationship with Section" do + verify_belongs_to_relationship(@question, @template.phases.first.sections.last) + end + + # --------------------------------------------------- + test "can manage belongs_to relationship with QuestionFormat" do + verify_belongs_to_relationship(@question, QuestionFormat.where(option_based: false).last) + end + + # --------------------------------------------------- + test "can manage has_many relationship with Answer" do + scaffold_plan + a = Answer.new(user: @user, plan: @plan, text: 'Test Answer') + verify_has_many_relationship(@question, a, @question.answers.count) + end + + # --------------------------------------------------- + test "can manage has_many relationship with QuestionOption" do + qo = QuestionOption.new(text: 'Test', number: 9) + verify_has_many_relationship(@question, qo, @question.question_options.count) + end + + # --------------------------------------------------- + test "can manage has_many relationship with SuggestedAnswer" do + sa = SuggestedAnswer.new(text: 'Suggested Answer', org: @user.org) + verify_has_many_relationship(@question, sa, @question.suggested_answers.count) + end + + # --------------------------------------------------- + test "can manage has_many relationship with Themes" do + t = Theme.new(title: 'Test Theme') + verify_has_many_relationship(@question, t, @question.themes.count) + end +end diff --git a/test/unit/region_test.rb b/test/unit/region_test.rb new file mode 100644 index 0000000..422f8fd --- /dev/null +++ b/test/unit/region_test.rb @@ -0,0 +1,55 @@ +require 'test_helper' + +class RegionTest < ActiveSupport::TestCase + + setup do + @region = Region.create(name: 'Test Super Region', abbreviation: 'TSR', description: 'Testing') + + @region.sub_regions = [Region.new(name: 'Test Sub Region 1'), Region.new(name: 'Test Sub Region 2')] + end + + # --------------------------------------------------- + test "required fields are required" do + assert_not Region.new.valid? + + # Ensure the bar minimum and complete versions are valid + a = Region.new(name: 'Test Region') + assert a.valid?, "expected the 'name' field to be enough to create an Region! - #{a.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" + end + + # --------------------------------------------------- + test "name and abbreviation should be unique" do + assert_not Region.new(name: 'Test Super Region').valid?, "expected that 'name' must be unique" + assert_not Region.new(abbreviation: 'TSR').valid?, "expected that 'abbreviation' must be unique" + + assert_not Region.new(name: 'Test Super Region', abbreviation: '123').valid?, "expected that 'name' must be unique even if abbreviation is unique" + assert_not Region.new(abbreviation: 'TSR', name: 'test super').valid?, "expected that 'abbreviation' must be unique even if name is unique" + end + + # --------------------------------------------------- + test "can CRUD Region" do + obj = Region.create(name: 'Test Region') + assert_not obj.id.nil?, "was expecting to be able to create a new Region: #{obj.errors.map{|f, m| f.to_s + ' ' + m}.join(', ')}" + + obj.abbreviation = 'ABC' + obj.save! + obj.reload + assert_equal 'ABC', obj.abbreviation, "Was expecting to be able to update the text of the Region!" + + assert obj.destroy!, "Was unable to delete the Region!" + end + + # Need to roll our own here because of the name of the relationship attributes + # --------------------------------------------------- + test "can manage has_and_belongs_to_many relationship with Region" do + count = Region.first.sub_regions.count + + @region.super_region = Region.first + @region.save! + + # Search the parent for the child + assert Region.first.sub_regions.include?(@region), "was expecting the Region.sub_regions to contain the test region" + assert_equal (count + 1), Region.first.sub_regions.count + end + +end \ No newline at end of file