Skip to content

Commit

Permalink
Add tests (#9)
Browse files Browse the repository at this point in the history
* Added Minitest

* Added rubocop-minitest

* Test Configuration

* Add Webmock

* Added tests for Resource

* Added tests for BanckAccount

* Added tests for Client

* Improved README configuration example

* Fetch environment variable first before using default value

* Standardize Establishment class name in singular

* Added tests for Establishment

* Added tests for Sale

* Added tests for Transfer

* Added simplecov

* Improved Rubocop workflow job

* Added job to run tests

* Optimized workflow to use less jobs
  • Loading branch information
Pedro Augusto authored Mar 21, 2024
1 parent b981dbb commit 64f6076
Show file tree
Hide file tree
Showing 21 changed files with 457 additions and 26 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ on:
pull_request:

jobs:
build:
rubocop_and_minitest:
runs-on: ubuntu-latest
name: Rubocop
name: Rubocop and Minitest
strategy:
matrix:
ruby:
Expand All @@ -24,4 +24,6 @@ jobs:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- name: Run Rubocop
run: bundle exec rake rubocop
run: bundle exec rubocop --parallel
- name: Run Minitest
run: bundle exec rake test
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
/.ruby-gemset
/*.gem
/.tool-versions
/.idea/
/.idea/
.byebug_history
4 changes: 3 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require: rubocop-rake
require:
- rubocop-rake
- rubocop-minitest

AllCops:
TargetRubyVersion: 3.0
Expand Down
5 changes: 4 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ gemspec
gem "activesupport"
gem "byebug", "~> 11.1"
gem "logger"
gem "minitest", "~> 5.14"
gem "rake", "~> 13.0"
gem "rubocop", "~> 1.21"

gem "rubocop-minitest", "~> 0.34.5", require: false
gem "rubocop-rake", "~> 0.6.0"
gem "simplecov", "~> 0.22.0", require: false
gem "webmock", "~> 3.23"
25 changes: 24 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@ GEM
public_suffix (>= 2.0.2, < 6.0)
ast (2.4.2)
base64 (0.2.0)
bigdecimal (3.1.6)
byebug (11.1.3)
concurrent-ruby (1.1.9)
crack (1.0.0)
bigdecimal
rexml
docile (1.4.0)
domain_name (0.6.20240107)
ffi (1.16.3)
ffi-compiler (1.0.1)
ffi (>= 1.0.0)
rake
hashdiff (1.1.0)
http (5.2.0)
addressable (~> 2.8)
base64 (~> 0.1)
Expand All @@ -41,7 +47,7 @@ GEM
ffi-compiler (~> 1.0)
rake (~> 13.0)
logger (1.4.3)
minitest (5.14.4)
minitest (5.22.3)
parallel (1.24.0)
parser (3.3.0.5)
ast (~> 2.4.1)
Expand All @@ -67,12 +73,25 @@ GEM
rubocop-ast (1.31.0)
parser (>= 3.3.0.4)
prism (>= 0.24.0)
rubocop-minitest (0.34.5)
rubocop (>= 1.39, < 2.0)
rubocop-ast (>= 1.30.0, < 2.0)
rubocop-rake (0.6.0)
rubocop (~> 1.0)
ruby-progressbar (1.13.0)
simplecov (0.22.0)
docile (~> 1.1)
simplecov-html (~> 0.11)
simplecov_json_formatter (~> 0.1)
simplecov-html (0.12.3)
simplecov_json_formatter (0.1.4)
tzinfo (2.0.4)
concurrent-ruby (~> 1.0)
unicode-display_width (2.5.0)
webmock (3.23.0)
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
zeitwerk (2.5.1)

PLATFORMS
Expand All @@ -82,9 +101,13 @@ DEPENDENCIES
activesupport
byebug (~> 11.1)
logger
minitest (~> 5.14)
rake (~> 13.0)
rubocop (~> 1.21)
rubocop-minitest (~> 0.34.5)
rubocop-rake (~> 0.6.0)
simplecov (~> 0.22.0)
webmock (~> 3.23)
zspay!

BUNDLED WITH
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ gem install zspay
Before using the Zspay gem, you must configure it with your Zspay API credentials. This is typically done in an initializer in your Ruby application, such as config/initializers/zspay.rb.
```rb
Zspay.configure do |config|
config.token = 'your_zspay_api_token'
config.token = ENV.fetch('YOUR_TOKEN')
end
```

Expand Down
5 changes: 3 additions & 2 deletions Rakefile.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# frozen_string_literal: true

require "bundler/gem_tasks"

require "minitest/test_task"
require "rubocop/rake_task"

Minitest::TestTask.create
RuboCop::RakeTask.new

task default: %i[rubocop]
task default: %i[test rubocop]
2 changes: 2 additions & 0 deletions lib/zspay.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
require "logger"
require "active_support/hash_with_indifferent_access"

require "zspay/version"

require "zspay/configuration"

require "zspay/resource"
Expand Down
18 changes: 8 additions & 10 deletions lib/zspay/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ module Zspay
# The Configuration class is used to manage configuration settings for the Zspay library.
# It provides class-level accessors and methods to set and retrieve the configuration settings.
class Configuration
class << self
# @!attribute [rw] token
# @return [String] the API token used for authenticating requests to the Zspay service.
attr_accessor :token
# @!attribute [rw] token
# @return [String] the API token used for authenticating requests to the Zspay service.
attr_accessor :token

# Returns the default API endpoint for the Zspay service.
#
# @return [String] the base URL of the Zspay API endpoint.
def endpoint
"https://api.zsystems.com.br"
end
# Returns the default API endpoint for the Zspay service.
#
# @return [String] the base URL of the Zspay API endpoint.
def endpoint
ENV.fetch("Z_SYSTEMS_API", "https://api.zsystems.com.br")
end
end
end
4 changes: 2 additions & 2 deletions lib/zspay/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def parse_body(response)
#
# @return [String] the API endpoint URL.
def endpoint
Zspay::Configuration.endpoint
Zspay.configuration.endpoint
end

# Constructs the headers for the request.
Expand All @@ -129,7 +129,7 @@ def endpoint
# @param custom_token [String, nil] an optional custom token for the request.
# @return [Hash] the request headers.
def headers(custom_token = nil)
token = custom_token ? "Bearer #{custom_token}" : "Bearer #{Zspay::Configuration.token}"
token = "Bearer #{custom_token || Zspay.configuration.token}"
{
Authorization: token
}
Expand Down
2 changes: 1 addition & 1 deletion lib/zspay/resources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

require "zspay/resources/bank_account"
require "zspay/resources/client"
require "zspay/resources/establishments"
require "zspay/resources/establishment"
require "zspay/resources/sale"
require "zspay/resources/transfer"
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# frozen_string_literal: true

module Zspay
# The Establishments class manages establishment-related actions within the Zspay platform.
# It allows for listing, creating, enabling, disabling establishments, and more, extending the functionality
# The Establishment class manages establishment-related actions within the Zspay platform.
# It allows for listing, creating, enabling, disabling establishment, and more, extending the functionality
# from Zspay::Resource.
class Establishments < Zspay::Resource
class Establishment < Zspay::Resource
class << self
# Retrieves a list of child establishments associated with the current establishment.
#
Expand Down
15 changes: 15 additions & 0 deletions test/test_configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require "test_helper"

class TestConfiguration < Minitest::Test
def setup
Zspay.configure do |config|
config.token = "my_token"
end
end

def test_that_it_assign_token
assert_equal "my_token", Zspay.configuration.token
end
end
15 changes: 15 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

$LOAD_PATH.unshift File.expand_path("../lib", __dir__)

require "simplecov"
SimpleCov.start do
add_filter "/test/"
end

require "zspay"

require "webmock"
require "webmock/minitest"

require "minitest/autorun"
9 changes: 9 additions & 0 deletions test/test_version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

require "test_helper"

class TestVersion < Minitest::Test
def test_that_it_has_a_version_number
refute_nil Zspay::VERSION
end
end
57 changes: 57 additions & 0 deletions test/zspay/resources/test_bank_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true

require "test_helper"

class TestBankAccount < Minitest::Test
def setup
@resource_path = "/estabelecimentos/contas_bancarias"
end

def test_index
mock = Minitest::Mock.new
mock.expect :call, [], [@resource_path]

Zspay::BankAccount.stub :get, mock do
Zspay::BankAccount.index
end

mock.verify
end

def test_create
account = { name: "Example" }
mock = Minitest::Mock.new
mock.expect :call, [], [@resource_path, account]

Zspay::BankAccount.stub :post, mock do
Zspay::BankAccount.create(account)
end

mock.verify
end

def test_active
account_id = 1
mock = Minitest::Mock.new
mock.expect :call, [], ["#{@resource_path}/#{account_id}/ativar"]

Zspay::BankAccount.stub :post, mock do
Zspay::BankAccount.active(account_id)
end

mock.verify
end

def test_destroy
establishment_id = 1
account_id = 1
mock = Minitest::Mock.new
mock.expect :call, [], ["/estabelecimentos/#{establishment_id}/contas_bancarias/#{account_id}/excluir"]

Zspay::BankAccount.stub :delete, mock do
Zspay::BankAccount.destroy(establishment_id, account_id)
end

mock.verify
end
end
34 changes: 34 additions & 0 deletions test/zspay/resources/test_client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

require "test_helper"

class TestClient < Minitest::Test
def setup
@clients_path = "/clientes"
end

def test_create
client_data = { name: "John Doe", email: "[email protected]" }
mock = Minitest::Mock.new
mock.expect :call, [], [@clients_path, client_data]

Zspay::Client.stub :post, mock do
Zspay::Client.create(client_data)
end

mock.verify
end

def test_create_card
client_id = "123"
card_data = { number: "4111111111111111", expiry_month: "12", expiry_year: "2023", cvv: "123" }
mock = Minitest::Mock.new
mock.expect :call, [], ["#{@clients_path}/#{client_id}/cartoes", card_data]

Zspay::Client.stub :post, mock do
Zspay::Client.create_card(client_id, card_data)
end

mock.verify
end
end
Loading

0 comments on commit 64f6076

Please sign in to comment.