pairwise_content_test.rb
9.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
require "test_helper"
require "#{Rails.root}/plugins/pairwise/test/fixtures/pairwise_content_fixtures"
require "#{Rails.root}/plugins/pairwise/test/fixtures/http_stub_fixtures"
# require 'vcr'
# VCR.configure do |c|
# c.cassette_library_dir = "#{Rails.root}/plugins/pairwise/test/fixtures/vcr_cassettes"
# c.hook_into :webmock
# c.before_playback do |i|
# puts "I in PLAYBACK: #{i.inspect}"
# end
# end
class PairwisePlugin::PairwiseContentTest < ActiveSupport::TestCase
fixtures :environments
def setup
pairwise_env_settings = { :api_host => "http://localhost:3030/",
:username => "abner.oliveira@serpro.gov.br",
:password => "serpro"
}
@profile = create_user('testing').person
@profile.environment = environments(:colivre_net)
@pairwise_client = Pairwise::Client.build(1, pairwise_env_settings)
@pairwise_content = PairwiseContentFixtures.pairwise_content
@pairwise_content.profile = @profile
#PairwisePlugin::PairwiseContent.any_instance.stubs(:send_question_to_service).returns(true)
#PairwisePlugin::PairwiseContent.any_instance.stubs(:pairwise_client).returns(@pairwise_client)
@http_stub_fixtures = HttpStubFixtures.new(pairwise_env_settings)
end
should 'be inactive when created' do
assert_equal false, @pairwise_content.published?
end
should 'get question from stubed api call' do
question = @http_stub_fixtures.create_question(2, 'Question 2', 'Choice X\nChoice Y')
assert_not_nil question
assert_equal 2, question.id
assert_equal 'Question 2', question.name
end
should 'provide proper short description' do
assert_equal 'Pairwise question', PairwisePlugin::PairwiseContent.short_description
end
should 'provide proper description' do
assert_equal 'Question managed by pairwise', PairwisePlugin::PairwiseContent.description
end
should 'have an html view' do
assert_not_nil @pairwise_content.to_html
end
should 'have result_url' do
assert_not_nil @pairwise_content.result_url
assert_equal @pairwise_content.profile.identifier, @pairwise_content.result_url[:profile]
assert_equal :pairwise_plugin_profile, @pairwise_content.result_url[:controller]
assert_equal :result, @pairwise_content.result_url[:action]
end
should 'get question from pairwise service' do
@question = Pairwise::Question.new(:id => @pairwise_content.pairwise_question_id, :name => 'Question 1')
@pairwise_content.expects(:pairwise_client).returns(@pairwise_client)
@pairwise_client.expects(:find_question_by_id).with(@question.id).returns(@question)
assert_equal @question, @pairwise_content.question
end
should 'prepare prompt' do
@question = Pairwise::Question.new(:id => @pairwise_content.pairwise_question_id, :name => 'Question 1')
@pairwise_content.expects(:pairwise_client).returns(@pairwise_client).at_least_once
@pairwise_client.expects(:question_with_prompt).with(@question.id,'any_user', nil).returns(@question)
prompt = @pairwise_content.prepare_prompt('any_user')
assert_not_nil prompt
end
should 'add error to base when the question does not exist' do
Response = Struct.new(:code, :message)
@response = Response.new(422, "Any error")
@pairwise_client.expects(:find_question_by_id).with(@pairwise_content.pairwise_question_id).raises(ActiveResource::ResourceNotFound.new(@response))
@pairwise_content.expects(:pairwise_client).returns(@pairwise_client)
assert @pairwise_content.errors[:base].blank?
@pairwise_content.question
assert !@pairwise_content.errors[:base].blank?
assert_match /Any error/, @pairwise_content.errors[:base].first
assert_match /422/, @pairwise_content.errors[:base].first
end
should 'send question to pairwise service' do
question = Pairwise::Question.new(:id => 3, :name => 'Question 1')
#expectations
pairwise_content = PairwiseContentFixtures.new_pairwise_content
pairwise_content.profile = @profile
pairwise_content.expects(:valid?).returns(true)
pairwise_content.expects(:create_pairwise_question).returns(question)
pairwise_content.expects(:toggle_autoactivate_ideas).at_least_once
#save should call before_save which sends the question to pairwise
pairwise_content.save!
#after save pairwise_question_id should store question id generated by pairwise
assert_equal question.id, pairwise_content.pairwise_question_id
end
should 'send changes in choices to pairwise service' do
@question = Pairwise::Question.new(:id => @pairwise_content.pairwise_question_id, :name => 'Question 1', :active => false)
@pairwise_content.expects(:question).returns(@question).at_least_once
@pairwise_content.expects(:pairwise_client).returns(@pairwise_client).at_least_once
@pairwise_content.expects('new_record?').returns(false).at_least_once
@pairwise_content.expects('valid?').returns(true).at_least_once
@pairwise_content.choices = []
@pairwise_content.choices_saved = {'1' => 'Choice 1', '2' => 'Choice 2'}
#save should call update_choice in pairwise_client for each choice already saved
@pairwise_client.expects(:update_choice).returns(true).times(2)
@pairwise_content.save
end
should 'send new choices to pairwise_service' do
@question = Pairwise::Question.new(:id => @pairwise_content.pairwise_question_id, :name => 'Question 1', :active => false)
@pairwise_content.expects('new_record?').returns(false).at_least_once
@pairwise_content.expects('valid?').returns(true).at_least_once
@pairwise_content.expects(:pairwise_client).returns(@pairwise_client).at_least_once
@pairwise_content.expects(:question).returns(@question).at_least_once
@pairwise_content.choices = ['New Choice 1', 'New Choice 2']
@pairwise_content.choices_saved = []
@pairwise_client.expects(:approve_choice).returns(true).at_least_once
choice_stub = Pairwise::Choice.new(:id=> 1, :data => 'txt')
@pairwise_client.expects(:add_choice).with(@pairwise_content.pairwise_question_id, "New Choice 1").returns(choice_stub)
@pairwise_client.expects(:add_choice).with(@pairwise_content.pairwise_question_id, "New Choice 2").returns(choice_stub)
@pairwise_client.expects(:update_question).with(@question.id, @question.name).returns(true)
@pairwise_content.save
puts @pairwise_content.errors.full_messages
end
should 'allow new ideas by default when created' do
assert_equal true, @pairwise_content.allow_new_ideas?
end
should 'add new ideas suggestions when new ideas are allowed' do
assert_equal true, @pairwise_content.allow_new_ideas?
@question = Pairwise::Question.new(:id => @pairwise_content.pairwise_question_id, :name => 'Question 1', :active => false)
@pairwise_content.expects(:pairwise_client).returns(@pairwise_client).at_least_once
@pairwise_client.expects(:add_new_idea).with(@question.id, "New idea", nil).returns(true)
assert_equal true, @pairwise_content.add_new_idea("New idea")
end
should 'not add new ideas suggestions when new ideas are not allowed' do
assert_equal true, @pairwise_content.allow_new_ideas?
@question = Pairwise::Question.new(:id => @pairwise_content.pairwise_question_id, :name => 'Question 1', :active => false)
@pairwise_content.allow_new_ideas = false
assert_equal false, @pairwise_content.add_new_idea("New idea")
end
should 'join similar choices' do
pairwise_content = PairwiseContentFixtures.content_stub_with_3_choices
assert_equal 3, pairwise_content.question.choices.size
choices_to_join = pairwise_content.question.choices[1..2].map { |choice| choice.id }
parent_choice = pairwise_content.question.choices[0].id
pairwise_content.profile = @profile
pairwise_content.stubs(:valid? => true)
pairwise_content.stubs(:send_question_to_service => true)
pairwise_content.join_choices(choices_to_join, parent_choice, user=nil)
choices_related = PairwisePlugin::ChoicesRelated.related_choices_for(parent_choice)
assert_equal 2, choices_related.size
assert_equal 1, choices_related.select { |c| c.choice_id == 2}.size
assert_equal 1, choices_related.select { |c| c.choice_id == 3 }.size
end
# should 'skip prompt' do
# end
should 'ask skip prompt reasons' do
prompt = Pairwise::Prompt.new({"left_choice_text"=>"Choice 1", "right_choice_text"=>"New inactive choice", "left_choice_id"=>1300, "right_choice_id"=>1302, "id"=>194, "tracking"=>nil, "votes_count"=>0})
reasons = @pairwise_content.ask_skip_reasons(prompt)
assert_not_nil reasons
assert_equal 7, reasons.size
assert reasons[0].include? PairwisePlugin::PairwiseContent::REASONS_ARRAY[0][:text]
assert reasons[1].include? PairwisePlugin::PairwiseContent::REASONS_ARRAY[1][:text]
assert reasons[2].include? PairwisePlugin::PairwiseContent::REASONS_ARRAY[2][:text]
assert reasons[3].include? PairwisePlugin::PairwiseContent::REASONS_ARRAY[3][:text]
assert reasons[4].include? PairwisePlugin::PairwiseContent::REASONS_ARRAY[4][:text]
assert reasons[5].include? PairwisePlugin::PairwiseContent::REASONS_ARRAY[4][:text]
assert reasons[6].include? PairwisePlugin::PairwiseContent::REASONS_ARRAY[5][:text]
end
end