<commit_before>require 'spec_helper'

describe Motion do
  let(:discussion) { create_discussion }

  describe "#unique_votes" do
    it "returns only the most recent votes of a user for a motion" do
      user = FactoryGirl.create :user
      motion = create :motion, discussion: discussion
      discussion.group.add_member! user
      vote1 = Vote.create(motion_id: motion.id, user_id: user.id, position: "no")
      vote2 = Vote.create(motion_id: motion.id, user_id: user.id, position: "yes")

      motion.votes.count.should == 2
      motion.unique_votes.count.should == 1
      motion.unique_votes.first.should == vote2
    end
  end

  describe "#voting?" do
    it "returns true if motion is open" do
      @motion = create :motion, discussion: discussion
      @motion.voting?.should be_true
      @motion.closed?.should be_false
    end
  end

  describe "#closed?" do
    it "returns true if motion is open" do
      @motion = create(:motion, closed_at: 2.days.ago, discussion: discussion)
      @motion.closed?.should be_true
      @motion.voting?.should be_false
    end
  end

  it "assigns the close_at from close_at_* fields" do
    close_date = "23-05-2013"
    close_time = "15:00"
    time_zone = "Saskatchewan"
    motion = build(:motion, close_at_date: close_date, close_at_time: close_time, close_at_time_zone: time_zone, discussion: discussion)
    motion.save!
    motion.closing_at.in_time_zone("Saskatchewan").to_s.should == "2013-05-23 15:00:00 -0600"
  end

  describe "#user_has_voted?(user)" do
    it "returns true if the given user has voted on motion" do
      @user = create(:user)
      @motion = create(:motion, :author => @user, discussion: discussion)
      @vote = build(:vote,:position => "yes", motion: @motion)
      @vote.user = @user
      @vote.motion = @motion
      @vote.save!
      @motion.user_has_voted?(@user).should == true
    end

    it "returns false if given nil" do
      @motion = create(:motion, discussion: discussion)
      @motion.user_has_voted?(nil).should == false
    end
  end

  describe "#search(query)" do
    before { @user = create(:user) }
    it "returns user's motions that match the query string" do
      motion = create(:motion, name: "<NAME>", author: @user, discussion: discussion)
      @user.motions.search("jam").should == [motion]
    end
    it "does not return discussions that don't belong to the user" do
      motion = create(:motion, name: "sandwich crumbs", discussion: discussion)
      @user.motions.search("sandwich").should_not == [motion]
    end
  end

  context "events" do
    before do
      @user = create :user
      @group = create :group
      @group.add_admin! @user
      @discussion = create_discussion :group => @group
    end
    it "fires new_motion event if a motion is created successfully" do
      motion = build(:motion, discussion: discussion)
      motion.should_receive(:fire_new_motion_event)
      motion.save!<commit_msg>remove dumb spec<commit_after>require 'spec_helper'

describe Motion do
  let(:discussion) { create_discussion }

  describe "#unique_votes" do
    it "returns only the most recent votes of a user for a motion" do
      user = FactoryGirl.create :user
      motion = create :motion, discussion: discussion
      discussion.group.add_member! user
      vote1 = Vote.create(motion_id: motion.id, user_id: user.id, position: "no")
      vote2 = Vote.create(motion_id: motion.id, user_id: user.id, position: "yes")

      motion.votes.count.should == 2
      motion.unique_votes.count.should == 1
      motion.unique_votes.first.should == vote2
    end
  end

  describe "#voting?" do
    it "returns true if motion is open" do
      @motion = create :motion, discussion: discussion
      @motion.voting?.should be_true
      @motion.closed?.should be_false
    end
  end

  describe "#closed?" do
    it "returns true if motion is open" do
      @motion = create(:motion, closed_at: 2.days.ago, discussion: discussion)
      @motion.closed?.should be_true
      @motion.voting?.should be_false
    end
  end

  it "assigns the close_at from close_at_* fields" do
    close_date = "23-05-2013"
    close_time = "15:00"
    time_zone = "Saskatchewan"
    motion = build(:motion, close_at_date: close_date, close_at_time: close_time, close_at_time_zone: time_zone, discussion: discussion)
    motion.save!
    motion.closing_at.in_time_zone("Saskatchewan").to_s.should == "2013-05-23 15:00:00 -0600"
  end

  describe "#user_has_voted?(user)" do
    it "returns true if the given user has voted on motion" do
      @user = create(:user)
      @motion = create(:motion, :author => @user, discussion: discussion)
      @vote = build(:vote,:position => "yes", motion: @motion)
      @vote.user = @user
      @vote.motion = @motion
      @vote.save!
      @motion.user_has_voted?(@user).should == true
    end
  end

  describe "#search(query)" do
    before { @user = create(:user) }
    it "returns user's motions that match the query string" do
      motion = create(:motion, name: "<NAME>", author: @user, discussion: discussion)
      @user.motions.search("jam").should == [motion]
    end
    it "does not return discussions that don't belong to the user" do
      motion = create(:motion, name: "<NAME>", discussion: discussion)
      @user.motions.search("sandwich").should_not == [motion]
    end
  end

  context "events" do
    before do
      @user = create :user
      @group = create :group
      @group.add_admin! @user
      @discussion = create_discussion :group => @group
    end
    it "fires new_motion event if a motion is created successfully" do
      motion = build(:motion, discussion: discussion)
      motion.should_receive(:fire_new_motion_event)
      motion.save!
{'fraction_non_alphanumeric': 0.11297363366509516, 'fraction_numerical': 0.012572027239392353, 'mean_word_length': 3.2273062730627307, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 12, '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': '3190249', 'n_tokens_mistral': 1893, 'n_tokens_neox': 1740, 'n_words': 520}