<commit_before>require 'rails_helper'
include LogInHelper

describe SignUpSheetController do
  before(:each) do
    instructor.save
    @user = User.find_by_name("instructor")

    @wiki = WikiType.new({"name"=>"No"})
    @wiki.save

    @assignment = Assignment.where(name: 'My assignment').first || Assignment.new({
                                                                                  "name"=>"My assignment",
                                                                                  "instructor_id"=>@user.id,
                                                                                  "wiki_type_id"=>@wiki.id
                                                                              })
    @assignment.save

    @topic1 = SignUpTopic.new({
                                 topic_name: "Topic1",
                                 topic_identifier: "Ch10",
                                 assignment_id: @assignment.id,
                                 max_choosers: 2
                             })
    @topic1.save

    @topic2 = SignUpTopic.new({
                                  topic_name: "Topic2",
                                  topic_identifier: "Ch10",
                                  assignment_id: @assignment.id,
                                  max_choosers: 2
                              })
    @topic2.save

    # simulate authorized session
    ApplicationController.any_instance.stub(:current_role_name).and_return('Instructor')
    ApplicationController.any_instance.stub(:undo_link).and_return(TRUE)
  end

  it "should be able to create topic for assignment" do
    get :create, id: @assignment.id, topic: {topic_name: "New Topic", max_choosers: 2, topic_identifier: "Ch1", category: "Programming"}
    expect(response).should redirect_to(edit_assignment_path(@assignment.id) + "#tabs-5")
  end

  it "should be able to update a topic for assignment that already has max choosers set" do
    sign_up_topic = SignUpTopic.new
    sign_up_topic.max_choosers = 2
    allow(SignUpTopic).to receive(:where) { sign_up_topic }
    allow(sign_up_topic).to receive(:first) { sign_up_topic }

    get :create, id: @assignment.id, topic: {topic_name: "New Topic", max_choosers: 2, topic_identifier: "Ch1", category: "Programming"}
    expect(response).should redirect_to(redirect_to :action => 'add_signup_topics', :id => @assignment.id)
  end

  it "should be able to update a topic for assignment that needs the waitlisted users updated" do
    sign_up_topic = SignUpTopic.new
    sign_up_topic.max_choosers = 0
    allow(SignUpTopic).to receive(:where) { sign_up_topic }
    allow(sign_up_topic).to receive(:first) { sign_up_topic }

    allow(SignedUpTeam).to receive(:find_by_topic_id) { SignedUpTeam.new }

    get :create, id: @assignment.id, topic: {topic_name: "New Topic", max_choosers: 2, topic_identifier: "Ch1", category: "Programming"}
    expect(response).should redirect_to(redirect_to :action => 'add_signup_topics', :id => @assignment.id)
  end

  it "should be able to update a topic for assignment but warn when max_choosers is too much" do
    sign_up_topic = SignUpTopic.new
    sign_up_topic.max_choosers = 4
    allow(SignUpTopic).to receive(:where) { sign_up_topic }
    allow(sign_up_topic).to receive(:first) { sign_up_topic }

    allow(SignedUpTeam).to receive(:find_by_topic_id) { SignedUpTeam.new }

    get :create, id: @assignment.id, topic: {topic_name: "New Topic", max_choosers: 2, topic_identifier: "Ch1", category: "Programming"}
    expect(response).should redirect_to(redirect_to :action => 'add_signup_topics', :id => @assignment.id)
    expect(flash[:error]).to eq('Value of maximum choosers can only be increased! No change has been made to max choosers.')
  end

  it "should be able to edit topic" do
    get :edit, id: @topic1.id
    expect(response).to be_success
  end

  it "should be able to delete topic" do
    delete :destroy, id: @topic1.id, assignment_id: @assignment.id
    expect(response).should redirect_to edit_assignment_path(@assignment.id) + "#tabs-5"
  end

  #it "should be able to generate topic dependency" do
  #  post :save_topic_dependencies, assignment_id: @assignment.id
  #  expect(File).to exist("public/assets/staggered_deadline_assignment_graph/graph_#{@assignment.id}.jpg")
  #end

  it "should be able to detect cycles" do
    post :save_topic_dependencies, assignment_id: @assignment.id,
         ('topic_dependencies_' + @topic1.id.to_s)=>{"dependent_on"=>[@topic2.id.to_s]},
         ('topic_dependencies_' + @topic2.id.to_s)=>{"dependent_on"=>[@topic1.id.to_s]}
    expect(flash[:error]).to eq("There may be one or more cycles in the dependencies. Please correct them")
  end
end<commit_msg>Testing to bring SignUpSheetController to 100% coverage on create<commit_after>require 'rails_helper'
include LogInHelper

describe SignUpSheetController do
  before(:each) do
    instructor.save
    @user = User.find_by_name("instructor")

    @wiki = WikiType.new({"name"=>"No"})
    @wiki.save

    @assignment = Assignment.where(name: 'My assignment').first || Assignment.new({
                                                                                  "name"=>"My assignment",
                                                                                  "instructor_id"=>@user.id,
                                                                                  "wiki_type_id"=>@wiki.id
                                                                              })
    @assignment.save

    @topic1 = SignUpTopic.new({
                                 topic_name: "Topic1",
                                 topic_identifier: "Ch10",
                                 assignment_id: @assignment.id,
                                 max_choosers: 2
                             })
    @topic1.save

    @topic2 = SignUpTopic.new({
                                  topic_name: "Topic2",
                                  topic_identifier: "Ch10",
                                  assignment_id: @assignment.id,
                                  max_choosers: 2
                              })
    @topic2.save

    # simulate authorized session
    ApplicationController.any_instance.stub(:current_role_name).and_return('Instructor')
    ApplicationController.any_instance.stub(:undo_link).and_return(TRUE)
  end

  it "should be able to create topic for assignment" do
    get :create, id: @assignment.id, topic: {topic_name: "New Topic", max_choosers: 2, topic_identifier: "Ch1", category: "Programming"}
    expect(response).should redirect_to(edit_assignment_path(@assignment.id) + "#tabs-5")
  end

  it "should be able to update a topic for assignment that already has max choosers set" do
    sign_up_topic = SignUpTopic.new
    sign_up_topic.max_choosers = 2
    allow(SignUpTopic).to receive(:where) { sign_up_topic }
    allow(sign_up_topic).to receive(:first) { sign_up_topic }

    get :create, id: @assignment.id, topic: {topic_name: "New Topic", max_choosers: 2, topic_identifier: "Ch1", category: "Programming"}
    expect(response).should redirect_to(redirect_to :action => 'add_signup_topics', :id => @assignment.id)
  end

  it "should be able to update a topic for assignment that needs the waitlisted users updated" do
    sign_up_topic = SignUpTopic.new
    sign_up_topic.max_choosers = 0
    allow(SignUpTopic).to receive(:where) { sign_up_topic }
    allow(sign_up_topic).to receive(:first) { sign_up_topic }

    allow(SignedUpTeam).to receive(:find_by_topic_id) { SignedUpTeam.new }

    get :create, id: @assignment.id, topic: {topic_name: "New Topic", max_choosers: 2, topic_identifier: "Ch1", category: "Programming"}
    expect(response).should redirect_to(redirect_to :action => 'add_signup_topics', :id => @assignment.id)
  end

  it "should be able to update a topic for assignment but warn when max_choosers is too much" do
    sign_up_topic = SignUpTopic.new
    sign_up_topic.max_choosers = 4
    allow(SignUpTopic).to receive(:where) { sign_up_topic }
    allow(sign_up_topic).to receive(:first) { sign_up_topic }

    allow(SignedUpTeam).to receive(:find_by_topic_id) { SignedUpTeam.new }

    get :create, id: @assignment.id, topic: {topic_name: "New Topic", max_choosers: 2, topic_identifier: "Ch1", category: "Programming"}
    expect(response).should redirect_to(redirect_to :action => 'add_signup_topics', :id => @assignment.id)
    expect(flash[:error]).to eq('Value of maximum choosers can only be increased! No change has been made to max choosers.')
  end

  it "should be able to update a topic with a microtask" do
    @assignment.microtask = true
    @assignment.save
    get :create, id: @assignment.id, topic: {topic_name: "New Topic", max_choosers: 2, topic_identifier: "Ch1", category: "Programming"}
    expect(response).should redirect_to(edit_assignment_path(@assignment.id) + "#tabs-5")
  end

  it "should be able to update a topic with staggard deadlines" do
    @assignment.staggered_deadline = true
    @assignment.save
    get :create, id: @assignment.id, topic: {topic_name: "New Topic", max_choosers: 2, topic_identifier: "Ch1", category: "Programming"}
    expect(response).should redirect_to(edit_assignment_path(@assignment.id) + "#tabs-5")
  end

  it "should fail gracefully when a topic cannot be saved" do
    get :create, id: @assignment.id, topic: {topic_name: "New Topic", topic_identifier: "Ch1", category: "Programming"}
    expect(response).should render_template("sign_up_sheet/new")
  end

  it "should be able to edit topic" do
    get :edit, id: @topic1.id
    expect(response).to be_success
  end

  it "should be able to delete topic" do
    delete :destroy, id: @topic1.id, assignment_id: @assignment.id
    expect(response).should redirect_to edit_assignment_path(@assignment.id) + "#tabs-5"
  end

  #it "should be able to generate topic dependency" do
  #  post :save_topic_dependencies, assignment_id: @assignment.id
  #  expect(File).to exist("public/assets/staggered_deadline_assignment_graph/graph_#{@assignment.id}.jpg")
  #end

  it "should be able to detect cycles" do
    post :save_topic_dependencies, assignment_id: @assignment.id,
         ('topic_dependencies_' + @topic1.id.to_s)=>{"dependent_on"=>[@topic2.id.to_s]},
         ('topic_dependencies_' + @topic2.id.to_s)=>{"dependent_on"=>[@topic1.id.to_s]}
    expect(flash[:error]).to eq("There may be one or more cycles in the dependencies. Please correct them")
  end
end
{'fraction_non_alphanumeric': 0.10708826901031716, 'fraction_numerical': 0.006878104700038212, 'mean_word_length': 2.641391304347826, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 40, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7644855', 'n_tokens_mistral': 3170, 'n_tokens_neox': 3087, 'n_words': 762}