Skip to content

Commit 463f8eb

Browse files
Fixed all requests spec and added it to the github workflow (#167)
* added db cleaner * removed unnecessary files * fixed roles creation * fixed roles_spec * fixed other controller specs * Fixing participants_controller_spec * Fixing questions spec and authentication spec * fixed questions_spec --------- Co-authored-by: bhavya1803 <[email protected]>
1 parent 6dcc728 commit 463f8eb

23 files changed

+576
-1006
lines changed

.github/workflows/main.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,12 @@ jobs:
6060
chmod +x ./cc-test-reporter
6161
./cc-test-reporter before-build
6262
63-
- name: Run tests
63+
- name: Run model tests
6464
run: bundle exec rspec spec/models
6565

66+
- name: Run controller tests
67+
run: bundle exec rspec spec/requests/
68+
6669
- name: Format code coverage report
6770
run: ./cc-test-reporter format-coverage -t simplecov -o "coverage/codeclimate.models.json" --debug
6871

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ gem 'find_with_order'
3939
group :development, :test do
4040
gem 'debug', platforms: %i[mri mingw x64_mingw]
4141
gem 'factory_bot_rails'
42+
gem 'database_cleaner-active_record'
4243
gem 'faker'
4344
gem 'rspec-rails'
4445
gem 'rswag-specs'

Gemfile.lock

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,14 @@ GEM
9191
term-ansicolor
9292
thor
9393
crass (1.0.6)
94+
database_cleaner-active_record (2.2.0)
95+
activerecord (>= 5.a)
96+
database_cleaner-core (~> 2.0.0)
97+
database_cleaner-core (2.0.1)
9498
date (3.4.1)
95-
debug (1.8.0)
96-
irb (>= 1.5.0)
97-
reline (>= 0.3.1)
99+
debug (1.10.0)
100+
irb (~> 1.10)
101+
reline (>= 0.3.8)
98102
diff-lcs (1.5.0)
99103
docile (1.4.0)
100104
domain_name (0.6.20240107)
@@ -116,7 +120,7 @@ GEM
116120
domain_name (~> 0.5)
117121
i18n (1.14.1)
118122
concurrent-ruby (~> 1.0)
119-
io-console (0.6.0)
123+
io-console (0.8.0)
120124
irb (1.15.1)
121125
pp (>= 0.6.0)
122126
rdoc (>= 4.0.0)
@@ -281,7 +285,7 @@ GEM
281285
simplecov-html (0.12.3)
282286
simplecov_json_formatter (0.1.4)
283287
spring (4.1.1)
284-
stringio (3.1.4)
288+
stringio (3.1.5)
285289
sync (0.5.0)
286290
term-ansicolor (1.11.2)
287291
tins (~> 1.0)
@@ -315,6 +319,7 @@ DEPENDENCIES
315319
bcrypt (~> 3.1.7)
316320
bootsnap (>= 1.18.4)
317321
coveralls
322+
database_cleaner-active_record
318323
debug
319324
factory_bot_rails
320325
faker

app/controllers/api/v1/participants_controller.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
class Api::V1::ParticipantsController < ApplicationController
22
include ParticipantsHelper
33

4-
# Returns true if the user has TA privileges; otherwise, denies access by returning false.
5-
def action_allowed?
6-
has_required_role?('Teaching Assistant')
7-
end
8-
94
# Return a list of participants for a given user
105
# params - user_id
116
# GET /participants/user/:user_id

app/controllers/api/v1/questions_controller.rb

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ def show
2929
end
3030
end
3131

32+
# GET /api/v1/questions/show_all/questionnaire/:id
33+
def show_all
34+
questionnaire = Questionnaire.find(params[:id])
35+
items = questionnaire.items.order(:id)
36+
render json: items, status: :ok
37+
rescue ActiveRecord::RecordNotFound
38+
render json: { error: "Couldn't find Questionnaire" }, status: :not_found
39+
end
40+
3241
# POST /questions
3342
def create
3443
questionnaire_id = params[:questionnaire_id]
@@ -72,7 +81,33 @@ def update
7281
render json: { error: @item.errors.full_messages.to_sentence }, status: :unprocessable_entity
7382
end
7483
end
75-
84+
85+
def destroy
86+
@item = Item.find(params[:id])
87+
@item.destroy
88+
head :no_content
89+
rescue ActiveRecord::RecordNotFound
90+
render json: { error: "Couldn't find Item" }, status: :not_found
91+
end
92+
93+
# DELETE /api/v1/questions/delete_all/questionnaire/:id
94+
def delete_all
95+
questionnaire = Questionnaire.find(params[:id])
96+
if questionnaire.items.delete_all
97+
render json: { message: "All questions deleted" }, status: :ok
98+
else
99+
render json: { error: "Deletion failed" }, status: :unprocessable_entity
100+
end
101+
rescue ActiveRecord::RecordNotFound
102+
render json: { error: "Couldn't find Questionnaire" }, status: :not_found
103+
end
104+
105+
def types
106+
types = Item.pluck(:question_type).uniq
107+
render json: types, status: :ok
108+
end
109+
110+
76111
private
77112

78113
def set_question

app/models/Item.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
class Item < ApplicationRecord
22
before_create :set_seq
33
belongs_to :questionnaire # each item belongs to a specific questionnaire
4-
has_many :answers, dependent: :destroy
5-
has_many :choices, dependent: :destroy
4+
has_many :answers, dependent: :destroy, foreign_key: 'question_id'
65
attr_accessor :choice_strategy
76

87
validates :seq, presence: true, numericality: true # sequence must be numeric

app/models/Strategies/scale_strategy.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Strategies
22
class ScaleStrategy < ChoiceStrategy
33
def render(item)
44
# Render scale (numeric sequence of options)
5-
item.alternatives.map { |alt| "<option value='#{alt}'>#{alt}</option>" }.join
5+
(item.alternatives || []).map { |alt| "<option value='#{alt}'>#{alt}</option>" }.join
66
end
77

88
def validate(item)

app/models/assignment.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Assignment < ApplicationRecord
99
has_many :response_maps, foreign_key: 'reviewed_object_id', dependent: :destroy, inverse_of: :assignment
1010
has_many :review_mappings, class_name: 'ReviewResponseMap', foreign_key: 'reviewed_object_id', dependent: :destroy, inverse_of: :assignment
1111
has_many :sign_up_topics , class_name: 'SignUpTopic', foreign_key: 'assignment_id', dependent: :destroy
12-
has_many :due_dates, class_name: 'DueDate', foreign_key: 'due_date_id', dependent: :destroy, as: :parent
12+
has_many :due_dates,as: :parent, class_name: 'DueDate', dependent: :destroy, as: :parent
1313
belongs_to :course, optional: true
1414
belongs_to :instructor, class_name: 'User', inverse_of: :assignments
1515

app/models/role.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def other_roles
6464
end
6565

6666
def as_json(options = nil)
67+
options = options || {} # Ensure options is a hash
6768
super(options.merge({ only: %i[id name parent_id] }))
6869
end
6970
end

app/models/sign_up_topic.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ class SignUpTopic < ApplicationRecord
22
has_many :signed_up_teams, foreign_key: 'topic_id', dependent: :destroy
33
has_many :teams, through: :signed_up_teams # list all teams choose this topic, no matter in waitlist or not
44
has_many :assignment_questionnaires, class_name: 'AssignmentQuestionnaire', foreign_key: 'topic_id', dependent: :destroy
5-
has_many :due_dates, class_name: 'DueDate', foreign_key: 'due_date_id', dependent: :destroy, as: :parent
5+
has_many :due_dates, as: :parent,class_name: 'DueDate', dependent: :destroy, as: :parent
66
belongs_to :assignment
77
end

0 commit comments

Comments
 (0)