Skip to content

Commit

Permalink
#70 last touch and rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
bshevchenko committed Sep 12, 2019
1 parent 5e48367 commit 2510186
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 63 deletions.
1 change: 1 addition & 0 deletions app/graphql/resolvers/watched_transaction_resolver.rb
Expand Up @@ -12,6 +12,7 @@ def resolve(txhash:)
unless (tx = WatchingTransaction.find_by(txhash: txhash))
return nil
end

WatchingTransaction.where(group_id: tx.group_id).order('created_at ASC').last
end

Expand Down
15 changes: 7 additions & 8 deletions app/graphql/types/scalar/json_object.rb
Expand Up @@ -8,15 +8,14 @@ class JSONObject < Types::Base::BaseScalar
EOS

def self.coerce_input(input, _context)
begin
data = JSON.parse(input)
unless data.is_a?(Hash)
raise GraphQL::CoercionError, "#{input.inspect} is not a JSON object"
end
data
rescue JSON::ParserError
raise GraphQL::CoercionError, "#{input.inspect} is not a valid JSON"
data = JSON.parse(input)
unless data.is_a?(Hash)
raise GraphQL::CoercionError, "#{input.inspect} is not a JSON object"
end

data
rescue JSON::ParserError
raise GraphQL::CoercionError, "#{input.inspect} is not a valid JSON"
end

def self.coerce_result(value, _context)
Expand Down
2 changes: 1 addition & 1 deletion app/models/watching_transaction.rb
Expand Up @@ -75,7 +75,7 @@ def resend_transactions
WatchingTransaction.order('created_at ASC').each do |tx|
ok_tx, data = EthereumApi.get_transaction_by_hash(tx.txhash)
if ok_tx == :error
Rails.logger.info "Failed to get transaction by hash. Killing job.."
Rails.logger.info 'Failed to get transaction by hash. Killing job..'
break
end
unless data
Expand Down
12 changes: 10 additions & 2 deletions test/graphql/watched_transaction_query_test.rb
Expand Up @@ -7,14 +7,22 @@ class WatchedTransactionQueryTest < ActiveSupport::TestCase
query($txhash: String!) {
watchedTransaction(txhash: $txhash) {
id
user {
displayName
}
transactionObject
}
}
EOS

test 'watched transaction should work' do
first_transaction = create(:watching_transaction)
sleep 1
last_transaction = create(:watching_transaction, group_id: first_transaction.group_id, user: first_transaction.user)
last_transaction = create(
:watching_transaction,
group_id: first_transaction.group_id,
user: first_transaction.user,
created_at: Date.current + 1
)
result = DaoServerSchema.execute(
QUERY,
context: { current_user: first_transaction.user },
Expand Down
102 changes: 50 additions & 52 deletions test/models/watching_transaction_test.rb
Expand Up @@ -36,14 +36,14 @@ class WatchingTransactionTest < ActiveSupport::TestCase
end

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

WatchingTransaction.resend_transactions

Expand All @@ -61,24 +61,24 @@ class WatchingTransactionTest < ActiveSupport::TestCase
end

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

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

WatchingTransaction.resend_transactions

Expand All @@ -95,25 +95,24 @@ class WatchingTransactionTest < ActiveSupport::TestCase
size.times do
FactoryBot.create(:watching_transaction, group_id: group_id, user: user)
end
sleep 1 # UUID is not sortable, so some delay is needed in order to properly sort via created_at
latest = FactoryBot.create(:watching_transaction, group_id: group_id, user: user)
latest = FactoryBot.create(:watching_transaction, group_id: group_id, user: user, created_at: Date.current + 1)

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

new_txhash = generate(:txhash)
send_transaction_stub = stub_request(:post, EthereumApi::SERVER_URL)
.with(body: /eth_sendRawTransaction/)
.to_return(
body: {
result: new_txhash
}.to_json
)
.with(body: /eth_sendRawTransaction/)
.to_return(
body: {
result: new_txhash
}.to_json
)

WatchingTransaction.resend_transactions

Expand All @@ -127,7 +126,7 @@ class WatchingTransactionTest < ActiveSupport::TestCase
'should update txhash for resent transaction'
end

test 'resend transactions should keep all if API is unavailable' do
test 'resend transactions should keep all if get transaction fails' do
user = create(:user)
group_id = SecureRandom.uuid
size = SecureRandom.rand(1..6)
Expand All @@ -136,8 +135,8 @@ class WatchingTransactionTest < ActiveSupport::TestCase
end

get_failed_stub = stub_request(:post, EthereumApi::SERVER_URL)
.with(body: /eth_getTransactionByHash/)
.to_return(status: [500, 'Internal Server Error'])
.with(body: /eth_getTransactionByHash/)
.to_return(status: [500, 'Internal Server Error'])

WatchingTransaction.resend_transactions

Expand All @@ -146,27 +145,26 @@ class WatchingTransactionTest < ActiveSupport::TestCase
'should work'
end

test 'resend transactions should destroy dropped and keep latest as it is if eth_sendRawTransaction has failed' do
test 'resend transactions should keep latest if send transaction fails' do
user = create(:user)
group_id = SecureRandom.uuid
size = SecureRandom.rand(1..6)
size.times do
FactoryBot.create(:watching_transaction, group_id: group_id, user: user)
end
sleep 1 # UUID is not sortable, so some delay is needed in order to properly sort via created_at
latest = FactoryBot.create(:watching_transaction, group_id: group_id, user: user)
latest = FactoryBot.create(:watching_transaction, group_id: group_id, user: user, created_at: Date.current + 1)

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

send_transaction_failed_stub = stub_request(:post, EthereumApi::SERVER_URL)
.with(body: /eth_sendRawTransaction/)
.to_return(status: [500, 'Internal Server Error'])
.with(body: /eth_sendRawTransaction/)
.to_return(status: [500, 'Internal Server Error'])

WatchingTransaction.resend_transactions

Expand Down

0 comments on commit 2510186

Please sign in to comment.