Skip to content

Commit

Permalink
WIP - add test for ledger
Browse files Browse the repository at this point in the history
  • Loading branch information
devton committed Jun 25, 2024
1 parent a72ad15 commit 2decb4b
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 11 deletions.
7 changes: 6 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
require 'bundler/gem_tasks'
require 'minitest/test_task'

Minitest::TestTask.create
Minitest::TestTask.create(:test) do |t|
t.libs << 'test'
t.libs << 'lib'
t.warning = false
t.test_globs = ['./test/**/test_*.rb']
end

require 'rubocop/rake_task'

Expand Down
8 changes: 3 additions & 5 deletions lib/blnk/ledger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ class Ledger < OpenStruct

def self.find(id)
response = new.get_request(path: "/ledgers/#{id}")
return response.parse unless response.status.success?
return response unless response.status.success?

new response.parse
end

def self.all
response = new.get_request(path: '/ledgers')
return response.parse unless response.status.success?
return response unless response.status.success?

response.parse.map do |r|
new r
Expand All @@ -27,9 +27,7 @@ def self.create(*)

def save
response = post_request(path: '/ledgers', body: body_data)
return response.parse unless response.status.success?

# self.class.new response.parse
return response unless response.status.success?

response.parse.each_pair { |k, v| self[k] = v }
self
Expand Down
114 changes: 114 additions & 0 deletions test/blnk/test_ledger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# frozen_string_literal: true

require 'test_helper'

def stub_find_ledger_request_with_error
stub_request(:get, %r{/ledgers/(.*)})
.to_return_json(body: { error: 'ledger with ID \'LEDGER_ID\' not found' }, status: 400)
end

def ledger_response_body
{
ledger_id: 'ldg_f2db7af4-a445-41e3-b233-c052e4283a6b',
name: 'ledger_name',
created_at: '2024-06-15T08:55:03.943273Z',
meta_data: {
'account_id' => '9a521e1d-6f76-4260-b35f-6180a64e0331',
'org_adm' => 'Lorem Ipsum dolor',
'org_slug' => 'lorem-ipsum'
}
}
end

def stub_find_ledger_request_with_success
stub_request(:get, %r{/ledgers/(.*)})
.to_return_json(body: ledger_response_body, status: 200)
end

def stub_create_ledger_request_with_success
stub_request(:post, %r{/ledgers})
.to_return_json(body: ledger_response_body, status: 200)
end

def stub_create_ledger_request_with_error
stub_request(:post, %r{/ledgers})
.to_return_json(body: { error: 'missing name' }, status: 400)
end

def stub_all_ledger_request_with_error
stub_request(:get, %r{/ledgers})
.to_return_json(body: { error: 'internal_server_error' }, status: 500)
end

def stub_all_ledger_request_with_success
stub_request(:get, %r{/ledgers})
.to_return_json(body: [ledger_response_body], status: 200)
end

class TestLedger < Minitest::Test
def test_that_ledger_not_found
stub_find_ledger_request_with_error
find = Blnk::Ledger.find 'LEDGER_ID'

assert find.status.bad_request?
end

def test_that_ledger_find_success
stub_find_ledger_request_with_success
find = Blnk::Ledger.find 'LEDGER_ID'

assert find.is_a?(Blnk::Ledger)
assert find.ledger_id.eql?(ledger_response_body[:ledger_id])
end

def test_that_ledger_all_error
stub_all_ledger_request_with_error

all = Blnk::Ledger.all

assert !all.status.success?
end

def test_that_ledger_all_success
stub_all_ledger_request_with_success

all = Blnk::Ledger.all

assert all.is_a?(Array)
assert all.first.is_a?(Blnk::Ledger)
assert all.first.ledger_id.eql?(ledger_response_body[:ledger_id])
end

def test_that_ledger_create_errosr
stub_create_ledger_request_with_error

create = Blnk::Ledger.create

assert create.status.bad_request?

create_with_new = Blnk::Ledger.new.save

assert create_with_new.status.bad_request?
end

def test_that_ledger_create_success
stub_create_ledger_request_with_success

create = Blnk::Ledger.create(name: 'ledger_name')

assert create.is_a?(Blnk::Ledger)
assert create.ledger_id.eql?(ledger_response_body[:ledger_id])
assert create.name.eql?(ledger_response_body[:name])
end

def test_that_ledger_create_success_using_new
stub_create_ledger_request_with_success

create_with_new = Blnk::Ledger.new name: 'ledger_name'
create_with_new.save

assert create_with_new.is_a?(Blnk::Ledger)
assert create_with_new.ledger_id.eql?(ledger_response_body[:ledger_id])
assert create_with_new.name.eql?(ledger_response_body[:name])
end
end
4 changes: 0 additions & 4 deletions test/test_blnk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,4 @@ class TestBlnk < Minitest::Test
def test_that_it_has_a_version_number
refute_nil ::Blnk::VERSION
end

def test_it_does_something_useful
assert false
end
end
5 changes: 4 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

$LOAD_PATH.unshift File.expand_path('../lib', __dir__)
require 'blnk'

require 'webmock/minitest'
require 'minitest/autorun'
require 'minitest/reporters'
Minitest::Reporters.use! [Minitest::Reporters::DefaultReporter.new(color: true)]

Blnk.address = 'http://localhost:5001'
Blnk.secret_token = 'secret_token'

0 comments on commit 2decb4b

Please sign in to comment.