Skip to content

Commit

Permalink
DigixGlobal#70 tests for watched_transaction_resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
bshevchenko committed Sep 11, 2019
1 parent e02e227 commit b329457
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/graphql/resolvers/watched_transaction_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class WatchedTransactionResolver < Resolvers::Base

def resolve(txhash:)
unless (tx = WatchingTransaction.find_by(txhash: txhash))
nil
return nil
end
WatchingTransaction.where(group_id: tx.group_id).last
WatchingTransaction.where(group_id: tx.group_id).order('created_at ASC').last
end

def self.authorized?(object, context)
Expand Down
43 changes: 43 additions & 0 deletions test/graphql/watched_transaction_query_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

require 'test_helper'

class UserQueryTest < ActiveSupport::TestCase
QUERY = <<~EOS
query($txhash: String!) {
watchedTransaction(txhash: $txhash) {
id
}
}
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)
result = DaoServerSchema.execute(
QUERY,
context: { current_user: first_transaction.user },
variables: { txhash: first_transaction.txhash }
)

assert_nil result['errors'],
'should work and have no errors'

data = result['data']['watchedTransaction']

assert_not_empty data,
'watchedTransaction type should work'
assert_equal last_transaction.id, data['id'],
'should return last transaction from group'

empty_result = DaoServerSchema.execute(
QUERY,
context: { current_user: first_transaction.user },
variables: { txhash: 'NON_EXISTENT_TXHASH' }
)

assert_nil empty_result['data']['watchedTransaction'],
'data should be empty on invalid txhash'
end
end

0 comments on commit b329457

Please sign in to comment.