<commit_before>require 'test_helper'

class StoriesControllerTest < ActionController::TestCase
  def setup
    @user = Factory.create(:user)
    @project = Factory.create(:project, :users => [@user])
    @story = Factory.create(:story, :project => @project,
                            :requested_by => @user)
  end

  test "should create a story" do
    sign_in @user
    assert_difference 'Story.count' do
      post :create, :project_id => @project.to_param,
        :story => {:title => 'Test story', :requested_by_id => @user.to_param}
    end
    assert_equal @user, assigns(:story).requested_by
    assert_equal @project, assigns(:project)
    assert_redirected_to project_url(@project)
  end

  test "should create a story from xhr" do
    sign_in @user
    assert_difference 'Story.count' do
      xhr :post, :create, :project_id => @project.to_param,
        :story => {:title => 'Test title', :editing => true}
    end
    assert_equal @user, assigns(:story).requested_by
    assert_equal @project, assigns(:project)
    assert_response :success
  end

  test "should destroy a story from xhr" do
    sign_in @user
    assert_difference 'Story.count', -1 do
      xhr :delete, :destroy, :project_id => @project.to_param,
        :id => @story.to_param
    end
  end

  test "should start a story" do
    sign_in @user
    assert_state_change(:start, 'started')
  end
  test "should finish a story" do
    sign_in @user
    @story.update_attribute :state, 'started'
    assert_state_change(:finish, 'finished')
  end
  test "should deliver a story" do
    sign_in @user
    @story.update_attribute :state, 'finished'
    assert_state_change(:deliver, 'delivered')
  end
  test "should accept a story" do
    sign_in @user
    @story.update_attribute :state, 'delivered'
    assert_state_change(:accept, 'accepted')
  end
  test "should reject a story" do
    sign_in @user
    @story.update_attribute :state, 'delivered'
    assert_state_change(:reject, 'rejected')
  end

  test "should get a single story in js format" do
    sign_in @user
    get :show, :project_id => @project.to_param, :id => @story.to_param,
      :format => 'js'
    assert_equal @project, assigns(:project)
    assert_equal @story, assigns(:story)
  end

  test "should get all stories in js format" do
    sign_in @user
    get :index, :project_id => @project.to_param, :format => 'js'
    assert_equal @project, assigns(:project)
    assert_equal @project.stories, assigns(:stories)
  end
  test "should get done stories in js format" do
    sign_in @user
    get :done, :project_id => @project.to_param, :format => 'js'
    assert_equal @project, assigns(:project)
    assert_equal @project.stories.done, assigns(:stories)
  end
  test "should get in progress stories in js format" do
    sign_in @user
    get :in_progress, :project_id => @project.to_param, :format => 'js'
    assert_equal @project, assigns(:project)
    assert_equal @project.stories.in_progress, assigns(:stories)
  end
  test "should get backlog stories in js format" do
    sign_in @user
    get :backlog, :project_id => @project.to_param, :format => 'js'
    assert_equal @project, assigns(:project)
    assert_equal @project.stories.backlog, assigns(:stories)
  end

  test "should get all stories in CSV format" do
    sign_in @user
    get :index, :project_id => @project.to_param, :format => 'csv'
    assert_equal @project, assigns(:project)
    assert_equal @project.stories, assigns(:stories)
    assert_equal 'text/csv',
      @response.headers['Content-Type'], 'Content type is CSV'
    assert_match /attachment; filename="Test Project-\d{8}_\d{4}\.csv"/,
      @response.headers['Content-Disposition'],
      "Filename should be 'Test Project-YYYYMMDD_HHMM.csv'"<commit_msg>Test number of lines in CSV export is correct<commit_after>require 'test_helper'

class StoriesControllerTest < ActionController::TestCase
  def setup
    @user = Factory.create(:user)
    @project = Factory.create(:project, :users => [@user])
    @story = Factory.create(:story, :project => @project,
                            :requested_by => @user)
  end

  test "should create a story" do
    sign_in @user
    assert_difference 'Story.count' do
      post :create, :project_id => @project.to_param,
        :story => {:title => 'Test story', :requested_by_id => @user.to_param}
    end
    assert_equal @user, assigns(:story).requested_by
    assert_equal @project, assigns(:project)
    assert_redirected_to project_url(@project)
  end

  test "should create a story from xhr" do
    sign_in @user
    assert_difference 'Story.count' do
      xhr :post, :create, :project_id => @project.to_param,
        :story => {:title => 'Test title', :editing => true}
    end
    assert_equal @user, assigns(:story).requested_by
    assert_equal @project, assigns(:project)
    assert_response :success
  end

  test "should destroy a story from xhr" do
    sign_in @user
    assert_difference 'Story.count', -1 do
      xhr :delete, :destroy, :project_id => @project.to_param,
        :id => @story.to_param
    end
  end

  test "should start a story" do
    sign_in @user
    assert_state_change(:start, 'started')
  end
  test "should finish a story" do
    sign_in @user
    @story.update_attribute :state, 'started'
    assert_state_change(:finish, 'finished')
  end
  test "should deliver a story" do
    sign_in @user
    @story.update_attribute :state, 'finished'
    assert_state_change(:deliver, 'delivered')
  end
  test "should accept a story" do
    sign_in @user
    @story.update_attribute :state, 'delivered'
    assert_state_change(:accept, 'accepted')
  end
  test "should reject a story" do
    sign_in @user
    @story.update_attribute :state, 'delivered'
    assert_state_change(:reject, 'rejected')
  end

  test "should get a single story in js format" do
    sign_in @user
    get :show, :project_id => @project.to_param, :id => @story.to_param,
      :format => 'js'
    assert_equal @project, assigns(:project)
    assert_equal @story, assigns(:story)
  end

  test "should get all stories in js format" do
    sign_in @user
    get :index, :project_id => @project.to_param, :format => 'js'
    assert_equal @project, assigns(:project)
    assert_equal @project.stories, assigns(:stories)
  end
  test "should get done stories in js format" do
    sign_in @user
    get :done, :project_id => @project.to_param, :format => 'js'
    assert_equal @project, assigns(:project)
    assert_equal @project.stories.done, assigns(:stories)
  end
  test "should get in progress stories in js format" do
    sign_in @user
    get :in_progress, :project_id => @project.to_param, :format => 'js'
    assert_equal @project, assigns(:project)
    assert_equal @project.stories.in_progress, assigns(:stories)
  end
  test "should get backlog stories in js format" do
    sign_in @user
    get :backlog, :project_id => @project.to_param, :format => 'js'
    assert_equal @project, assigns(:project)
    assert_equal @project.stories.backlog, assigns(:stories)
  end

  test "should get all stories in CSV format" do
    sign_in @user
    get :index, :project_id => @project.to_param, :format => 'csv'
    assert_equal @project, assigns(:project)
    assert_equal @project.stories, assigns(:stories)
    assert_equal 'text/csv',
      @response.headers['Content-Type'], 'Content type is CSV'
    assert_match /attachment; filename="Test Project-\d{8}_\d{4}\.csv"/,
      @response.headers['Content-Disposition'],
      "Filename should be 'Test Project-YYYYMMDD_HHMM.csv'"
    assert_equal @project.stories.count + 1, @response.body.lines.count,
      "body should have 1 header line plus 1 line for each story"
{'fraction_non_alphanumeric': 0.1150985253817043, 'fraction_numerical': 0.001174474748792901, 'mean_word_length': 3.6308157099697884, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 55, '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': '6949274', 'n_tokens_mistral': 2483, 'n_tokens_neox': 2400, 'n_words': 701}