Skip to content

Commit

Permalink
Random IBAN generator tests. Fixed cached_variables handling
Browse files Browse the repository at this point in the history
  • Loading branch information
leio10 committed Oct 5, 2017
1 parent f77f527 commit d7a8647
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 55 deletions.
45 changes: 31 additions & 14 deletions lib/iban_bic/core.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

module IbanBic
@cached_variables = []

module_function

def country_validators
Expand Down Expand Up @@ -62,6 +64,10 @@ def calculate_check(iban)
end .join.to_i % 97
end

def valid?(iban)
valid_check?(iban) && valid_country_check?(iban)
end

def valid_check?(iban)
calculate_check(iban) == 97
end
Expand All @@ -88,41 +94,52 @@ def calculate_bic(iban)
end

def clear_cache
@iban_meta = @parser = @static_bics = @dynamic_bics = nil
@cached_variables.each { |variable| instance_variable_set(variable, nil) }
@cached_variables.clear
end

def iban_meta
@iban_meta ||= ::YAML.load_file(configuration.iban_meta_path)
end

def parser
@parser ||= Hash[
iban_meta.map do |country, meta|
[country, /^(?<country>#{country})#{meta["parts"].delete(" ")}$/]
end
].freeze
@parser ||= begin
@cached_variables << :@parser
Hash[
iban_meta.map do |country, meta|
[country, /^(?<country>#{country})#{meta["parts"].delete(" ")}$/]
end
].freeze
end
end

def tags
@tags ||= Hash[
iban_meta.map do |country, meta|
[country, meta["tags"]&.split&.map(&:to_sym) || []]
end
].freeze
@tags ||= begin
@cached_variables << :@tags
Hash[
iban_meta.map do |country, meta|
[country, meta["tags"]&.split&.map(&:to_sym) || []]
end
].freeze
end
end

def bics
configuration.static_bics? ? static_bics : dynamic_bics
end

def static_bics
@static_bics ||= Hash[Dir.glob(File.join(configuration.static_bics_path, "*.yml")).map do |file|
[File.basename(file).delete(".yml").upcase, YAML.load_file(file)]
end].freeze
@static_bics ||= begin
@cached_variables << :@static_bics
Hash[Dir.glob(File.join(configuration.static_bics_path, "*.yml")).map do |file|
[File.basename(file).delete(".yml").upcase, YAML.load_file(file)]
end].freeze
end
end

def dynamic_bics
@dynamic_bics ||= begin
@cached_variables << :@dynamic_bics
ret = {}
Bic.find_each do |bic|
ret[bic.country] = {} unless ret[bic.country]
Expand Down
19 changes: 8 additions & 11 deletions lib/iban_bic/random.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@ def random_iban(options = {})
end

def random_generator
@random_generator ||= Hash[
iban_meta.map do |country, meta|
[country, /^#{country}#{meta["parts"].delete(" ").gsub(/\(\?\<\w+\>([^\)]*)\)/, "\\1")}$/]
end
].freeze
end

alias _clear_cache clear_cache
def clear_cache
_clear_cache
@random_generator = nil
@random_generator ||= begin
@cached_variables << :@random_generator
Hash[
iban_meta.map do |country, meta|
[country, /^#{country}#{meta["parts"].delete(" ").gsub(/\(\?\<\w+\>([^\)]*)\)/, "\\1")}$/]
end
].freeze
end
end
end
78 changes: 48 additions & 30 deletions spec/iban_bic/core_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,6 @@
let(:iban_digits) { "87" }
let(:country_digits) { "30" }

describe "#parse" do
subject(:method) { IbanBic.parse(iban) }

it { is_expected.to include(country: "ES", bank: "0003", branch: "0000", check: country_digits, account: "0000000000") }
end

describe "#valid_check?" do
subject(:method) { IbanBic.valid_check?(iban) }

it { is_expected.to be_truthy }

context "when iban is invalid" do
let(:iban_digits) { "00" }

it { is_expected.to be_falsey }
end
end

describe "#calculate_check" do
subject(:method) { IbanBic.calculate_check(iban) }

Expand Down Expand Up @@ -75,18 +57,6 @@
end
end

describe "#valid_country_check?" do
subject(:method) { IbanBic.valid_country_check?(iban) }

it { is_expected.to be_truthy }

context "when country control digits are wrong" do
let(:country_digits) { "12" }

it { is_expected.to be_falsey }
end
end

describe "#fix" do
subject(:method) { IbanBic.fix(iban) }
let(:correct_iban) { "ES8700030000300000000000" }
Expand Down Expand Up @@ -146,4 +116,52 @@
it { is_expected.to be_nil }
end
end

describe "#parse" do
subject(:method) { IbanBic.parse(iban) }

it { is_expected.to include(country: "ES", bank: "0003", branch: "0000", check: country_digits, account: "0000000000") }
end

describe "#valid?" do
subject(:method) { IbanBic.valid?(iban) }

it { is_expected.to be_truthy }

context "when iban is invalid" do
let(:iban_digits) { "00" }

it { is_expected.to be_falsey }
end

context "when country control digits are wrong" do
let(:country_digits) { "12" }

it { is_expected.to be_falsey }
end
end

describe "#valid_check?" do
subject(:method) { IbanBic.valid_check?(iban) }

it { is_expected.to be_truthy }

context "when iban is invalid" do
let(:iban_digits) { "00" }

it { is_expected.to be_falsey }
end
end

describe "#valid_country_check?" do
subject(:method) { IbanBic.valid_country_check?(iban) }

it { is_expected.to be_truthy }

context "when country control digits are wrong" do
let(:country_digits) { "12" }

it { is_expected.to be_falsey }
end
end
end
51 changes: 51 additions & 0 deletions spec/iban_bic/random_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

require "rails_helper"
require "iban_bic/random"

RSpec.describe(::IbanBic) do
describe "#random_iban" do
subject(:method) { IbanBic.random_iban params }
let(:params) { {} }

it "returns a valid IBAN" do
expect(IbanBic.valid?(subject)).to be_truthy
end

context "when a country code is given" do
let(:params) { { country: "ES" } }

it "returns a valid IBAN" do
expect(IbanBic.valid?(subject)).to be_truthy
end

it "returns a valid IBAN from the country" do
expect(subject[0..1]).to eq("ES")
end
end

context "when tags are given" do
let(:params) { { tags: [:sepa] } }

it "returns a valid IBAN" do
expect(IbanBic.valid?(subject)).to be_truthy
end

it "returns a valid IBAN from a country with that tags" do
expect(IbanBic.has_tags?(subject, [:sepa])).to be_truthy
end
end

context "when not_tags are given" do
let(:params) { { not_tags: [:sepa] } }

it "returns a valid IBAN" do
expect(IbanBic.valid?(subject)).to be_truthy
end

it "returns a valid IBAN from a country without that tags" do
expect(IbanBic.has_tags?(subject, [:sepa])).to be_falsey
end
end
end
end

0 comments on commit d7a8647

Please sign in to comment.