<commit_before>require "spec_helper"

describe Events::Base do
  describe ".add(params)" do
    it "creates an event with the given parameters" do
      gpdb_instance1 = FactoryGirl.create(:gpdb_instance)
      gpdb_instance2 = FactoryGirl.create(:gpdb_instance)
      user1 = FactoryGirl.create(:user)
      user2 = FactoryGirl.create(:user)
      user3 = FactoryGirl.create(:user)
      dataset = FactoryGirl.create(:gpdb_table)
      hdfs_entry = HdfsEntry.create({:hadoop_instance_id => 1234, :path => "/path/file.txt"})
      workspace = FactoryGirl.create(:workspace)

      Events::GreenplumInstanceCreated.by(user1).add(:greenplum_instance => gpdb_instance1)
      Events::GreenplumInstanceChangedOwner.by(user2).add(:greenplum_instance => gpdb_instance2, :new_owner => user3)
      Events::WorkspaceAddHdfsAsExtTable.by(user1).add(:dataset => dataset, :hdfs_file => hdfs_entry, :workspace => workspace)

      event1 = Events::GreenplumInstanceCreated.first
      event2 = Events::GreenplumInstanceChangedOwner.first
      event3 = Events::WorkspaceAddHdfsAsExtTable.first

      event1.actor.should == user1
      event1.greenplum_instance.should == gpdb_instance1

      event2.actor.should == user2
      event2.greenplum_instance.should == gpdb_instance2
      event2.new_owner.should == user3

      event3.workspace.should == workspace
    end
  end

  it "is ordered with the most recent items first, by default" do
    events = Events::Base.limit(2).to_a
    events[0].created_at.should > events[1].created_at
  end

  describe '.notification_for_current_user' do
    let(:notification) { notifications(:bobs_notification1) }
    let(:event) { notification.notification_event }

    it "retrieves the notification for the event" do
      stub(ActiveRecord::Base).current_user { users(:owner) }
      event.notification_for_current_user.should be_present
    end

    it "does not retrieve the notification for a user for whom there is none" do
      stub(ActiveRecord::Base).current_user { users(:no_collaborators) }
      event.notification_for_current_user.should be_nil
    end
  end

  describe ".for_dashboard_of(user)" do
    let(:user) { users(:no_collaborators) }
    let(:the_events) do
      [
        events(:no_collaborators_creates_private_workfile),
        events(:private_workfile_created),
        events(:public_workfile_created),
        events(:no_collaborators_creates_private_workfile)
      ]
    end

    let(:other_workspace1) { workspaces(:public) }
    let(:other_workspace2) { workspaces(:private) }
    let(:user_workspace) { workspaces(:public_with_no_collaborators) }
<commit_msg>don't use bobs notifications directly<commit_after>require "spec_helper"

describe Events::Base do
  describe ".add(params)" do
    it "creates an event with the given parameters" do
      gpdb_instance1 = FactoryGirl.create(:gpdb_instance)
      gpdb_instance2 = FactoryGirl.create(:gpdb_instance)
      user1 = FactoryGirl.create(:user)
      user2 = FactoryGirl.create(:user)
      user3 = FactoryGirl.create(:user)
      dataset = FactoryGirl.create(:gpdb_table)
      hdfs_entry = HdfsEntry.create({:hadoop_instance_id => 1234, :path => "/path/file.txt"})
      workspace = FactoryGirl.create(:workspace)

      Events::GreenplumInstanceCreated.by(user1).add(:greenplum_instance => gpdb_instance1)
      Events::GreenplumInstanceChangedOwner.by(user2).add(:greenplum_instance => gpdb_instance2, :new_owner => user3)
      Events::WorkspaceAddHdfsAsExtTable.by(user1).add(:dataset => dataset, :hdfs_file => hdfs_entry, :workspace => workspace)

      event1 = Events::GreenplumInstanceCreated.first
      event2 = Events::GreenplumInstanceChangedOwner.first
      event3 = Events::WorkspaceAddHdfsAsExtTable.first

      event1.actor.should == user1
      event1.greenplum_instance.should == gpdb_instance1

      event2.actor.should == user2
      event2.greenplum_instance.should == gpdb_instance2
      event2.new_owner.should == user3

      event3.workspace.should == workspace
    end
  end

  it "is ordered with the most recent items first, by default" do
    events = Events::Base.limit(2).to_a
    events[0].created_at.should > events[1].created_at
  end

  describe '.notification_for_current_user' do
    let(:event) { event_owner.notification_events.first }
    let(:event_owner) { users(:owner) }

    it "retrieves the notification for the event" do
      stub(ActiveRecord::Base).current_user { event_owner }
      event.notification_for_current_user.should be_present
    end

    it "does not retrieve the notification for a user for whom there is none" do
      stub(ActiveRecord::Base).current_user { users(:no_collaborators) }
      event.notification_for_current_user.should be_nil
    end
  end

  describe ".for_dashboard_of(user)" do
    let(:user) { users(:no_collaborators) }
    let(:the_events) do
      [
        events(:no_collaborators_creates_private_workfile),
        events(:private_workfile_created),
        events(:public_workfile_created),
        events(:no_collaborators_creates_private_workfile)
      ]
    end

    let(:other_workspace1) { workspaces(:public) }
    let(:other_workspace2) { workspaces(:private) }
    let(:user_workspace) { workspaces(:public_with_no_collaborators) }

{'fraction_non_alphanumeric': 0.10167236792094261, 'fraction_numerical': 0.013112884834663626, 'mean_word_length': 4.0899419729206965, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '1187174', 'n_tokens_mistral': 1663, 'n_tokens_neox': 1605, 'n_words': 292}