Skip to content

Commit

Permalink
feat: continue to incorporate feedback feature stage, criterion, crit…
Browse files Browse the repository at this point in the history
…erion_option, feedback_comment, and feedback_comment_template.
  • Loading branch information
wakedreamer authored and macite committed Nov 30, 2023
1 parent 54296e6 commit 5a56346
Show file tree
Hide file tree
Showing 20 changed files with 269 additions and 43 deletions.
6 changes: 6 additions & 0 deletions app/api/entities/feedback_entities/feedback_comment_entity.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Entities
class FeedbackCommentEntity < Grape::Entity
expose :id
expose :comment
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Entities
class FeedbackCommentTemplateEntity < Grape::Entity
expose :id
expose :comment_text_situation
expose :comment_text_next_action
end
end
2 changes: 1 addition & 1 deletion app/models/feedback/criterion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Criterion < ApplicationRecord
has_many :criterion_options, dependent: :destroy

# Constraints
# validates_associated :stage
validates_associated :stage
validates :order, :description, presence: true
validates :order, numericality: {
greater_than_or_equal_to: 0, only_integer: true,
Expand Down
3 changes: 3 additions & 0 deletions app/models/feedback/criterion_option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ class CriterionOption < ApplicationRecord
belongs_to :task_status
has_many :feedback_comment_templates
has_many :feedback_comments

# Constraints
validates_associated :criterion
end
2 changes: 1 addition & 1 deletion app/models/feedback/feedback_comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ class FeedbackComment < TaskComment
belongs_to :criterion_option

# Constraints
validates :comment_text, presence: true
validates :comment, presence: true
end
1 change: 0 additions & 1 deletion app/models/feedback/feedback_comment_template.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
class FeedbackCommentTemplate < ApplicationRecord
# Associations
# belongs_to :stage -- other direction
belongs_to :criterion_option

# Constraints
Expand Down
1 change: 0 additions & 1 deletion app/models/feedback/stage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ class Stage < ApplicationRecord
# Associations
belongs_to :task_definition
has_many :criteria, dependent: :destroy
has_many :feedback_comment_templates
has_one :unit, through: :task_definition

# Constraints
Expand Down
1 change: 1 addition & 0 deletions app/models/task_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class TaskStatus < ApplicationRecord

# Model associations
has_many :tasks
has_many :criterion_options

#
# Override find to ensure that task status objects are cached - these do not change
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def change

# Foreign keys
t.references :task_definition
t.references :feedback_comment_template
end

create_table :criteria do |t| # "criteria" is set as plural of "criterion" in 'doubtfire-api/config/initializers/inflections.rb'
Expand Down
2 changes: 0 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,6 @@
t.string "exit_message_good"
t.string "exit_message_resubmit"
t.bigint "task_definition_id"
t.bigint "feedback_comment_template_id"
t.index ["feedback_comment_template_id"], name: "index_stages_on_feedback_comment_template_id"
t.index ["task_definition_id"], name: "index_stages_on_task_definition_id"
end

Expand Down
18 changes: 18 additions & 0 deletions test/factories/feedback/criterion_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

FactoryBot.define do
factory :criterion do
transient do
number_of_criterion_options {0}
end

stage

sequence(:order) { |n| n }
description { Faker::Lorem.sentence }
help_text { Faker::Lorem.sentence }

after(:create) do |criterion, evaluator|
create_list(:criterion_option, evaluator.number_of_criterion_options, criterion: criterion)
end
end
end
19 changes: 19 additions & 0 deletions test/factories/feedback/criterion_option_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

FactoryBot.define do
factory :criterion_option do
transient do
number_of_feedback_comment_templates {0}
end

association :criterion # associates according to relationship within models
task_status { TaskStatus.all.sample }

# outcome_status
resolved_message_text { Faker::Lorem.sentence }
unresolved_message_text { Faker::Lorem.sentence }

after(:create) do |criterion_option, evaluator|
create_list(:feedback_comment_template, evaluator.number_of_feedback_comment_templates, criterion_option: criterion_option)
end
end
end
8 changes: 8 additions & 0 deletions test/factories/feedback/feedback_comment_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

FactoryBot.define do
factory :feedback_comment do

association :feedback_comment_template
association :criterion_option
end
end
8 changes: 8 additions & 0 deletions test/factories/feedback/feedback_comment_template_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FactoryBot.define do
factory :feedback_comment_template do
criterion_option

comment_text_situation { Faker::Lorem.sentence }
comment_text_next_action { Faker::Lorem.sentence }
end
end
17 changes: 9 additions & 8 deletions test/factories/feedback/stage_factory.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
# Read about factories at https://github.com/thoughtbot/factory_bot

FactoryBot.define do

factory :stage do
transient do
number_of_criterion {0}
end

task_definition
association :task_definition

transient do # transient: not persisted to database
number_of_criterion {0} # `0` criteria created unless otherwise specified
# E.g., "FactoryBot.create(:stage, number_of_criterion: 3)"
end

sequence(:order) { |n| n }
sequence(:title) { |n| "Stage-#{n}" }

help_text { Faker::Lorem.sentence }
entry_message { Faker::Lorem.sentence }
exit_message_good { Faker::Lorem.sentence }
exit_message_resubmit { Faker::Lorem.sentence }

after(:build) do |stage, eval|
# Create a list of criterion that refer to the created stage
create_list(:criteria, eval.number_of_criterion, stage: stage)
after(:create) do |stage, evaluator|
create_list(:criteria, evaluator.number_of_criterion, stage: stage)
end
end
end
23 changes: 13 additions & 10 deletions test/models/feedback/criterion_model_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
# Tests for Criterion model objects - not accessed via API
#
class CriterionModelTest < ActiveSupport::TestCase
def app
Rails.application

setup do
DatabaseCleaner.start
@stage = FactoryBot.create(:stage)
end

# Test you can create a valid criterion
def test_criterion_creation
td = FactoryBot.create(:task_definition)
stage = Stage.create(task_definition: td, title: 'Stage 1', order: 1)
criterion = Criterion.create(stage: stage, description: 'Criterion 1', order: 1)
def test_valid_criterion_creation
criterion = Criterion.create(stage: @stage, description: 'Criterion 1', order: 1)

assert criterion
assert criterion.stage
Expand All @@ -22,17 +22,20 @@ def test_criterion_creation

# Test you cannot create an invalid criterion
def test_criterion_order_and_description_are_required
stage = FactoryBot.create(:stage)
criterion = Criterion.new(stage: stage)
# stage = FactoryBot.create(:stage)
criterion = Criterion.create(stage: @stage)

refute criterion.valid? # "refute": fail if true
refute criterion.valid? # "refute": fail if true, pass if false
# Validator is included in criterion model (@ doubtfire-api/app/models/feedback/criterion.rb)
criterion.description = 'Criterion 1'
refute criterion.valid?
refute criterion.valid? # still shoul not be valid until order is added
criterion.order = 1
assert criterion.valid?
criterion.description = nil
refute criterion.valid?
criterion.description = 'Criterion'
criterion.order = nil
refute criterion.valid?

refute criterion.save # fail if criterion is saved
end
Expand Down
44 changes: 44 additions & 0 deletions test/models/feedback/criterion_option_model_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require 'test_helper'

#
# Tests for CriterionOption model objects - not accessed via API
#
class CriterionOptionModelTest < ActiveSupport::TestCase

# Setup objects for testing
setup do
DatabaseCleaner.start
@criterion = FactoryBot.create(:criterion)
@task_status = TaskStatus.new
end

# Test you can create a valid criterion option
def test_valid_criterion_option_creation
criterion_option = CriterionOption.create(criterion: @criterion, task_status: @task_status)
assert criterion_option.valid?
criterion_option.resolved_message_text = Faker::Lorem.sentence
assert criterion_option.valid?
criterion_option.unresolved_message_text = Faker::Lorem.sentence
assert criterion_option.valid?
end

# Test you cannot create an invalid criterion option
def test_invalid_criterion_option_creation
criterion_option = CriterionOption.new

refute criterion_option.valid?
criterion_option.resolved_message_text = Faker::Lorem.sentence
refute criterion_option.valid?
criterion_option.unresolved_message_text = Faker::Lorem.sentence
refute criterion_option.valid?

criterion_option.task_status = @task_status
refute criterion_option.valid?
criterion_option.criterion = @criterion
assert criterion_option.valid?
criterion_option.task_status = nil
refute criterion_option.valid?

refute criterion_option.save
end
end
54 changes: 54 additions & 0 deletions test/models/feedback/feedback_comment_model_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require 'test_helper'

#
# Contains tests for FeedbackComment model objects - not accessed via API
#
class FeedbackCommentModelTest < ActiveSupport::TestCase

# Setup objects for testing
def setup
DatabaseCleaner.start
criterion = FactoryBot.create(:criterion)
task_status = TaskStatus.new
@criterion_option = CriterionOption.create(criterion: criterion, task_status: task_status)

@task = Task.new
@user = FactoryBot.create(:user)
@recipient = FactoryBot.create(:user)
end

# Test the creation of a valid feedback comment
def test_valid_feedback_comment_creation
feedback_comment = FeedbackComment.new(task: @task, user: @user, recipient: @recipient, comment: 'Test Comment', criterion_option: @criterion_option)
assert feedback_comment.valid?
end

# Test the creation of an invalid feedback comment
def test_invalid_feedback_comment_creation
feedback_comment = FeedbackComment.new
refute feedback_comment.valid?

feedback_comment.task = @task
refute feedback_comment.valid?

feedback_comment.user = @user
refute feedback_comment.valid?

feedback_comment.recipient = @recipient
refute feedback_comment.valid?

feedback_comment.comment = 'Test Comment'
refute feedback_comment.valid?

feedback_comment.criterion_option = @criterion_option
assert feedback_comment.valid?

feedback_comment.task = nil
refute feedback_comment.valid?

feedback_comment.task = @task
feedback_comment.user = nil

refute feedback_comment.save
end
end
41 changes: 41 additions & 0 deletions test/models/feedback/feedback_comment_template_model_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'test_helper'

#
# Contains tests for FeedbackCommentTemplate model objects - not accessed via API
#
class FeedbackCommentTemplateModelTest < ActiveSupport::TestCase

# Setup objects for testing
def setup
DatabaseCleaner.start
task_status = TaskStatus.new
@criterion_option = CriterionOption.create(task_status: task_status)
end

# Test you can create a valid feedback comment template
def test_valid_feedback_comment_template_creation
feedback_comment_template = FeedbackCommentTemplate.create(criterion_option: @criterion_option, comment_text_situation: 'This is the situation')

assert feedback_comment_template.valid?

assert feedback_comment_template.criterion_option == @criterion_option
assert feedback_comment_template.comment_text_situation == 'This is the situation'
end

# Test you cannot create an invalid feedback comment template
def test_invalid_feedback_comment_template_creation
feedback_comment_template = FeedbackCommentTemplate.new
refute feedback_comment_template.valid?

feedback_comment_template.comment_text_situation = 'This is a comment'
refute feedback_comment_template.valid?

feedback_comment_template.criterion_option = @criterion_option
assert feedback_comment_template.valid?

feedback_comment_template.comment_text_situation = nil
refute feedback_comment_template.valid?

refute feedback_comment_template.save
end
end
Loading

0 comments on commit 5a56346

Please sign in to comment.