liquid_migrator_spec.rb 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. require 'spec_helper'
  2. describe LiquidMigrator do
  3. describe "converting JSONPath strings" do
  4. it "should work" do
  5. LiquidMigrator.convert_string("$.data", true).should == "{{data}}"
  6. LiquidMigrator.convert_string("$.data.test", true).should == "{{data.test}}"
  7. LiquidMigrator.convert_string("$first_title", true).should == "{{first_title}}"
  8. end
  9. it "should ignore strings which just contain a JSONPath" do
  10. LiquidMigrator.convert_string("$.data").should == "$.data"
  11. LiquidMigrator.convert_string("$first_title").should == "$first_title"
  12. LiquidMigrator.convert_string(" $.data", true).should == " $.data"
  13. LiquidMigrator.convert_string("lorem $.data", true).should == "lorem $.data"
  14. end
  15. it "should raise an exception when encountering complex JSONPaths" do
  16. expect { LiquidMigrator.convert_string("$.data.test.*", true) }.
  17. to raise_error("JSONPath '$.data.test.*' is too complex, please check your migration.")
  18. end
  19. end
  20. describe "converting escaped JSONPath strings" do
  21. it "should work" do
  22. LiquidMigrator.convert_string("Weather looks like <$.conditions> according to the forecast at <$.pretty_date.time>").should ==
  23. "Weather looks like {{conditions}} according to the forecast at {{pretty_date.time}}"
  24. end
  25. it "should convert the 'escape' method correctly" do
  26. LiquidMigrator.convert_string("Escaped: <escape $.content.name>\nNot escaped: <$.content.name>").should ==
  27. "Escaped: {{content.name | uri_escape}}\nNot escaped: {{content.name}}"
  28. end
  29. it "should raise an exception when encountering complex JSONPaths" do
  30. expect { LiquidMigrator.convert_string("Received <$.content.text.*> from <$.content.name> .") }.
  31. to raise_error("JSONPath '$.content.text.*' is too complex, please check your migration.")
  32. end
  33. end
  34. describe "migrating a hash" do
  35. it "should convert every attribute" do
  36. LiquidMigrator.convert_hash({'a' => "$.data", 'b' => "This is a <$.test>"}).should ==
  37. {'a' => "$.data", 'b' => "This is a {{test}}"}
  38. end
  39. it "should work with leading_dollarsign_is_jsonpath" do
  40. LiquidMigrator.convert_hash({'a' => "$.data", 'b' => "This is a <$.test>"}, leading_dollarsign_is_jsonpath: true).should ==
  41. {'a' => "{{data}}", 'b' => "This is a {{test}}"}
  42. end
  43. it "should use the corresponding *_path attributes when using merge_path_attributes"do
  44. LiquidMigrator.convert_hash({'a' => "default", 'a_path' => "$.data"}, {leading_dollarsign_is_jsonpath: true, merge_path_attributes: true}).should ==
  45. {'a' => "{{data}}"}
  46. end
  47. it "should raise an exception when encountering complex JSONPaths" do
  48. expect { LiquidMigrator.convert_hash({'b' => "This is <$.complex[2]>"}) }.
  49. to raise_error("JSONPath '$.complex[2]' is too complex, please check your migration.")
  50. end
  51. end
  52. describe "migrating the 'make_message' format" do
  53. it "should work" do
  54. LiquidMigrator.convert_make_message('<message>').should == '{{message}}'
  55. LiquidMigrator.convert_make_message('<new.message>').should == '{{new.message}}'
  56. LiquidMigrator.convert_make_message('Hello <world>. How is <nested.life>').should == 'Hello {{world}}. How is {{nested.life}}'
  57. end
  58. end
  59. describe "migrating an actual agent" do
  60. before do
  61. valid_params = {
  62. 'auth_token' => 'token',
  63. 'room_name' => 'test',
  64. 'room_name_path' => '',
  65. 'username' => "Huginn",
  66. 'username_path' => '$.username',
  67. 'message' => "Hello from Huginn!",
  68. 'message_path' => '$.message',
  69. 'notify' => false,
  70. 'notify_path' => '',
  71. 'color' => 'yellow',
  72. 'color_path' => '',
  73. }
  74. @agent = Agents::HipchatAgent.new(:name => "somename", :options => valid_params)
  75. @agent.user = users(:jane)
  76. @agent.save!
  77. end
  78. it "should work" do
  79. LiquidMigrator.convert_all_agent_options(@agent)
  80. @agent.reload.options.should == {"auth_token" => 'token', 'color' => 'yellow', 'notify' => false, 'room_name' => 'test', 'username' => '{{username}}', 'message' => '{{message}}'}
  81. end
  82. it "should work with nested hashes" do
  83. @agent.options['very'] = {'nested' => '$.value'}
  84. LiquidMigrator.convert_all_agent_options(@agent)
  85. @agent.reload.options.should == {"auth_token" => 'token', 'color' => 'yellow', 'very' => {'nested' => '{{value}}'}, 'notify' => false, 'room_name' => 'test', 'username' => '{{username}}', 'message' => '{{message}}'}
  86. end
  87. it "should work with nested arrays" do
  88. @agent.options['array'] = ["one", "$.two"]
  89. LiquidMigrator.convert_all_agent_options(@agent)
  90. @agent.reload.options.should == {"auth_token" => 'token', 'color' => 'yellow', 'array' => ['one', '{{two}}'], 'notify' => false, 'room_name' => 'test', 'username' => '{{username}}', 'message' => '{{message}}'}
  91. end
  92. it "should raise an exception when encountering complex JSONPaths" do
  93. @agent.options['username_path'] = "$.very.complex[*]"
  94. expect { LiquidMigrator.convert_all_agent_options(@agent) }.
  95. to raise_error("JSONPath '$.very.complex[*]' is too complex, please check your migration.")
  96. end
  97. it "should work with the human task agent" do
  98. valid_params = {
  99. 'expected_receive_period_in_days' => 2,
  100. 'trigger_on' => "event",
  101. 'hit' =>
  102. {
  103. 'assignments' => 1,
  104. 'title' => "Sentiment evaluation",
  105. 'description' => "Please rate the sentiment of this message: '<$.message>'",
  106. 'reward' => 0.05,
  107. 'lifetime_in_seconds' => 24 * 60 * 60,
  108. 'questions' =>
  109. [
  110. {
  111. 'type' => "selection",
  112. 'key' => "sentiment",
  113. 'name' => "Sentiment",
  114. 'required' => "true",
  115. 'question' => "Please select the best sentiment value:",
  116. 'selections' =>
  117. [
  118. { 'key' => "happy", 'text' => "Happy" },
  119. { 'key' => "sad", 'text' => "Sad" },
  120. { 'key' => "neutral", 'text' => "Neutral" }
  121. ]
  122. },
  123. {
  124. 'type' => "free_text",
  125. 'key' => "feedback",
  126. 'name' => "Have any feedback for us?",
  127. 'required' => "false",
  128. 'question' => "Feedback",
  129. 'default' => "Type here...",
  130. 'min_length' => "2",
  131. 'max_length' => "2000"
  132. }
  133. ]
  134. }
  135. }
  136. @agent = Agents::HumanTaskAgent.new(:name => "somename", :options => valid_params)
  137. @agent.user = users(:jane)
  138. LiquidMigrator.convert_all_agent_options(@agent)
  139. @agent.reload.options['hit']['description'].should == "Please rate the sentiment of this message: '{{message}}'"
  140. end
  141. end
  142. end