Skip to content

Commit

Permalink
DigixGlobal#70 WatchingTransaction model tests improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
bshevchenko committed Sep 11, 2019
1 parent cc7c332 commit 57f8a80
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 37 deletions.
4 changes: 2 additions & 2 deletions app/models/watching_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class WatchingTransaction < ApplicationRecord
uniqueness: true

def set_uuid
self.id = SecureRandom.uuid
self.group_id = id unless group_id
self.id ||= SecureRandom.uuid
self.group_id ||= id
end

def transaction_object
Expand Down
98 changes: 63 additions & 35 deletions test/models/watching_transaction_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class WatchingTransactionTest < ActiveSupport::TestCase

assert_equal :ok, ok,
'should work'
assert_equal user.id, tx.user.id,
'should have correct user id'
end

test 'resend should work' do
Expand All @@ -36,53 +38,77 @@ class WatchingTransactionTest < ActiveSupport::TestCase

assert_equal :ok, ok,
'should work'
assert_equal watching_transaction.group_id, tx.group_id,
'should have the same group as the previous transaction'
end

test 'resend_transactions' do
original_transaction = FactoryBot.create(:watching_transaction)
group_id = original_transaction.group_id
test 'resend transactions should keep all if none is mined' do
original = FactoryBot.create(:watching_transaction)
4.times do
FactoryBot.create(:watching_transaction, group_id: group_id)
FactoryBot.create(:watching_transaction, group_id: original.group_id, user: original.user)
end
latest_transaction = FactoryBot.create(:watching_transaction, group_id: group_id)

stub_request(:post, EthereumApi::SERVER_URL)
.with(body: /eth_getTransactionByHash/)
.to_return(
body: {
result: {
'block_number' => nil,
}
}.to_json
)
stub_post = stub_request(:post, EthereumApi::SERVER_URL)
.with(body: /eth_getTransactionByHash/)
.to_return(
body: {
result: {
'block_number' => nil,
}
}.to_json
)

WatchingTransaction.resend_transactions

assert_equal 6, WatchingTransaction.where(group_id: group_id).count,
'should keep all if none is mined'
assert_requested(stub_post, times: 5)
assert_equal 5, WatchingTransaction.where(group_id: original.group_id).count,
'should work'
end

stub_request(:post, EthereumApi::SERVER_URL)
.with(body: /eth_getTransactionByHash.*#{latest_transaction.txhash}/)
.to_return(
body: {
result: {
'block_number' => '0x1',
}
}.to_json
)
test 'resend transactions should destroy all if one is mined' do
original = FactoryBot.create(:watching_transaction)
3.times do
FactoryBot.create(:watching_transaction, group_id: original.group_id, user: original.user)
end
latest = FactoryBot.create(:watching_transaction, group_id: original.group_id, user: original.user)

stub_post = stub_request(:post, EthereumApi::SERVER_URL)
.with(body: /eth_getTransactionByHash/)
.to_return(
body: {
result: {
'block_number' => nil,
}
}.to_json
)

stub_post_one = stub_request(:post, EthereumApi::SERVER_URL)
.with(body: /eth_getTransactionByHash.*#{latest.txhash}/)
.to_return(
body: {
result: {
'block_number' => SecureRandom.hex(8),
}
}.to_json
)

WatchingTransaction.resend_transactions

assert_equal 0, WatchingTransaction.where(group_id: group_id).count,
assert_requested(stub_post, times: 5)
assert_requested(stub_post_one, times: 1)
assert_equal 0, WatchingTransaction.where(group_id: original.group_id).count,
'should destroy all if one is mined'
end

4.times do
FactoryBot.create(:watching_transaction, group_id: group_id)
test 'resend transactions should resend latest if rest are dropped' do
original = FactoryBot.create(:watching_transaction)
2.times do
FactoryBot.create(:watching_transaction, group_id: original.group_id, user: original.user)
end
sleep 1 # UUID is not sortable, so some delay is needed in order to properly sort via created_at
latest_transaction = FactoryBot.create(:watching_transaction, group_id: group_id)
latest = FactoryBot.create(:watching_transaction, group_id: original.group_id, user: original.user)

stub_request(:post, EthereumApi::SERVER_URL)
stub_post = stub_request(:post, EthereumApi::SERVER_URL)
.with(body: /eth_getTransactionByHash/)
.to_return(
body: {
Expand All @@ -91,7 +117,7 @@ class WatchingTransactionTest < ActiveSupport::TestCase
)

new_txhash = generate(:txhash)
stub_request(:post, EthereumApi::SERVER_URL)
stub_post_send = stub_request(:post, EthereumApi::SERVER_URL)
.with(body: /eth_sendRawTransaction/)
.to_return(
body: {
Expand All @@ -101,11 +127,13 @@ class WatchingTransactionTest < ActiveSupport::TestCase

WatchingTransaction.resend_transactions

assert_equal 1, WatchingTransaction.where(group_id: group_id).count,
'should resend latest if rest are dropped'
assert_requested(stub_post, times: 4)
assert_requested(stub_post_send, times: 1)
assert_equal 1, WatchingTransaction.where(group_id: original.group_id).count,
'should work'

resent_transaction = WatchingTransaction.find_by(id: latest_transaction.id)
assert_equal new_txhash, resent_transaction.txhash,
resent = WatchingTransaction.find_by(id: latest.id)
assert_equal new_txhash, resent.txhash,
'should update txhash for resent transaction'
end
end

0 comments on commit 57f8a80

Please sign in to comment.