Fixed activity log bug, added unit tests.

This commit is contained in:
Martin Edenhofer 2014-06-27 00:42:27 +02:00
parent 3d2619672a
commit 67c77ae88d
2 changed files with 56 additions and 27 deletions

View file

@ -42,14 +42,14 @@ add a new activity entry for an object
role_id = role.id role_id = role.id
end end
# check if entry is needed # check newest entry - is needed
result = ActivityStream.where( result = ActivityStream.where(
:o_id => data[:o_id], :o_id => data[:o_id],
# :activity_stream_type_id => type.id, # :activity_stream_type_id => type.id,
:role_id => role_id, :role_id => role_id,
:activity_stream_object_id => object.id, :activity_stream_object_id => object.id,
:created_by_id => data[:created_by_id] :created_by_id => data[:created_by_id]
).order('created_at DESC, id ASC').last ).order('created_at DESC, id DESC').first
# resturn if old entry is really fresh # resturn if old entry is really fresh
return result if result && result.created_at.to_i >= ( data[:created_at].to_i - 12 ) return result if result && result.created_at.to_i >= ( data[:created_at].to_i - 12 )

View file

@ -51,17 +51,28 @@ class ActivityStreamTest < ActiveSupport::TestCase
:priority_id => Ticket::Priority.lookup( :name => '1 low' ).id, :priority_id => Ticket::Priority.lookup( :name => '1 low' ).id,
}, },
}, },
:update2 => {
:ticket => {
:title => 'Unit Test 2 (äöüß) - update!',
:priority_id => Ticket::Priority.lookup( :name => '2 normal' ).id,
},
},
:check => [ :check => [
{ {
:result => true, :result => true,
:object => 'Ticket', :object => 'Ticket',
:type => 'created', :type => 'updated',
}, },
{ {
:result => true, :result => true,
:object => 'Ticket::Article', :object => 'Ticket::Article',
:type => 'created', :type => 'created',
}, },
{
:result => true,
:object => 'Ticket',
:type => 'created',
},
{ {
:result => false, :result => false,
:object => 'User', :object => 'User',
@ -80,8 +91,9 @@ class ActivityStreamTest < ActiveSupport::TestCase
ticket = Ticket.create( test[:create][:ticket] ) ticket = Ticket.create( test[:create][:ticket] )
test[:check][0][:o_id] = ticket.id test[:check][0][:o_id] = ticket.id
test[:check][0][:created_at] = ticket.created_at test[:check][2][:o_id] = ticket.id
test[:check][0][:created_by_id] = current_user.id test[:check][2][:created_at] = ticket.created_at
test[:check][2][:created_by_id] = current_user.id
sleep 2 sleep 2
test[:create][:article][:ticket_id] = ticket.id test[:create][:article][:ticket_id] = ticket.id
@ -98,14 +110,26 @@ class ActivityStreamTest < ActiveSupport::TestCase
ticket.update_attributes( test[:update][:ticket] ) ticket.update_attributes( test[:update][:ticket] )
# check updated user # check updated user
test[:check][2][:o_id] = current_user.id test[:check][3][:o_id] = current_user.id
test[:check][2][:created_at] = ticket.created_at test[:check][3][:created_at] = ticket.created_at
test[:check][2][:created_by_id] = current_user.id test[:check][3][:created_by_id] = current_user.id
end
if test[:update2][:ticket]
ticket = Ticket.find( ticket.id )
ticket.update_attributes( test[:update2][:ticket] )
end end
if test[:update][:article] if test[:update][:article]
article.update_attributes( test[:update][:article] ) article.update_attributes( test[:update][:article] )
end end
sleep 15
if test[:update][:ticket]
ticket.update_attributes( test[:update][:ticket] )
end
if test[:update2][:ticket]
ticket.update_attributes( test[:update2][:ticket] )
end
# remember ticket # remember ticket
tickets.push ticket tickets.push ticket
@ -148,12 +172,12 @@ class ActivityStreamTest < ActiveSupport::TestCase
{ {
:result => true, :result => true,
:object => 'Organization', :object => 'Organization',
:type => 'created', :type => 'updated',
}, },
{ {
:result => true, :result => true,
:object => 'Organization', :object => 'Organization',
:type => 'updated', :type => 'created',
}, },
] ]
}, },
@ -252,7 +276,7 @@ class ActivityStreamTest < ActiveSupport::TestCase
users.push user users.push user
# check activity_stream # check activity_stream
activity_stream_check( admin_user.activity_stream(2), test[:check] ) activity_stream_check( admin_user.activity_stream(3), test[:check] )
} }
# delete tickets # delete tickets
@ -293,12 +317,12 @@ class ActivityStreamTest < ActiveSupport::TestCase
{ {
:result => true, :result => true,
:object => 'User', :object => 'User',
:type => 'created', :type => 'updated',
}, },
{ {
:result => true, :result => true,
:object => 'User', :object => 'User',
:type => 'updated', :type => 'created',
}, },
] ]
}, },
@ -344,25 +368,30 @@ class ActivityStreamTest < ActiveSupport::TestCase
end end
def activity_stream_check( activity_stream_list, checks ) def activity_stream_check( activity_stream_list, checks )
#activity_stream_list = activity_stream_list.reverse
#puts 'AS ' + activity_stream_list.inspect #puts 'AS ' + activity_stream_list.inspect
check_count = 0
checks.each { |check_item| checks.each { |check_item|
check_count += 1
#puts '+++++++++++' #puts '+++++++++++'
#puts check_item.inspect #puts check_item.inspect
match = false check_list = 0
activity_stream_list.each { |item| activity_stream_list.each { |item|
next if match check_list += 1
#puts '--------' next if check_list != check_count
#puts item.inspect # next if match
next if item['object'] != check_item[:object] #puts '--------'
next if item['type'] != check_item[:type] #puts item.inspect
next if item['o_id'] != check_item[:o_id] #puts check_item.inspect
match = true if check_item[:result]
assert_equal( check_item[:object], item['object'] )
assert_equal( check_item[:type], item['type'] )
assert_equal( check_item[:o_id], item['o_id'] )
else
assert_not_equal( check_item[:object], item['object'] )
end
} }
if check_item[:result]
assert( match, "activity stream check not matched! #{ check_item.inspect } not in #{ activity_stream_list.inspect }")
else
assert( !match, "activity stream check matched but should not! #{ check_item.inspect } not in #{ activity_stream_list.inspect }")
end
} }
end end