liquid_migrator_spec.rb 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 an actual agent" do
  53. before do
  54. valid_params = {
  55. 'auth_token' => 'token',
  56. 'room_name' => 'test',
  57. 'room_name_path' => '',
  58. 'username' => "Huginn",
  59. 'username_path' => '$.username',
  60. 'message' => "Hello from Huginn!",
  61. 'message_path' => '$.message',
  62. 'notify' => false,
  63. 'notify_path' => '',
  64. 'color' => 'yellow',
  65. 'color_path' => '',
  66. }
  67. @agent = Agents::HipchatAgent.new(:name => "somename", :options => valid_params)
  68. @agent.user = users(:jane)
  69. @agent.save!
  70. end
  71. it "should work" do
  72. LiquidMigrator.convert_all_agent_options(@agent)
  73. @agent.reload.options.should == {"auth_token" => 'token', 'color' => 'yellow', 'notify' => false, 'room_name' => 'test', 'username' => '{{username}}', 'message' => '{{message}}'}
  74. end
  75. it "should work with nested hashes" do
  76. @agent.options['very'] = {'nested' => '$.value'}
  77. LiquidMigrator.convert_all_agent_options(@agent)
  78. @agent.reload.options.should == {"auth_token" => 'token', 'color' => 'yellow', 'very' => {'nested' => '{{value}}'}, 'notify' => false, 'room_name' => 'test', 'username' => '{{username}}', 'message' => '{{message}}'}
  79. end
  80. it "should work with nested arrays" do
  81. @agent.options['array'] = ["one", "$.two"]
  82. LiquidMigrator.convert_all_agent_options(@agent)
  83. @agent.reload.options.should == {"auth_token" => 'token', 'color' => 'yellow', 'array' => ['one', '{{two}}'], 'notify' => false, 'room_name' => 'test', 'username' => '{{username}}', 'message' => '{{message}}'}
  84. end
  85. it "should raise an exception when encountering complex JSONPaths" do
  86. @agent.options['username_path'] = "$.very.complex[*]"
  87. expect { LiquidMigrator.convert_all_agent_options(@agent) }.
  88. to raise_error("JSONPath '$.very.complex[*]' is too complex, please check your migration.")
  89. end
  90. end
  91. end