google_translation_agent_spec.rb 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. require 'rails_helper'
  2. describe Agents::GoogleTranslationAgent, :vcr do
  3. before do
  4. @valid_params = {
  5. name: "somename",
  6. options: {
  7. to: "sv",
  8. from: "en",
  9. google_api_key: 'some_api_key',
  10. expected_receive_period_in_days: 1,
  11. content: {
  12. text: "{{message}}",
  13. content: "{{xyz}}"
  14. }
  15. }
  16. }
  17. @checker = Agents::GoogleTranslationAgent.new(@valid_params)
  18. @checker.user = users(:jane)
  19. @checker.save!
  20. @event = Event.new
  21. @event.agent = agents(:jane_weather_agent)
  22. @event.payload = {
  23. message: "hey what are you doing",
  24. xyz: "do tell more"
  25. }
  26. end
  27. describe "#receive" do
  28. it "checks if it can handle multiple events" do
  29. event1 = Event.new
  30. event1.agent = agents(:bob_weather_agent)
  31. event1.payload = {
  32. xyz: "value1",
  33. message: "value2"
  34. }
  35. expect {
  36. @checker.receive([@event,event1])
  37. }.to change { Event.count }.by(2)
  38. end
  39. end
  40. describe "#working?" do
  41. it "checks if events have been received within expected receive period" do
  42. expect(@checker).not_to be_working
  43. Agents::GoogleTranslationAgent.async_receive @checker.id, [@event.id]
  44. expect(@checker.reload).to be_working
  45. two_days_from_now = 2.days.from_now
  46. stub(Time).now { two_days_from_now }
  47. expect(@checker.reload).not_to be_working
  48. end
  49. end
  50. describe "validation" do
  51. before do
  52. expect(@checker).to be_valid
  53. end
  54. it "should validate presence of content key" do
  55. @checker.options[:content] = nil
  56. expect(@checker).not_to be_valid
  57. end
  58. it "should validate presence of expected_receive_period_in_days key" do
  59. @checker.options[:expected_receive_period_in_days] = nil
  60. expect(@checker).not_to be_valid
  61. end
  62. it "should validate presence of google_api_key" do
  63. @checker.options[:google_api_key] = nil
  64. expect(@checker).not_to be_valid
  65. end
  66. it "should validate presence of 'to' key" do
  67. @checker.options[:to] = ""
  68. expect(@checker).not_to be_valid
  69. end
  70. end
  71. end