Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require "spec_helper"
- describe Hive do
- context "#validation" do
- it "is not valid without name" do
- hive = Hive.new(colony_count: 1)
- expect(hive).not_to be_valid
- end
- it "is not valid without colony count" do
- hive = Hive.new(name: "foo")
- expect(hive).not_to be_valid
- end
- it "is not valid without numerical colony count" do
- hive = Hive.new(name: "foo", colony_count: "not-a-number")
- expect(hive).not_to be_valid
- end
- end
- context "#scale_association" do
- before do
- @hive = FactoryGirl.create(:hive)
- @scale = FactoryGirl.create(:scale, hive_id: @hive.id)
- end
- context ".unlink_from_current_scale" do
- before do
- @hive_scale = HiveScale.create(hive_id: @hive.id, scale_id: @scale.id)
- end
- it "removes association of hive and scale at the moment" do
- @hive.unlink_from_current_scale
- @hive.reload
- @scale.reload
- expect(@hive.scale).to be_nil
- expect(@scale.hive_id).to be_nil
- end
- it "sets the to_date of association between hive_scale" do
- datetime = Date.strptime("04-10-2013", "%d-%m-%Y")
- clock = double(now: datetime)
- @hive.unlink_from_current_scale(clock)
- @hive_scale.reload
- expect(@hive_scale.to_date.to_date).to eq(datetime)
- end
- end
- context ".use_scale" do
- it "detaches from current scale" do
- new_scale = connect_hive_and_scale_and_create_new_scale
- @hive.use_scale(new_scale)
- @scale.reload
- expect(@scale.hive_id).to be_nil
- end
- it "connects to new scale" do
- new_scale = connect_hive_and_scale_and_create_new_scale
- @hive.use_scale(new_scale)
- @hive.reload
- expect(@hive.scale).to eq(new_scale)
- end
- it "creates the hive_scale record correspondingly" do
- expect do
- scale = FactoryGirl.create(:scale, vendor: "bar", serial_number: "02")
- @hive.use_scale(scale)
- end.to change(HiveScale, :count).by 1
- end
- it "keeps connected to current scale when given nil" do
- @hive.use_scale(nil)
- expect(@hive.scale).to eq(@scale)
- end
- def connect_hive_and_scale_and_create_new_scale
- HiveScale.create(hive_id: @hive.id,
- scale_id: @scale.id,
- from_date: Time.now)
- FactoryGirl.create(:scale, vendor: "bar",
- serial_number: "02")
- end
- end
- end
- context ".get_messages" do
- let(:start) { Date.strptime("19-10-2013", "%d-%m-%Y") }
- let(:middle) { Date.strptime("20-10-2013", "%d-%m-%Y") }
- let(:finish) { Date.strptime("21-10-2013", "%d-%m-%Y") }
- before do
- @hive = FactoryGirl.create(:hive)
- @m1 = FactoryGirl.create(:message, occurance_time: start, hive_id: @hive.id)
- @m2 = FactoryGirl.create(:message, occurance_time: middle, hive_id: @hive.id)
- @m3 = FactoryGirl.create(:message, occurance_time: finish, hive_id: @hive.id)
- end
- it "returns messages in a specified period (exclusive for end)" do
- messages = @hive.get_messages(start, finish)
- expect(messages.count).to eq(2)
- expect(messages).to eq([@m1, @m2])
- end
- it "returns empty when 'from' is later than 'to' date" do
- messages = @hive.get_messages(finish, start)
- expect(messages).to be_empty
- end
- end
- context "#hive_visibility" do
- let(:hive) { FactoryGirl.create(:hive) }
- context ".share_with" do
- it "is shared with no one by default" do
- expect(hive.people_shared_with).to be_empty
- end
- it "can share with specified user" do
- user = FactoryGirl.create(:user)
- shared = hive.share_with(user.email)
- expect(hive.reload.people_shared_with).to eq([user.id])
- expect(shared).to eq(HiveSharingCodes::SUCCESSFULLYSHARED)
- end
- it "gives 'already shared status' when sharing again with the same user" do
- user = FactoryGirl.create(:user)
- hive.share_with(user.email)
- shared = hive.share_with(user.email)
- expect(shared).to eq(HiveSharingCodes::ALREADYSHARED)
- end
- it "shares with no one when email does not exist" do
- shared = hive.share_with("not-existing@example.com")
- expect(hive.reload.people_shared_with).to be_empty
- expect(shared).to eq(HiveSharingCodes::USERNOTFOUND)
- end
- end
- context ".already_shared_with_user?" do
- before do
- @bob = FactoryGirl.create(:user)
- end
- it "knows when it is already shared" do
- hive.share_with(@bob.email)
- expect(hive.already_shared_with_user?(@bob)).to be_truthy
- end
- it "knows when it is NOT already shared" do
- alice = FactoryGirl.create(:user, email: "alice@example.com")
- hive.share_with(@bob.email)
- expect(hive.already_shared_with_user?(alice)).to be_falsey
- end
- end
- context ".only_shared_publicly?" do
- it "knows when it's publicly shared" do
- hive.make_public!
- expect(hive.only_shared_publicly?).to be_truthy
- end
- it "knows whne it is NOT publicly shared" do
- expect(hive.only_shared_publicly?).to be_falsey
- end
- it "is NOT only publicly shared when shared with user" do
- user = FactoryGirl.create(:user)
- hive.share_with(user.email)
- hive.make_public!
- expect(hive.only_shared_publicly?).to be_falsey
- end
- end
- context ".make_public!" do
- it "can be public" do
- hive.make_public!
- expect(hive.reload).to be_public
- end
- end
- context ".disable_sharing" do
- it "disable sharing when requested" do
- hive.make_public!
- hive.disable_sharing!
- hive.reload
- expect(hive).not_to be_public
- expect(hive.people_shared_with).to be_empty
- end
- context ".remove_user_from_sharing_list" do
- it "removes the user from the shared ones" do
- bob = FactoryGirl.create(:user)
- alice = FactoryGirl.create(:user, email: "alice@example.com")
- hive.share_with(bob.email)
- hive.share_with(alice.email)
- hive.remove_user_from_sharing_list(alice.id)
- expect(hive.reload.people_shared_with).to eq([bob.id])
- end
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement