<commit_before>require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

describe Joyce::Activity do
  
  describe "validation" do
    context "with missing parameters" do
      [
        {:verb => Acted},
        {:actor => Thing.create(:name => "Ulysses")}
      ].each do |params|
        context "with #{params}" do
          it { Joyce::Activity.new(params).should_not be_valid }
        end
      end
    end
  end
  
  describe "#get_targets" do
    let(:activity) { create(:activity) }
    it { activity.get_targets.should be_empty }
    
    context "with default target" do
      before do
        @default_target = build(:thing)
        Joyce::ActivityTarget.create(:activity => activity, :target => @default_target)
      end
      
      it { activity.get_targets.should == [@default_target] }
    end
    
    context "with named target" do
      before do
        @test_target = build(:thing)
        Joyce::ActivityTarget.create(:activity => activity, :target => @test_target, :name => :test)
      end
      
      it { activity.get_targets(:test).should == [@test_target] }
    end
    
    context "with array targets" do
      before do
        @test_targets = 2.times.map{ |i| build(:thing) }
        @test_targets.each do |target|
          Joyce::ActivityTarget.create(:activity => activity, :target => target, :name => :test)
        end
      end
      
      it { activity.get_targets(:test).should == @test_targets }
    end
  end
  
  describe "#set_targets" do
    let(:activity) { create(:activity) }
    let(:thing) { build(:thing) }
    
    context "when param is not a hash" do
      it { expect{ activity.set_targets(thing) }.to raise_error ArgumentError }
    end
    
    it "should set the target" do
      expect{
        activity.set_targets(:thing => thing)
      }.to change{ activity.get_targets(:thing) }.to([thing])
    end
    
    context "when hash value is an array" do
      let(:things) { 2.times.map{ |i| create(:thing) } }
      
      it "should set the targets" do
        expect{
          activity.set_targets(:things => things)
        }.to change{ activity.get_targets(:things) }.to(things)
      end
    end
    
  end
  
  describe "scopes" do
    describe "#since" do
      before do
        Timecop.travel(2.weeks.ago) do
          @activity_to_drop = create(:activity)
        end
        @activity_to_show = create(:activity)
      end
      
      it { Joyce::Activity.since(1.week.ago).should == [@activity_to_show] }
    end
  end
  
  describe "#verb=" do
    before{ @activity = Joyce::Activity.new }
    
    context "with a class" do
      before{ @activity.verb = Object }
      <commit_msg>Added test for setting an empty array as a target.<commit_after>require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

describe Joyce::Activity do
  
  describe "validation" do
    context "with missing parameters" do
      [
        {:verb => Acted},
        {:actor => Thing.create(:name => "Ulysses")}
      ].each do |params|
        context "with #{params}" do
          it { Joyce::Activity.new(params).should_not be_valid }
        end
      end
    end
  end
  
  describe "#get_targets" do
    let(:activity) { create(:activity) }
    it { activity.get_targets.should be_empty }
    
    context "with default target" do
      before do
        @default_target = build(:thing)
        Joyce::ActivityTarget.create(:activity => activity, :target => @default_target)
      end
      
      it { activity.get_targets.should == [@default_target] }
    end
    
    context "with named target" do
      before do
        @test_target = build(:thing)
        Joyce::ActivityTarget.create(:activity => activity, :target => @test_target, :name => :test)
      end
      
      it { activity.get_targets(:test).should == [@test_target] }
    end
    
    context "with array targets" do
      before do
        @test_targets = 2.times.map{ |i| build(:thing) }
        @test_targets.each do |target|
          Joyce::ActivityTarget.create(:activity => activity, :target => target, :name => :test)
        end
      end
      
      it { activity.get_targets(:test).should == @test_targets }
    end
  end
  
  describe "#set_targets" do
    let(:activity) { create(:activity) }
    let(:thing) { build(:thing) }
    
    context "when param is not a hash" do
      it { expect{ activity.set_targets(thing) }.to raise_error ArgumentError }
    end
    
    it "should set the target" do
      expect{
        activity.set_targets(:thing => thing)
      }.to change{ activity.get_targets(:thing) }.to([thing])
    end
    
    context "when hash value is an array" do
      let(:things) { 2.times.map{ |i| create(:thing) } }
      
      it "should set the targets" do
        expect{
          activity.set_targets(:things => things)
        }.to change{ activity.get_targets(:things) }.to(things)
      end
      
      context "when empty" do
        it "should not do anything" do
          expect{
            activity.set_targets(:things => [])
          }.not_to change{ activity.get_targets(:things) }
        end
      end
    end
    
  end
  
  describe "scopes" do
    describe "#since" do
      before do
        Timecop.travel(2.weeks.ago) do
          @activity_to_drop = create(:activity)
        end
        @activity_to_show = create(:activity)
      end
      
      it { Joyce::Activity.since(1.week.ago).should == [@activity_to_show] }
    end
  end
  
  describe "#verb=" do
    before{ @activity = Joyce::Activity.new }
    
    context "with a class" do
      before{ @activity.verb = Object }
      
{'fraction_non_alphanumeric': 0.11844452372458081, 'fraction_numerical': 0.0014270424545130217, 'mean_word_length': 2.2579895409645556, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 30, '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': '197577', 'n_tokens_mistral': 1738, 'n_tokens_neox': 1664, 'n_words': 436}