<commit_before>require 'rails_helper'

RSpec.describe Question, :type => :model do

  before do
    @question_set = Fabricate(:question_set, questions: 
      [
        Fabricate(:attendance_question), 
        Fabricate(:short_answer_question), 
        Fabricate(:multi_choice_question)
      ]
    )

    @attendance_question = @question_set.questions[0]
    @short_answer_question = @question_set.questions[1]
    @multi_choice_question = @question_set.questions[2]
  end

  describe 'Question Model Association' do
    it "belongs to same question_set_id" do
      attendance_qset_id = @attendance_question.question_set_id
      short_ans_qset_id = @short_answer_question.question_set_id
      multi_choice_qset_id = @multi_choice_question.question_set_id

      expect(attendance_qset_id).to eq(@question_set.id)
      expect(short_ans_qset_id).to eq(@question_set.id)
      expect(multi_choice_qset_id).to eq(@question_set.id)
    end

  end

  describe 'AttendanceQuestion' do

    it "is an instance of AttendanceQuestion class" do
      expect(@attendance_question).to be_a(AttendanceQuestion) 
    end

    it "contains the word 'attendance check'" do
      attendance_question_title = @attendance_question.content["question"]
      
      expect(attendance_question_title).to match(/^(A|a)ttendance (C|c)heck/) 
    end

    it "has only one answer as 'true'" do
      attendance_answer = @attendance_question.content["answer"]

      expect(attendance_answer).to eq("true")
    end
  end

  describe 'ShortAnswerQuestion' do

    it "is an instance of ShortAnswerQuestion class" do
      expect(@short_answer_question).to be_a(ShortAnswerQuestion) 
    end

    it "contains an answer of any variable type" do
      answer = @short_answer_question.content["answer"]

      expect(answer).to_not eq(nil)
    end
  end

  describe 'MultiChoiceQuestion' do

    it "is an instance of MultiChoiceQuestion class" do
      expect(@multi_choice_question).to be_a(MultiChoiceQuestion) 
    end

    it "contains at least one choice with corresponding answer" do
      multi_choices = @multi_choice_question.content["choices"].map do |x|
        x.keys
      end
      multi_answers = @multi_choice_question.content["choices"].map do |x|
        x.values
      end

      expect(multi_choices).to_not eq(nil)
      expect(multi_answers).to_not eq(nil)
      expect(multi_choices.count).to eq(multi_answers.count)
    end
  end

end<commit_msg>Tests for all types of questions' presense, choices, and answer (within choices) passing<commit_after>require 'rails_helper'

RSpec.describe Question, :type => :model do

  before do
    @question_set = Fabricate(:question_set, questions: 
      [
        Fabricate(:attendance_question), 
        Fabricate(:short_answer_question), 
        Fabricate(:multi_choice_question)
      ]
    )

    @attendance_question = @question_set.questions[0]
    @short_answer_question = @question_set.questions[1]
    @multi_choice_question = @question_set.questions[2]
  end

  describe "AttendanceQuestion's content validation" do
    it "validates the question key/value" do
      attendance_check = @attendance_question["content"]["question"] 

      expect(@attendance_question.question_set_id).to eq(@question_set.id)
      expect(attendance_check).to_not eq(nil)
    end
    it "validates absence of choice" do
      expect(@attendance_question["content"]["choices"]).to eq(nil)
    end

    it "ensures the answer set to 'true'" do
      expect(@attendance_question["content"]["answer"]).to eq("true")
    end
  end

  describe "ShortAnswerQuestion's content validation" do
    it "validates the question key/value" do
      short_answer_check = @short_answer_question["content"]

      expect(@short_answer_question.question_set_id).to eq(@question_set.id)
      expect(short_answer_check).to_not eq(nil)
    end
    it "validates absence of choice" do
      expect(@short_answer_question["content"]["choices"]).to eq(nil)
    end
    it "ensures the answer is present" do
      expect(@short_answer_question["content"]["answer"]).to_not eq(nil)
    end
  end

  describe "MultiChoiceQuestion's content validation" do
    it "validates the question key/value" do
      multi_choice_check = @multi_choice_question["content"]

      expect(@multi_choice_question.question_set_id).to eq(@question_set.id)
      expect(multi_choice_check).to_not eq(nil)
    end
    it "validates the choices key/value" do
      multi_choices = @multi_choice_question["content"]["choices"]

      expect(multi_choices).to_not eq(nil)

      multi_choices_keys = multi_choices.map { |x| x.keys }
      multi_answers = multi_choices.map { |x| x.values }

      expect(multi_choices_keys.count).to eq(multi_answers.count)
    end

    it "ensures the answer index is within the number of choices" do
      multi_choices_check = @multi_choice_question["content"]

      choices = multi_choices_check["choices"]
      answer = multi_choices_check["answer"]

      confirm = choices.map{ |x| x.keys }.flat_map{ |e| e }.find{ |i| i == answer }

      expect(confirm).to_not eq(nil) 
    end
  end


end
{'fraction_non_alphanumeric': 0.09693179597420364, 'fraction_numerical': 0.001172562048075044, 'mean_word_length': 3.7170506912442396, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2202012', 'n_tokens_mistral': 1625, 'n_tokens_neox': 1598, 'n_words': 345}