Skip to content

Commit

Permalink
Add github workflows and fixing rspec model cases. (#147)
Browse files Browse the repository at this point in the history
Removed Assignment as foreign key from questionnaire.

Co-authored-by: kmalick <[email protected]>
  • Loading branch information
Kashika08 and kmalick authored Dec 11, 2024
1 parent 13a0fdc commit 5ef1236
Show file tree
Hide file tree
Showing 25 changed files with 370 additions and 87 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Lint

on: [workflow_dispatch, pull_request]

jobs:
codespell:
name: Check spelling all files with codespell
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.8]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install codespell
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Check spelling with codespell
run: |
codespell --skip="./db/migrate, ./config/name.yml"
misspell:
name: Check spelling all files in commit with misspell
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install
run: wget -O - -q https://git.io/misspell | sh -s -- -b .
- name: Misspell
run: git ls-files --empty-directory | xargs ./misspell -i 'aircrafts,devels,invertions' -error
76 changes: 76 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
name: CI/CD

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
test:
runs-on: ubuntu-latest

env:
DATABASE_URL: mysql2://root:[email protected]:3306/expertiza_test
RAILS_ENV: test

services:
mysql:
image: mysql:8.0
env:
MYSQL_ROOT_PASSWORD: expertiza
MYSQL_DATABASE: expertiza_test
ports:
- 3306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

steps:
- uses: actions/checkout@v3

- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.2.1
bundler-cache: true

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18.x'

- name: Install system dependencies
run: |
sudo apt-get update
sudo apt-get install -y netcat
- name: Install Ruby dependencies
run: |
gem update --system
gem install bundler:2.4.7
bundle install
- name: Setup database
run: |
bundle exec rails db:create RAILS_ENV=test
bundle exec rails db:schema:load RAILS_ENV=test
- name: Run tests
run: bundle exec rspec spec/models

docker:
needs: test
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Build Docker image
uses: docker/build-push-action@v4
with:
context: .
push: false
tags: expertiza-backend:latest
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ group :development, :test do
gem 'rswag-specs'
gem 'rubocop'
gem 'simplecov', require: false, group: :test
gem 'database_cleaner-active_record'
end

group :development do
Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ GEM
builder (3.2.4)
concurrent-ruby (1.2.2)
crass (1.0.6)
database_cleaner-active_record (2.2.0)
activerecord (>= 5.a)
database_cleaner-core (~> 2.0.0)
database_cleaner-core (2.0.1)
date (3.3.3)
debug (1.8.0)
irb (>= 1.5.0)
Expand Down Expand Up @@ -252,6 +256,7 @@ PLATFORMS
DEPENDENCIES
bcrypt (~> 3.1.7)
bootsnap
database_cleaner-active_record
debug
factory_bot_rails
faker
Expand Down
3 changes: 1 addition & 2 deletions app/models/questionnaire.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class Questionnaire < ApplicationRecord
belongs_to :assignment, foreign_key: 'assignment_id', inverse_of: false
belongs_to :instructor
has_many :questions, dependent: :destroy # the collection of questions associated with this Questionnaire
before_destroy :check_for_question_associations
Expand Down Expand Up @@ -37,7 +36,7 @@ def validate_questionnaire
# Check_for_question_associations checks if questionnaire has associated questions or not
def check_for_question_associations
if questions.any?
raise ActiveRecord::DeleteRestrictionError.new(:base, "Cannot delete record because dependent questions exist")
raise ActiveRecord::DeleteRestrictionError.new( "Cannot delete record because dependent questions exist")
end
end

Expand Down
2 changes: 2 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,6 @@

# Uncomment if you wish to allow Action Cable access from any origin.
# config.action_cable.disable_request_forgery_protection = true
config.hosts << 'localhost'
config.hosts << "www.example.com"
end
2 changes: 2 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,6 @@

# Annotate rendered view with file names.
# config.action_view.annotate_rendered_view_with_filenames = true
config.hosts << 'localhost'
config.hosts << "www.example.com"
end
55 changes: 55 additions & 0 deletions spec/factories/assignments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# spec/factories/assignments.rb
FactoryBot.define do
factory :assignment do
sequence(:name) { |n| "Assignment #{n}" }
directory_path { "assignment_#{name.downcase.gsub(/\s+/, '_')}" }

# Required associations
association :instructor, factory: [:user, :instructor]

# Default values
num_reviews { 3 }
num_reviews_required { 3 }
num_reviews_allowed { 3 }
num_metareviews_required { 3 }
num_metareviews_allowed { 3 }
rounds_of_reviews { 1 } # This is the correct attribute name

# Boolean flags with default values
is_calibrated { false }
has_badge { false }
enable_pair_programming { false }
staggered_deadline { false }
show_teammate_reviews { false }
is_coding_assignment { false }

# Optional association
course { nil }

trait :with_course do
association :course
end

trait :with_badge do
has_badge { true }
end

trait :with_teams do
after(:create) do |assignment|
create_list(:team, 2, assignment: assignment)
end
end

trait :with_participants do
after(:create) do |assignment|
create_list(:participant, 2, assignment: assignment)
end
end

trait :with_questionnaires do
after(:create) do |assignment|
create(:assignment_questionnaire, assignment: assignment)
end
end
end
end
8 changes: 8 additions & 0 deletions spec/factories/courses.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FactoryBot.define do
factory :course do
sequence(:name) { |n| "Course #{n}" }
sequence(:directory_path) { |n| "course_#{n}" }
association :instructor, factory: [:user, :instructor]
association :institution
end
end
31 changes: 31 additions & 0 deletions spec/factories/questionnaires.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# spec/factories/questionnaires.rb
FactoryBot.define do
factory :questionnaire do
sequence(:name) { |n| "Questionnaire #{n}" }
private { false }
min_question_score { 0 }
max_question_score { 10 }
association :instructor
association :assignment

# Trait for questionnaire with questions
trait :with_questions do
after(:create) do |questionnaire|
create(:question, questionnaire: questionnaire, weight: 1, seq: 1, txt: "que 1", question_type: "Scale")
create(:question, questionnaire: questionnaire, weight: 10, seq: 2, txt: "que 2", question_type: "Checkbox")
end
end
end
end

# spec/factories/questions.rb
FactoryBot.define do
factory :question do
sequence(:txt) { |n| "Question #{n}" }
sequence(:seq) { |n| n }
weight { 1 }
question_type { "Scale" }
break_before { true }
association :questionnaire
end
end
53 changes: 53 additions & 0 deletions spec/factories/roles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# spec/factories/roles.rb
FactoryBot.define do
factory :role do
sequence(:name) { |n| "Role #{n}" }

trait :student do
id { Role::STUDENT }
name { 'Student' }
end

trait :ta do
id { Role::TEACHING_ASSISTANT }
name { 'Teaching Assistant' }
end

trait :instructor do
id { Role::INSTRUCTOR }
name { 'Instructor' }
end

trait :administrator do
id { Role::ADMINISTRATOR }
name { 'Administrator' }
end

trait :super_administrator do
id { Role::SUPER_ADMINISTRATOR }
name { 'Super Administrator' }
end
end
end

# spec/factories/institutions.rb
FactoryBot.define do
factory :institution do
sequence(:name) { |n| "Institution #{n}" }
end
end

# spec/factories/teams_users.rb
FactoryBot.define do
factory :teams_user do
association :user
association :team
end
end

# spec/factories/teams.rb
FactoryBot.define do
factory :team do
sequence(:name) { |n| "Team #{n}" }
end
end
11 changes: 7 additions & 4 deletions spec/models/course_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
require 'rails_helper'

RSpec.describe Course, type: :model do
let(:course) { build(:course, id: 1, name: 'ECE517') }
let(:user1) { User.new name: 'abc', fullname: 'abc bbc', email: '[email protected]', password: '123456789', password_confirmation: '123456789' }
let(:institution) { build(:institution, id: 1) }
describe Course, type: :model do
let(:role) {Role.create(name: 'Instructor', parent_id: nil, id: 2, default_page_id: nil)}
let(:instructor) { Instructor.create(name: 'testinstructor', email: '[email protected]', full_name: 'Test Instructor', password: '123456', role: role) }
let(:institution) { create(:institution, id: 1) }
let(:course) { create(:course, id: 1, name: 'ECE517', instructor: instructor, institution: institution) }
let(:user1) { create(:user, name: 'abcdef', full_name:'abc bbc', email: '[email protected]', password: '123456789', password_confirmation: '123456789') }

describe 'validations' do
it 'validates presence of name' do
course.name = ''
Expand Down
12 changes: 11 additions & 1 deletion spec/models/invitation_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
require 'rails_helper'

RSpec.describe Invitation, type: :model do
include ActiveJob::TestHelper
let(:user1) { create :user, name: 'rohitgeddam' }
let(:user2) { create :user, name: 'superman' }
let(:invalid_user) { build :user, name: 'INVALID' }
let(:assignment) { create(:assignment) }
let(:role) {Role.create(name: 'Instructor', parent_id: nil, id: 3, default_page_id: nil)}
let(:instructor) { Instructor.create(name: 'testinstructor', email: '[email protected]', full_name: 'Test Instructor', password: '123456', role: role) }
let(:assignment) { create(:assignment, instructor: instructor) }
before(:each) do
ActiveJob::Base.queue_adapter = :test
end

after(:each) do
clear_enqueued_jobs
end


it 'is invitation_factory returning new Invitation' do
Expand Down
4 changes: 2 additions & 2 deletions spec/models/question_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Creating dummy objects for the test with the help of let statement
let(:role) { Role.create(name: 'Instructor', parent_id: nil, id: 2, default_page_id: nil) }
let(:instructor) do
Instructor.create(id: 1234, name: 'testinstructor', email: '[email protected]', fullname: 'Test Instructor',
Instructor.create(id: 1234, name: 'testinstructor', email: '[email protected]', full_name: 'test instructor',
password: '123456', role:)
end
let(:questionnaire) do
Expand Down Expand Up @@ -67,7 +67,7 @@
instructor.save!
questionnaire.save!
question = Question.create(seq: 1, txt: 'Sample question', question_type: 'multiple_choice',
break_before: true, questionnaire:)
break_before: true, questionnaire:questionnaire)
expect { question.delete }.to change { Question.count }.by(-1)
end
end
Expand Down
Loading

0 comments on commit 5ef1236

Please sign in to comment.