key_value_store_agent_spec.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. require "rails_helper"
  2. describe Agents::KeyValueStoreAgent do
  3. let(:value_template) { "{{ _event_ | as_object }}" }
  4. let(:agent) do
  5. Agents::KeyValueStoreAgent.create!(
  6. name: "somename",
  7. options: {
  8. key: "{{ id }}",
  9. value: value_template,
  10. variable: "kvs",
  11. max_keys: 3,
  12. },
  13. user: users(:jane)
  14. )
  15. end
  16. let(:source_agent) do
  17. agents(:jane_weather_agent)
  18. end
  19. def create_event(payload)
  20. source_agent.events.create!(payload:)
  21. end
  22. let(:events) do
  23. [
  24. create_event({ id: 1, name: "foo" }),
  25. create_event({ id: 2, name: "bar" }),
  26. create_event({ id: 3, name: "baz" }),
  27. create_event({ id: 1, name: "FOO" }),
  28. create_event({ id: 4, name: "quux" }),
  29. ]
  30. end
  31. describe "validation" do
  32. before do
  33. expect(agent).to be_valid
  34. end
  35. it "should validate key" do
  36. # empty key is OK
  37. agent.options[:key] = ""
  38. expect(agent).to be_valid
  39. agent.options.delete(:key)
  40. expect(agent).not_to be_valid
  41. end
  42. it "should validate value" do
  43. agent.options[:value] = ""
  44. expect(agent).to be_valid
  45. agent.options.delete(:value)
  46. expect(agent).not_to be_valid
  47. end
  48. it "should validate variable" do
  49. agent.options[:variable] = "1abc"
  50. expect(agent).not_to be_valid
  51. agent.options[:variable] = ""
  52. expect(agent).not_to be_valid
  53. agent.options[:variable] = {}
  54. expect(agent).not_to be_valid
  55. agent.options.delete(:variable)
  56. expect(agent).not_to be_valid
  57. end
  58. it "should validate max_keys" do
  59. agent.options.delete(:max_keys)
  60. expect(agent).to be_valid
  61. expect(agent.max_keys).to eq 100
  62. agent.options[:max_keys] = 0
  63. expect(agent).not_to be_valid
  64. end
  65. end
  66. describe "#receive" do
  67. it "receives and updates the storage" do
  68. agent.receive(events[0..2])
  69. expect(agent.reload.memory).to match(
  70. {
  71. "1" => { id: 1, name: "foo" },
  72. "2" => { id: 2, name: "bar" },
  73. "3" => { id: 3, name: "baz" },
  74. }
  75. )
  76. agent.receive([events[3]])
  77. expect(agent.reload.memory).to match(
  78. {
  79. "1" => { id: 1, name: "FOO" },
  80. "2" => { id: 2, name: "bar" },
  81. "3" => { id: 3, name: "baz" },
  82. }
  83. )
  84. agent.receive([events[4]])
  85. # The key "bar" should have been removed because it is the oldest.
  86. expect(agent.reload.memory).to match(
  87. {
  88. "1" => { id: 1, name: "FOO" },
  89. "3" => { id: 3, name: "baz" },
  90. "4" => { id: 4, name: "quux" },
  91. }
  92. )
  93. expect {
  94. agent.receive([create_event({ name: "empty key" })])
  95. }.not_to(change { agent.reload.memory })
  96. end
  97. describe "empty value" do
  98. let(:value_template) { "{{ name | as_object }}" }
  99. it "deletes the key" do
  100. agent.receive(events[0..2])
  101. expect(agent.reload.memory).to match(
  102. {
  103. "1" => "foo",
  104. "2" => "bar",
  105. "3" => "baz",
  106. }
  107. )
  108. agent.receive([create_event({ id: 1, name: "" })])
  109. expect(agent.reload.memory).to match(
  110. {
  111. "2" => "bar",
  112. "3" => "baz",
  113. }
  114. )
  115. agent.receive([create_event({ id: 2, name: [] })])
  116. expect(agent.reload.memory).to match(
  117. {
  118. "3" => "baz",
  119. }
  120. )
  121. agent.receive([create_event({ id: 3, name: {} })])
  122. expect(agent.reload.memory).to eq({})
  123. end
  124. end
  125. describe "using _value_" do
  126. let(:value_template) { "{% if _value_ %}{{ _value_ }}, {% endif %}{{ name }}" }
  127. it "represents the existing value" do
  128. agent.receive(events[0..2])
  129. expect(agent.reload.memory).to match(
  130. {
  131. "1" => "foo",
  132. "2" => "bar",
  133. "3" => "baz",
  134. }
  135. )
  136. agent.receive([events[3]])
  137. expect(agent.reload.memory).to match(
  138. {
  139. "1" => "foo, FOO",
  140. "2" => "bar",
  141. "3" => "baz",
  142. }
  143. )
  144. end
  145. end
  146. end
  147. describe "control target" do
  148. let(:value_template) { "{{ name }}" }
  149. before do
  150. agent.receive(events[0..2])
  151. end
  152. let!(:target_agent) do
  153. agents(:jane_website_agent).tap { |target_agent|
  154. target_agent.options[:url] = "https://example.com/{{ kvs['3'] }}"
  155. target_agent.controllers << agent
  156. target_agent.save!
  157. }
  158. end
  159. it "can refer to the storage" do
  160. expect(target_agent.interpolated[:url]).to eq "https://example.com/baz"
  161. end
  162. end
  163. end