mirror of
https://github.com/Fishwaldo/huginn.git
synced 2025-03-15 19:31:26 +00:00
fix specs and add support for translation in Scenario exports
This commit is contained in:
parent
5e1730351f
commit
b31a0ec3e7
7 changed files with 182 additions and 76 deletions
|
@ -142,7 +142,7 @@ class ScenarioImport
|
|||
def generate_diff
|
||||
@agent_diffs = (parsed_data['agents'] || []).map.with_index do |agent_data, index|
|
||||
# AgentDiff is defined at the end of this file.
|
||||
agent_diff = AgentDiff.new(agent_data)
|
||||
agent_diff = AgentDiff.new(agent_data, parsed_data['schema_version'])
|
||||
if existing_scenario
|
||||
# If this Agent exists already, update the AgentDiff with the local version's information.
|
||||
agent_diff.diff_with! existing_scenario.agents.find_by(:guid => agent_data['guid'])
|
||||
|
@ -184,14 +184,16 @@ class ScenarioImport
|
|||
end
|
||||
end
|
||||
|
||||
def initialize(agent_data)
|
||||
def initialize(agent_data, schema_version)
|
||||
super()
|
||||
@schema_version = schema_version
|
||||
@requires_merge = false
|
||||
self.agent = nil
|
||||
store! agent_data
|
||||
end
|
||||
|
||||
BASE_FIELDS = %w[name schedule keep_events_for propagate_immediately disabled guid]
|
||||
FIELDS_REQUIRING_TRANSLATION = %w[keep_events_for]
|
||||
|
||||
def agent_exists?
|
||||
!!agent
|
||||
|
@ -209,10 +211,27 @@ class ScenarioImport
|
|||
self.type = FieldDiff.new(agent_data["type"].split("::").pop)
|
||||
self.options = FieldDiff.new(agent_data['options'] || {})
|
||||
BASE_FIELDS.each do |option|
|
||||
self[option] = FieldDiff.new(agent_data[option]) if agent_data.has_key?(option)
|
||||
if agent_data.has_key?(option)
|
||||
value = agent_data[option]
|
||||
value = send(:"translate_#{option}", value) if option.in?(FIELDS_REQUIRING_TRANSLATION)
|
||||
self[option] = FieldDiff.new(value)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def translate_keep_events_for(old_value)
|
||||
if schema_version < 1
|
||||
# Was stored in days, now is stored in seconds.
|
||||
old_value.to_i.days
|
||||
else
|
||||
old_value
|
||||
end
|
||||
end
|
||||
|
||||
def schema_version
|
||||
(@schema_version || 0).to_i
|
||||
end
|
||||
|
||||
def diff_with!(agent)
|
||||
return unless agent.present?
|
||||
|
||||
|
|
142
db/schema.rb
142
db/schema.rb
|
@ -14,36 +14,36 @@
|
|||
ActiveRecord::Schema.define(version: 20150507153436) do
|
||||
|
||||
create_table "agent_logs", force: :cascade do |t|
|
||||
t.integer "agent_id", limit: 4, null: false
|
||||
t.text "message", limit: 65535, null: false, charset: "utf8mb4", collation: "utf8mb4_bin"
|
||||
t.integer "level", limit: 4, default: 3, null: false
|
||||
t.integer "agent_id", limit: 4, null: false
|
||||
t.text "message", limit: 16777215, null: false, charset: "utf8mb4", collation: "utf8mb4_bin"
|
||||
t.integer "level", limit: 4, default: 3, null: false
|
||||
t.integer "inbound_event_id", limit: 4
|
||||
t.integer "outbound_event_id", limit: 4
|
||||
t.datetime "created_at"
|
||||
t.datetime "updated_at"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
end
|
||||
|
||||
create_table "agents", force: :cascade do |t|
|
||||
t.integer "user_id", limit: 4
|
||||
t.text "options", limit: 65535, charset: "utf8mb4", collation: "utf8mb4_bin"
|
||||
t.string "type", limit: 191, collation: "utf8_bin"
|
||||
t.string "name", limit: 191, charset: "utf8mb4", collation: "utf8mb4_bin"
|
||||
t.string "schedule", limit: 191, collation: "utf8_bin"
|
||||
t.text "options", limit: 16777215, charset: "utf8mb4", collation: "utf8mb4_bin"
|
||||
t.string "type", limit: 255, collation: "utf8_bin"
|
||||
t.string "name", limit: 255, charset: "utf8mb4", collation: "utf8mb4_bin"
|
||||
t.string "schedule", limit: 255, collation: "utf8_bin"
|
||||
t.integer "events_count", limit: 4, default: 0, null: false
|
||||
t.datetime "last_check_at"
|
||||
t.datetime "last_receive_at"
|
||||
t.integer "last_checked_event_id", limit: 4
|
||||
t.datetime "created_at"
|
||||
t.datetime "updated_at"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.text "memory", limit: 4294967295, charset: "utf8mb4", collation: "utf8mb4_bin"
|
||||
t.datetime "last_web_request_at"
|
||||
t.integer "keep_events_for", limit: 4, default: 0, null: false
|
||||
t.datetime "last_event_at"
|
||||
t.datetime "last_error_log_at"
|
||||
t.integer "keep_events_for", limit: 4, default: 0, null: false
|
||||
t.boolean "propagate_immediately", limit: 1, default: false, null: false
|
||||
t.boolean "disabled", limit: 1, default: false, null: false
|
||||
t.string "guid", limit: 255, null: false, charset: "ascii", collation: "ascii_bin"
|
||||
t.integer "service_id", limit: 4
|
||||
t.string "guid", limit: 191, null: false
|
||||
end
|
||||
|
||||
add_index "agents", ["guid"], name: "index_agents_on_guid", using: :btree
|
||||
|
@ -51,6 +51,14 @@ ActiveRecord::Schema.define(version: 20150507153436) do
|
|||
add_index "agents", ["type"], name: "index_agents_on_type", using: :btree
|
||||
add_index "agents", ["user_id", "created_at"], name: "index_agents_on_user_id_and_created_at", using: :btree
|
||||
|
||||
create_table "contacts", force: :cascade do |t|
|
||||
t.text "message", limit: 65535, charset: "utf8mb4", collation: "utf8mb4_bin"
|
||||
t.string "name", limit: 255, charset: "utf8mb4", collation: "utf8mb4_bin"
|
||||
t.string "email", limit: 255, collation: "utf8_bin"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
end
|
||||
|
||||
create_table "control_links", force: :cascade do |t|
|
||||
t.integer "controller_id", limit: 4, null: false
|
||||
t.integer "control_target_id", limit: 4, null: false
|
||||
|
@ -64,15 +72,15 @@ ActiveRecord::Schema.define(version: 20150507153436) do
|
|||
create_table "delayed_jobs", force: :cascade do |t|
|
||||
t.integer "priority", limit: 4, default: 0
|
||||
t.integer "attempts", limit: 4, default: 0
|
||||
t.text "handler", limit: 16777215, charset: "utf8mb4", collation: "utf8mb4_bin"
|
||||
t.text "last_error", limit: 65535, charset: "utf8mb4", collation: "utf8mb4_bin"
|
||||
t.text "handler", limit: 16777215, charset: "utf8mb4", collation: "utf8mb4_bin"
|
||||
t.text "last_error", limit: 16777215, charset: "utf8mb4", collation: "utf8mb4_bin"
|
||||
t.datetime "run_at"
|
||||
t.datetime "locked_at"
|
||||
t.datetime "failed_at"
|
||||
t.string "locked_by", limit: 191
|
||||
t.string "queue", limit: 191
|
||||
t.datetime "created_at"
|
||||
t.datetime "updated_at"
|
||||
t.string "locked_by", limit: 255
|
||||
t.string "queue", limit: 255
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
end
|
||||
|
||||
add_index "delayed_jobs", ["priority", "run_at"], name: "delayed_jobs_priority", using: :btree
|
||||
|
@ -80,11 +88,11 @@ ActiveRecord::Schema.define(version: 20150507153436) do
|
|||
create_table "events", force: :cascade do |t|
|
||||
t.integer "user_id", limit: 4
|
||||
t.integer "agent_id", limit: 4
|
||||
t.decimal "lat", precision: 15, scale: 10
|
||||
t.decimal "lng", precision: 15, scale: 10
|
||||
t.text "payload", limit: 16777215, charset: "utf8mb4", collation: "utf8mb4_bin"
|
||||
t.datetime "created_at"
|
||||
t.datetime "updated_at"
|
||||
t.decimal "lat", precision: 15, scale: 10
|
||||
t.decimal "lng", precision: 15, scale: 10
|
||||
t.text "payload", limit: 4294967295, charset: "utf8mb4", collation: "utf8mb4_bin"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.datetime "expires_at"
|
||||
end
|
||||
|
||||
|
@ -95,14 +103,27 @@ ActiveRecord::Schema.define(version: 20150507153436) do
|
|||
create_table "links", force: :cascade do |t|
|
||||
t.integer "source_id", limit: 4
|
||||
t.integer "receiver_id", limit: 4
|
||||
t.datetime "created_at"
|
||||
t.datetime "updated_at"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.integer "event_id_at_creation", limit: 4, default: 0, null: false
|
||||
end
|
||||
|
||||
add_index "links", ["receiver_id", "source_id"], name: "index_links_on_receiver_id_and_source_id", using: :btree
|
||||
add_index "links", ["source_id", "receiver_id"], name: "index_links_on_source_id_and_receiver_id", using: :btree
|
||||
|
||||
create_table "rails_admin_histories", force: :cascade do |t|
|
||||
t.text "message", limit: 65535, charset: "latin1", collation: "latin1_swedish_ci"
|
||||
t.string "username", limit: 255, charset: "latin1", collation: "latin1_swedish_ci"
|
||||
t.integer "item", limit: 4
|
||||
t.string "table", limit: 255, charset: "latin1", collation: "latin1_swedish_ci"
|
||||
t.integer "month", limit: 2
|
||||
t.integer "year", limit: 8
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
end
|
||||
|
||||
add_index "rails_admin_histories", ["item", "table", "month", "year"], name: "index_rails_admin_histories", using: :btree
|
||||
|
||||
create_table "scenario_memberships", force: :cascade do |t|
|
||||
t.integer "agent_id", limit: 4, null: false
|
||||
t.integer "scenario_id", limit: 4, null: false
|
||||
|
@ -114,69 +135,86 @@ ActiveRecord::Schema.define(version: 20150507153436) do
|
|||
add_index "scenario_memberships", ["scenario_id"], name: "index_scenario_memberships_on_scenario_id", using: :btree
|
||||
|
||||
create_table "scenarios", force: :cascade do |t|
|
||||
t.string "name", limit: 191, null: false, charset: "utf8mb4", collation: "utf8mb4_bin"
|
||||
t.string "name", limit: 255, null: false, charset: "utf8mb4", collation: "utf8mb4_bin"
|
||||
t.integer "user_id", limit: 4, null: false
|
||||
t.datetime "created_at"
|
||||
t.datetime "updated_at"
|
||||
t.text "description", limit: 65535, charset: "utf8mb4", collation: "utf8mb4_bin"
|
||||
t.boolean "public", limit: 1, default: false, null: false
|
||||
t.string "guid", limit: 191, null: false, charset: "ascii", collation: "ascii_bin"
|
||||
t.string "source_url", limit: 191
|
||||
t.string "tag_bg_color", limit: 191
|
||||
t.string "tag_fg_color", limit: 191
|
||||
t.string "guid", limit: 255, null: false, charset: "ascii", collation: "ascii_bin"
|
||||
t.string "source_url", limit: 255
|
||||
t.string "tag_bg_color", limit: 255
|
||||
t.string "tag_fg_color", limit: 255
|
||||
end
|
||||
|
||||
add_index "scenarios", ["user_id", "guid"], name: "index_scenarios_on_user_id_and_guid", unique: true, using: :btree
|
||||
|
||||
create_table "services", force: :cascade do |t|
|
||||
t.integer "user_id", limit: 4, null: false
|
||||
t.string "provider", limit: 191, null: false
|
||||
t.string "name", limit: 191, null: false
|
||||
t.text "token", limit: 65535, null: false
|
||||
t.text "secret", limit: 65535
|
||||
t.text "refresh_token", limit: 65535
|
||||
t.string "provider", limit: 255, null: false, charset: "latin1", collation: "latin1_swedish_ci"
|
||||
t.string "name", limit: 255, null: false, charset: "latin1", collation: "latin1_swedish_ci"
|
||||
t.text "token", limit: 65535, null: false, charset: "latin1", collation: "latin1_swedish_ci"
|
||||
t.text "secret", limit: 65535, charset: "latin1", collation: "latin1_swedish_ci"
|
||||
t.text "refresh_token", limit: 65535, charset: "latin1", collation: "latin1_swedish_ci"
|
||||
t.datetime "expires_at"
|
||||
t.boolean "global", limit: 1, default: false
|
||||
t.text "options", limit: 65535
|
||||
t.text "options", limit: 65535, charset: "latin1", collation: "latin1_swedish_ci"
|
||||
t.datetime "created_at"
|
||||
t.datetime "updated_at"
|
||||
t.string "uid", limit: 191
|
||||
t.string "uid", limit: 255, charset: "latin1", collation: "latin1_swedish_ci"
|
||||
end
|
||||
|
||||
add_index "services", ["provider"], name: "index_services_on_provider", using: :btree
|
||||
add_index "services", ["uid"], name: "index_services_on_uid", using: :btree
|
||||
add_index "services", ["user_id", "global"], name: "index_services_on_user_id_and_global", using: :btree
|
||||
|
||||
create_table "taggings", force: :cascade do |t|
|
||||
t.integer "tag_id", limit: 4
|
||||
t.integer "taggable_id", limit: 4
|
||||
t.string "taggable_type", limit: 255, collation: "utf8_general_ci"
|
||||
t.integer "tagger_id", limit: 4
|
||||
t.string "tagger_type", limit: 255, collation: "utf8_general_ci"
|
||||
t.string "context", limit: 128, collation: "utf8_general_ci"
|
||||
t.datetime "created_at"
|
||||
end
|
||||
|
||||
add_index "taggings", ["tag_id"], name: "index_taggings_on_tag_id", using: :btree
|
||||
add_index "taggings", ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context", using: :btree
|
||||
|
||||
create_table "tags", force: :cascade do |t|
|
||||
t.string "name", limit: 255, collation: "utf8_general_ci"
|
||||
end
|
||||
|
||||
create_table "user_credentials", force: :cascade do |t|
|
||||
t.integer "user_id", limit: 4, null: false
|
||||
t.string "credential_name", limit: 191, null: false
|
||||
t.string "credential_name", limit: 255, null: false
|
||||
t.text "credential_value", limit: 65535, null: false
|
||||
t.datetime "created_at"
|
||||
t.datetime "updated_at"
|
||||
t.string "mode", limit: 191, default: "text", null: false, collation: "utf8_bin"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.string "mode", limit: 255, default: "text", null: false, collation: "utf8_bin"
|
||||
end
|
||||
|
||||
add_index "user_credentials", ["user_id", "credential_name"], name: "index_user_credentials_on_user_id_and_credential_name", unique: true, using: :btree
|
||||
|
||||
create_table "users", force: :cascade do |t|
|
||||
t.string "email", limit: 191, default: "", null: false, collation: "utf8_bin"
|
||||
t.string "encrypted_password", limit: 191, default: "", null: false, charset: "ascii", collation: "ascii_bin"
|
||||
t.string "reset_password_token", limit: 191, collation: "utf8_bin"
|
||||
t.string "email", limit: 255, default: "", null: false, collation: "utf8_bin"
|
||||
t.string "encrypted_password", limit: 255, default: "", null: false, charset: "ascii", collation: "ascii_bin"
|
||||
t.string "reset_password_token", limit: 255, collation: "utf8_bin"
|
||||
t.datetime "reset_password_sent_at"
|
||||
t.datetime "remember_created_at"
|
||||
t.integer "sign_in_count", limit: 4, default: 0
|
||||
t.datetime "current_sign_in_at"
|
||||
t.datetime "last_sign_in_at"
|
||||
t.string "current_sign_in_ip", limit: 191
|
||||
t.string "last_sign_in_ip", limit: 191
|
||||
t.datetime "created_at"
|
||||
t.datetime "updated_at"
|
||||
t.string "current_sign_in_ip", limit: 255
|
||||
t.string "last_sign_in_ip", limit: 255
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.boolean "admin", limit: 1, default: false, null: false
|
||||
t.integer "failed_attempts", limit: 4, default: 0
|
||||
t.string "unlock_token", limit: 191
|
||||
t.string "unlock_token", limit: 255
|
||||
t.datetime "locked_at"
|
||||
t.string "username", limit: 191, charset: "utf8mb4", collation: "utf8mb4_unicode_ci"
|
||||
t.string "invitation_code", limit: 191, null: false
|
||||
t.string "username", limit: 191, null: false, charset: "utf8mb4", collation: "utf8mb4_unicode_ci"
|
||||
t.string "invitation_code", limit: 255, null: false, collation: "utf8_bin"
|
||||
t.integer "scenario_count", limit: 4, default: 0, null: false
|
||||
end
|
||||
|
||||
|
|
|
@ -12,6 +12,7 @@ class AgentsExporter
|
|||
|
||||
def as_json(opts = {})
|
||||
{
|
||||
:schema_version => 1,
|
||||
:name => options[:name].presence || 'No name provided',
|
||||
:description => options[:description].presence || 'No description provided',
|
||||
:source_url => options[:source_url],
|
||||
|
|
4
spec/fixtures/agents.yml
vendored
4
spec/fixtures/agents.yml
vendored
|
@ -38,7 +38,7 @@ bob_weather_agent:
|
|||
schedule: "midnight"
|
||||
name: "SF Weather"
|
||||
guid: <%= SecureRandom.hex %>
|
||||
keep_events_for: 45
|
||||
keep_events_for: <%= 45.days %>
|
||||
options: <%= { :location => 94102, :lat => 37.779329, :lng => -122.41915, :api_key => 'test' }.to_json.inspect %>
|
||||
|
||||
jane_weather_agent:
|
||||
|
@ -47,7 +47,7 @@ jane_weather_agent:
|
|||
schedule: "midnight"
|
||||
name: "SF Weather"
|
||||
guid: <%= SecureRandom.hex %>
|
||||
keep_events_for: 30
|
||||
keep_events_for: <%= 30.days %>
|
||||
options: <%= { :location => 94103, :lat => 37.779329, :lng => -122.41915, :api_key => 'test' }.to_json.inspect %>
|
||||
|
||||
jane_rain_notifier_agent:
|
||||
|
|
|
@ -21,6 +21,7 @@ describe AgentsExporter do
|
|||
expect(data[:description]).to eq(description)
|
||||
expect(data[:source_url]).to eq(source_url)
|
||||
expect(data[:guid]).to eq(guid)
|
||||
expect(data[:schema_version]).to eq(1)
|
||||
expect(data[:tag_fg_color]).to eq(tag_fg_color)
|
||||
expect(data[:tag_bg_color]).to eq(tag_bg_color)
|
||||
expect(Time.parse(data[:exported_at])).to be_within(2).of(Time.now.utc)
|
||||
|
|
|
@ -546,11 +546,11 @@ describe Agent do
|
|||
expect(agent).to have(1).errors_on(:keep_events_for)
|
||||
agent.keep_events_for = ""
|
||||
expect(agent).to have(1).errors_on(:keep_events_for)
|
||||
agent.keep_events_for = 5
|
||||
agent.keep_events_for = 5.days.to_i
|
||||
expect(agent).to be_valid
|
||||
agent.keep_events_for = 0
|
||||
expect(agent).to be_valid
|
||||
agent.keep_events_for = 365
|
||||
agent.keep_events_for = 365.days.to_i
|
||||
expect(agent).to be_valid
|
||||
|
||||
# Rails seems to call to_i on the input. This guards against future changes to that behavior.
|
||||
|
@ -564,7 +564,7 @@ describe Agent do
|
|||
@time = "2014-01-01 01:00:00 +00:00"
|
||||
time_travel_to @time do
|
||||
@agent = Agents::SomethingSource.new(:name => "something")
|
||||
@agent.keep_events_for = 5
|
||||
@agent.keep_events_for = 5.days
|
||||
@agent.user = users(:bob)
|
||||
@agent.save!
|
||||
@event = @agent.create_event :payload => { "hello" => "world" }
|
||||
|
@ -580,7 +580,7 @@ describe Agent do
|
|||
@agent.save!
|
||||
|
||||
@agent.options[:foo] = "bar1"
|
||||
@agent.keep_events_for = 5
|
||||
@agent.keep_events_for = 5.days
|
||||
@agent.save!
|
||||
end
|
||||
end
|
||||
|
@ -590,7 +590,7 @@ describe Agent do
|
|||
time_travel_to @time do
|
||||
expect {
|
||||
@agent.options[:foo] = "bar1"
|
||||
@agent.keep_events_for = 3
|
||||
@agent.keep_events_for = 3.days
|
||||
@agent.save!
|
||||
}.to change { @event.reload.expires_at }
|
||||
expect(@event.expires_at.to_i).to be_within(2).of(3.days.from_now.to_i)
|
||||
|
@ -603,7 +603,7 @@ describe Agent do
|
|||
|
||||
expect {
|
||||
@agent.options[:foo] = "bar2"
|
||||
@agent.keep_events_for = 3
|
||||
@agent.keep_events_for = 3.days
|
||||
@agent.save!
|
||||
}.to change { @event.reload.expires_at }
|
||||
expect(@event.expires_at.to_i).to be_within(60 * 61).of(1.days.from_now.to_i) # The larger time is to deal with daylight savings
|
||||
|
@ -635,7 +635,7 @@ describe Agent do
|
|||
@receiver = Agents::CannotBeScheduled.new(
|
||||
name: 'Agent',
|
||||
options: { foo: 'bar3' },
|
||||
keep_events_for: 3,
|
||||
keep_events_for: 3.days,
|
||||
propagate_immediately: true)
|
||||
@receiver.user = users(:bob)
|
||||
@receiver.sources << @sender
|
||||
|
@ -747,7 +747,7 @@ describe Agent do
|
|||
|
||||
it "sets expires_at on created events" do
|
||||
event = agents(:jane_weather_agent).create_event :payload => { 'hi' => 'there' }
|
||||
expect(event.expires_at.to_i).to be_within(5).of(agents(:jane_weather_agent).keep_events_for.days.from_now.to_i)
|
||||
expect(event.expires_at.to_i).to be_within(5).of(agents(:jane_weather_agent).keep_events_for.seconds.from_now.to_i)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -836,7 +836,7 @@ describe AgentDrop do
|
|||
},
|
||||
},
|
||||
schedule: 'every_1h',
|
||||
keep_events_for: 2)
|
||||
keep_events_for: 2.days)
|
||||
@wsa1.user = users(:bob)
|
||||
@wsa1.save!
|
||||
|
||||
|
@ -853,7 +853,7 @@ describe AgentDrop do
|
|||
},
|
||||
},
|
||||
schedule: 'every_12h',
|
||||
keep_events_for: 2)
|
||||
keep_events_for: 2.days)
|
||||
@wsa2.user = users(:bob)
|
||||
@wsa2.save!
|
||||
|
||||
|
@ -868,7 +868,7 @@ describe AgentDrop do
|
|||
matchers: [],
|
||||
skip_created_at: 'false',
|
||||
},
|
||||
keep_events_for: 2,
|
||||
keep_events_for: 2.days,
|
||||
propagate_immediately: true)
|
||||
@efa.user = users(:bob)
|
||||
@efa.sources << @wsa1 << @wsa2
|
||||
|
|
|
@ -30,7 +30,7 @@ describe ScenarioImport do
|
|||
:type => "Agents::WeatherAgent",
|
||||
:name => "a weather agent",
|
||||
:schedule => "5pm",
|
||||
:keep_events_for => 14,
|
||||
:keep_events_for => 14.days,
|
||||
:disabled => true,
|
||||
:guid => "a-weather-agent",
|
||||
:options => weather_agent_options
|
||||
|
@ -61,6 +61,7 @@ describe ScenarioImport do
|
|||
end
|
||||
let(:valid_parsed_data) do
|
||||
{
|
||||
:schema_version => 1,
|
||||
:name => name,
|
||||
:description => description,
|
||||
:guid => guid,
|
||||
|
@ -203,7 +204,7 @@ describe ScenarioImport do
|
|||
|
||||
expect(weather_agent.name).to eq("a weather agent")
|
||||
expect(weather_agent.schedule).to eq("5pm")
|
||||
expect(weather_agent.keep_events_for).to eq(14)
|
||||
expect(weather_agent.keep_events_for).to eq(14.days)
|
||||
expect(weather_agent.propagate_immediately).to be_falsey
|
||||
expect(weather_agent).to be_disabled
|
||||
expect(weather_agent.memory).to be_empty
|
||||
|
@ -226,6 +227,23 @@ describe ScenarioImport do
|
|||
scenario_import.import
|
||||
}.to change { users(:bob).agents.count }.by(2)
|
||||
end
|
||||
|
||||
context "when the schema_version is less than 1" do
|
||||
before do
|
||||
valid_parsed_weather_agent_data[:keep_events_for] = 2
|
||||
valid_parsed_data.delete(:schema_version)
|
||||
end
|
||||
|
||||
it "translates keep_events_for from days to seconds" do
|
||||
scenario_import.import
|
||||
expect(scenario_import.errors).to be_empty
|
||||
weather_agent = scenario_import.scenario.agents.find_by(:guid => "a-weather-agent")
|
||||
trigger_agent = scenario_import.scenario.agents.find_by(:guid => "a-trigger-agent")
|
||||
|
||||
expect(weather_agent.keep_events_for).to eq(2.days)
|
||||
expect(trigger_agent.keep_events_for).to eq(0)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#generate_diff" do
|
||||
|
@ -309,7 +327,7 @@ describe ScenarioImport do
|
|||
|
||||
expect(weather_agent.name).to eq("a weather agent")
|
||||
expect(weather_agent.schedule).to eq("5pm")
|
||||
expect(weather_agent.keep_events_for).to eq(14)
|
||||
expect(weather_agent.keep_events_for).to eq(14.days)
|
||||
expect(weather_agent.propagate_immediately).to be_falsey
|
||||
expect(weather_agent).to be_disabled
|
||||
expect(weather_agent.memory).to be_empty
|
||||
|
@ -330,7 +348,7 @@ describe ScenarioImport do
|
|||
"0" => {
|
||||
"name" => "updated name",
|
||||
"schedule" => "6pm",
|
||||
"keep_events_for" => "2",
|
||||
"keep_events_for" => 2.days.to_i,
|
||||
"disabled" => "false",
|
||||
"options" => weather_agent_options.merge("api_key" => "foo").to_json
|
||||
}
|
||||
|
@ -343,7 +361,7 @@ describe ScenarioImport do
|
|||
weather_agent = existing_scenario.agents.find_by(:guid => "a-weather-agent")
|
||||
expect(weather_agent.name).to eq("updated name")
|
||||
expect(weather_agent.schedule).to eq("6pm")
|
||||
expect(weather_agent.keep_events_for).to eq(2)
|
||||
expect(weather_agent.keep_events_for).to eq(2.days)
|
||||
expect(weather_agent).not_to be_disabled
|
||||
expect(weather_agent.options).to eq(weather_agent_options.merge("api_key" => "foo"))
|
||||
end
|
||||
|
@ -353,7 +371,7 @@ describe ScenarioImport do
|
|||
"0" => {
|
||||
"name" => "",
|
||||
"schedule" => "foo",
|
||||
"keep_events_for" => "2",
|
||||
"keep_events_for" => 2.days.to_i.to_s,
|
||||
"options" => weather_agent_options.merge("api_key" => "").to_json
|
||||
}
|
||||
}
|
||||
|
@ -386,12 +404,40 @@ describe ScenarioImport do
|
|||
end
|
||||
end
|
||||
|
||||
context "when the schema_version is less than 1" do
|
||||
it "translates keep_events_for from days to seconds" do
|
||||
valid_parsed_data.delete(:schema_version)
|
||||
valid_parsed_data[:agents] = [valid_parsed_weather_agent_data.merge(keep_events_for: 5)]
|
||||
|
||||
scenario_import.merges = {
|
||||
"0" => {
|
||||
"name" => "a new name",
|
||||
"schedule" => "6pm",
|
||||
"keep_events_for" => 2.days.to_i.to_s,
|
||||
"disabled" => "true",
|
||||
"options" => weather_agent_options.merge("api_key" => "foo").to_json
|
||||
}
|
||||
}
|
||||
|
||||
expect(scenario_import).to be_valid
|
||||
|
||||
weather_agent_diff = scenario_import.agent_diffs[0]
|
||||
|
||||
expect(weather_agent_diff.name.current).to eq(agents(:bob_weather_agent).name)
|
||||
expect(weather_agent_diff.name.incoming).to eq('a weather agent')
|
||||
expect(weather_agent_diff.name.updated).to eq('a new name')
|
||||
expect(weather_agent_diff.keep_events_for.current).to eq(45.days.to_i)
|
||||
expect(weather_agent_diff.keep_events_for.incoming).to eq(5.days.to_i)
|
||||
expect(weather_agent_diff.keep_events_for.updated).to eq(2.days.to_i.to_s)
|
||||
end
|
||||
end
|
||||
|
||||
it "sets the 'updated' FieldDiff values based on any feedback from the user" do
|
||||
scenario_import.merges = {
|
||||
"0" => {
|
||||
"name" => "a new name",
|
||||
"schedule" => "6pm",
|
||||
"keep_events_for" => "2",
|
||||
"keep_events_for" => 2.days.to_s,
|
||||
"disabled" => "true",
|
||||
"options" => weather_agent_options.merge("api_key" => "foo").to_json
|
||||
},
|
||||
|
@ -411,7 +457,8 @@ describe ScenarioImport do
|
|||
expect(weather_agent_diff.name.updated).to eq("a new name")
|
||||
|
||||
expect(weather_agent_diff.schedule.updated).to eq("6pm")
|
||||
expect(weather_agent_diff.keep_events_for.updated).to eq("2")
|
||||
expect(weather_agent_diff.keep_events_for.current).to eq(45.days)
|
||||
expect(weather_agent_diff.keep_events_for.updated).to eq(2.days.to_s)
|
||||
expect(weather_agent_diff.disabled.updated).to eq("true")
|
||||
expect(weather_agent_diff.options.updated).to eq(weather_agent_options.merge("api_key" => "foo"))
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue