Advertisement
Guest User

Untitled

a guest
Nov 16th, 2015
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.20 KB | None | 0 0
  1. require "spec_helper"
  2.  
  3. describe Hive do
  4. context "#validation" do
  5. it "is not valid without name" do
  6. hive = Hive.new(colony_count: 1)
  7. expect(hive).not_to be_valid
  8. end
  9.  
  10. it "is not valid without colony count" do
  11. hive = Hive.new(name: "foo")
  12. expect(hive).not_to be_valid
  13. end
  14.  
  15. it "is not valid without numerical colony count" do
  16. hive = Hive.new(name: "foo", colony_count: "not-a-number")
  17. expect(hive).not_to be_valid
  18. end
  19. end
  20.  
  21. context "#scale_association" do
  22. before do
  23. @hive = FactoryGirl.create(:hive)
  24. @scale = FactoryGirl.create(:scale, hive_id: @hive.id)
  25. end
  26.  
  27. context ".unlink_from_current_scale" do
  28. before do
  29. @hive_scale = HiveScale.create(hive_id: @hive.id, scale_id: @scale.id)
  30. end
  31.  
  32. it "removes association of hive and scale at the moment" do
  33. @hive.unlink_from_current_scale
  34.  
  35. @hive.reload
  36. @scale.reload
  37. expect(@hive.scale).to be_nil
  38. expect(@scale.hive_id).to be_nil
  39. end
  40.  
  41. it "sets the to_date of association between hive_scale" do
  42. datetime = Date.strptime("04-10-2013", "%d-%m-%Y")
  43. clock = double(now: datetime)
  44.  
  45. @hive.unlink_from_current_scale(clock)
  46.  
  47. @hive_scale.reload
  48. expect(@hive_scale.to_date.to_date).to eq(datetime)
  49. end
  50. end
  51.  
  52. context ".use_scale" do
  53. it "detaches from current scale" do
  54. new_scale = connect_hive_and_scale_and_create_new_scale
  55. @hive.use_scale(new_scale)
  56.  
  57. @scale.reload
  58. expect(@scale.hive_id).to be_nil
  59. end
  60.  
  61. it "connects to new scale" do
  62. new_scale = connect_hive_and_scale_and_create_new_scale
  63. @hive.use_scale(new_scale)
  64.  
  65. @hive.reload
  66. expect(@hive.scale).to eq(new_scale)
  67. end
  68.  
  69. it "creates the hive_scale record correspondingly" do
  70. expect do
  71. scale = FactoryGirl.create(:scale, vendor: "bar", serial_number: "02")
  72. @hive.use_scale(scale)
  73. end.to change(HiveScale, :count).by 1
  74. end
  75.  
  76. it "keeps connected to current scale when given nil" do
  77. @hive.use_scale(nil)
  78. expect(@hive.scale).to eq(@scale)
  79. end
  80.  
  81. def connect_hive_and_scale_and_create_new_scale
  82. HiveScale.create(hive_id: @hive.id,
  83. scale_id: @scale.id,
  84. from_date: Time.now)
  85. FactoryGirl.create(:scale, vendor: "bar",
  86. serial_number: "02")
  87. end
  88. end
  89. end
  90.  
  91. context ".get_messages" do
  92. let(:start) { Date.strptime("19-10-2013", "%d-%m-%Y") }
  93. let(:middle) { Date.strptime("20-10-2013", "%d-%m-%Y") }
  94. let(:finish) { Date.strptime("21-10-2013", "%d-%m-%Y") }
  95.  
  96. before do
  97. @hive = FactoryGirl.create(:hive)
  98. @m1 = FactoryGirl.create(:message, occurance_time: start, hive_id: @hive.id)
  99. @m2 = FactoryGirl.create(:message, occurance_time: middle, hive_id: @hive.id)
  100. @m3 = FactoryGirl.create(:message, occurance_time: finish, hive_id: @hive.id)
  101. end
  102.  
  103. it "returns messages in a specified period (exclusive for end)" do
  104. messages = @hive.get_messages(start, finish)
  105. expect(messages.count).to eq(2)
  106. expect(messages).to eq([@m1, @m2])
  107. end
  108.  
  109. it "returns empty when 'from' is later than 'to' date" do
  110. messages = @hive.get_messages(finish, start)
  111. expect(messages).to be_empty
  112. end
  113. end
  114.  
  115. context "#hive_visibility" do
  116. let(:hive) { FactoryGirl.create(:hive) }
  117.  
  118. context ".share_with" do
  119. it "is shared with no one by default" do
  120. expect(hive.people_shared_with).to be_empty
  121. end
  122.  
  123. it "can share with specified user" do
  124. user = FactoryGirl.create(:user)
  125. shared = hive.share_with(user.email)
  126. expect(hive.reload.people_shared_with).to eq([user.id])
  127. expect(shared).to eq(HiveSharingCodes::SUCCESSFULLYSHARED)
  128. end
  129.  
  130. it "gives 'already shared status' when sharing again with the same user" do
  131. user = FactoryGirl.create(:user)
  132. hive.share_with(user.email)
  133. shared = hive.share_with(user.email)
  134. expect(shared).to eq(HiveSharingCodes::ALREADYSHARED)
  135. end
  136.  
  137. it "shares with no one when email does not exist" do
  138. shared = hive.share_with("not-existing@example.com")
  139. expect(hive.reload.people_shared_with).to be_empty
  140. expect(shared).to eq(HiveSharingCodes::USERNOTFOUND)
  141. end
  142. end
  143.  
  144. context ".already_shared_with_user?" do
  145. before do
  146. @bob = FactoryGirl.create(:user)
  147. end
  148.  
  149. it "knows when it is already shared" do
  150. hive.share_with(@bob.email)
  151. expect(hive.already_shared_with_user?(@bob)).to be_truthy
  152. end
  153.  
  154. it "knows when it is NOT already shared" do
  155. alice = FactoryGirl.create(:user, email: "alice@example.com")
  156. hive.share_with(@bob.email)
  157. expect(hive.already_shared_with_user?(alice)).to be_falsey
  158. end
  159. end
  160.  
  161. context ".only_shared_publicly?" do
  162. it "knows when it's publicly shared" do
  163. hive.make_public!
  164. expect(hive.only_shared_publicly?).to be_truthy
  165. end
  166.  
  167. it "knows whne it is NOT publicly shared" do
  168. expect(hive.only_shared_publicly?).to be_falsey
  169. end
  170.  
  171. it "is NOT only publicly shared when shared with user" do
  172. user = FactoryGirl.create(:user)
  173. hive.share_with(user.email)
  174. hive.make_public!
  175. expect(hive.only_shared_publicly?).to be_falsey
  176. end
  177. end
  178.  
  179. context ".make_public!" do
  180. it "can be public" do
  181. hive.make_public!
  182. expect(hive.reload).to be_public
  183. end
  184. end
  185.  
  186. context ".disable_sharing" do
  187. it "disable sharing when requested" do
  188. hive.make_public!
  189. hive.disable_sharing!
  190. hive.reload
  191. expect(hive).not_to be_public
  192. expect(hive.people_shared_with).to be_empty
  193. end
  194.  
  195. context ".remove_user_from_sharing_list" do
  196. it "removes the user from the shared ones" do
  197. bob = FactoryGirl.create(:user)
  198. alice = FactoryGirl.create(:user, email: "alice@example.com")
  199. hive.share_with(bob.email)
  200. hive.share_with(alice.email)
  201.  
  202. hive.remove_user_from_sharing_list(alice.id)
  203. expect(hive.reload.people_shared_with).to eq([bob.id])
  204. end
  205. end
  206. end
  207. end
  208. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement