1
0

jq_agent_spec.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. require 'rails_helper'
  2. describe Agents::JqAgent do
  3. def create_event(payload)
  4. agents(:jane_weather_agent).events.create!(payload: payload)
  5. end
  6. let!(:agent) {
  7. Agents::JqAgent.create!(
  8. name: 'somename',
  9. options: {
  10. filter: '.+{"total": .numbers | add} | del(.numbers)'
  11. },
  12. user: users(:jane)
  13. )
  14. }
  15. describe '.should_run?' do
  16. it 'should be true' do
  17. expect(Agents::JqAgent).to be_should_run
  18. end
  19. context 'when not enabled' do
  20. before do
  21. allow(ENV).to receive(:[]) { nil }
  22. allow(ENV).to receive(:[]).with('USE_JQ') { nil }
  23. end
  24. it 'should be false' do
  25. expect(Agents::JqAgent).not_to be_should_run
  26. end
  27. end
  28. context 'when jq command is not available' do
  29. before do
  30. allow(Agents::JqAgent).to receive(:jq_version) { nil }
  31. end
  32. it 'should be false' do
  33. expect(Agents::JqAgent).not_to be_should_run
  34. end
  35. end
  36. end
  37. describe 'validation' do
  38. before do
  39. expect(agent).to be_valid
  40. end
  41. it 'should validate filter' do
  42. agent.options.delete(:filter)
  43. expect(agent).not_to be_valid
  44. agent.options[:filter] = [1]
  45. expect(agent).not_to be_valid
  46. # An empty expression is OK
  47. agent.options[:filter] = ''
  48. expect(agent).to be_valid
  49. end
  50. it 'should validate variables' do
  51. agent.options[:variables] = []
  52. expect(agent).not_to be_valid
  53. agent.options[:variables] = ''
  54. expect(agent).not_to be_valid
  55. agent.options[:variables] = { 'x' => [1, 2, 3] }
  56. expect(agent).to be_valid
  57. end
  58. end
  59. describe '#receive' do
  60. let!(:event) { create_event({ name: 'foo', numbers: [1, 2, 3, 4] }) }
  61. it 'should filter an event and create a single event if the result is an object' do
  62. expect {
  63. agent.receive([event])
  64. }.to change(Event, :count).by(1)
  65. created_event = agent.events.last
  66. expect(created_event.payload).to eq({
  67. 'name' => 'foo',
  68. 'total' => 10
  69. })
  70. end
  71. it 'should filter an event and create no event if the result is an empty array' do
  72. agent.update!(options: { filter: '[]' })
  73. expect {
  74. agent.receive([event])
  75. }.not_to change(Event, :count)
  76. end
  77. it 'should filter an event and create no event if the result is a scalar value' do
  78. agent.update!(options: { filter: '.numbers | add' })
  79. expect {
  80. agent.receive([event])
  81. }.not_to change(Event, :count)
  82. end
  83. it 'should filter an event and create no event if the result is an array of scalar values' do
  84. agent.update!(options: { filter: '.numbers' })
  85. expect {
  86. agent.receive([event])
  87. }.not_to change(Event, :count)
  88. end
  89. it 'should filter an event and create multiple events if the result is an array of objects' do
  90. agent.update!(options: { filter: '. as $original | .numbers[] | $original + { "number": . } | del(.numbers)' })
  91. expect {
  92. agent.receive([event])
  93. }.to change(Event, :count).by(4)
  94. created_events = agent.events.limit(4)
  95. expect(created_events.map(&:payload)).to eq([
  96. {
  97. 'name' => 'foo',
  98. 'number' => 4
  99. },
  100. {
  101. 'name' => 'foo',
  102. 'number' => 3
  103. },
  104. {
  105. 'name' => 'foo',
  106. 'number' => 2
  107. },
  108. {
  109. 'name' => 'foo',
  110. 'number' => 1
  111. }
  112. ])
  113. end
  114. it 'should reference passed in variables and filter an event' do
  115. agent.update!(
  116. options: {
  117. filter: '.+{ "extra": $somevar }',
  118. variables: {
  119. somevar: { foo: ['bar', 'baz'] }
  120. }
  121. }
  122. )
  123. expect {
  124. agent.receive([event])
  125. }.to change(Event, :count).by(1)
  126. created_event = agent.events.last
  127. expect(created_event.payload).to eq({
  128. 'name' => 'foo',
  129. 'numbers' => [1, 2, 3, 4],
  130. 'extra' => { 'foo' => ['bar', 'baz'] }
  131. })
  132. end
  133. end
  134. end