<commit_before>require 'spec_helper'

describe WorkspaceDatasetsController do
  ignore_authorization!

  let(:user) { FactoryGirl.create(:user) }
  let(:workspace) { FactoryGirl.create(:workspace) }
  let(:gpdb_view) { FactoryGirl.create(:gpdb_view) }
  let(:gpdb_table) { FactoryGirl.create(:gpdb_table) }
  let(:datasets) { fake_relation [gpdb_table, gpdb_view] }

  before do
    log_in user

    mock(Workspace).
      workspaces_for(user).mock!.
      find(workspace.to_param) { workspace }

    stub(workspace).datasets { datasets }
  end

  describe "#index" do
    it "presents the workspace's datasets, ordered by name and paginated" do
      mock_present { |collection| collection.should =~ datasets }
      get :index, :workspace_id => workspace.to_param
      response.should be_success
    end

    it "orders and paginates the datasets" do
      mock(datasets).order("lower(name)") { datasets }
      mock(datasets).paginate("page" => "2", "per_page" => "25") { datasets }
      get :index, :workspace_id => workspace.to_param, :page => "2", :per_page => "25"
    end

    it "passes the workspace to the presenter" do
      mock_present { |collection, _, options| options[:workspace].should be_true }
      get :index, :workspace_id => workspace.to_param
    end
  end

  describe "#create" do
    it "should associate one table to the workspace" do
      post :create, :workspace_id => workspace.to_param, :dataset_ids => gpdb_table.to_param
      response.code.should == "201"
      response.decoded_body.should_not be_nil
      workspace.bound_datasets.should include(gpdb_table)
    end

    it "should associate multiple tables/views to the workspace for one table" do
      table_ids = gpdb_table.to_param + "," + gpdb_view.to_param

      post :create, :workspace_id => workspace.to_param, :dataset_ids => table_ids
      response.code.should == "201"

      workspace.bound_datasets.should include(gpdb_table)
      workspace.bound_datasets.should include(gpdb_view)
    end

    it "should create event and activity" do
      table_ids = [gpdb_table.to_param, gpdb_view.to_param]
      post :create, :workspace_id => workspace.to_param, :dataset_ids => table_ids

      events = Events::SOURCE_TABLE_CREATED.by(user)
      events.count.should == 2
    end

    xit "should not associate if the table/view is already in workspace sandbox" do
      workspace.sandbox = gpdb_table.schema
      post :create, :workspace_id => workspace.to_param, :dataset_ids => gpdb_table.to_param
      response.code.should == "400"
      decoded_errors.fields.dataset.ALREADY_ASSOCIATED.should_not be_nil
    end

    context "when associating multiple datasets with a workspace" do
      let(:gpdb_table2) { FactoryGirl.create(:gpdb_table) }

      before do
        workspace.bound_datasets << gpdb_table2
      end

      it "should associate all workspaces even if some are already associated" do
        table_ids = [gpdb_table.to_param, gpdb_table2.to_param]
        post :create, :workspace_id => workspace.to_param, :dataset_ids => table_ids
        response.code.should == "201"

        workspace.bound_datasets.should include(gpdb_table)
        workspace.bound_datasets.should include(gpdb_table2)
      end
    end
  end<commit_msg>Remove test validating that a dataset is not already associated to a workspace when running an association.  This should not be allowed in the UI. [#34035585]<commit_after>require 'spec_helper'

describe WorkspaceDatasetsController do
  ignore_authorization!

  let(:user) { FactoryGirl.create(:user) }
  let(:workspace) { FactoryGirl.create(:workspace) }
  let(:gpdb_view) { FactoryGirl.create(:gpdb_view) }
  let(:gpdb_table) { FactoryGirl.create(:gpdb_table) }
  let(:datasets) { fake_relation [gpdb_table, gpdb_view] }

  before do
    log_in user

    mock(Workspace).
      workspaces_for(user).mock!.
      find(workspace.to_param) { workspace }

    stub(workspace).datasets { datasets }
  end

  describe "#index" do
    it "presents the workspace's datasets, ordered by name and paginated" do
      mock_present { |collection| collection.should =~ datasets }
      get :index, :workspace_id => workspace.to_param
      response.should be_success
    end

    it "orders and paginates the datasets" do
      mock(datasets).order("lower(name)") { datasets }
      mock(datasets).paginate("page" => "2", "per_page" => "25") { datasets }
      get :index, :workspace_id => workspace.to_param, :page => "2", :per_page => "25"
    end

    it "passes the workspace to the presenter" do
      mock_present { |collection, _, options| options[:workspace].should be_true }
      get :index, :workspace_id => workspace.to_param
    end
  end

  describe "#create" do
    it "should associate one table to the workspace" do
      post :create, :workspace_id => workspace.to_param, :dataset_ids => gpdb_table.to_param
      response.code.should == "201"
      response.decoded_body.should_not be_nil
      workspace.bound_datasets.should include(gpdb_table)
    end

    it "should associate multiple tables/views to the workspace for one table" do
      table_ids = gpdb_table.to_param + "," + gpdb_view.to_param

      post :create, :workspace_id => workspace.to_param, :dataset_ids => table_ids
      response.code.should == "201"

      workspace.bound_datasets.should include(gpdb_table)
      workspace.bound_datasets.should include(gpdb_view)
    end

    it "should create event and activity" do
      table_ids = [gpdb_table.to_param, gpdb_view.to_param]
      post :create, :workspace_id => workspace.to_param, :dataset_ids => table_ids

      events = Events::SOURCE_TABLE_CREATED.by(user)
      events.count.should == 2
    end

    context "when associating multiple datasets with a workspace" do
      let(:gpdb_table2) { FactoryGirl.create(:gpdb_table) }

      before do
        workspace.bound_datasets << gpdb_table2
      end

      it "should associate all workspaces even if some are already associated" do
        table_ids = [gpdb_table.to_param, gpdb_table2.to_param]
        post :create, :workspace_id => workspace.to_param, :dataset_ids => table_ids
        response.code.should == "201"

        workspace.bound_datasets.should include(gpdb_table)
        workspace.bound_datasets.should include(gpdb_table2)
      end
    end
  end
{'fraction_non_alphanumeric': 0.09860494610019023, 'fraction_numerical': 0.008084971464806594, 'mean_word_length': 3.52258064516129, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 35, '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': '5522877', 'n_tokens_mistral': 2016, 'n_tokens_neox': 1935, 'n_words': 482}