agents_exporter_spec.rb 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # encoding: utf-8
  2. require 'spec_helper'
  3. describe AgentsExporter do
  4. describe "#as_json" do
  5. let(:name) { "My set of Agents" }
  6. let(:description) { "These Agents work together nicely!" }
  7. let(:guid) { "some-guid" }
  8. let(:source_url) { "http://yourhuginn.com/scenarios/2/export.json" }
  9. let(:agent_list) { [agents(:jane_weather_agent), agents(:jane_rain_notifier_agent)] }
  10. let(:exporter) { AgentsExporter.new(:agents => agent_list, :name => name, :description => description, :source_url => source_url, :guid => guid) }
  11. it "outputs a structure containing name, description, the date, all agents & their links" do
  12. data = exporter.as_json
  13. data[:name].should == name
  14. data[:description].should == description
  15. data[:source_url].should == source_url
  16. data[:guid].should == guid
  17. Time.parse(data[:exported_at]).should be_within(2).of(Time.now.utc)
  18. data[:links].should == [{ :source => 0, :receiver => 1 }]
  19. data[:agents].should == agent_list.map { |agent| exporter.agent_as_json(agent) }
  20. data[:agents].all? { |agent_json| agent_json[:guid].present? && agent_json[:type].present? && agent_json[:name].present? }.should be_true
  21. data[:agents][0].should_not have_key(:propagate_immediately) # can't receive events
  22. data[:agents][1].should_not have_key(:schedule) # can't be scheduled
  23. end
  24. it "does not output links to other agents outside of the incoming set" do
  25. Link.create!(:source_id => agents(:jane_weather_agent).id, :receiver_id => agents(:jane_website_agent).id)
  26. Link.create!(:source_id => agents(:jane_website_agent).id, :receiver_id => agents(:jane_rain_notifier_agent).id)
  27. exporter.as_json[:links].should == [{ :source => 0, :receiver => 1 }]
  28. end
  29. end
  30. describe "#filename" do
  31. it "strips special characters" do
  32. AgentsExporter.new(:name => "ƏfooƐƕƺbar").filename.should == "foo-bar.json"
  33. end
  34. it "strips punctuation" do
  35. AgentsExporter.new(:name => "foo,bar").filename.should == "foo-bar.json"
  36. end
  37. it "strips leading and trailing dashes" do
  38. AgentsExporter.new(:name => ",foo,").filename.should == "foo.json"
  39. end
  40. it "has a default when options[:name] is nil" do
  41. AgentsExporter.new(:name => nil).filename.should == "exported-agents.json"
  42. end
  43. it "has a default when the result is empty" do
  44. AgentsExporter.new(:name => "").filename.should == "exported-agents.json"
  45. AgentsExporter.new(:name => "Ə").filename.should == "exported-agents.json"
  46. AgentsExporter.new(:name => "-").filename.should == "exported-agents.json"
  47. AgentsExporter.new(:name => ",,").filename.should == "exported-agents.json"
  48. end
  49. end
  50. end