PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/test/functional/ft_56_filter_attribute.rb

http://github.com/jmettraux/ruote
Ruby | 297 lines | 183 code | 84 blank | 30 comment | 0 complexity | a5c5bf2980a00c18dab0beb046857df1 MD5 | raw file
  1. #
  2. # testing ruote
  3. #
  4. # Tue Feb 8 12:39:35 JST 2011
  5. #
  6. require File.expand_path('../base', __FILE__)
  7. class FtFilterAttributeTest < Test::Unit::TestCase
  8. include FunctionalBase
  9. class AlphaParticipant
  10. include Ruote::LocalParticipant
  11. def consume(workitem)
  12. @context.tracer << 'fields: ' + workitem.fields.keys.sort.join(' ') + "\n"
  13. reply_to_engine(workitem)
  14. end
  15. end
  16. def test_filter_in_variable
  17. pdef = Ruote.define do
  18. set 'v:f' => {
  19. :in => [ { :fields => '/^private_/', :remove => true } ],
  20. :out => [ { :fields => '/^~~.private_/', :merge_to => '.' } ]
  21. }
  22. alpha :filter => 'f'
  23. alpha
  24. end
  25. @dashboard.register :alpha, AlphaParticipant
  26. wfid = @dashboard.launch(
  27. pdef,
  28. 'private_a' => 'x', 'a' => 'y')
  29. @dashboard.wait_for(wfid)
  30. assert_equal(
  31. "fields: a dispatched_at params\n" +
  32. "fields: a dispatched_at params private_a",
  33. @tracer.to_s)
  34. end
  35. def test_filter_restore
  36. pdef = Ruote.define do
  37. set 'v:f' => {
  38. :in => [],
  39. :out => [
  40. { :fields => '/^protected_/', :restore => true },
  41. { :fields => '__result__', :del => true }
  42. ]
  43. }
  44. sequence :filter => 'f' do
  45. bravo
  46. echo '${f:protected_thing}'
  47. end
  48. end
  49. @dashboard.register :bravo do |wi|
  50. wi.fields['protected_thing'] = 'stolen'
  51. wi.fields['other_thing'] = 'stolen'
  52. end
  53. wfid = @dashboard.launch(
  54. pdef,
  55. 'protected_thing' => 'here', 'other_thing' => 'here')
  56. r = @dashboard.wait_for(wfid)
  57. assert_equal(
  58. { 'protected_thing' => 'here', 'other_thing' => 'stolen' },
  59. r['workitem']['fields'])
  60. end
  61. def test_broken_filter_apply
  62. pdef = Ruote.define do
  63. alpha :filter => 'f'
  64. end
  65. @dashboard.register :alpha, Ruote::NoOpParticipant
  66. wfid = @dashboard.launch(pdef)
  67. r = @dashboard.wait_for(wfid)
  68. assert_not_nil r['error']
  69. assert_equal 'ArgumentError', r['error']['class']
  70. end
  71. def test_broken_filter_reply
  72. pdef = Ruote.define do
  73. set 'v:f' => {
  74. :in => [],
  75. :out => 'nada'
  76. }
  77. alpha :filter => 'f'
  78. end
  79. @dashboard.register :alpha, AlphaParticipant
  80. wfid = @dashboard.launch(pdef)
  81. r = @dashboard.wait_for(wfid)
  82. assert_not_nil r['error']
  83. assert_equal 'ArgumentError', r['error']['class']
  84. end
  85. class AaFilterParticipant
  86. def consume(wi)
  87. (wi.fields['seen'] ||= []) << wi.fields['__filter_direction__']
  88. end
  89. end
  90. def test_filter_participant__consume
  91. pdef = Ruote.define do
  92. alpha :filter => 'filter_a'
  93. end
  94. @dashboard.register :alpha, AlphaParticipant
  95. @dashboard.register :filter_a, AaFilterParticipant
  96. wfid = @dashboard.launch(pdef)
  97. r = @dashboard.wait_for(wfid)
  98. assert_nil r['workitem']['fields']['__filter_direction__']
  99. assert_equal %w[ in out ], r['workitem']['fields']['seen']
  100. assert_equal 'fields: dispatched_at params seen', @tracer.to_s
  101. end
  102. class BbFilterParticipant
  103. def filter(fields, direction)
  104. (fields['seen'] ||= []) << direction
  105. fields
  106. end
  107. end
  108. def test_filter_participant__filter
  109. pdef = Ruote.define do
  110. alpha :filter => 'filter_b'
  111. end
  112. @dashboard.register :alpha, AlphaParticipant
  113. @dashboard.register :filter_b, BbFilterParticipant
  114. wfid = @dashboard.launch(pdef)
  115. r = @dashboard.wait_for(wfid)
  116. assert_equal %w[ in out ], r['workitem']['fields']['seen']
  117. assert_equal 'fields: dispatched_at params seen', @tracer.to_s
  118. end
  119. class CcFilterParticipant
  120. def consume(wi)
  121. raise 'something goes horribly wrong'
  122. end
  123. end
  124. def test_filter_participant_with_error
  125. pdef = Ruote.define do
  126. alpha :filter => 'filter_c'
  127. end
  128. @dashboard.register :alpha, AlphaParticipant
  129. @dashboard.register :filter_c, CcFilterParticipant
  130. wfid = @dashboard.launch(pdef)
  131. @dashboard.wait_for(wfid)
  132. assert_equal 1, @dashboard.ps(wfid).errors.size
  133. assert_equal '', @tracer.to_s
  134. end
  135. class DdFilterParticipant
  136. def consume(workitem)
  137. workitem.fields[workitem.participant_name] =
  138. workitem.fields['__filter_direction__']
  139. end
  140. end
  141. def test_filter_participant__in_and_out
  142. pdef = Ruote.define do
  143. alpha :filter => { :in => 'f0', :out => 'f1' }
  144. end
  145. @dashboard.register :alpha, AlphaParticipant
  146. @dashboard.register :f0, DdFilterParticipant
  147. @dashboard.register :f1, DdFilterParticipant
  148. wfid = @dashboard.launch(pdef)
  149. r = @dashboard.wait_for(wfid)
  150. assert_equal({ 'f0' => 'in', 'f1' => 'out' }, r['workitem']['fields'])
  151. assert_equal('fields: dispatched_at f0 params', @tracer.to_s)
  152. end
  153. def test_filter_empty
  154. @dashboard.register '.+' do |workitem|
  155. context.tracer << workitem.participant_name + "\n"
  156. end
  157. pdef = Ruote.define do
  158. alpha :filter => {}
  159. bravo :filter => { 'in' => [] }
  160. charly :filter => { 'out' => [] }
  161. end
  162. wfid = @dashboard.launch(pdef)
  163. r = @dashboard.wait_for(wfid)
  164. assert_equal %w[ alpha bravo charly ], @tracer.to_a
  165. end
  166. def test_cancel_after_error
  167. @dashboard.register :alpha, Ruote::NoOpParticipant
  168. pdef = Ruote.define do
  169. alpha :filter => { :in => [ { :field => 'x', :type => :number } ] }
  170. end
  171. wfid = @dashboard.launch(pdef)
  172. @dashboard.wait_for('error_intercepted')
  173. @dashboard.cancel(wfid)
  174. @dashboard.wait_for('terminated')
  175. assert_nil @dashboard.ps(wfid)
  176. end
  177. def test_cancel_after_error_out
  178. @dashboard.register :alpha, Ruote::NoOpParticipant
  179. pdef = Ruote.define do
  180. alpha :filter => { :out => [ { :field => 'x', :type => :number } ] }
  181. end
  182. wfid = @dashboard.launch(pdef)
  183. @dashboard.wait_for('error_intercepted')
  184. @dashboard.cancel(wfid)
  185. @dashboard.wait_for('terminated')
  186. assert_nil @dashboard.ps(wfid)
  187. end
  188. # def test_filter_record
  189. #
  190. # pdef = Ruote.define do
  191. # set 'v:f' => {
  192. # :in => [ { :fields => 'x', :type => 'number' } ],
  193. # :out => []
  194. # }
  195. # alpha :filter => 'f'
  196. # end
  197. #
  198. # @dashboard.register :alpha, AlphaParticipant
  199. #
  200. # wfid = @dashboard.launch(
  201. # pdef,
  202. # 'x' => 'not a number')
  203. #
  204. # @dashboard.wait_for(wfid)
  205. #
  206. # assert_equal(
  207. # "fields: a dispatched_at params\n" +
  208. # "fields: a dispatched_at params private_a",
  209. # @tracer.to_s)
  210. # end
  211. #
  212. # in the fridge for now
  213. end