From d7a86477731a4b9d53326a7814b19b95433f8576 Mon Sep 17 00:00:00 2001 From: Leonardo Diez Date: Thu, 5 Oct 2017 18:35:40 +0200 Subject: [PATCH] Random IBAN generator tests. Fixed cached_variables handling --- lib/iban_bic/core.rb | 45 ++++++++++++++------- lib/iban_bic/random.rb | 19 ++++----- spec/iban_bic/core_spec.rb | 78 ++++++++++++++++++++++-------------- spec/iban_bic/random_spec.rb | 51 +++++++++++++++++++++++ 4 files changed, 138 insertions(+), 55 deletions(-) create mode 100644 spec/iban_bic/random_spec.rb diff --git a/lib/iban_bic/core.rb b/lib/iban_bic/core.rb index f4427c9..17b39d5 100644 --- a/lib/iban_bic/core.rb +++ b/lib/iban_bic/core.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true module IbanBic + @cached_variables = [] + module_function def country_validators @@ -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 @@ -88,7 +94,8 @@ 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 @@ -96,19 +103,25 @@ def iban_meta end def parser - @parser ||= Hash[ - iban_meta.map do |country, meta| - [country, /^(?#{country})#{meta["parts"].delete(" ")}$/] - end - ].freeze + @parser ||= begin + @cached_variables << :@parser + Hash[ + iban_meta.map do |country, meta| + [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 @@ -116,13 +129,17 @@ def 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] diff --git a/lib/iban_bic/random.rb b/lib/iban_bic/random.rb index 66baa8b..790345b 100644 --- a/lib/iban_bic/random.rb +++ b/lib/iban_bic/random.rb @@ -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 diff --git a/spec/iban_bic/core_spec.rb b/spec/iban_bic/core_spec.rb index 09941f2..847f498 100644 --- a/spec/iban_bic/core_spec.rb +++ b/spec/iban_bic/core_spec.rb @@ -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) } @@ -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" } @@ -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 diff --git a/spec/iban_bic/random_spec.rb b/spec/iban_bic/random_spec.rb new file mode 100644 index 0000000..fe2577e --- /dev/null +++ b/spec/iban_bic/random_spec.rb @@ -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